Skip to content
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

[No QA]: Add safe check for policy id instead of -1 fallback #47165

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 51 additions & 36 deletions src/pages/workspace/WorkspaceMoreFeaturesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,6 @@ type SectionObject = {
items: Item[];
};

// TODO: remove when Onyx data is available
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const mockedCardsList = {
test1: {
cardholder: {accountID: 1, lastName: 'Smith', firstName: 'Bob', displayName: 'Bob Smith', avatar: ''},
name: 'Test 1',
limit: 1000,
lastFourPAN: '1234',
},
test2: {
cardholder: {accountID: 2, lastName: 'Miller', firstName: 'Alex', displayName: 'Alex Miller', avatar: ''},
name: 'Test 2',
limit: 2000,
lastFourPAN: '1234',
},
test3: {
cardholder: {accountID: 3, lastName: 'Brown', firstName: 'Kevin', displayName: 'Kevin Brown', avatar: ''},
name: 'Test 3',
limit: 3000,
lastFourPAN: '1234',
},
};

function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPageProps) {
const styles = useThemeStyles();
const {shouldUseNarrowLayout} = useResponsiveLayout();
Expand All @@ -91,13 +68,10 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
!!policy?.connections?.quickbooksOnline?.config?.syncTax ||
!!policy?.connections?.xero?.config?.importTaxRates ||
!!policy?.connections?.netsuite?.options?.config?.syncOptions?.syncTax;
const policyID = policy?.id ?? '';
const policyID = policy?.id;
// @ts-expect-error a new props will be added during feed api implementation
const workspaceAccountID = (policy?.workspaceAccountID as string) ?? '';
const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}${CONST.EXPENSIFY_CARD.BANK}`);
// Uncomment this line for testing disabled toggle feature - for c+
// const [cardsList = mockedCardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`);

const [isOrganizeWarningModalOpen, setIsOrganizeWarningModalOpen] = useState(false);
const [isIntegrateWarningModalOpen, setIsIntegrateWarningModalOpen] = useState(false);
const [isReportFieldsWarningModalOpen, setIsReportFieldsWarningModalOpen] = useState(false);
Expand All @@ -111,7 +85,10 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
isActive: policy?.areDistanceRatesEnabled ?? false,
pendingAction: policy?.pendingFields?.areDistanceRatesEnabled,
action: (isEnabled: boolean) => {
DistanceRate.enablePolicyDistanceRates(policy?.id ?? '-1', isEnabled);
if (!policyID) {
return;
}
DistanceRate.enablePolicyDistanceRates(policyID, isEnabled);
},
},
{
Expand All @@ -121,7 +98,10 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
isActive: policy?.areWorkflowsEnabled ?? false,
pendingAction: policy?.pendingFields?.areWorkflowsEnabled,
action: (isEnabled: boolean) => {
Policy.enablePolicyWorkflows(policy?.id ?? '-1', isEnabled);
if (!policyID) {
return;
}
Policy.enablePolicyWorkflows(policyID, isEnabled);
},
},
];
Expand All @@ -136,7 +116,10 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
pendingAction: policy?.pendingFields?.areExpensifyCardsEnabled,
disabled: !isEmptyObject(cardsList),
action: (isEnabled: boolean) => {
Policy.enableExpensifyCard(policy?.id ?? '-1', isEnabled);
if (!policyID) {
return;
}
Policy.enableExpensifyCard(policyID, isEnabled);
},
disabledAction: () => {
setIsDisableExpensifyCardWarningModalOpen(true);
Expand All @@ -152,7 +135,10 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
isActive: policy?.areInvoicesEnabled ?? false,
pendingAction: policy?.pendingFields?.areInvoicesEnabled,
action: (isEnabled: boolean) => {
Policy.enablePolicyInvoicing(policy?.id ?? '-1', isEnabled);
if (!policyID) {
return;
}
Policy.enablePolicyInvoicing(policyID, isEnabled);
},
},
];
Expand All @@ -166,11 +152,14 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
disabled: hasAccountingConnection,
pendingAction: policy?.pendingFields?.areCategoriesEnabled,
action: (isEnabled: boolean) => {
if (!policyID) {
return;
}
if (hasAccountingConnection) {
setIsOrganizeWarningModalOpen(true);
return;
}
Category.enablePolicyCategories(policy?.id ?? '-1', isEnabled);
Category.enablePolicyCategories(policyID, isEnabled);
},
},
{
Expand All @@ -181,11 +170,14 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
disabled: hasAccountingConnection,
pendingAction: policy?.pendingFields?.areTagsEnabled,
action: (isEnabled: boolean) => {
if (!policyID) {
return;
}
if (hasAccountingConnection) {
setIsOrganizeWarningModalOpen(true);
return;
}
Tag.enablePolicyTags(policy?.id ?? '-1', isEnabled);
Tag.enablePolicyTags(policyID, isEnabled);
},
},
{
Expand All @@ -196,11 +188,14 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
disabled: hasAccountingConnection,
pendingAction: policy?.pendingFields?.tax,
action: (isEnabled: boolean) => {
if (!policyID) {
return;
}
if (hasAccountingConnection) {
setIsOrganizeWarningModalOpen(true);
return;
}
Policy.enablePolicyTaxes(policy?.id ?? '-1', isEnabled);
Policy.enablePolicyTaxes(policyID, isEnabled);
},
},
];
Expand All @@ -214,6 +209,9 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
disabled: hasAccountingConnection,
pendingAction: policy?.pendingFields?.areReportFieldsEnabled,
action: (isEnabled: boolean) => {
if (!policyID) {
return;
}
if (hasAccountingConnection) {
setIsOrganizeWarningModalOpen(true);
return;
Expand Down Expand Up @@ -242,15 +240,23 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
isActive: isAccountingEnabled,
pendingAction: policy?.pendingFields?.areConnectionsEnabled,
action: (isEnabled: boolean) => {
if (!policyID) {
return;
}
if (hasAccountingConnection) {
setIsIntegrateWarningModalOpen(true);
return;
}
Policy.enablePolicyConnections(policy?.id ?? '-1', isEnabled);
Policy.enablePolicyConnections(policyID, isEnabled);
},
disabled: hasAccountingConnection,
errors: ErrorUtils.getLatestErrorField(policy ?? {}, CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED),
onCloseError: () => Policy.clearPolicyErrorField(policy?.id ?? '-1', CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED),
onCloseError: () => {
if (!policyID) {
return;
}
Policy.clearPolicyErrorField(policyID, CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED);
},
},
];

Expand Down Expand Up @@ -357,6 +363,9 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
<ConfirmModal
title={translate('workspace.moreFeatures.connectionsWarningModal.featureEnabledTitle')}
onConfirm={() => {
if (!policyID) {
return;
}
setIsOrganizeWarningModalOpen(false);
Navigation.navigate(ROUTES.POLICY_ACCOUNTING.getRoute(policyID));
}}
Expand All @@ -369,6 +378,9 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
<ConfirmModal
title={translate('workspace.moreFeatures.connectionsWarningModal.featureEnabledTitle')}
onConfirm={() => {
if (!policyID) {
return;
}
setIsIntegrateWarningModalOpen(false);
Navigation.navigate(ROUTES.POLICY_ACCOUNTING.getRoute(policyID));
}}
Expand All @@ -382,6 +394,9 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
title={translate('workspace.reportFields.disableReportFields')}
isVisible={isReportFieldsWarningModalOpen}
onConfirm={() => {
if (!policyID) {
return;
}
setIsReportFieldsWarningModalOpen(false);
Policy.enablePolicyReportFields(policyID, false);
}}
Expand Down
Loading