meal.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { defineStore } from 'pinia'
  2. import type { FoodMealMenu, FoodMealServiceArea } from '@/types/meal'
  3. const CURRENT_SERVICE_AREA_ID_KEY = 'meal_current_service_area_id'
  4. const LEGACY_CURRENT_RESTAURANT_ID_KEY = 'meal_current_restaurant_id'
  5. export const useMealStore = defineStore('meal', {
  6. state: () => ({
  7. selectedDate: '',
  8. selectedMealType: '',
  9. menu: null as FoodMealMenu | null,
  10. quantities: {} as Record<string, number>,
  11. serviceAreas: [] as FoodMealServiceArea[],
  12. currentServiceArea: null as FoodMealServiceArea | null
  13. }),
  14. getters: {
  15. selectedItems: state => Object.entries(state.quantities).filter(([, quantity]) => quantity > 0),
  16. currentServiceAreaId: state => state.currentServiceArea?.id || '',
  17. restaurants: state => state.serviceAreas,
  18. currentRestaurant: state => state.currentServiceArea,
  19. currentRestaurantId: state => state.currentServiceArea?.id || ''
  20. },
  21. actions: {
  22. setServiceAreas(serviceAreas: FoodMealServiceArea[]) {
  23. this.serviceAreas = serviceAreas
  24. const savedId = uni.getStorageSync(CURRENT_SERVICE_AREA_ID_KEY) || uni.getStorageSync(LEGACY_CURRENT_RESTAURANT_ID_KEY)
  25. const saved = savedId ? serviceAreas.find(item => item.id === savedId) : null
  26. const currentStillExists = this.currentServiceArea ? serviceAreas.find(item => item.id === this.currentServiceArea?.id) : null
  27. this.currentServiceArea = saved || currentStillExists || serviceAreas[0] || null
  28. if (this.currentServiceArea?.id) uni.setStorageSync(CURRENT_SERVICE_AREA_ID_KEY, this.currentServiceArea.id)
  29. },
  30. setRestaurants(serviceAreas: FoodMealServiceArea[]) {
  31. this.setServiceAreas(serviceAreas)
  32. },
  33. setCurrentServiceArea(serviceArea: FoodMealServiceArea) {
  34. if (this.currentServiceArea?.id !== serviceArea.id) this.clearServiceAreaRelatedState()
  35. this.currentServiceArea = serviceArea
  36. uni.setStorageSync(CURRENT_SERVICE_AREA_ID_KEY, serviceArea.id)
  37. },
  38. setCurrentRestaurant(serviceArea: FoodMealServiceArea) {
  39. this.setCurrentServiceArea(serviceArea)
  40. },
  41. setDate(date: string) {
  42. this.selectedDate = date
  43. },
  44. setMealType(mealType: string) {
  45. this.selectedMealType = mealType
  46. },
  47. setMenu(menu: FoodMealMenu | null) {
  48. this.menu = menu
  49. this.quantities = {}
  50. },
  51. setQuantity(menuItemId: string, quantity: number) {
  52. if (quantity <= 0) delete this.quantities[menuItemId]
  53. else this.quantities[menuItemId] = quantity
  54. },
  55. clearCart() {
  56. this.quantities = {}
  57. },
  58. clearServiceAreaRelatedState() {
  59. this.selectedDate = ''
  60. this.selectedMealType = ''
  61. this.menu = null
  62. this.quantities = {}
  63. },
  64. clearRestaurantRelatedState() {
  65. this.clearServiceAreaRelatedState()
  66. }
  67. }
  68. })