-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new HOC to get policy.connections
data
#39132
Changes from all commits
06e5ea4
c945585
e0532d5
e6292e3
b509d0f
8e32fde
d8139a8
9a44aeb
cf2cce6
2370278
db3252e
0eca9cd
b7ab9e0
1b09ba0
32e979b
6f5d49b
14c8e92
357ce25
d9340cf
68db14d
4ba46b9
2254f3e
b1272d4
d29646d
124f534
c1e0cac
515833e
f21307a
a44ab1c
170cf3a
f25a3e5
31cd1ec
9457554
c4593e7
4804fd1
e19060b
a084e52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type OpenPolicyAccountingPageParams = { | ||
policyID: string; | ||
}; | ||
|
||
export default OpenPolicyAccountingPageParams; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Onyx from 'react-native-onyx'; | ||
import type {OnyxUpdate} from 'react-native-onyx'; | ||
import * as API from '@libs/API'; | ||
import type {OpenPolicyAccountingPageParams} from '@libs/API/parameters'; | ||
import {READ_COMMANDS} from '@libs/API/types'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
||
function openPolicyAccountingPage(policyID: string) { | ||
const hasConnectionsDataBeenFetchedKey = `${ONYXKEYS.COLLECTION.POLICY_HAS_CONNECTIONS_DATA_BEEN_FETCHED}${policyID}` as const; | ||
|
||
const optimisticData: OnyxUpdate[] = [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: hasConnectionsDataBeenFetchedKey, | ||
value: false, | ||
}, | ||
]; | ||
const finallyData: OnyxUpdate[] = [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: hasConnectionsDataBeenFetchedKey, | ||
value: true, | ||
}, | ||
]; | ||
Comment on lines
+18
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hayata-suenaga After playing with this, I think you missed the If you do:
I would have expected that an Offline blocker would appear since we haven't loaded the policy connections, but it doesn't because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the PR to fix the issue. Thank you for letting me know of the issue 🙇 |
||
|
||
const parameters: OpenPolicyAccountingPageParams = { | ||
policyID, | ||
}; | ||
|
||
API.read(READ_COMMANDS.OPEN_POLICY_ACCOUNTING_PAGE, parameters, { | ||
optimisticData, | ||
finallyData, | ||
}); | ||
} | ||
|
||
// More action functions will be added later | ||
// eslint-disable-next-line import/prefer-default-export | ||
export {openPolicyAccountingPage}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, {useEffect} from 'react'; | ||
import type {ComponentType} from 'react'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView'; | ||
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import useNetwork from '@hooks/useNetwork'; | ||
import {openPolicyAccountingPage} from '@libs/actions/PolicyConnections'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import withPolicy from './withPolicy'; | ||
import type {WithPolicyProps} from './withPolicy'; | ||
|
||
type WithPolicyConnectionsProps = WithPolicyProps; | ||
|
||
/** | ||
* Higher-order component that fetches the connections data and populates | ||
* the corresponding field of the policy object if the field is empty. It then passes the policy object | ||
* to the wrapped component. | ||
* | ||
* Use this HOC when you need the policy object with its connections field populated. | ||
* | ||
* Only the active policy gets the complete policy data upon app start that includes the connections data. | ||
* For other policies, the connections data needs to be fetched when it's needed. | ||
*/ | ||
function withPolicyConnections(WrappedComponent: ComponentType<WithPolicyConnectionsProps>) { | ||
function WithPolicyConnections({policy, policyDraft, route}: WithPolicyConnectionsProps) { | ||
const {isOffline} = useNetwork(); | ||
const [hasConnectionsDataBeenFetched, {status}] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_HAS_CONNECTIONS_DATA_BEEN_FETCHED}${policy?.id ?? '0'}`, { | ||
initWithStoredValues: false, | ||
}); | ||
|
||
useEffect(() => { | ||
// When the accounting feature is not enabled, or if the connections data already exists, | ||
// there is no need to fetch the connections data. | ||
if (!policy || !policy.areConnectionsEnabled || !!hasConnectionsDataBeenFetched || !!policy.connections) { | ||
return; | ||
} | ||
|
||
openPolicyAccountingPage(policy.id); | ||
}, [hasConnectionsDataBeenFetched, policy]); | ||
|
||
if (status === 'loading' || !hasConnectionsDataBeenFetched) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always show the loader when the policy hasn't enabled the accounting feature. Should we let the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm, how is this accessible in the UI if accounting isn't enabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mollfpr, this page won't be acceesible for users who hasn't enabled the accounting feature. Also, even if the user somehow be able to access this page, it won't cause the infinite loading state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, just to be clear, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems broken. I'm getting infinite loader when I try to visit the page again Screen.Recording.2024-04-18.at.5.48.40.PM.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @s77rt similar problem with going offline and then offline: #39132 (comment) @hayata-suenaga is working on it here #40375 |
||
if (isOffline) { | ||
return ( | ||
<FullPageOfflineBlockingView> | ||
<WrappedComponent | ||
policy={policy} | ||
policyDraft={policyDraft} | ||
route={route} | ||
/> | ||
</FullPageOfflineBlockingView> | ||
); | ||
} | ||
|
||
return <FullScreenLoadingIndicator />; | ||
} | ||
|
||
return ( | ||
<WrappedComponent | ||
policy={policy} | ||
policyDraft={policyDraft} | ||
route={route} | ||
/> | ||
); | ||
} | ||
|
||
return withPolicy(WithPolicyConnections); | ||
} | ||
|
||
export default withPolicyConnections; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
as const
necessary here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: a084e52
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you removed it from a different spot. Is it still necessary here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
const
is necessary here, otherwise thehasConnectionsDataBeenFetchedKey
getsstring
as a type and thenOnyxUpdate
is not happy with such key:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha I definitely moved the
const
from a different place but it didn't cause the TS error 😆 for this particular one