-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWriteCapabilityPage.tsx
89 lines (80 loc) · 3.85 KB
/
WriteCapabilityPage.tsx
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
import {useRoute} from '@react-navigation/native';
import React, {useCallback} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import useLocalize from '@hooks/useLocalize';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackRouteProp, PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import * as ReportUtils from '@libs/ReportUtils';
import type {ReportSettingsNavigatorParamList} from '@navigation/types';
import withReportOrNotFound from '@pages/home/report/withReportOrNotFound';
import type {WithReportOrNotFoundProps} from '@pages/home/report/withReportOrNotFound';
import * as ReportActions from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Policy} from '@src/types/onyx';
type WriteCapabilityPageOnyxProps = {
/** The policy object for the current route */
policy: OnyxEntry<Policy>;
};
type WriteCapabilityPageProps = WriteCapabilityPageOnyxProps &
WithReportOrNotFoundProps &
PlatformStackScreenProps<ReportSettingsNavigatorParamList, typeof SCREENS.REPORT_SETTINGS.WRITE_CAPABILITY>;
function WriteCapabilityPage({report, policy}: WriteCapabilityPageProps) {
const route = useRoute<PlatformStackRouteProp<ReportSettingsNavigatorParamList, typeof SCREENS.REPORT_SETTINGS.WRITE_CAPABILITY>>();
const {translate} = useLocalize();
const writeCapabilityOptions = Object.values(CONST.REPORT.WRITE_CAPABILITIES).map((value) => ({
value,
text: translate(`writeCapabilityPage.writeCapability.${value}`),
keyForList: value,
isSelected: value === (report?.writeCapability ?? CONST.REPORT.WRITE_CAPABILITIES.ALL),
}));
const isAbleToEdit = ReportUtils.canEditWriteCapability(report, policy);
const goBack = useCallback(() => {
Navigation.goBack(ROUTES.REPORT_SETTINGS.getRoute(report.reportID, route.params.backTo));
}, [report.reportID, route.params.backTo]);
const updateWriteCapability = useCallback(
(newValue: ValueOf<typeof CONST.REPORT.WRITE_CAPABILITIES>) => {
ReportActions.updateWriteCapability(report, newValue);
goBack();
},
[report, goBack],
);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={WriteCapabilityPage.displayName}
>
<FullPageNotFoundView shouldShow={!isAbleToEdit}>
<HeaderWithBackButton
title={translate('writeCapabilityPage.label')}
shouldShowBackButton
onBackButtonPress={goBack}
/>
<SelectionList
sections={[{data: writeCapabilityOptions}]}
ListItem={RadioListItem}
onSelectRow={(option) => updateWriteCapability(option.value)}
shouldSingleExecuteRowSelect
initiallyFocusedOptionKey={writeCapabilityOptions.find((locale) => locale.isSelected)?.keyForList}
/>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
WriteCapabilityPage.displayName = 'WriteCapabilityPage';
export default withReportOrNotFound()(
withOnyx<WriteCapabilityPageProps, WriteCapabilityPageOnyxProps>({
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`,
},
})(WriteCapabilityPage),
);