178 lines
4.5 KiB
TypeScript
178 lines
4.5 KiB
TypeScript
// src/actions/party-event.ts
|
|
//
|
|
import { useMemo } from 'react';
|
|
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
|
|
import type { IPartyEventItem } from 'src/types/party-event';
|
|
import type { SWRConfiguration } from 'swr';
|
|
import useSWR, { mutate } from 'swr';
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
const swrOptions: SWRConfiguration = {
|
|
revalidateIfStale: false,
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
};
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type PartyEventsData = {
|
|
partyEvents: IPartyEventItem[];
|
|
};
|
|
|
|
export function useGetPartyEvents() {
|
|
const url = endpoints.partyEvent.list;
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<PartyEventsData>(
|
|
url,
|
|
fetcher,
|
|
swrOptions
|
|
);
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
partyEvents: data?.partyEvents || [],
|
|
partyEventsLoading: isLoading,
|
|
partyEventsError: error,
|
|
partyEventsValidating: isValidating,
|
|
partyEventsEmpty: !isLoading && !isValidating && !data?.partyEvents.length,
|
|
}),
|
|
[data?.partyEvents, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type PartyEventData = {
|
|
partyEvent: IPartyEventItem;
|
|
};
|
|
|
|
export function useGetPartyEvent(partyEventId: string) {
|
|
const url = partyEventId ? [endpoints.partyEvent.details, { params: { partyEventId } }] : '';
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<PartyEventData>(url, fetcher, swrOptions);
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
partyEvent: data?.partyEvent,
|
|
partyEventLoading: isLoading,
|
|
partyEventError: error,
|
|
partyEventValidating: isValidating,
|
|
mutate,
|
|
}),
|
|
[data?.partyEvent, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
type SearchResultsData = {
|
|
results: IPartyEventItem[];
|
|
};
|
|
|
|
// TODO: update useSearchPartyEvents
|
|
export function useSearchProducts(query: string) {
|
|
const url = query ? [endpoints.product.search, { params: { query } }] : '';
|
|
|
|
const { data, isLoading, error, isValidating } = useSWR<SearchResultsData>(url, fetcher, {
|
|
...swrOptions,
|
|
keepPreviousData: true,
|
|
});
|
|
|
|
const memoizedValue = useMemo(
|
|
() => ({
|
|
searchResults: data?.results || [],
|
|
searchLoading: isLoading,
|
|
searchError: error,
|
|
searchValidating: isValidating,
|
|
searchEmpty: !isLoading && !isValidating && !data?.results.length,
|
|
}),
|
|
[data?.results, error, isLoading, isValidating]
|
|
);
|
|
|
|
return memoizedValue;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
export async function createPartyEvent(partyEventData: IPartyEventItem) {
|
|
/**
|
|
* Work on server
|
|
*/
|
|
const data = { partyEventData };
|
|
const {
|
|
data: { id },
|
|
} = await axiosInstance.post(endpoints.partyEvent.create, data);
|
|
|
|
/**
|
|
* Work in local
|
|
*/
|
|
mutate(
|
|
endpoints.partyEvent.list,
|
|
(currentData: any) => {
|
|
const currentPartyEvents: IPartyEventItem[] = currentData?.partyEvents;
|
|
|
|
const partyEvents = [...currentPartyEvents, { ...partyEventData, id }];
|
|
|
|
return { ...currentData, partyEvents };
|
|
},
|
|
false
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
export async function updatePartyEvent(partyEventData: Partial<IPartyEventItem>) {
|
|
/**
|
|
* Work on server
|
|
*/
|
|
const data = { partyEventData };
|
|
await axiosInstance.put(endpoints.partyEvent.update, data);
|
|
|
|
/**
|
|
* Work in local
|
|
*/
|
|
mutate(
|
|
endpoints.partyEvent.list,
|
|
(currentData: any) => {
|
|
const currentPartyEvents: IPartyEventItem[] = currentData?.partyEvents;
|
|
|
|
const partyEvents = currentPartyEvents.map((partyEvent) =>
|
|
partyEvent.id === partyEventData.id ? { ...partyEvent, ...partyEventData } : partyEvent
|
|
);
|
|
|
|
return { ...currentData, partyEvents };
|
|
},
|
|
false
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
export async function deletePartyEvent(partyEventId: string) {
|
|
/**
|
|
* Work on server
|
|
*/
|
|
const data = { partyEventId };
|
|
await axiosInstance.patch(endpoints.partyEvent.delete, data);
|
|
|
|
/**
|
|
* Work in local
|
|
*/
|
|
mutate(
|
|
endpoints.partyEvent.list,
|
|
(currentData: any) => {
|
|
const currentProducts: IPartyEventItem[] = currentData?.partyEvents;
|
|
|
|
const partyEvents = currentProducts.filter((partyEvent) => partyEvent.id !== partyEventId);
|
|
|
|
return { ...currentData, partyEvents };
|
|
},
|
|
false
|
|
);
|
|
}
|