-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Create TagSettingsPage #38335
Create TagSettingsPage #38335
Changes from 6 commits
88a193d
b71b348
f1122da
2038d55
e72a1e1
8654a07
397e102
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,10 @@ | ||
type SetPolicyTagsEnabled = { | ||
policyID: string; | ||
/** | ||
* Stringified JSON object with type of following structure: | ||
* Array<{name: string; enabled: boolean}> | ||
*/ | ||
tags: string; | ||
}; | ||
|
||
export default SetPolicyTagsEnabled; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ import Navigation from '@libs/Navigation/Navigation'; | |
import * as NumberUtils from '@libs/NumberUtils'; | ||
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; | ||
import * as PhoneNumber from '@libs/PhoneNumber'; | ||
import * as PolicyUtils from '@libs/PolicyUtils'; | ||
import * as ReportActionsUtils from '@libs/ReportActionsUtils'; | ||
import * as ReportUtils from '@libs/ReportUtils'; | ||
import * as TransactionUtils from '@libs/TransactionUtils'; | ||
|
@@ -69,6 +70,7 @@ import type { | |
PolicyCategory, | ||
PolicyMember, | ||
PolicyTagList, | ||
PolicyTags, | ||
RecentlyUsedCategories, | ||
RecentlyUsedTags, | ||
ReimbursementAccount, | ||
|
@@ -2900,6 +2902,91 @@ function createPolicyTag(policyID: string, tagName: string) { | |
API.write(WRITE_COMMANDS.CREATE_POLICY_TAG, parameters, onyxData); | ||
} | ||
|
||
function setWorkspaceTagEnabled(policyID: string, tagsToUpdate: Record<string, {name: string; enabled: boolean}>) { | ||
const policyTag = PolicyUtils.getTagLists(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})?.[0] ?? {}; | ||
|
||
const onyxData: OnyxData = { | ||
optimisticData: [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, | ||
value: { | ||
[policyTag.name]: { | ||
tags: { | ||
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => { | ||
acc[key] = { | ||
...policyTag.tags[key], | ||
...tagsToUpdate[key], | ||
errors: null, | ||
pendingFields: { | ||
enabled: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, | ||
}, | ||
}; | ||
|
||
return acc; | ||
}, {}), | ||
waterim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
successData: [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, | ||
value: { | ||
[policyTag.name]: { | ||
tags: { | ||
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => { | ||
acc[key] = { | ||
...policyTag.tags[key], | ||
errors: null, | ||
pendingFields: { | ||
enabled: null, | ||
}, | ||
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. @waterim I think because of our success optimistic data here. It reset data 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. yes, updated 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. Because of type check, it makes us update more data to Onyx here. Usually we just update acc[key] = {
errors: null,
pendingFields: {
enabled: null,
},
}; 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. we can leave as now, as categories done, to have consistency |
||
}; | ||
|
||
return acc; | ||
}, {}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
failureData: [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, | ||
value: { | ||
[policyTag.name]: { | ||
tags: { | ||
...Object.keys(tagsToUpdate).reduce<PolicyTags>((acc, key) => { | ||
acc[key] = { | ||
...policyTag.tags[key], | ||
...tagsToUpdate[key], | ||
waterim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
errors: ErrorUtils.getMicroSecondOnyxError('workspace.tags.genericFailureMessage'), | ||
pendingFields: { | ||
enabled: null, | ||
}, | ||
}; | ||
|
||
return acc; | ||
}, {}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const parameters = { | ||
policyID, | ||
tags: JSON.stringify(Object.keys(tagsToUpdate).map((key) => tagsToUpdate[key])), | ||
}; | ||
|
||
API.write(WRITE_COMMANDS.SET_POLICY_TAGS_ENABLED, parameters, onyxData); | ||
} | ||
|
||
function clearPolicyTagErrors(policyID: string, tagName: string) { | ||
const tagListName = Object.keys(allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {})[0]; | ||
const tag = allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`]?.[tagListName].tags?.[tagName]; | ||
|
@@ -3696,4 +3783,5 @@ export { | |
clearPolicyTagErrors, | ||
clearWorkspaceReimbursementErrors, | ||
deleteWorkspaceCategories, | ||
setWorkspaceTagEnabled, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type {StackScreenProps} from '@react-navigation/stack'; | ||
import React, {useMemo} from 'react'; | ||
import {View} from 'react-native'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription'; | ||
import OfflineWithFeedback from '@components/OfflineWithFeedback'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import Switch from '@components/Switch'; | ||
import Text from '@components/Text'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import {setWorkspaceTagEnabled} from '@libs/actions/Policy'; | ||
import * as ErrorUtils from '@libs/ErrorUtils'; | ||
import * as PolicyUtils from '@libs/PolicyUtils'; | ||
import type {SettingsNavigatorParamList} from '@navigation/types'; | ||
import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; | ||
import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAccessOrNotFoundWrapper'; | ||
import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper'; | ||
import * as Policy from '@userActions/Policy'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type SCREENS from '@src/SCREENS'; | ||
import type {PolicyTagList} from '@src/types/onyx'; | ||
|
||
type TagSettingsPageOnyxProps = { | ||
/** All policy tags */ | ||
policyTags: OnyxEntry<PolicyTagList>; | ||
}; | ||
|
||
type TagSettingsPageProps = TagSettingsPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.TAG_SETTINGS>; | ||
|
||
function TagSettingsPage({route, policyTags}: TagSettingsPageProps) { | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
const policyTag = useMemo(() => PolicyUtils.getTagList(policyTags, 0), [policyTags]); | ||
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. Crash App from this line Screen.Recording.2024-03-18.at.23.49.51.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. Do you mean not found page appears? |
||
|
||
const currentPolicyTag = policyTag.tags[decodeURIComponent(route.params.tagName)]; | ||
|
||
if (!currentPolicyTag) { | ||
return <NotFoundPage />; | ||
} | ||
|
||
const updateWorkspaceTagEnabled = (value: boolean) => { | ||
setWorkspaceTagEnabled(route.params.policyID, {[currentPolicyTag.name]: {name: currentPolicyTag.name, enabled: value}}); | ||
}; | ||
|
||
return ( | ||
<AdminPolicyAccessOrNotFoundWrapper policyID={route.params.policyID}> | ||
<PaidPolicyAccessOrNotFoundWrapper policyID={route.params.policyID}> | ||
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
style={[styles.defaultModalContainer]} | ||
testID={TagSettingsPage.displayName} | ||
> | ||
<HeaderWithBackButton title={route.params.tagName} /> | ||
<View style={styles.flexGrow1}> | ||
<OfflineWithFeedback | ||
errors={ErrorUtils.getLatestErrorMessageField(currentPolicyTag)} | ||
pendingAction={currentPolicyTag.pendingFields?.enabled} | ||
errorRowStyles={styles.mh5} | ||
onClose={() => Policy.clearPolicyTagErrors(route.params.policyID, route.params.tagName)} | ||
> | ||
<View style={[styles.mt2, styles.mh5]}> | ||
<View style={[styles.flexRow, styles.mb5, styles.mr2, styles.alignItemsCenter, styles.justifyContentBetween]}> | ||
<Text>{translate('workspace.tags.enableTag')}</Text> | ||
<Switch | ||
isOn={currentPolicyTag.enabled} | ||
accessibilityLabel={translate('workspace.tags.enableTag')} | ||
onToggle={updateWorkspaceTagEnabled} | ||
/> | ||
</View> | ||
</View> | ||
</OfflineWithFeedback> | ||
<MenuItemWithTopDescription | ||
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. Coming from #39169. |
||
title={currentPolicyTag.name} | ||
description={translate(`workspace.tags.tagName`)} | ||
/> | ||
</View> | ||
</ScreenWrapper> | ||
</PaidPolicyAccessOrNotFoundWrapper> | ||
</AdminPolicyAccessOrNotFoundWrapper> | ||
); | ||
} | ||
|
||
TagSettingsPage.displayName = 'TagSettingsPage'; | ||
|
||
export default withOnyx<TagSettingsPageProps, TagSettingsPageOnyxProps>({ | ||
policyTags: { | ||
key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${route.params.policyID}`, | ||
}, | ||
})(TagSettingsPage); |
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.
@waterim NAB we don't need a spread operator here.