From 29e9dd6bdaa9259d6c518663924dd1b42d0e3340 Mon Sep 17 00:00:00 2001 From: Patryk Borowiec <56939183+Zefir12@users.noreply.github.com> Date: Wed, 24 Apr 2024 23:22:42 +0200 Subject: [PATCH] feat(addFood): added uuid's --- package.json | 2 ++ pnpm-lock.yaml | 17 ++++++++++++++++ .../supabase/services/supabaseFoodService.ts | 6 +++--- .../supabase/supabaseSchemas/supaDatabase.ts | 6 ++++++ src/pages/dashboard/store/dashboardStore.ts | 10 +++++++++- src/pages/dashboard/subviews/FoodInfoView.vue | 1 + src/pages/food/store/addFoodStore.ts | 20 ++++++++++++++++--- src/pages/food/store/newFoodStore.ts | 12 +++++------ src/pages/food/subviews/CreateNewFoodView.vue | 8 +++++++- 9 files changed, 68 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 0a5a929..ca7dfdb 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,12 @@ "globals": "^15.0.0", "pinia": "^2.1.7", "primevue": "^3.50.0", + "uuid": "^9.0.1", "vue": "^3.4.21", "vue-router": "^4.3.0" }, "devDependencies": { + "@types/uuid": "^9.0.8", "@typescript-eslint/eslint-plugin": "^7.7.0", "@typescript-eslint/parser": "^7.7.0", "@vitejs/plugin-vue": "^5.0.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d8c017c..9eaa477 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: primevue: specifier: ^3.50.0 version: 3.50.0(vue@3.4.21(typescript@5.4.3)) + uuid: + specifier: ^9.0.1 + version: 9.0.1 vue: specifier: ^3.4.21 version: 3.4.21(typescript@5.4.3) @@ -33,6 +36,9 @@ importers: specifier: ^4.3.0 version: 4.3.0(vue@3.4.21(typescript@5.4.3)) devDependencies: + '@types/uuid': + specifier: ^9.0.8 + version: 9.0.8 '@typescript-eslint/eslint-plugin': specifier: ^7.7.0 version: 7.7.0(@typescript-eslint/parser@7.7.0(eslint@9.0.0)(typescript@5.4.3))(eslint@9.0.0)(typescript@5.4.3) @@ -395,6 +401,9 @@ packages: '@types/semver@7.5.8': resolution: {integrity: sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==} + '@types/uuid@9.0.8': + resolution: {integrity: sha512-jg+97EGIcY9AGHJJRaaPVgetKDsrTgbRjQ5Msgjh/DQKEFl0DtyRr/VCOyD1T2R1MNeWPK/u7JoGhlDZnKBAfA==} + '@types/ws@8.5.10': resolution: {integrity: sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==} @@ -1074,6 +1083,10 @@ packages: util-deprecate@1.0.2: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} + uuid@9.0.1: + resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==} + hasBin: true + vite@5.2.3: resolution: {integrity: sha512-+i1oagbvkVIhEy9TnEV+fgXsng13nZM90JQbrcPrf6DvW2mXARlz+DK7DLiDP+qeKoD1FCVx/1SpFL1CLq9Mhw==} engines: {node: ^18.0.0 || >=20.0.0} @@ -1415,6 +1428,8 @@ snapshots: '@types/semver@7.5.8': {} + '@types/uuid@9.0.8': {} + '@types/ws@8.5.10': dependencies: '@types/node': 20.11.30 @@ -2138,6 +2153,8 @@ snapshots: util-deprecate@1.0.2: {} + uuid@9.0.1: {} + vite@5.2.3(@types/node@20.11.30): dependencies: esbuild: 0.20.2 diff --git a/src/lib/supabase/services/supabaseFoodService.ts b/src/lib/supabase/services/supabaseFoodService.ts index 9691090..bfadea8 100644 --- a/src/lib/supabase/services/supabaseFoodService.ts +++ b/src/lib/supabase/services/supabaseFoodService.ts @@ -5,9 +5,9 @@ export async function getFoodsWithData(startDate: Date, endDate: Date) { const { error, data } = await supabase .from("food") .select("food_amount, time_of_intake, food_types(*)") - .gt("created_at", startDate.toLocaleDateString()) - .lt("created_at", endDate.toLocaleDateString()) - .order("created_at", { ascending: false }); + .gt("time_of_intake", startDate.toLocaleDateString()) + .lt("time_of_intake", endDate.toLocaleDateString()) + .order("time_of_intake", { ascending: false }); if (error) { console.log(error); throw new Error(); diff --git a/src/lib/supabase/supabase/supabaseSchemas/supaDatabase.ts b/src/lib/supabase/supabase/supabaseSchemas/supaDatabase.ts index a0f06ea..e9e87dc 100644 --- a/src/lib/supabase/supabase/supabaseSchemas/supaDatabase.ts +++ b/src/lib/supabase/supabase/supabaseSchemas/supaDatabase.ts @@ -128,16 +128,19 @@ export type Database = { created_at: string; id: number; price: number | null; + seller_id: number | null; }; Insert: { created_at?: string; id?: number; price?: number | null; + seller_id?: number | null; }; Update: { created_at?: string; id?: number; price?: number | null; + seller_id?: number | null; }; Relationships: []; }; @@ -147,6 +150,7 @@ export type Database = { food_amount: number; food_id: number; id: number; + meal_id: string | null; time_of_intake: string | null; user_id: string | null; }; @@ -155,6 +159,7 @@ export type Database = { food_amount: number; food_id: number; id?: number; + meal_id?: string | null; time_of_intake?: string | null; user_id?: string | null; }; @@ -163,6 +168,7 @@ export type Database = { food_amount?: number; food_id?: number; id?: number; + meal_id?: string | null; time_of_intake?: string | null; user_id?: string | null; }; diff --git a/src/pages/dashboard/store/dashboardStore.ts b/src/pages/dashboard/store/dashboardStore.ts index ad9cbb5..7b5aab3 100644 --- a/src/pages/dashboard/store/dashboardStore.ts +++ b/src/pages/dashboard/store/dashboardStore.ts @@ -48,6 +48,13 @@ export const useDashboardStore = defineStore("dashboardStore", () => { }); return totalCarbs; }); + const kcal = computed(() => { + let totalKcal = 0; + foodData.value.forEach((food) => { + totalKcal += food.food_types.kcal; + }); + return totalKcal; + }); onMounted(() => { changeDay(0); @@ -60,6 +67,7 @@ export const useDashboardStore = defineStore("dashboardStore", () => { foodData, proteins, carbohydrates, - fats + fats, + kcal }; }); diff --git a/src/pages/dashboard/subviews/FoodInfoView.vue b/src/pages/dashboard/subviews/FoodInfoView.vue index 15132d5..b3433a1 100644 --- a/src/pages/dashboard/subviews/FoodInfoView.vue +++ b/src/pages/dashboard/subviews/FoodInfoView.vue @@ -2,6 +2,7 @@
+ {{ dashboardStore.kcal }}
diff --git a/src/pages/food/store/addFoodStore.ts b/src/pages/food/store/addFoodStore.ts index 2b61acc..c1a3e38 100644 --- a/src/pages/food/store/addFoodStore.ts +++ b/src/pages/food/store/addFoodStore.ts @@ -5,6 +5,8 @@ import { useToast } from "primevue/usetoast"; import { Tables, TablesInsert } from "../../../lib/supabase/supabase/supabaseSchemas/supaDatabase"; import { ref } from "vue"; import { addFood } from "@/lib/supabase/services/supabaseFoodService"; +import { v4 as uuidv4 } from "uuid"; + export type FoodInsertItemCombined = TablesInsert<"food"> & Tables<"food_types">; export const useAddFoodStore = defineStore("addFoodStore", () => { @@ -21,6 +23,10 @@ export const useAddFoodStore = defineStore("addFoodStore", () => { sortFoods(); } + const showSuccess = () => { + toast.add({ severity: "success", summary: "Succes", detail: "Succesfully added food", life: 3000 }); + }; + const combinedVariables = computed(() => { return `${time.value}_${selectedFoodTypes.value.length}`; }); @@ -36,15 +42,19 @@ export const useAddFoodStore = defineStore("addFoodStore", () => { ); async function addToDatabase() { + const uuid = uuidv4(); const foodsToAdd = [] as TablesInsert<"food">[]; selectedFoodTypes.value.forEach((item) => { foodsToAdd.push({ food_amount: item.food_amount, food_id: item.id, - time_of_intake: item.time_of_intake + time_of_intake: item.time_of_intake, + meal_id: uuid }); }); await addFood(foodsToAdd); + clearFoods(); + showSuccess(); } function sortFoods() { @@ -85,6 +95,10 @@ export const useAddFoodStore = defineStore("addFoodStore", () => { } onMounted(async () => { + await fetchFoodTypesData(); + }); + + async function fetchFoodTypesData() { const { data, error } = await supabase.from("food_types").select("*").returns[]>(); if (error) { @@ -93,7 +107,7 @@ export const useAddFoodStore = defineStore("addFoodStore", () => { foodTypes.value = data as FoodInsertItemCombined[]; sortFoods(); } - }); + } - return { foodTypes, selectedFoodTypes, time, query, selectItem, deselectItem, clearFoods, addToDatabase }; + return { foodTypes, selectedFoodTypes, time, query, selectItem, deselectItem, clearFoods, addToDatabase, fetchFoodTypesData }; }); diff --git a/src/pages/food/store/newFoodStore.ts b/src/pages/food/store/newFoodStore.ts index 4288719..fac209a 100644 --- a/src/pages/food/store/newFoodStore.ts +++ b/src/pages/food/store/newFoodStore.ts @@ -2,7 +2,7 @@ import { defineStore } from "pinia"; import { ref } from "vue"; import { supabase } from "../../../lib/supabase/supabase/supabase"; -import { useToast } from 'primevue/usetoast'; +import { useToast } from "primevue/usetoast"; interface Serving { value: number; @@ -32,7 +32,7 @@ export const useNewFoodStore = defineStore("newFoodStore", () => { const servings = ref([ { name: "Standard", value: 100 }, - { name: "Gram", value: 1 }, + { name: "Gram", value: 1 } ] as Serving[]); const showSuccess = () => { @@ -43,7 +43,7 @@ export const useNewFoodStore = defineStore("newFoodStore", () => { tost.add({ severity: "error", summary: "Error", detail: error }); }; - async function addNewFoodToDatabase() { + async function addNewFoodToDatabase(callback: () => void) { const { error } = await supabase.from("food_types").insert({ carbs: carbs.value, fat: fats.value, @@ -58,12 +58,13 @@ export const useNewFoodStore = defineStore("newFoodStore", () => { water_percentage: waterPercentage.value, portion_weigth: 100, nova_score: novaScore.value, - nutri_score: nutriScore.value, + nutri_score: nutriScore.value }); if (error) { showError(JSON.stringify(error)); } else { showSuccess(); + await callback(); } } @@ -93,7 +94,6 @@ export const useNewFoodStore = defineStore("newFoodStore", () => { waterPercentage, addNewServing, removeServing, - addNewFoodToDatabase, + addNewFoodToDatabase }; }); - diff --git a/src/pages/food/subviews/CreateNewFoodView.vue b/src/pages/food/subviews/CreateNewFoodView.vue index 34919f0..7c89359 100644 --- a/src/pages/food/subviews/CreateNewFoodView.vue +++ b/src/pages/food/subviews/CreateNewFoodView.vue @@ -6,7 +6,13 @@ import StyledNumberInput from "@/components/global/StyledNumberInput.vue"; import { useNewFoodStore } from "../../../pages/food/store/newFoodStore"; import Rating from "primevue/rating"; import IconButton from "@/components/global/IconButton.vue"; +import { useAddFoodStore } from "../store/addFoodStore"; const newFoodStore = useNewFoodStore(); +const addFoodStore = useAddFoodStore(); + +function addNewFoodToDatabase() { + newFoodStore.addNewFoodToDatabase(addFoodStore.fetchFoodTypesData); +}