removed unused vars and improved auto sync conditions

This commit is contained in:
vabene1111 2024-01-19 20:48:48 +08:00
parent 57100baf7c
commit 12db67bd96
2 changed files with 7 additions and 91 deletions

View File

@ -61,7 +61,7 @@
</b-col> </b-col>
</b-row> </b-row>
<!-- shopping list table --> <!-- --------------------------------------- shopping list table -->
<b-row v-for="c in shopping_list_store.get_entries_by_group" v-bind:key="c.id" class="pr-1 pl-1"> <b-row v-for="c in shopping_list_store.get_entries_by_group" v-bind:key="c.id" class="pr-1 pl-1">
<b-col cols="12" <b-col cols="12"
v-if="c.count_unchecked > 0 || user_preference_store.device_settings.shopping_show_checked_entries && (c.count_unchecked + c.count_checked) > 0"> v-if="c.count_unchecked > 0 || user_preference_store.device_settings.shopping_show_checked_entries && (c.count_unchecked + c.count_checked) > 0">
@ -88,7 +88,7 @@
</b-row> </b-row>
</b-tab> </b-tab>
<!-- recipe tab --> <!-- --------------------------------------- recipe tab -->
<b-tab :title="$t('Recipes')"> <b-tab :title="$t('Recipes')">
<template #title> <template #title>
<i class="fas fa-book fa-fw"></i> <i class="fas fa-book fa-fw"></i>
@ -126,7 +126,7 @@
</b-col> </b-col>
</b-row> </b-row>
</b-tab> </b-tab>
<!-- supermarkets tab --> <!-- --------------------------------------- supermarkets tab -->
<b-tab> <b-tab>
<template #title> <template #title>
<i class="fas fa-store-alt fa-fw"></i> <i class="fas fa-store-alt fa-fw"></i>
@ -328,7 +328,7 @@
</div> </div>
</div> </div>
</b-tab> </b-tab>
<!-- settings tab --> <!-- --------------------------------------- settings tab -->
<b-tab> <b-tab>
<template #title> <template #title>
<i class="fas fa-user-cog fa-fw"></i> <i class="fas fa-user-cog fa-fw"></i>
@ -502,44 +502,19 @@ export default {
data() { data() {
return { return {
// this.Models and this.Actions inherited from ApiMixin
items: [],
current_tab: 0, current_tab: 0,
supermarkets: [],
shopping_categories: [],
show_undefined_categories: true,
supermarket_categories_only: false,
shopcat: null,
delay: 0,
generic_action: null,
generic_model: null,
ui: {
entry_mode_simple: true,
selected_supermarket: undefined,
},
user_id: parseInt(localStorage.getItem('USER_ID')), user_id: parseInt(localStorage.getItem('USER_ID')),
editing_supermarket_categories: [], editing_supermarket_categories: [],
editing_supermarket: null, editing_supermarket: null,
new_supermarket: {entrymode: false, value: undefined, editmode: undefined}, new_supermarket: {entrymode: false, value: undefined, editmode: undefined},
new_category: {entrymode: false, value: undefined}, new_category: {entrymode: false, value: undefined},
autosync_id: undefined, autosync_id: undefined,
auto_sync_running: false, // track to not start a new sync before old one was finished
auto_sync_blocked: false, // blocking auto sync while request to check item is still running
show_delay: false,
drag: false,
show_modal: false,
fields: ["checked", "amount", "category", "unit", "food", "recipe", "details"],
loading: true,
entrymode: false,
new_item: {amount: 1, unit: undefined, food: undefined, ingredient: undefined}, new_item: {amount: 1, unit: undefined, food: undefined, ingredient: undefined},
online: true, online: true,
new_recipe: { new_recipe: {
id: undefined, id: undefined,
}, },
add_recipe_servings: 1,
shopping_list_height: '60vh',
shopping_list_store: useShoppingListStore(), shopping_list_store: useShoppingListStore(),
user_preference_store: useUserPreferenceStore(), user_preference_store: useUserPreferenceStore(),
@ -658,63 +633,6 @@ export default {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_DELETE, err) StandardToasts.makeStandardToast(this, StandardToasts.FAIL_DELETE, err)
}) })
}, },
getShoppingList: function (autosync = false) {
let params = {}
params.supermarket = this.ui.selected_supermarket
params.options = {query: {recent: 1}}
if (autosync) {
params.options.query["autosync"] = 1
} else {
this.loading = true
}
// this.genericAPI(this.Models.SHOPPING_LIST, this.Actions.LIST, params).then((results) => {
// if (!autosync) {
// if (results.data?.length) {
// this.items = results.data
// } else {
// console.log("no data returned")
// }
// this.loading = false
// } else {
// if (!this.auto_sync_blocked) {
// this.getSyncQueueLength().then((r) => {
// if (r === 0) {
// this.mergeShoppingList(results.data)
// } else {
// this.auto_sync_running = false
// this.replaySyncQueue()
// }
// })
// }
// }
// })
// .catch((err) => {
// if (!autosync) {
// StandardToasts.makeStandardToast(this, StandardToasts.FAIL_FETCH, err)
// }
// })
},
mergeShoppingList: function (data) {
this.items.map((x) =>
data.map((y) => {
if (y.id === x.id) {
x.checked = y.checked
return x
}
})
)
this.auto_sync_running = false
let new_entries = data.map((x) => x.id).filter((y) => !this.items.map((z) => z.id).includes(y))
if (new_entries.length > 0) {
let api = new ApiApiFactory()
new_entries.forEach((new_id) => {
api.retrieveShoppingListEntry(new_id).then((result) => {
this.items.push(result.data)
})
})
}
},
onHand: function (item) { onHand: function (item) {
let api = new ApiApiFactory() let api = new ApiApiFactory()

View File

@ -171,7 +171,6 @@ export const useShoppingListStore = defineStore(_STORE_ID, {
}, },
autosync() { autosync() {
if (!this.currently_updating && this.autosync_has_focus) { if (!this.currently_updating && this.autosync_has_focus) {
console.log('running autosync')
this.currently_updating = true this.currently_updating = true
let previous_autosync = this.last_autosync let previous_autosync = this.last_autosync
@ -183,15 +182,14 @@ export const useShoppingListStore = defineStore(_STORE_ID, {
}).then((r) => { }).then((r) => {
r.data.forEach((e) => { r.data.forEach((e) => {
// dont update stale client data // dont update stale client data
//TODO validate the django datetime can be parsed in all browsers if (!(Object.keys(this.entries).includes(e.id.toString())) || Date.parse(this.entries[e.id].updated_at) < Date.parse(e.updated_at)) {
if (!(e.id in this.entries) || Date.parse(this.entries[e.id].updated_at) <= Date.parse(e.updated_at)) { console.log('auto sync updating entry ', e)
console.log('updating entry ', e)
Vue.set(this.entries, e.id, e) Vue.set(this.entries, e.id, e)
} }
}) })
this.currently_updating = false this.currently_updating = false
}).catch((err) => { }).catch((err) => {
console.log('auto sync failed') console.warn('auto sync failed')
this.currently_updating = false this.currently_updating = false
}) })
} }