Update gift-context.tsx

This commit is contained in:
2025-03-16 16:35:35 +01:00
parent 88c9e634de
commit f6ac22731d

View File

@@ -48,29 +48,34 @@ interface GiftContextState {
refetchCategories: (eventId: string) => Promise<any>;
fetchCategoryById: (id: string, eventId?: string) => void;
createCategory: (data: GiftCategoryCreate) => Promise<GiftCategory | undefined>;
createCategory: (
data: GiftCategoryCreate,
) => Promise<GiftCategory | undefined>;
updateCategory: (
id: string,
data: GiftCategoryUpdate,
eventId?: string
eventId?: string,
) => Promise<GiftCategory | undefined>;
deleteCategory: (
id: string,
eventId?: string,
) => Promise<GiftCategory | undefined>;
deleteCategory: (id: string, eventId?: string) => Promise<GiftCategory | undefined>;
associateCategoryWithEvent: (
categoryId: string,
eventId: string,
displayOrder?: number,
isVisible?: boolean
isVisible?: boolean,
) => Promise<GiftCategory | undefined>;
updateCategoryEventSettings: (
categoryId: string,
eventId: string,
displayOrder?: number,
isVisible?: boolean
isVisible?: boolean,
) => Promise<GiftCategory | undefined>;
getEventsForCategory: (categoryId: string) => Promise<any>;
reorderGiftsInCategory: (
categoryId: string,
giftIds: string[]
giftIds: string[],
) => Promise<GiftCategory | undefined>;
// Gift Items
@@ -84,21 +89,21 @@ interface GiftContextState {
createItem: (data: GiftItemCreate) => Promise<GiftItem | undefined>;
updateItem: (
id: string,
data: GiftItemUpdate
data: GiftItemUpdate,
) => Promise<GiftItem | undefined>;
deleteItem: (id: string) => Promise<GiftItem | undefined>;
updateItemStatus: (
id: string,
status: GiftStatus
status: GiftStatus,
) => Promise<GiftItem | undefined>;
reserveItem: (
id: string,
guestId: string,
quantity?: number
quantity?: number,
) => Promise<GiftItem | undefined>;
cancelReservation: (
id: string,
guestId: string
guestId: string,
) => Promise<GiftItem | undefined>;
// Gift Purchases
@@ -109,9 +114,13 @@ interface GiftContextState {
refetchPurchases: (giftId?: string, guestId?: string) => Promise<any>;
fetchPurchaseById: (id: string) => void;
createPurchase: (data: GiftPurchaseCreate) => Promise<GiftPurchase | undefined>;
createPurchase: (
data: GiftPurchaseCreate,
) => Promise<GiftPurchase | undefined>;
fetchPurchasesByGift: (giftId: string) => Promise<GiftPurchase[] | undefined>;
fetchPurchasesByGuest: (guestId: string) => Promise<GiftPurchase[] | undefined>;
fetchPurchasesByGuest: (
guestId: string,
) => Promise<GiftPurchase[] | undefined>;
// Current selections
currentCategoryId: string | null;
@@ -242,10 +251,16 @@ interface GiftProviderProps {
// Gift Provider Component
export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
const queryClient = useQueryClient();
const [currentCategoryId, setCurrentCategoryId] = React.useState<string | null>(null);
const [currentCategoryId, setCurrentCategoryId] = React.useState<
string | null
>(null);
const [currentItemId, setCurrentItemId] = React.useState<string | null>(null);
const [currentPurchaseId, setCurrentPurchaseId] = React.useState<string | null>(null);
const [currentEventId, setCurrentEventId] = React.useState<string | null>(null);
const [currentPurchaseId, setCurrentPurchaseId] = React.useState<
string | null
>(null);
const [currentEventId, setCurrentEventId] = React.useState<string | null>(
null,
);
// Fetch all categories for an event
const {
@@ -255,10 +270,10 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
refetch: refetchCategoriesInternal,
} = useQuery({
queryKey: ["giftCategories", currentEventId],
queryFn: () =>
currentEventId
? readGiftCategories({
path: { event_id: currentEventId }
queryFn: () =>
currentEventId
? readGiftCategories({
path: { event_id: currentEventId },
}).then((res) => res.data)
: Promise.resolve(undefined),
enabled: !!currentEventId,
@@ -270,19 +285,19 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
};
// Fetch specific category
const {
data: category,
const {
data: category,
isLoading: isLoadingCategory,
error: categoryError,
} = useQuery({
queryKey: ["giftCategory", currentCategoryId, currentEventId],
queryFn: () =>
currentCategoryId
? readGiftCategory({
path: {
category_id: currentCategoryId
queryFn: () =>
currentCategoryId
? readGiftCategory({
path: {
category_id: currentCategoryId,
},
query: currentEventId ? { event_id: currentEventId } : undefined
query: currentEventId ? { event_id: currentEventId } : undefined,
}).then((res) => res.data)
: Promise.resolve(undefined),
enabled: !!currentCategoryId,
@@ -298,36 +313,45 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
// Create Category Mutation
const createCategoryMutation = useMutation({
mutationFn: (data: GiftCategoryCreate) =>
createGiftCategory({ body: data }).then((res) => res.data),
createGiftCategory({
body: data,
query: { event_id: currentEventId || "" },
}).then((res) => res.data),
onSuccess: () => {
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftCategories", currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftCategories", currentEventId],
});
}
},
});
// Update Category Mutation
const updateCategoryMutation = useMutation({
mutationFn: ({
id,
data,
eventId
}: {
id: string;
data: GiftCategoryUpdate;
mutationFn: ({
id,
data,
eventId,
}: {
id: string;
data: GiftCategoryUpdate;
eventId?: string;
}) =>
updateGiftCategory({
updateGiftCategory({
path: { category_id: id },
body: data,
query: eventId ? { event_id: eventId } : undefined
query: eventId ? { event_id: eventId } : undefined,
}).then((res) => res.data),
onSuccess: () => {
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftCategories", currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftCategories", currentEventId],
});
}
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftCategory", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftCategory", currentCategoryId],
});
}
},
});
@@ -335,13 +359,15 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
// Delete Category Mutation
const deleteCategoryMutation = useMutation({
mutationFn: ({ id, eventId }: { id: string; eventId?: string }) =>
deleteGiftCategory({
deleteGiftCategory({
path: { category_id: id },
query: eventId ? { event_id: eventId } : undefined
query: eventId ? { event_id: eventId } : undefined,
}).then((res) => res.data),
onSuccess: () => {
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftCategories", currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftCategories", currentEventId],
});
}
},
});
@@ -359,13 +385,16 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
if (currentCategoryId) {
query.category_id = currentCategoryId;
}
if (currentEventId) {
query.event_id = currentEventId;
// We don't need to add event_id to query since it's part of the path
if (!currentEventId) {
return Promise.resolve(undefined);
}
return Object.keys(query).length > 0
? readGiftItems({ query }).then((res) => res.data)
: Promise.resolve(undefined);
return readGiftItems({
path: { event_id: currentEventId },
query,
}).then((res) => res.data);
},
enabled: !!(currentCategoryId || currentEventId),
});
@@ -381,18 +410,18 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
};
// Fetch specific item
const {
data: item,
const {
data: item,
isLoading: isLoadingItem,
error: itemError,
} = useQuery({
queryKey: ["giftItem", currentItemId],
queryFn: () =>
currentItemId
? readGiftItem({
path: {
gift_id: currentItemId
}
queryFn: () =>
currentItemId
? readGiftItem({
path: {
item_id: currentItemId,
},
}).then((res) => res.data)
: Promise.resolve(undefined),
enabled: !!currentItemId,
@@ -408,36 +437,40 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
createGiftItem({ body: data }).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", null, currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", null, currentEventId],
});
}
},
});
// Update Item Mutation
const updateItemMutation = useMutation({
mutationFn: ({
id,
data
}: {
id: string;
data: GiftItemUpdate;
}) =>
updateGiftItem({
path: { gift_id: id },
body: data
mutationFn: ({ id, data }: { id: string; data: GiftItemUpdate }) =>
updateGiftItem({
path: { item_id: id },
body: data,
}).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", null, currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", null, currentEventId],
});
}
if (currentItemId) {
queryClient.invalidateQueries({ queryKey: ["giftItem", currentItemId] });
queryClient.invalidateQueries({
queryKey: ["giftItem", currentItemId],
});
}
},
});
@@ -445,163 +478,181 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
// Delete Item Mutation
const deleteItemMutation = useMutation({
mutationFn: (id: string) =>
deleteGiftItem({
path: { gift_id: id }
deleteGiftItem({
path: { item_id: id },
}).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", null, currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", null, currentEventId],
});
}
},
});
// Update Item Status Mutation
const updateItemStatusMutation = useMutation({
mutationFn: ({
id,
status
}: {
id: string;
status: GiftStatus;
}) =>
updateGiftItemStatus({
path: { gift_id: id },
body: { status }
mutationFn: ({ id, status }: { id: string; status: GiftStatus }) =>
updateGiftItemStatus({
path: { item_id: id },
query: { status },
}).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", null, currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", null, currentEventId],
});
}
if (currentItemId) {
queryClient.invalidateQueries({ queryKey: ["giftItem", currentItemId] });
queryClient.invalidateQueries({
queryKey: ["giftItem", currentItemId],
});
}
},
});
// Reserve Item Mutation
const reserveItemMutation = useMutation({
mutationFn: ({
id,
mutationFn: ({
id,
guestId,
quantity
}: {
id: string;
quantity,
}: {
id: string;
guestId: string;
quantity?: number;
}) =>
reserveGiftItem({
path: { gift_id: id },
body: {
reserveGiftItem({
path: { item_id: id },
query: {
guest_id: guestId,
quantity
}
quantity,
},
}).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", null, currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", null, currentEventId],
});
}
if (currentItemId) {
queryClient.invalidateQueries({ queryKey: ["giftItem", currentItemId] });
queryClient.invalidateQueries({
queryKey: ["giftItem", currentItemId],
});
}
},
});
// Cancel Reservation Mutation
const cancelReservationMutation = useMutation({
mutationFn: ({
id,
guestId
}: {
id: string;
guestId: string;
}) =>
cancelGiftReservation({
path: {
gift_id: id,
guest_id: guestId
}
mutationFn: ({ id, guestId }: { id: string; guestId: string }) =>
cancelGiftReservation({
path: {
item_id: id,
},
query: {
guest_id: guestId,
},
}).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftItems", null, currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftItems", null, currentEventId],
});
}
if (currentItemId) {
queryClient.invalidateQueries({ queryKey: ["giftItem", currentItemId] });
queryClient.invalidateQueries({
queryKey: ["giftItem", currentItemId],
});
}
},
});
// Associate Category With Event Mutation
const associateCategoryWithEventMutation = useMutation({
mutationFn: ({
categoryId,
eventId,
displayOrder,
isVisible
}: {
categoryId: string;
eventId: string;
displayOrder?: number;
mutationFn: ({
categoryId,
eventId,
displayOrder,
isVisible,
}: {
categoryId: string;
eventId: string;
displayOrder?: number;
isVisible?: boolean;
}) =>
associateCategoryWithEvent({
path: {
associateCategoryWithEvent({
path: {
category_id: categoryId,
event_id: eventId
event_id: eventId,
},
body: {
query: {
display_order: displayOrder,
is_visible: isVisible
}
is_visible: isVisible,
},
}).then((res) => res.data),
onSuccess: () => {
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftCategories", currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftCategories", currentEventId],
});
}
},
});
// Update Category Event Settings Mutation
const updateCategoryEventSettingsMutation = useMutation({
mutationFn: ({
categoryId,
eventId,
displayOrder,
isVisible
}: {
categoryId: string;
eventId: string;
displayOrder?: number;
mutationFn: ({
categoryId,
eventId,
displayOrder,
isVisible,
}: {
categoryId: string;
eventId: string;
displayOrder?: number;
isVisible?: boolean;
}) =>
updateCategoryEventSettings({
path: {
updateCategoryEventSettings({
path: {
category_id: categoryId,
event_id: eventId
event_id: eventId,
},
body: {
query: {
display_order: displayOrder,
is_visible: isVisible
}
is_visible: isVisible,
},
}).then((res) => res.data),
onSuccess: () => {
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftCategories", currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftCategories", currentEventId],
});
}
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftCategory", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftCategory", currentCategoryId],
});
}
},
});
@@ -609,29 +660,33 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
// Get Events For Category Query
const getEventsForCategoryQuery = async (categoryId: string) => {
return getEventsForCategory({
path: { category_id: categoryId }
path: { category_id: categoryId },
}).then((res) => res.data);
};
// Reorder Gifts In Category Mutation
const reorderGiftsInCategoryMutation = useMutation({
mutationFn: ({
categoryId,
giftIds
}: {
categoryId: string;
mutationFn: ({
categoryId,
giftIds,
}: {
categoryId: string;
giftIds: string[];
}) =>
reorderGiftsInCategory({
reorderGiftsInCategory({
path: { category_id: categoryId },
body: { gift_ids: giftIds }
body: giftIds.reduce((acc, id, index) => ({ ...acc, [id]: index }), {}),
}).then((res) => res.data),
onSuccess: () => {
if (currentCategoryId) {
queryClient.invalidateQueries({ queryKey: ["giftCategory", currentCategoryId] });
queryClient.invalidateQueries({
queryKey: ["giftCategory", currentCategoryId],
});
}
if (currentEventId) {
queryClient.invalidateQueries({ queryKey: ["giftCategories", currentEventId] });
queryClient.invalidateQueries({
queryKey: ["giftCategories", currentEventId],
});
}
},
});
@@ -644,10 +699,10 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
refetch: refetchPurchasesInternal,
} = useQuery({
queryKey: ["giftPurchases", currentItemId, null],
queryFn: () =>
currentItemId
? readGiftPurchasesByGift({
path: { gift_id: currentItemId }
queryFn: () =>
currentItemId
? readGiftPurchasesByGift({
path: { gift_id: currentItemId },
}).then((res) => res.data)
: Promise.resolve(undefined),
enabled: !!currentItemId,
@@ -661,16 +716,16 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
};
// Fetch specific purchase
const {
data: purchase,
const {
data: purchase,
isLoading: isLoadingPurchase,
error: purchaseError,
} = useQuery({
queryKey: ["giftPurchase", currentPurchaseId],
queryFn: () =>
currentPurchaseId
? readGiftPurchase({
path: { purchase_id: currentPurchaseId }
queryFn: () =>
currentPurchaseId
? readGiftPurchase({
path: { purchase_id: currentPurchaseId },
}).then((res) => res.data)
: Promise.resolve(undefined),
enabled: !!currentPurchaseId,
@@ -686,8 +741,12 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
createGiftPurchase({ body: data }).then((res) => res.data),
onSuccess: () => {
if (currentItemId) {
queryClient.invalidateQueries({ queryKey: ["giftPurchases", currentItemId] });
queryClient.invalidateQueries({ queryKey: ["giftItem", currentItemId] });
queryClient.invalidateQueries({
queryKey: ["giftPurchases", currentItemId],
});
queryClient.invalidateQueries({
queryKey: ["giftItem", currentItemId],
});
}
},
});
@@ -695,14 +754,14 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
// Fetch Purchases By Gift
const fetchPurchasesByGiftQuery = async (giftId: string) => {
return readGiftPurchasesByGift({
path: { gift_id: giftId }
path: { gift_id: giftId },
}).then((res) => res.data);
};
// Fetch Purchases By Guest
const fetchPurchasesByGuestQuery = async (guestId: string) => {
return readGiftPurchasesByGuest({
path: { guest_id: guestId }
path: { guest_id: guestId },
}).then((res) => res.data);
};
@@ -716,9 +775,9 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
fetchCategoryById,
createCategory: createCategoryMutation.mutateAsync,
updateCategory: (id, data, eventId) =>
updateCategory: (id, data, eventId) =>
updateCategoryMutation.mutateAsync({ id, data, eventId }),
deleteCategory: (id, eventId) =>
deleteCategory: (id, eventId) =>
deleteCategoryMutation.mutateAsync({ id, eventId }),
// Gift Items
@@ -730,8 +789,7 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
fetchItemById,
createItem: createItemMutation.mutateAsync,
updateItem: (id, data) =>
updateItemMutation.mutateAsync({ id, data }),
updateItem: (id, data) => updateItemMutation.mutateAsync({ id, data }),
deleteItem: deleteItemMutation.mutateAsync,
updateItemStatus: (id, status) =>
updateItemStatusMutation.mutateAsync({ id, status }),
@@ -741,10 +799,30 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
cancelReservationMutation.mutateAsync({ id, guestId }),
// Gift Categories additional methods
associateCategoryWithEvent: (categoryId, eventId, displayOrder, isVisible) =>
associateCategoryWithEventMutation.mutateAsync({ categoryId, eventId, displayOrder, isVisible }),
updateCategoryEventSettings: (categoryId, eventId, displayOrder, isVisible) =>
updateCategoryEventSettingsMutation.mutateAsync({ categoryId, eventId, displayOrder, isVisible }),
associateCategoryWithEvent: (
categoryId,
eventId,
displayOrder,
isVisible,
) =>
associateCategoryWithEventMutation.mutateAsync({
categoryId,
eventId,
displayOrder,
isVisible,
}),
updateCategoryEventSettings: (
categoryId,
eventId,
displayOrder,
isVisible,
) =>
updateCategoryEventSettingsMutation.mutateAsync({
categoryId,
eventId,
displayOrder,
isVisible,
}),
getEventsForCategory: getEventsForCategoryQuery,
reorderGiftsInCategory: (categoryId, giftIds) =>
reorderGiftsInCategoryMutation.mutateAsync({ categoryId, giftIds }),
@@ -771,7 +849,12 @@ export const GiftProvider: React.FC<GiftProviderProps> = ({ children }) => {
currentEventId,
setCurrentEventId,
error: (categoriesError || categoryError || itemsError || itemError || purchasesError || purchaseError) as Error | null,
error: (categoriesError ||
categoryError ||
itemsError ||
itemError ||
purchasesError ||
purchaseError) as Error | null,
};
return (