From f04d38f3a2383c03dc910a39fd5acce80bbe24c0 Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Sat, 26 Feb 2022 17:29:29 +0100 Subject: [PATCH] ANNOUNCEMENTS FINALLY god damn --- frontend-new/src/components/Header.vue | 37 ++++++++++++-------------- frontend-new/src/store/api.ts | 18 +++++++++++++ 2 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 frontend-new/src/store/api.ts diff --git a/frontend-new/src/components/Header.vue b/frontend-new/src/components/Header.vue index db622007..4996f13d 100644 --- a/frontend-new/src/components/Header.vue +++ b/frontend-new/src/components/Header.vue @@ -3,6 +3,7 @@ import type {Announcement as AnnouncementObject} from "hangar-api"; import { Popover, PopoverButton, PopoverPanel } from '@headlessui/vue' import type {Ref} from 'vue'; import {useThemeStore} from '~/store/theme' +import {useAPI} from '~/store/api' import {useInitialState} from "~/composables/useInitialState"; import {useInternalApi} from "~/composables/useApi"; @@ -12,32 +13,28 @@ const theme = useThemeStore() const { t } = useI18n(); -const empty: AnnouncementObject[] | null = []; -const announcements: Ref = ref(empty); +const empty: AnnouncementObject[] = []; +const announcements: Ref = ref(empty); -async function loadAnnouncements(){ - return await useInitialState( - "announcements", - async () => await useInternalApi("data/announcements", false) - ); -} -loadAnnouncements().then((value) => { - const vallue: Ref = value; - if(vallue.value){ - const firstObject: AnnouncementObject | null = vallue.value[0]; - if(firstObject){ - console.log(`Res: ${ firstObject.text}`) +const api = useAPI(); + +api.getAnnouncements() + .then((value) => { + if(value){ + const firstObject: AnnouncementObject | null = value[0]; + if(firstObject){ + console.log(`Res: ${ firstObject.text}`) + }else{ + console.log("Res is undefined") + } }else{ - console.log("Res is undefined") + console.log("value is null") } - }else{ - console.log("vallue is null") - } + announcements.value = value; + }); - announcements.value = unref(vallue); -}); const navBarLinks = [ diff --git a/frontend-new/src/store/api.ts b/frontend-new/src/store/api.ts new file mode 100644 index 00000000..c2a6462a --- /dev/null +++ b/frontend-new/src/store/api.ts @@ -0,0 +1,18 @@ +import { defineStore } from "pinia"; +import type { Ref} from "vue"; +import { ref, unref } from "vue"; + +import type {Announcement as AnnouncementObject} from "hangar-api"; +import {useInternalApi} from '~/composables/useApi'; + +export const useAPI = defineStore("api", () => { + const announcements: Ref = ref(); + + + + async function getAnnouncements(): Promise { + return await useInternalApi("data/announcements", false) + } + + return { announcements, getAnnouncements }; +});