Skip to content

Commit

Permalink
feat(addFood): added uuid's
Browse files Browse the repository at this point in the history
  • Loading branch information
Zefir12 committed Apr 24, 2024
1 parent 6fc0ebc commit 29e9dd6
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 14 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/lib/supabase/services/supabaseFoodService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/lib/supabase/supabase/supabaseSchemas/supaDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [];
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand Down
10 changes: 9 additions & 1 deletion src/pages/dashboard/store/dashboardStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -60,6 +67,7 @@ export const useDashboardStore = defineStore("dashboardStore", () => {
foodData,
proteins,
carbohydrates,
fats
fats,
kcal
};
});
1 change: 1 addition & 0 deletions src/pages/dashboard/subviews/FoodInfoView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="container">
<div class="inner-container">
<FoodInfoChart :proteins="dashboardStore.proteins" :fats="dashboardStore.fats" :carbohydrates="dashboardStore.carbohydrates" />
{{ dashboardStore.kcal }}
<button v-for="item in dashboardStore.foodData">{{ item.food_types.name }}</button>
</div>
</div>
Expand Down
20 changes: 17 additions & 3 deletions src/pages/food/store/addFoodStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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}`;
});
Expand All @@ -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() {
Expand Down Expand Up @@ -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<Tables<"food_types">[]>();

if (error) {
Expand All @@ -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 };
});
12 changes: 6 additions & 6 deletions src/pages/food/store/newFoodStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = () => {
Expand All @@ -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,
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -93,7 +94,6 @@ export const useNewFoodStore = defineStore("newFoodStore", () => {
waterPercentage,
addNewServing,
removeServing,
addNewFoodToDatabase,
addNewFoodToDatabase
};
});

8 changes: 7 additions & 1 deletion src/pages/food/subviews/CreateNewFoodView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
</script>
<template>
<div class="group">
Expand All @@ -16,7 +22,7 @@ const newFoodStore = useNewFoodStore();
<div class="content-stack">
<CardInput v-model="newFoodStore.name" label="Name" />
<StyledNumberInput v-model="newFoodStore.waterPercentage" label="Water Percentage" />
<StyledButton name="Add" @click="newFoodStore.addNewFoodToDatabase" />
<StyledButton name="Add" @click="addNewFoodToDatabase" />
</div>
</template>
</Card>
Expand Down

0 comments on commit 29e9dd6

Please sign in to comment.