feat: refactor party event action types and functions, update imports and naming conventions to use PartyEvent naming scheme, add createPartyUser function to party-user actions

This commit is contained in:
louiscklaw
2025-06-17 20:06:47 +08:00
parent ae7f005236
commit 583e31fd4d
2 changed files with 35 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
// src/actions/product.ts
// src/actions/party-event.ts
//
import { useMemo } from 'react';
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
@@ -74,6 +74,7 @@ type SearchResultsData = {
results: IPartyEventItem[];
};
// TODO: update useSearchPartyEvents
export function useSearchProducts(query: string) {
const url = query ? [endpoints.product.search, { params: { query } }] : '';
@@ -135,7 +136,6 @@ export async function updatePartyEvent(partyEventData: Partial<IPartyEventItem>)
/**
* Work in local
*/
mutate(
endpoints.partyEvent.list,
(currentData: any) => {
@@ -163,7 +163,6 @@ export async function deletePartyEvent(partyEventId: string) {
/**
* Work in local
*/
mutate(
endpoints.partyEvent.list,
(currentData: any) => {

View File

@@ -1,8 +1,9 @@
// src/actions/party-user.ts
//
import { useMemo } from 'react';
import axiosInstance, { endpoints, fetcher } from 'src/lib/axios';
import type { IPartyUserItem } from 'src/types/party-user';
import type { IProductItem } from 'src/types/product';
import type { IUserItem } from 'src/types/user';
import type { SWRConfiguration } from 'swr';
import useSWR, { mutate } from 'swr';
@@ -16,15 +17,15 @@ const swrOptions: SWRConfiguration = {
// ----------------------------------------------------------------------
type UsersData = {
partyUsers: IUserItem[];
type PartyUsersData = {
partyUsers: IPartyUserItem[];
};
// TODO: i want to refactor / tidy here
export function useGetPartyUsers() {
const url = endpoints.partyUser.list;
const { data, isLoading, error, isValidating } = useSWR<UsersData>(url, fetcher, swrOptions);
const { data, isLoading, error, isValidating } = useSWR<PartyUsersData>(url, fetcher, swrOptions);
const memoizedValue = useMemo(
() => ({
@@ -72,6 +73,7 @@ type SearchResultsData = {
results: IProductItem[];
};
// TODO: update useSearchProducts
export function useSearchProducts(query: string) {
const url = query ? [endpoints.product.search, { params: { query } }] : '';
@@ -115,6 +117,33 @@ type SaveUserData = {
password: string;
};
export async function createPartyUser(partyUserData: CreateUserData) {
/**
* Work on server
*/
const data = { partyUserData };
const {
data: { id },
} = await axiosInstance.post(endpoints.partyUser.create, data);
/**
* Work in local
*/
mutate(
endpoints.partyUser.list,
(currentData: any) => {
const currentPartyUsers: IPartyUserItem[] = currentData?.partyUsers;
const partyUsers = [...currentPartyUsers, { ...partyUserData, id }];
return { ...currentData, partyUsers };
},
false
);
}
// ----------------------------------------------------------------------
export async function updatePartyUser(partyUserData: Partial<IPartyUserItem>) {
/**
* Work on server
@@ -184,33 +213,6 @@ type CreateUserData = {
password: string;
};
export async function createPartyUser(partyUserData: CreateUserData) {
/**
* Work on server
*/
const data = { partyUserData };
const {
data: { id },
} = await axiosInstance.post(endpoints.partyUser.create, data);
/**
* Work in local
*/
mutate(
endpoints.partyUser.list,
(currentData: any) => {
const currentPartyUsers: IPartyUserItem[] = currentData?.partyUsers;
const partyUsers = [...currentPartyUsers, { ...partyUserData, id }];
return { ...currentData, partyUsers };
},
false
);
}
// ----------------------------------------------------------------------
export async function deletePartyUser(partyUserId: string) {
/**
* Work on server