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

fix: separate pending fields in workspace general settings #47312

Merged
merged 9 commits into from
Aug 22, 2024
48 changes: 41 additions & 7 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,9 @@ function updateGeneralSettings(policyID: string, name: string, currencyValue?: s
const customUnitID = distanceUnit?.customUnitID;
const currency = currencyValue ?? policy?.outputCurrency ?? CONST.CURRENCY.USD;

const currencyPendingAction = currency !== policy?.outputCurrency ? CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE : null;
const namePendingAction = name !== policy?.name ? CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE : null;
Copy link
Contributor

@DylanDylann DylanDylann Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dominictb

  1. Go offline
  2. Change name
  3. Change the currency
    --> Both the name and the currency should be greyed out

In this case, the pendingFields.name is overwritten with null value. We can use undefined to avoid overwriting

Screen.Recording.2024-08-21.at.16.00.27.mov


const currentRates = distanceUnit?.rates ?? {};
const optimisticRates: Record<string, Rate> = {};
const finallyRates: Record<string, Rate> = {};
Expand Down Expand Up @@ -1073,12 +1076,14 @@ function updateGeneralSettings(policyID: string, name: string, currencyValue?: s

pendingFields: {
...policy.pendingFields,
generalSettings: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
name: namePendingAction,
outputCurrency: currencyPendingAction,
},

// Clear errorFields in case the user didn't dismiss the general settings error
errorFields: {
generalSettings: null,
name: null,
outputCurrency: null,
},
name,
outputCurrency: currency,
Expand All @@ -1099,7 +1104,8 @@ function updateGeneralSettings(policyID: string, name: string, currencyValue?: s
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
pendingFields: {
generalSettings: null,
name: null,
outputCurrency: null,
},
...(customUnitID && {
customUnits: {
Expand All @@ -1113,14 +1119,20 @@ function updateGeneralSettings(policyID: string, name: string, currencyValue?: s
},
];

const errorFields: Policy['errorFields'] = {
name: namePendingAction && ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.editor.genericFailureMessage'),
};

if (!errorFields.name && currencyPendingAction) {
errorFields.outputCurrency = ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.editor.genericFailureMessage');
}

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
errorFields: {
generalSettings: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.editor.genericFailureMessage'),
},
errorFields,
...(customUnitID && {
customUnits: {
[customUnitID]: {
Expand Down Expand Up @@ -1280,12 +1292,29 @@ function updateAddress(policyID: string, newAddress: CompanyAddress) {
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
address: newAddress,
pendingFields: {
address: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
},
},
];

const finallyData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
address: newAddress,
pendingFields: {
address: null,
},
},
},
];

API.write(WRITE_COMMANDS.UPDATE_POLICY_ADDRESS, parameters, {
optimisticData,
finallyData,
});
}

Expand Down Expand Up @@ -1597,7 +1626,9 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName
autoReporting: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
approvalMode: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
reimbursementChoice: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
generalSettings: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
name: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
outputCurrency: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
address: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
description: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
Expand Down Expand Up @@ -1674,6 +1705,9 @@ function buildPolicyData(policyOwnerEmail = '', makeMeAdmin = false, policyName
autoReporting: null,
approvalMode: null,
reimbursementChoice: null,
name: null,
outputCurrency: null,
address: null,
},
},
},
Expand Down
6 changes: 5 additions & 1 deletion src/pages/workspace/WorkspaceInitialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, reimbursementAcc

const hasMembersError = PolicyUtils.hasEmployeeListError(policy);
const hasPolicyCategoryError = PolicyUtils.hasPolicyCategoriesError(policyCategories);
const hasGeneralSettingsError = !isEmptyObject(policy?.errorFields?.generalSettings ?? {}) || !isEmptyObject(policy?.errorFields?.avatarURL ?? {});
const hasGeneralSettingsError =
dominictb marked this conversation as resolved.
Show resolved Hide resolved
!isEmptyObject(policy?.errorFields?.name ?? {}) ||
!isEmptyObject(policy?.errorFields?.avatarURL ?? {}) ||
!isEmptyObject(policy?.errorFields?.ouputCurrency ?? {}) ||
!isEmptyObject(policy?.errorFields?.address ?? {});
const {login} = useCurrentUserPersonalDetails();
const shouldShowProtectedItems = PolicyUtils.isPolicyAdmin(policy, login);
const isPaidGroupPolicy = PolicyUtils.isPaidGroupPolicy(policy);
Expand Down
8 changes: 4 additions & 4 deletions src/pages/workspace/WorkspaceProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import withPolicy from './withPolicy';
import type {WithPolicyProps} from './withPolicy';
import withPolicy from './withPolicy';
import WorkspacePageWithSections from './WorkspacePageWithSections';

type WorkspaceProfilePageOnyxProps = {
Expand Down Expand Up @@ -196,7 +196,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
disabledStyle={styles.cursorDefault}
errorRowStyles={styles.mt3}
/>
<OfflineWithFeedback pendingAction={policy?.pendingFields?.generalSettings}>
<OfflineWithFeedback pendingAction={policy?.pendingFields?.name}>
<MenuItemWithTopDescription
title={policyName}
titleStyle={styles.workspaceTitleStyle}
Expand Down Expand Up @@ -229,7 +229,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
</OfflineWithFeedback>
)}
<OfflineWithFeedback
pendingAction={policy?.pendingFields?.generalSettings}
pendingAction={policy?.pendingFields?.outputCurrency}
errors={ErrorUtils.getLatestErrorField(policy ?? {}, CONST.POLICY.COLLECTION_KEYS.GENERAL_SETTINGS)}
onClose={() => Policy.clearPolicyErrorField(policy?.id ?? '-1', CONST.POLICY.COLLECTION_KEYS.GENERAL_SETTINGS)}
errorRowStyles={[styles.mt2]}
Expand All @@ -249,7 +249,7 @@ function WorkspaceProfilePage({policyDraft, policy: policyProp, currencyList = {
</View>
</OfflineWithFeedback>
{canUseSpotnanaTravel && shouldShowAddress && (
<OfflineWithFeedback pendingAction={policy?.pendingFields?.generalSettings}>
<OfflineWithFeedback pendingAction={policy?.pendingFields?.address}>
<View>
<MenuItemWithTopDescription
title={formattedAddress}
Expand Down
2 changes: 1 addition & 1 deletion src/types/onyx/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ type Policy = OnyxCommon.OnyxValueWithOfflineFeedback<
/** Workspace account ID configured for Expensify Card */
workspaceAccountID?: number;
} & Partial<PendingJoinRequestPolicy>,
'generalSettings' | 'addWorkspaceRoom' | 'employeeList' | keyof ACHAccount | keyof Attributes
'addWorkspaceRoom' | 'employeeList' | keyof ACHAccount | keyof Attributes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add name, outputCurrency, address here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DylanDylann we don't need as name, outputCurrency and address is part of the Policy type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh see 😄

>;

/** Stages of policy connection sync */
Expand Down
Loading