diff --git a/frontend/dashboard/src/api/overlays/now-playing.ts b/frontend/dashboard/src/api/overlays/now-playing.ts index 0b68103f9..48796741c 100644 --- a/frontend/dashboard/src/api/overlays/now-playing.ts +++ b/frontend/dashboard/src/api/overlays/now-playing.ts @@ -1,57 +1,63 @@ -import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'; -import type { - UpdateRequest, - GetAllResponse, - CreateRequest, -} from '@twir/api/messages/overlays_now_playing/overlays_now_playing'; -import { unref } from 'vue'; -import type { MaybeRef } from 'vue'; +import { useQuery } from '@urql/vue' -import { protectedApiClient } from '@/api/twirp.js'; +import { useMutation } from '@/composables/use-mutation' +import { graphql } from '@/gql' -export const useNowPlayingOverlayManager = () => { - const queryClient = useQueryClient(); - const queryKey = 'nowPlayingOverlay'; +export function useNowPlayingOverlayApi() { + const cacheKey = ['nowPlayingOverlay'] + + const useNowPlayingQuery = () => useQuery({ + query: graphql(` + query NowPlayingOverlays { + nowPlayingOverlays { + id + channelId + preset + hideTimeout + fontWeight + fontFamily + backgroundColor + showImage + } + } + `), + context: { + additionalTypenames: cacheKey, + }, + variables: {}, + }) + + const useNowPlayingCreate = () => useMutation( + graphql(` + mutation NowPlayingOverlayCreate($input: NowPlayingOverlayMutateOpts!) { + nowPlayingOverlayCreate(opts: $input) + } + `), + cacheKey, + ) + + const useNowPlayingUpdate = () => useMutation( + graphql(` + mutation NowPlayingOverlayUpdate($id: String!, $input: NowPlayingOverlayMutateOpts!) { + nowPlayingOverlayUpdate(id: $id, opts: $input) + } + `), + cacheKey, + ) + + const useNowPlayingDelete = () => useMutation( + graphql(` + mutation NowPlayingOverlayDelete($id: String!) { + nowPlayingOverlayDelete(id: $id) + } + `), + cacheKey, + ) return { - useGetAll: () => useQuery({ - queryKey: [queryKey], - queryFn: async (): Promise => { - const call = await protectedApiClient.overlaysNowPlayingGetAll({}); - return call.response; - }, - }), - useCreate: () => useMutation({ - mutationKey: ['nowPlayingOverlayCreate'], - mutationFn: async (opts: CreateRequest) => { - const call = await protectedApiClient.overlaysNowPlayingCreate(opts); - return call.response; - }, - onSuccess: async () => { - await queryClient.invalidateQueries([queryKey]); - }, - }), - useUpdate: () => useMutation({ - mutationKey: ['nowPlayingOverlayUpdate'], - mutationFn: async (opts: MaybeRef) => { - const data = unref(opts); - await protectedApiClient.overlaysNowPlayingUpdate(data); - }, - onSuccess: async (_, opts) => { - const data = unref(opts); - await queryClient.invalidateQueries([queryKey, data.id]); - }, - }), - useDelete: () => useMutation({ - mutationKey: ['nowPlayingOverlayDelete'], - mutationFn: async (id: MaybeRef) => { - await protectedApiClient.overlaysNowPlayingDelete({ - id: unref(id), - }); - }, - onSuccess: async () => { - await queryClient.invalidateQueries([queryKey]); - }, - }), - }; -}; + useNowPlayingQuery, + useNowPlayingCreate, + useNowPlayingUpdate, + useNowPlayingDelete, + } +} diff --git a/frontend/dashboard/src/pages/overlays/now-playing/now-playing-form.vue b/frontend/dashboard/src/pages/overlays/now-playing/now-playing-form.vue index 0c0e601f9..8ca040cae 100644 --- a/frontend/dashboard/src/pages/overlays/now-playing/now-playing-form.vue +++ b/frontend/dashboard/src/pages/overlays/now-playing/now-playing-form.vue @@ -7,7 +7,7 @@ import { useI18n } from 'vue-i18n' import { useNowPlayingForm } from './use-now-playing-form' import { - useNowPlayingOverlayManager, + useNowPlayingOverlayApi, useProfile, useUserAccessFlagChecker, } from '@/api' @@ -29,20 +29,22 @@ const canCopyLink = computed(() => { return profile?.value?.selectedDashboardId === profile.value?.id && userCanEditOverlays }) -const manager = useNowPlayingOverlayManager() -const updater = manager.useUpdate() +const manager = useNowPlayingOverlayApi() +const updater = manager.useNowPlayingUpdate() async function save() { if (!formValue.value?.id) return - await updater.mutateAsync({ + await updater.executeMutation({ id: formValue.value.id, - preset: formValue.value.preset, - fontFamily: formValue.value.fontFamily, - fontWeight: formValue.value.fontWeight, - backgroundColor: formValue.value.backgroundColor, - showImage: formValue.value.showImage, - hideTimeout: formValue.value.hideTimeout, + input: { + preset: formValue.value.preset, + fontFamily: formValue.value.fontFamily, + fontWeight: formValue.value.fontWeight, + backgroundColor: formValue.value.backgroundColor, + showImage: formValue.value.showImage, + hideTimeout: formValue.value.hideTimeout, + }, }) discrete.notification.success({ diff --git a/frontend/dashboard/src/pages/overlays/now-playing/now-playing.vue b/frontend/dashboard/src/pages/overlays/now-playing/now-playing.vue index d2b1573a0..d01c516ed 100644 --- a/frontend/dashboard/src/pages/overlays/now-playing/now-playing.vue +++ b/frontend/dashboard/src/pages/overlays/now-playing/now-playing.vue @@ -1,12 +1,12 @@