From b2fc33b137d9bc90e1053bc50c28edccf6f6631e Mon Sep 17 00:00:00 2001 From: Mika Date: Thu, 11 Jul 2024 19:47:55 +0200 Subject: [PATCH] web: initial edit menu --- web/components.d.ts | 16 +- web/src/components/CategoryContainer.vue | 9 -- web/src/components/HeaderBar.vue | 13 +- web/src/components/ListViewElement.vue | 193 ++++++++++++++++++++++- web/src/types.ts | 2 + 5 files changed, 202 insertions(+), 31 deletions(-) diff --git a/web/components.d.ts b/web/components.d.ts index 79bb902..6b4427f 100644 --- a/web/components.d.ts +++ b/web/components.d.ts @@ -5,14 +5,14 @@ // Read more: https://github.com/vuejs/core/pull/3399 export {} -declare module "vue" { +declare module 'vue' { export interface GlobalComponents { - CategoryContainer: typeof import("./src/components/CategoryContainer.vue")["default"]; - Clipboard: typeof import("./src/components/Clipboard.vue")["default"]; - HeaderBar: typeof import("./src/components/HeaderBar.vue")["default"]; - ListViewElement: typeof import("./src/components/ListViewElement.vue")["default"]; - LoginPage: typeof import("./src/components/loginPage.vue")["default"]; - MainPage: typeof import("./src/components/MainPage.vue")["default"]; - UsersDialog: typeof import("./src/components/NavBarIcons/UsersDialog.vue")["default"]; + CategoryContainer: typeof import('./src/components/CategoryContainer.vue')['default'] + Clipboard: typeof import('./src/components/Clipboard.vue')['default'] + HeaderBar: typeof import('./src/components/HeaderBar.vue')['default'] + ListViewElement: typeof import('./src/components/ListViewElement.vue')['default'] + LoginPage: typeof import('./src/components/loginPage.vue')['default'] + MainPage: typeof import('./src/components/MainPage.vue')['default'] + UsersDialog: typeof import('./src/components/NavBarIcons/UsersDialog.vue')['default'] } } diff --git a/web/src/components/CategoryContainer.vue b/web/src/components/CategoryContainer.vue index f458970..fc72a13 100644 --- a/web/src/components/CategoryContainer.vue +++ b/web/src/components/CategoryContainer.vue @@ -12,15 +12,6 @@ notes="license.notes" /> - diff --git a/web/src/components/HeaderBar.vue b/web/src/components/HeaderBar.vue index e4e3a2c..af3bacf 100644 --- a/web/src/components/HeaderBar.vue +++ b/web/src/components/HeaderBar.vue @@ -113,18 +113,7 @@ label="Start Date (optional)" variant="outlined" prepend-icon="" - prepend-inner-icon="mdi-calendar-check" - clearable - v-model="addStartDate" - hide-details - > - -
- - + + + + + + Add License + + + Add an extra License to the database + + +
+ + + + + +
+
+ +
+
+ +
+
+ + + + + all fields marked with * are required + +
+
+ + + + + + + + + + +
+
+
@@ -102,9 +212,13 @@ import { Trash, } from "lucide-vue-next"; import { ref } from "vue"; +import { useQuery, useMutation, useQueryClient } from "@tanstack/vue-query"; +import { axiosInstance } from "@/client"; +import { SubmitEventPromise } from "vuetify"; +import { LicenseGroup, UpdateLicenseDto } from "@/types"; const props = defineProps<{ - id: number; + id: string; licenseName: string; licenseKey: string; startDate: Date | null; @@ -116,6 +230,81 @@ const props = defineProps<{ // EDIT SECTION const edit = ref(false); +const editLicenseName = ref(""); +const editLicenseGroup = ref(); +const editAmount = ref(); +const editLicenseKey = ref(""); +const editStartDate = ref(); +const editStopDate = ref(); +const editNotes = ref(""); + +//Give initial values to the fields +editLicenseName.value = props.licenseName; +editLicenseKey.value = props.licenseKey; +editAmount.value = props.amount; +editStartDate.value = props.startDate; +editStopDate.value = props.endDate; +editNotes.value = props.notes; + +const editNameRules = [ + (value: string) => { + if (value) return true; + + return "YOU MUST ENTER (A GROUP NAME)"; + }, +]; + +async function submitEdit(event: SubmitEventPromise) { + const result = await event; + if (result.valid) { + const editData = { + name: editLicenseName.value, + group_id: editLicenseGroup.value, + amount: editAmount.value, + key: editLicenseKey.value, + start: editStartDate.value ?? null, + end: editStopDate.value ?? null, + notes: editNotes.value, + }; + console.log(editData); + editMutate(editData); + } else { + console.log("Invalid"); + } +} + +const queryClient = useQueryClient(); + +const gyros = props.id; + +const { mutate: editMutate } = useMutation({ + mutationFn: async (newEdit: UpdateLicenseDto) => { + await axiosInstance.put(`/licenses/${gyros}`, newEdit); + }, + onError: (error) => { + console.log(error); + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["licenses"] }); + edit.value = false; + }, +}); + +const { data } = useQuery({ + queryKey: ["licenses"], + queryFn: async () => { + const res = await axiosInstance.get("/licenses"); + return res.data; + }, + select: (data) => { + return data.map((group) => { + return { + title: group.name, + value: group.id, + }; + }); + }, +});