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