-
Notifications
You must be signed in to change notification settings - Fork 9
/
account-details.js
116 lines (89 loc) · 3.25 KB
/
account-details.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021-2022 Ellucian Company L.P. and its affiliates.
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
// eslint-disable-next-line camelcase
import { unstable_batchedUpdates } from 'react-dom';
import log from 'loglevel';
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
import { useCache, useCardInfo, useData } from '@ellucian/experience-extension/extension-utilities';
import { fetchAccountDetails } from '../data/account-details';
const logger = log.getLogger('default');
const Context = createContext()
const cacheKey = 'account-details';
const queryClient = new QueryClient();
function AccountDetailsProviderInternal({children}) {
// Experience SDK hooks
const { getItem, storeItem } = useCache();
const { configuration, cardConfiguration, cardId } = useCardInfo();
const { getExtensionJwt } = useData();
const { lambdaUrl } = configuration || cardConfiguration || {};
const [ loadDataFromQuery, setLoadDataFromQuery ] = useState(false);
const [ loadDataFromCache, setLoadDataFromCache ] = useState(true);
const [ cachedData, setCachedData ] = useState();
const { data, isError, isLoading } = useQuery(
['account-details', {getExtensionJwt, lambdaUrl}],
fetchAccountDetails,
{
enabled: Boolean(loadDataFromQuery && getExtensionJwt && lambdaUrl),
placeholderData: cachedData
}
);
useEffect(() => {
if (cardId && loadDataFromCache) {
(async () => {
// check for cached data
const { data: cacheData } = await getItem({key: cacheKey, scope: cardId});
unstable_batchedUpdates(() => {
setLoadDataFromCache(false);
if (cacheData) {
setCachedData(cacheData);
}
setLoadDataFromQuery(true);
})
})();
}
}, [cardId, loadDataFromCache]);
useEffect(() => {
if (cardId && data) {
storeItem({data, key: cacheKey, scope: cardId});
setLoadDataFromQuery(false);
setLoadDataFromCache(false);
}
}, [cardId, data]);
const contextValue = useMemo(() => {
return {
data: data || cachedData,
isError,
isLoading
}
}, [ cachedData, data, isError, isLoading ]);
useEffect(() => {
logger.debug('AccountDetailsProvider mounted');
return () => {
logger.debug('AccountDetailsProvider unmounted');
}
}, []);
return (
<Context.Provider value={contextValue}>
{children}
</Context.Provider>
)
}
AccountDetailsProviderInternal.propTypes = {
children: PropTypes.object.isRequired
}
export function AccountDetailsProvider({children}) {
return (
<QueryClientProvider client={queryClient}>
<AccountDetailsProviderInternal>
{children}
</AccountDetailsProviderInternal>
</QueryClientProvider>
)
}
AccountDetailsProvider.propTypes = {
children: PropTypes.object.isRequired
}
export function useAccountDetails() {
return useContext(Context);
}