Skip to content

Commit

Permalink
refactor: create a reusable shared modal for delegate validation
Browse files Browse the repository at this point in the history
  • Loading branch information
getusha committed Sep 29, 2024
1 parent 0ea996a commit 92a43b5
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 32 deletions.
4 changes: 0 additions & 4 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ const ROUTES = {
route: 'settings/security/delegate/:login/role/:role/confirm',
getRoute: (login: string, role: string) => `settings/security/delegate/${encodeURIComponent(login)}/role/${role}/confirm` as const,
},
SETTINGS_DELEGATE_MAGIC_CODE: {
route: 'settings/security/delegate/:login/role/:role/magic-code',
getRoute: (login: string, role: string) => `settings/security/delegate/${encodeURIComponent(login)}/role/${role}/magic-code` as const,
},
SETTINGS_ABOUT: 'settings/about',
SETTINGS_APP_DOWNLOAD_LINKS: 'settings/about/app-download-links',
SETTINGS_WALLET: 'settings/wallet',
Expand Down
1 change: 0 additions & 1 deletion src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ const SCREENS = {
ADD_DELEGATE: 'Settings_Delegate_Add',
DELEGATE_ROLE: 'Settings_Delegate_Role',
DELEGATE_CONFIRM: 'Settings_Delegate_Confirm',
DELEGATE_MAGIC_CODE: 'Settings_Delegate_Magic_Code',
},
},
SAVE_THE_WORLD: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ const SettingsModalStackNavigator = createModalStackNavigator<SettingsNavigatorP
[SCREENS.SETTINGS.DELEGATE.ADD_DELEGATE]: () => require<ReactComponentModule>('../../../../pages/settings/Security/AddDelegate/AddDelegatePage').default,
[SCREENS.SETTINGS.DELEGATE.DELEGATE_ROLE]: () => require<ReactComponentModule>('../../../../pages/settings/Security/AddDelegate/SelectDelegateRolePage').default,
[SCREENS.SETTINGS.DELEGATE.DELEGATE_CONFIRM]: () => require<ReactComponentModule>('../../../../pages/settings/Security/AddDelegate/ConfirmDelegatePage').default,
[SCREENS.SETTINGS.DELEGATE.DELEGATE_MAGIC_CODE]: () => require<ReactComponentModule>('../../../../pages/settings/Security/AddDelegate/DelegateMagicCodePage').default,
[SCREENS.WORKSPACE.RULES_CUSTOM_NAME]: () => require<ReactComponentModule>('../../../../pages/workspace/rules/RulesCustomNamePage').default,
[SCREENS.WORKSPACE.RULES_AUTO_APPROVE_REPORTS_UNDER]: () => require<ReactComponentModule>('../../../../pages/workspace/rules/RulesAutoApproveReportsUnderPage').default,
[SCREENS.WORKSPACE.RULES_RANDOM_REPORT_AUDIT]: () => require<ReactComponentModule>('../../../../pages/workspace/rules/RulesRandomReportAuditPage').default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const CENTRAL_PANE_TO_RHP_MAPPING: Partial<Record<CentralPaneName, string[]>> =
SCREENS.SETTINGS.DELEGATE.ADD_DELEGATE,
SCREENS.SETTINGS.DELEGATE.DELEGATE_ROLE,
SCREENS.SETTINGS.DELEGATE.DELEGATE_CONFIRM,
SCREENS.SETTINGS.DELEGATE.DELEGATE_MAGIC_CODE,
],
[SCREENS.SETTINGS.ABOUT]: [SCREENS.SETTINGS.APP_DOWNLOAD_LINKS],
[SCREENS.SETTINGS.SAVE_THE_WORLD]: [SCREENS.I_KNOW_A_TEACHER, SCREENS.INTRO_SCHOOL_PRINCIPAL, SCREENS.I_AM_A_TEACHER],
Expand Down
6 changes: 0 additions & 6 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,6 @@ const config: LinkingOptions<RootStackParamList>['config'] = {
login: (login: string) => decodeURIComponent(login),
},
},
[SCREENS.SETTINGS.DELEGATE.DELEGATE_MAGIC_CODE]: {
path: ROUTES.SETTINGS_DELEGATE_MAGIC_CODE.route,
parse: {
login: (login: string) => decodeURIComponent(login),
},
},
[SCREENS.SETTINGS.PROFILE.STATUS]: {
path: ROUTES.SETTINGS_STATUS,
exact: true,
Expand Down
4 changes: 0 additions & 4 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,6 @@ type SettingsNavigatorParamList = {
login: string;
role: string;
};
[SCREENS.SETTINGS.DELEGATE.DELEGATE_MAGIC_CODE]: {
login: string;
role: string;
};
[SCREENS.SETTINGS.REPORT_CARD_LOST_OR_DAMAGED]: {
/** cardID of selected card */
cardID: string;
Expand Down
22 changes: 19 additions & 3 deletions src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React from 'react';
import React, { useEffect, useState } from 'react';

Check failure on line 2 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Replace `·useEffect,·useState·}·from·'react` with `useEffect,·useState}·from·'react';⏎import·{useOnyx}·from·'react-native-onyx`

Check failure on line 2 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'useEffect' is defined but never used

Check failure on line 2 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Replace `·useEffect,·useState·}·from·'react` with `useEffect,·useState}·from·'react';⏎import·{useOnyx}·from·'react-native-onyx`

Check failure on line 2 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'useEffect' is defined but never used
import type {ValueOf} from 'type-fest';
import Button from '@components/Button';
import HeaderPageLayout from '@components/HeaderPageLayout';
Expand All @@ -17,6 +17,13 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import CONST from '@src/CONST';

Check failure on line 17 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Replace `CONST·from·'@src/CONST';⏎import·ROUTES·from·'@src/ROUTES';⏎import·type·SCREENS·from·'@src/SCREENS';⏎import·ValidateCodeActionModal·from·"@components/ValidateCodeActionModal"` with `DelegateMagicCodeModal·from·'@pages/settings/Security/AddDelegate/DelegateMagicCodeModal'`

Check failure on line 17 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Replace `CONST·from·'@src/CONST';⏎import·ROUTES·from·'@src/ROUTES';⏎import·type·SCREENS·from·'@src/SCREENS';⏎import·ValidateCodeActionModal·from·"@components/ValidateCodeActionModal"` with `DelegateMagicCodeModal·from·'@pages/settings/Security/AddDelegate/DelegateMagicCodeModal'`
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import ValidateCodeActionModal from "@components/ValidateCodeActionModal";

Check failure on line 20 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'ValidateCodeActionModal' is defined but never used

Check failure on line 20 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'ValidateCodeActionModal' is defined but never used
import { useOnyx } from "react-native-onyx";

Check failure on line 21 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Replace `{·useOnyx·}·from·"react-native-onyx"` with `*·as·Delegate·from·'@userActions/Delegate'`

Check failure on line 21 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'useOnyx' is defined but never used

Check failure on line 21 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Replace `{·useOnyx·}·from·"react-native-onyx"` with `*·as·Delegate·from·'@userActions/Delegate'`

Check failure on line 21 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'useOnyx' is defined but never used
import ONYXKEYS from "@src/ONYXKEYS";

Check failure on line 22 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Replace `ONYXKEYS·from·"@src/ONYXKEYS";⏎import·*·as·ErrorUtils·from·'@libs/ErrorUtils` with `CONST·from·'@src/CONST`

Check failure on line 22 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'ONYXKEYS' is defined but never used

Check failure on line 22 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Replace `ONYXKEYS·from·"@src/ONYXKEYS";⏎import·*·as·ErrorUtils·from·'@libs/ErrorUtils` with `CONST·from·'@src/CONST`

Check failure on line 22 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'ONYXKEYS' is defined but never used
import * as ErrorUtils from '@libs/ErrorUtils';
import * as Delegate from '@userActions/Delegate';
import * as User from '@libs/actions/User';
import DelegateMagicCodeModal from "@pages/settings/Security/AddDelegate/DelegateMagicCodeModal";

Check warning on line 26 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected subpath import via alias '@pages/settings/Security/AddDelegate/DelegateMagicCodeModal'. Use './DelegateMagicCodeModal' instead

Check warning on line 26 in src/pages/settings/Security/AddDelegate/ConfirmDelegatePage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected subpath import via alias '@pages/settings/Security/AddDelegate/DelegateMagicCodeModal'. Use './DelegateMagicCodeModal' instead

type ConfirmDelegatePageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.DELEGATE.DELEGATE_CONFIRM>;

Expand All @@ -28,8 +35,9 @@ function ConfirmDelegatePage({route}: ConfirmDelegatePageProps) {
const role = route.params.role as ValueOf<typeof CONST.DELEGATE_ROLE>;
const {isOffline} = useNetwork();

const personalDetails = PersonalDetailsUtils.getPersonalDetailByEmail(login);
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(false);

const personalDetails = PersonalDetailsUtils.getPersonalDetailByEmail(login);
const avatarIcon = personalDetails?.avatar ?? FallbackAvatar;
const formattedLogin = formatPhoneNumber(login ?? '');
const displayName = personalDetails?.displayName ?? formattedLogin;
Expand All @@ -42,7 +50,7 @@ function ConfirmDelegatePage({route}: ConfirmDelegatePageProps) {
text={translate('delegate.addCopilot')}
style={styles.mt6}
pressOnEnter
onPress={() => Navigation.navigate(ROUTES.SETTINGS_DELEGATE_MAGIC_CODE.getRoute(login, role))}
onPress={() => setIsValidateCodeActionModalVisible(true)}
/>
);

Expand Down Expand Up @@ -70,6 +78,14 @@ function ConfirmDelegatePage({route}: ConfirmDelegatePageProps) {
onPress={() => Navigation.navigate(ROUTES.SETTINGS_DELEGATE_ROLE.getRoute(login, role))}
shouldShowRightIcon
/>

{isValidateCodeActionModalVisible && (
<DelegateMagicCodeModal
login={login}
role={role}
/>
)}

</HeaderPageLayout>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useEffect, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
Expand All @@ -7,23 +6,21 @@ import useLocalize from '@hooks/useLocalize';
import * as User from '@libs/actions/User';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as Delegate from '@userActions/Delegate';
import type CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';

type DelegateMagicCodePageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.DELEGATE.DELEGATE_CONFIRM>;
type DelegateMagicCodeModalProps = {
login: string,
role: ValueOf<typeof CONST.DELEGATE_ROLE>
}

function DelegateMagicCodePage({route}: DelegateMagicCodePageProps) {
function DelegateMagicCodeModal({login, role}: DelegateMagicCodeModalProps) {
const {translate} = useLocalize();
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(true);

const login = route.params.login;
const role = route.params.role as ValueOf<typeof CONST.DELEGATE_ROLE>;

const currentDelegate = account?.delegatedAccess?.delegates?.find((d) => d.email === login);
const validateLoginError = ErrorUtils.getLatestErrorField(currentDelegate, 'addDelegate');

Expand Down Expand Up @@ -62,6 +59,6 @@ function DelegateMagicCodePage({route}: DelegateMagicCodePageProps) {
);
}

DelegateMagicCodePage.displayName = 'DelegateMagicCodePage';
DelegateMagicCodeModal.displayName = 'DelegateMagicCodeModal';

export default DelegateMagicCodePage;
export default DelegateMagicCodeModal;
21 changes: 19 additions & 2 deletions src/pages/settings/Security/SecuritySettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useMemo} from 'react';
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand Down Expand Up @@ -30,6 +30,13 @@ import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

Check warning on line 32 in src/pages/settings/Security/SecuritySettingsPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected subpath import via alias '@pages/settings/Security/AddDelegate/DelegateMagicCodeModal'. Use './AddDelegate/DelegateMagicCodeModal' instead

Check warning on line 32 in src/pages/settings/Security/SecuritySettingsPage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected subpath import via alias '@pages/settings/Security/AddDelegate/DelegateMagicCodeModal'. Use './AddDelegate/DelegateMagicCodeModal' instead
import DelegateMagicCodeModal from "@pages/settings/Security/AddDelegate/DelegateMagicCodeModal";
import { ValueOf } from "type-fest";

type Delegate = {
login?: string,
role?: ValueOf<typeof CONST.DELEGATE_ROLE>,
}

function SecuritySettingsPage() {
const styles = useThemeStyles();
Expand All @@ -39,6 +46,9 @@ function SecuritySettingsPage() {
const theme = useTheme();
const {canUseNewDotCopilot} = usePermissions();
const [account] = useOnyx(ONYXKEYS.ACCOUNT);

const [selectedDelegate, setSelectedDelegate] = useState<Delegate>({});

const isActingAsDelegate = !!account?.delegatedAccess?.delegate ?? false;

const delegates = account?.delegatedAccess?.delegates ?? [];
Expand Down Expand Up @@ -87,7 +97,8 @@ function SecuritySettingsPage() {
Navigation.navigate(ROUTES.SETTINGS_DELEGATE_ROLE.getRoute(email));
return;
}
Navigation.navigate(ROUTES.SETTINGS_DELEGATE_MAGIC_CODE.getRoute(email, role));

setSelectedDelegate({login: email, role});
};

const formattedEmail = formatPhoneNumber(email);
Expand Down Expand Up @@ -206,6 +217,12 @@ function SecuritySettingsPage() {
)}
</View>
</ScrollView>
{(selectedDelegate.login && selectedDelegate.role) && (
<DelegateMagicCodeModal
login={selectedDelegate?.login}
role={selectedDelegate?.role}
/>
)}
</>
)}
</ScreenWrapper>
Expand Down

0 comments on commit 92a43b5

Please sign in to comment.