-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNonprofitContext.tsx
More file actions
95 lines (85 loc) · 2.61 KB
/
NonprofitContext.tsx
File metadata and controls
95 lines (85 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {createContext, FunctionalComponent} from 'preact';
import {useCallback, useEffect, useState} from 'preact/hooks';
import {getCustomization, getNonprofit} from 'src/components/widget/api';
import {
Nonprofit,
NonprofitFetchError,
NonprofitFetching
} from 'src/components/widget/types/Nonprofit';
import {
DonateFlowCustomization,
DonateFlowCustomizationFetchError,
DonateFlowCustomizationFetching
} from 'src/components/widget/types/DonateFlowCustomization';
interface NonprofitContextData {
nonprofit: Nonprofit | typeof NonprofitFetchError | typeof NonprofitFetching;
parentNonprofit?: Nonprofit;
customization:
| DonateFlowCustomization
| typeof DonateFlowCustomizationFetching
| typeof DonateFlowCustomizationFetchError
| undefined;
}
export const NonprofitContext = createContext<NonprofitContextData>({
nonprofit: NonprofitFetching,
customization: DonateFlowCustomizationFetching
});
export const NonprofitContextProvider: FunctionalComponent<{
nonprofitSlug?: string;
code?: string;
}> = ({children, nonprofitSlug, code}) => {
const [nonprofit, setNonprofit] =
useState<NonprofitContextData['nonprofit']>(NonprofitFetching);
const [parentNonprofit, setParentNonprofitNonprofit] =
useState<NonprofitContextData['parentNonprofit']>();
const [customization, setCustomization] = useState<
NonprofitContextData['customization']
>(DonateFlowCustomizationFetching);
const fetchNonprofit = useCallback(async () => {
try {
if (!nonprofitSlug) {
throw new Error('No nonprofit slug provided');
}
const response = await getNonprofit(nonprofitSlug);
setNonprofit(response);
const parentNonprofitId =
response.eligibleDonationRecipientNonprofitIds?.length === 1
? response.eligibleDonationRecipientNonprofitIds[0]
: undefined;
if (parentNonprofitId) {
try {
const response = await getNonprofit(parentNonprofitId);
setParentNonprofitNonprofit(response);
} catch {
setParentNonprofitNonprofit(undefined);
}
}
if (code) {
try {
const customizationResponse = await getCustomization(
response.id,
code
);
setCustomization(customizationResponse);
} catch {
setCustomization(DonateFlowCustomizationFetchError);
}
} else {
setCustomization(undefined);
}
} catch {
setNonprofit(NonprofitFetchError);
}
}, [nonprofitSlug, code]);
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
fetchNonprofit();
}, [fetchNonprofit]);
return (
<NonprofitContext.Provider
value={{nonprofit, parentNonprofit, customization}}
>
{children}
</NonprofitContext.Provider>
);
};