web: add support for adding a license

This commit is contained in:
Mika 2024-07-11 13:12:14 +02:00
parent 768b36da56
commit 77abeb558e
4 changed files with 43 additions and 15 deletions

11
web/src/client.ts Normal file
View file

@ -0,0 +1,11 @@
import axios from "axios";
import { store } from "@/store";
let axiosInstance = axios.create({
baseURL: import.meta.env.VITE_BACKEND_URL,
});
axiosInstance.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${store.token}`;
return config;
});
export { axiosInstance };

View file

@ -215,6 +215,9 @@
</v-btn> </v-btn>
</v-toolbar> </v-toolbar>
</div> </div>
<v-snackbar v-model="snackbar" :timeout="2000" elevation="24" location="top">
<span> Who am I??? </span>
</v-snackbar>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -222,9 +225,12 @@ import { store } from "@/store";
import { SubmitEventPromise } from "vuetify"; import { SubmitEventPromise } from "vuetify";
import { ref } from "vue"; import { ref } from "vue";
import { Plus, LogOut, Pencil, FolderPlus } from "lucide-vue-next"; import { Plus, LogOut, Pencil, FolderPlus } from "lucide-vue-next";
import { useQuery } from "@tanstack/vue-query"; import { useQuery, useMutation, useQueryClient } from "@tanstack/vue-query";
import axios from "axios"; import axios from "axios";
import { LicenseGroup } from "@/types"; import { axiosInstance } from "@/client";
import { LicenseGroup, CreateLicenseDto } from "@/types";
const queryClient = useQueryClient();
const { data } = useQuery({ const { data } = useQuery({
queryKey: ["licenses"], queryKey: ["licenses"],
@ -249,14 +255,27 @@ const { data } = useQuery({
}, },
}); });
const { isPending, mutate } = useMutation({
mutationFn: async (newLicense: CreateLicenseDto) => {
await axiosInstance.post("/licenses", newLicense);
},
onError: (error) => {
snackbar.value = true;
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["licenses"] });
add.value = false;
},
});
function handlelogout() { function handlelogout() {
store.setToken(null); store.setToken(null);
} }
async function submit(event: SubmitEventPromise) { async function submit(event: SubmitEventPromise) {
const result = await event; const result = await event;
if (result.valid) { if (result.valid && addLicenseGroup.value) {
console.log({ const data = {
name: addLicenseName.value, name: addLicenseName.value,
group_id: addLicenseGroup.value, group_id: addLicenseGroup.value,
amount: addAmount.value, amount: addAmount.value,
@ -264,12 +283,16 @@ async function submit(event: SubmitEventPromise) {
start: addStartDate.value, start: addStartDate.value,
stop: addStopDate.value, stop: addStopDate.value,
notes: addNotes.value, notes: addNotes.value,
}); };
console.log(data);
mutate(data);
} else { } else {
console.log("Invalid"); console.log("Invalid");
} }
} }
const snackbar = ref(false);
// //
// GROUP SECTION // GROUP SECTION
// //

View file

@ -20,21 +20,13 @@
import HeaderBar from "./HeaderBar.vue"; import HeaderBar from "./HeaderBar.vue";
import CategoryContainer from "./CategoryContainer.vue"; import CategoryContainer from "./CategoryContainer.vue";
import { useQuery } from "@tanstack/vue-query"; import { useQuery } from "@tanstack/vue-query";
import axios from "axios"; import { axiosInstance } from "@/client";
import { store } from "@/store";
import { LicenseGroup } from "@/types"; import { LicenseGroup } from "@/types";
const { isPending, isError, data, error } = useQuery({ const { isPending, isError, data, error } = useQuery({
queryKey: ["licenses"], queryKey: ["licenses"],
queryFn: async () => { queryFn: async () => {
const res = await axios.get<LicenseGroup[]>( const res = await axiosInstance.get<LicenseGroup[]>("/licenses");
import.meta.env.VITE_BACKEND_URL + "/licenses",
{
headers: {
Authorization: `Bearer ${store.token}`,
},
}
);
return res.data; return res.data;
}, },
}); });

View file

@ -12,3 +12,5 @@ export interface License {
key: string; key: string;
amount?: number; amount?: number;
} }
export type CreateLicenseDto = Omit<License, "id"> & { group_id: string };