munch-ease-frontend/src/components/shopping/MyShoppingPage.vue

89 lines
2.6 KiB
Vue
Raw Normal View History

2024-05-11 06:03:29 +00:00
<template>
2025-10-18 02:08:22 +00:00
<div>
<h1>My Shopping List</h1>
<router-link :to="`/shopping/current`">Full Shopping List</router-link>
<editable-ingredients-panel
@on-add="addIngredient"
@on-delete="deleteIngredient"
@on-update-ingredient="updateIngredient"
@on-editing="onEditing"
:ingredients="ingredients"
/>
</div>
2024-05-11 06:03:29 +00:00
2025-10-18 02:08:22 +00:00
<!--
2024-05-11 06:03:29 +00:00
# Functions
* Add a random item to next shop
* Add meals to next shop
* Show cards for the next 7 meals
* Slider / drawer for more meals
* Check meal to add
* Update shopping list automatically as meals change
* Visual indicator for meals that are already in the list
* Visual indicator for purchased meals
* Aggregate items from meals and random items into a single list of products
* For each product, need to have visibility of
* Name
* Link
* Image
* Quantity
* Source meal / person
* Date added / for
* Need to be able to mark list as done
* Keeps history - track purchased meals
* Clears list
* History view of purchased lists (seperate page ofc)
-->
</template>
2025-10-18 02:08:22 +00:00
<style scoped></style>
2024-05-11 06:03:29 +00:00
2025-10-18 02:01:18 +00:00
<script setup>
import { ref, onBeforeMount } from 'vue'
import { useRouter } from 'vue-router'
2025-10-18 01:42:23 +00:00
import { useAuth } from '@/composables/useAuth'
2025-10-18 02:01:18 +00:00
import { useShopping } from '@/composables/useShopping'
2024-05-12 06:32:28 +00:00
import EditableIngredientsPanel from '@/components/ingredients/EditableIngredientsPanel.vue'
2024-05-11 06:03:29 +00:00
2025-10-18 02:01:18 +00:00
const router = useRouter()
const { loadUser } = useAuth()
const { getMyShoppingList, saveMyShoppingList } = useShopping()
const person = ref(null)
const ingredients = ref([])
async function updateShoppingList(save = false) {
2025-10-18 02:08:22 +00:00
const newIngredients = save
? await saveMyShoppingList(ingredients.value)
: await getMyShoppingList()
ingredients.value = newIngredients
2025-10-18 02:01:18 +00:00
}
function addIngredient() {
2025-10-18 02:08:22 +00:00
ingredients.value = [{ id: -1 }, ...ingredients.value]
2025-10-18 02:01:18 +00:00
}
function deleteIngredient(ingredient) {
2025-10-18 02:08:22 +00:00
ingredients.value = ingredients.value.filter((i) => i !== ingredient)
2025-10-18 02:01:18 +00:00
}
2024-05-13 03:45:46 +00:00
2025-10-18 02:01:18 +00:00
function updateIngredient(oldIngredient, newIngredient) {
2025-10-18 02:08:22 +00:00
ingredients.value = ingredients.value.map((source) =>
source === oldIngredient ? newIngredient : source
)
2025-10-18 02:01:18 +00:00
}
2024-05-12 06:32:28 +00:00
2025-10-18 02:01:18 +00:00
async function onEditing(isStartingEdit) {
2025-10-18 02:08:22 +00:00
await updateShoppingList(!isStartingEdit)
if (isStartingEdit && ingredients.value.length === 0) addIngredient()
2024-05-11 06:03:29 +00:00
}
2025-10-18 02:01:18 +00:00
onBeforeMount(async () => {
2025-10-18 02:08:22 +00:00
const u = await loadUser()
if (!u) return router.push({ name: 'login' })
person.value = u
await updateShoppingList()
2025-10-18 02:01:18 +00:00
})
2025-10-18 02:08:22 +00:00
</script>