Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | <template> <h3>Full shopping list</h3> <h4>Included Meals</h4> <meal-selection-list :checked="includedMeals" :meals="availableMeals" @meal-selected="mealSelected" @meal-unselected="mealUnselected" /> <ul class="full-shopping-list"> <li v-for="group in outstandingItemGroups" :key="groupKey(group)" class="selectable" :class="{ selected: isSelected(group) }" @click="toggleSelect(group)" > <shopping-list-item :shopping-list-item-group="group" /> </li> </ul> <div v-if="outstandingItemGroups.length === 0"> <p>No items to purchase</p> </div> <div class="purchased-slider"> <span v-if="purchasedItemGroups.length === 0" /> <button v-else-if="showPurchased" @click="showPurchased = false" > ⏶ Hide Purchased ⏶ </button> <button v-else @click="showPurchased = true" > ⏷ Show Purchased ⏷ </button> <div v-if="showPurchased && purchasedItemGroups.length > 0"> <h4>Purchased Meals</h4> <meal-selection-list :checked="includedMeals" :meals="purchasedMeals" @meal-selected="mealSelected" @meal-unselected="mealUnselected" /> <h4>Purchased Items</h4> <ul class="full-shopping-list"> <li v-for="item in purchasedItemGroups" :key="groupKey(item)" > <shopping-list-item :shopping-list-item-group="item" /> </li> </ul> </div> </div> <div v-if="selected.length" class="spacer" > </div> <!-- Display 'Stocked', 'Purchased' and 'Cancel' buttons in a vertical stack fixed to the bottom of the screen when any elements are selected --> <div v-if="selected.length" class="footer-buttons" > <p v-if="selected.length === 1"> Mark '{{ selected[0] ? groupLabel(selected[0]) : '' }}' as </p> <p v-else> Mark {{ selected.length }} items as </p> <div class="button-group"> <button @click="markFound"> <img :src="houseCheck"><br> Found </button> <button @click="markPurchased"> <img :src="shoppingCart"><br> Purchased </button> <button @click="selected = []"> <img :src="closeIcon"><br> Cancel </button> </div> </div> </template> <script setup lang="ts"> import { ref, computed, onBeforeMount } from 'vue' import { useRouter } from 'vue-router' import { useAlert } from '@/composables/useAlert' import { useShopping } from '@/composables/useShopping' import { getUpcomingMeals } from '@/api/sdk' import { type Group } from '@/composables/useShopping' type UIMeal = import('@/domain/types').Meal import MealSelectionList from './MealSelectionList.vue' import ShoppingListItem from './ShoppingListItem.vue' const houseCheck = new URL('@/assets/house-check.svg', import.meta.url).toString() const shoppingCart = new URL('@/assets/shopping-cart.svg', import.meta.url).toString() const closeIcon = new URL('@/assets/close.svg', import.meta.url).toString() const router = useRouter() const { show: showAlert } = useAlert() const { getCurrentShoppingList, requestMeal, unrequestMeal, purchaseFromGroups, groupsFrom, mealsFrom } = useShopping() const from = new Date() from.setTime(0) const to = new Date() to.setDate(to.getDate() + 7) type Meal = import('@/domain/types').Meal const shoppingList = ref<import('@/domain/types').CurrentShoppingListDTO | null>(null) const upcomingMeals = ref<Meal[]>([]) const selected = ref<Group[]>([]) const showPurchased = ref(false) const groupsMatch = (a: Group, b: Group) => { if (a.type !== b.type) return false if (a.type === 'name' && b.type === 'name') return a.name === b.name if (a.type === 'product' && b.type === 'product') return a.product.id === b.product.id return false } const outstandingItemGroups = computed<Group[]>(() => groupsFrom(shoppingList.value?.outstandingItems)) const purchasedItemGroups = computed<Group[]>(() => groupsFrom(shoppingList.value?.purchasedItems)) const purchasedMeals = computed<UIMeal[]>(() => mealsFrom(shoppingList.value?.purchasedItems)) const availableMeals = computed(() => { const lookup: Record<string | number, Meal> = shoppingList.value?.mealsLookup ?? {} const meals: Record<string | number, Meal> = { ...lookup } upcomingMeals.value?.forEach((m) => { if (!meals[m.id]) meals[m.id] = m }) return Object.values(meals) .filter((m) => !m.purchaseDate) .sort((a, b) => ((a.suggestedDate && a.suggestedDate.getTime()) || 0) - ((b.suggestedDate && b.suggestedDate.getTime()) || 0)) }) const includedMeals = computed(() => (shoppingList.value?.requestedMeals ?? []).map((i) => i.meal).filter((m): m is Meal => !!m)) async function loadData() { upcomingMeals.value = await getUpcomingMeals(from, to) shoppingList.value = await getCurrentShoppingList() } async function mealSelected(meal: { id: number }) { await requestMeal(meal.id) await loadData() } async function mealUnselected(meal: { id: number }) { await unrequestMeal(meal.id) await loadData() } async function markFound() { const result = await purchaseFromGroups(selected.value) if (!result) { showAlert({ type: 'error', message: 'No items selected.' }) return } selected.value = [] await loadData() } async function markPurchased() { const shopping = await purchaseFromGroups(selected.value) if (!shopping || !shopping.id) { showAlert({ type: 'error', message: 'Failed to purchase.' }) return } selected.value = [] router.push(`/shopping/${shopping.id}`) } function toggleSelect(item: Group) { const index = selected.value.findIndex((i) => groupsMatch(i, item)) if (index === -1) selected.value.push(item) else selected.value.splice(index, 1) } function isSelected(item: Group) { return selected.value.some((i) => groupsMatch(i, item)) } function groupKey(group: Group) { if (group.type === 'product') return `p-${group.product.id}` if (group.type === 'name') return `n-${group.name}` return Math.random().toString(36) } function groupLabel(group: Group) { if (group.type === 'product') return group.product.name ?? 'item' if (group.type === 'name') return group.name return 'item' } onBeforeMount(loadData) </script> <style scoped> .full-shopping-list { padding: 0; } .full-shopping-list li { list-style-type: none; outline: 1px solid #ccc; border-radius: 0.5em; margin-bottom: 1em; } .full-shopping-list li.selectable { cursor: pointer; outline: 1px solid #ccc; } .full-shopping-list li.selected { background-color: #f0f0f0; outline-width: 3px; } .footer-buttons { position: fixed; bottom: 0; left: 0; right: 0; padding: 1ex; background-color: #f0f0f0; border-top: 1px solid #ccc; } .footer-buttons p { margin: 0; text-align: left; font-style: italic; } .button-group { /* Display as vertical fixed to the bottom of the screen */ display: flex; } .button-group button { flex: 1; padding: 1em; border: 1px solid #ccc; border-radius: 0.5em; background-color: #f0f0f0; cursor: pointer; } button img { width: 2em; height: 2em; } .spacer { height: 12em; } </style> |