mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 09:23:51 +00:00
1849b4d39f
* chore: migrate eslint to oxlint * chore: migrate prettier to oxfmt * chore: migrate typescript * chore: toThrow should have a expected value * chore: cast test value as Day * chore: small title fix * chore: mocks should be hoisted * chore: incorrect async useage * chore: test should be inside description * chore: test sohuld include an expeced * chore: oxfmt --------- Co-authored-by: alex-Arc <omnivox@LAPTOP-RC5SNBVV.localdomain>
83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { ProjectRundownsList } from 'ontime-types';
|
|
|
|
import { queryRefetchIntervalSlow } from '../../ontimeConfig';
|
|
import { PROJECT_RUNDOWNS } from '../api/constants';
|
|
import {
|
|
createRundown,
|
|
deleteRundown,
|
|
duplicateRundown,
|
|
fetchProjectRundownList,
|
|
loadRundown,
|
|
renameRundown,
|
|
} from '../api/rundown';
|
|
|
|
/**
|
|
* Project rundowns
|
|
*/
|
|
export function useProjectRundowns() {
|
|
const { data, status, isError, refetch, isFetching } = useQuery<ProjectRundownsList>({
|
|
queryKey: PROJECT_RUNDOWNS,
|
|
queryFn: ({ signal }) => fetchProjectRundownList({ signal }),
|
|
placeholderData: (previousData, _previousQuery) => previousData,
|
|
refetchInterval: queryRefetchIntervalSlow,
|
|
});
|
|
return { data: data ?? { loaded: '', rundowns: [] }, status, isError, refetch, isFetching };
|
|
}
|
|
|
|
export function useMutateProjectRundowns() {
|
|
const ontimeQueryClient = useQueryClient();
|
|
|
|
const { mutateAsync: create } = useMutation({
|
|
mutationFn: createRundown,
|
|
onMutate: () => {
|
|
ontimeQueryClient.cancelQueries({ queryKey: PROJECT_RUNDOWNS });
|
|
},
|
|
onSuccess: (response) => {
|
|
ontimeQueryClient.setQueryData(PROJECT_RUNDOWNS, response.data);
|
|
},
|
|
});
|
|
|
|
const { mutateAsync: duplicate } = useMutation({
|
|
mutationFn: duplicateRundown,
|
|
onMutate: () => {
|
|
ontimeQueryClient.cancelQueries({ queryKey: PROJECT_RUNDOWNS });
|
|
},
|
|
onSuccess: (response) => {
|
|
ontimeQueryClient.setQueryData(PROJECT_RUNDOWNS, response.data);
|
|
},
|
|
});
|
|
|
|
const { mutateAsync: rename } = useMutation({
|
|
mutationFn: ([rundownId, title]: Parameters<typeof renameRundown>) => renameRundown(rundownId, title),
|
|
onMutate: () => {
|
|
ontimeQueryClient.cancelQueries({ queryKey: PROJECT_RUNDOWNS });
|
|
},
|
|
onSuccess: (response) => {
|
|
ontimeQueryClient.setQueryData(PROJECT_RUNDOWNS, response.data);
|
|
},
|
|
});
|
|
|
|
const { mutateAsync: remove } = useMutation({
|
|
mutationFn: deleteRundown,
|
|
onMutate: () => {
|
|
ontimeQueryClient.cancelQueries({ queryKey: PROJECT_RUNDOWNS });
|
|
},
|
|
onSuccess: (response) => {
|
|
ontimeQueryClient.setQueryData(PROJECT_RUNDOWNS, response.data);
|
|
},
|
|
});
|
|
|
|
const { mutateAsync: load } = useMutation({
|
|
mutationFn: loadRundown,
|
|
onMutate: () => {
|
|
ontimeQueryClient.cancelQueries({ queryKey: PROJECT_RUNDOWNS });
|
|
},
|
|
onSuccess: (response) => {
|
|
ontimeQueryClient.setQueryData(PROJECT_RUNDOWNS, response.data);
|
|
},
|
|
});
|
|
|
|
return { create, duplicate, remove, load, rename };
|
|
}
|