2021-08-16 13:44:00 +00:00
|
|
|
import React, { createContext } from "react";
|
|
|
|
|
|
|
|
import { useGiftCardDetailsQuery } from "../../queries";
|
|
|
|
import { GiftCardDetails_giftCard } from "../../types/GiftCardDetails";
|
2021-09-20 13:41:18 +00:00
|
|
|
import { ExtendedGiftCard } from "./types";
|
|
|
|
import { getExtendedGiftCard } from "./utils";
|
2021-08-16 13:44:00 +00:00
|
|
|
|
|
|
|
interface GiftCardDetailsProviderProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GiftCardDetailsConsumerProps {
|
2021-09-20 13:41:18 +00:00
|
|
|
giftCard: ExtendedGiftCard<GiftCardDetails_giftCard> | undefined;
|
2021-08-16 13:44:00 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const GiftCardDetailsContext = createContext<
|
|
|
|
GiftCardDetailsConsumerProps
|
|
|
|
>(null);
|
|
|
|
|
|
|
|
const GiftCardDetailsProvider: React.FC<GiftCardDetailsProviderProps> = ({
|
|
|
|
children,
|
|
|
|
id
|
|
|
|
}) => {
|
|
|
|
const { data, loading } = useGiftCardDetailsQuery({
|
|
|
|
displayLoader: true,
|
|
|
|
variables: { id }
|
|
|
|
});
|
|
|
|
|
|
|
|
const providerValues: GiftCardDetailsConsumerProps = {
|
2021-09-20 13:41:18 +00:00
|
|
|
giftCard: getExtendedGiftCard(data?.giftCard),
|
2021-08-16 13:44:00 +00:00
|
|
|
loading
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GiftCardDetailsContext.Provider value={providerValues}>
|
|
|
|
{children}
|
|
|
|
</GiftCardDetailsContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GiftCardDetailsProvider;
|