"add PartyEvent frontend module with mock data, API actions, UI components and routing configuration"
This commit is contained in:
175
03_source/frontend/src/actions/party-event.ts
Normal file
175
03_source/frontend/src/actions/party-event.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
// src/actions/product.ts
|
||||
//
|
||||
import { useMemo } from 'react';
|
||||
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
|
||||
import type { IProductItem } 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 ProductsData = {
|
||||
products: IProductItem[];
|
||||
};
|
||||
|
||||
export function useGetProducts() {
|
||||
const url = endpoints.product.list;
|
||||
|
||||
const { data, isLoading, error, isValidating } = useSWR<ProductsData>(url, fetcher, swrOptions);
|
||||
|
||||
const memoizedValue = useMemo(
|
||||
() => ({
|
||||
products: data?.products || [],
|
||||
productsLoading: isLoading,
|
||||
productsError: error,
|
||||
productsValidating: isValidating,
|
||||
productsEmpty: !isLoading && !isValidating && !data?.products.length,
|
||||
}),
|
||||
[data?.products, error, isLoading, isValidating]
|
||||
);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type ProductData = {
|
||||
product: IProductItem;
|
||||
};
|
||||
|
||||
export function useGetPartyEvent(productId: string) {
|
||||
const url = productId ? [endpoints.product.details, { params: { productId } }] : '';
|
||||
|
||||
const { data, isLoading, error, isValidating } = useSWR<ProductData>(url, fetcher, swrOptions);
|
||||
|
||||
const memoizedValue = useMemo(
|
||||
() => ({
|
||||
partyEvent: data?.product,
|
||||
partyEventLoading: isLoading,
|
||||
partyEventError: error,
|
||||
partyEventValidating: isValidating,
|
||||
mutate,
|
||||
}),
|
||||
[data?.product, error, isLoading, isValidating]
|
||||
);
|
||||
|
||||
return memoizedValue;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
type SearchResultsData = {
|
||||
results: IProductItem[];
|
||||
};
|
||||
|
||||
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 createProduct(productData: IProductItem) {
|
||||
/**
|
||||
* Work on server
|
||||
*/
|
||||
const data = { productData };
|
||||
const {
|
||||
data: { id },
|
||||
} = await axiosInstance.post(endpoints.product.create, data);
|
||||
|
||||
/**
|
||||
* Work in local
|
||||
*/
|
||||
mutate(
|
||||
endpoints.product.list,
|
||||
(currentData: any) => {
|
||||
const currentProducts: IProductItem[] = currentData?.products;
|
||||
|
||||
const products = [...currentProducts, { ...productData, id }];
|
||||
|
||||
return { ...currentData, products };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
export async function updateProduct(productData: Partial<IProductItem>) {
|
||||
/**
|
||||
* Work on server
|
||||
*/
|
||||
const data = { productData };
|
||||
await axiosInstance.put(endpoints.product.update, data);
|
||||
|
||||
/**
|
||||
* Work in local
|
||||
*/
|
||||
|
||||
mutate(
|
||||
endpoints.product.list,
|
||||
(currentData: any) => {
|
||||
const currentProducts: IProductItem[] = currentData?.products;
|
||||
|
||||
const products = currentProducts.map((product) =>
|
||||
product.id === productData.id ? { ...product, ...productData } : product
|
||||
);
|
||||
|
||||
return { ...currentData, products };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
export async function deleteProduct(productId: string) {
|
||||
/**
|
||||
* Work on server
|
||||
*/
|
||||
const data = { productId };
|
||||
await axiosInstance.patch(endpoints.product.delete, data);
|
||||
|
||||
/**
|
||||
* Work in local
|
||||
*/
|
||||
|
||||
mutate(
|
||||
endpoints.product.list,
|
||||
(currentData: any) => {
|
||||
console.log({ currentData });
|
||||
const currentProducts: IProductItem[] = currentData?.products;
|
||||
|
||||
const products = currentProducts.filter((product) => product.id !== productId);
|
||||
|
||||
return { ...currentData, products };
|
||||
},
|
||||
false
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user