Skip to content

Commit 211fae2

Browse files
committed
pass policy data from useOnyx
1 parent 7a6393d commit 211fae2

File tree

6 files changed

+285
-33
lines changed

6 files changed

+285
-33
lines changed

src/libs/actions/Policy/Policy.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ function createPolicyExpenseChats(
16311631
/**
16321632
* Updates a workspace avatar image
16331633
*/
1634-
function updateWorkspaceAvatar(policyID: string, file: File) {
1634+
function updateWorkspaceAvatar(policyID: string, currentAvatarURL: string | undefined, file: File) {
16351635
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
16361636
{
16371637
onyxMethod: Onyx.METHOD.MERGE,
@@ -1664,7 +1664,7 @@ function updateWorkspaceAvatar(policyID: string, file: File) {
16641664
onyxMethod: Onyx.METHOD.MERGE,
16651665
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
16661666
value: {
1667-
avatarURL: deprecatedAllPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.avatarURL,
1667+
avatarURL: currentAvatarURL,
16681668
},
16691669
},
16701670
];
@@ -1680,10 +1680,7 @@ function updateWorkspaceAvatar(policyID: string, file: File) {
16801680
/**
16811681
* Deletes the avatar image for the workspace
16821682
*/
1683-
function deleteWorkspaceAvatar(policyID: string) {
1684-
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
1685-
// eslint-disable-next-line @typescript-eslint/no-deprecated
1686-
const policy = getPolicy(policyID);
1683+
function deleteWorkspaceAvatar(policyID: string, currentAvatarURL: string, currentOriginalFileName: string) {
16871684
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
16881685
{
16891686
onyxMethod: Onyx.METHOD.MERGE,
@@ -1716,8 +1713,8 @@ function deleteWorkspaceAvatar(policyID: string) {
17161713
onyxMethod: Onyx.METHOD.MERGE,
17171714
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
17181715
value: {
1719-
avatarURL: policy?.avatarURL,
1720-
originalFileName: policy?.originalFileName,
1716+
avatarURL: currentAvatarURL,
1717+
originalFileName: currentOriginalFileName,
17211718
errorFields: {
17221719
avatarURL: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('avatarWithImagePicker.deleteWorkspaceError'),
17231720
},
@@ -1748,13 +1745,8 @@ function clearAvatarErrors(policyID: string) {
17481745
* Optimistically update the general settings. Set the general settings as pending until the response succeeds.
17491746
* If the response fails set a general error message. Clear the error message when updating.
17501747
*/
1751-
function updateGeneralSettings(policyID: string | undefined, name: string, currencyValue?: string) {
1752-
if (!policyID) {
1753-
return;
1754-
}
1755-
1756-
const policy = deprecatedAllPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
1757-
if (!policy) {
1748+
function updateGeneralSettings(policy: OnyxEntry<Policy>, name: string, currencyValue?: string) {
1749+
if (!policy?.id) {
17581750
return;
17591751
}
17601752

@@ -1767,8 +1759,8 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
17671759

17681760
const currentRates = distanceUnit?.rates ?? {};
17691761
const optimisticRates: Record<string, Rate> = {};
1770-
const finallyRates: Record<string, Rate> = {};
1771-
const failureRates: Record<string, Rate> = {};
1762+
const finallyRates: Record<string, Partial<Rate>> = {};
1763+
const failureRates: Record<string, Partial<Rate>> = {};
17721764

17731765
if (customUnitID) {
17741766
for (const rateID of Object.keys(currentRates)) {
@@ -1778,13 +1770,10 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
17781770
currency,
17791771
};
17801772
finallyRates[rateID] = {
1781-
...currentRates[rateID],
17821773
pendingFields: {currency: null},
1783-
currency,
17841774
};
17851775
failureRates[rateID] = {
1786-
...currentRates[rateID],
1787-
pendingFields: {currency: null},
1776+
currency: currentRates[rateID].currency,
17881777
errorFields: {currency: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage')},
17891778
};
17901779
}
@@ -1794,7 +1783,7 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
17941783
{
17951784
// We use SET because it's faster than merge and avoids a race condition when setting the currency and navigating the user to the Bank account page in confirmCurrencyChangeAndHideModal
17961785
onyxMethod: Onyx.METHOD.SET,
1797-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
1786+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
17981787
value: {
17991788
...policy,
18001789

@@ -1826,7 +1815,7 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
18261815
const finallyData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
18271816
{
18281817
onyxMethod: Onyx.METHOD.MERGE,
1829-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
1818+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
18301819
value: {
18311820
pendingFields: {
18321821
name: null,
@@ -1855,9 +1844,11 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
18551844
const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [
18561845
{
18571846
onyxMethod: Onyx.METHOD.MERGE,
1858-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
1847+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}`,
18591848
value: {
18601849
errorFields,
1850+
name: policy.name,
1851+
outputCurrency: policy.outputCurrency,
18611852
...(customUnitID && {
18621853
customUnits: {
18631854
[customUnitID]: {
@@ -1871,14 +1862,14 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
18711862
];
18721863

18731864
const params: UpdateWorkspaceGeneralSettingsParams = {
1874-
policyID,
1865+
policyID: policy.id,
18751866
workspaceName: name,
18761867
currency,
18771868
};
18781869

18791870
const persistedRequests = PersistedRequests.getAll();
18801871
const createWorkspaceRequestChangedIndex = persistedRequests.findIndex(
1881-
(request) => request.data?.policyID === policyID && request.command === WRITE_COMMANDS.CREATE_WORKSPACE && request.data?.policyName !== name,
1872+
(request) => request.data?.policyID === policy.id && request.command === WRITE_COMMANDS.CREATE_WORKSPACE && request.data?.policyName !== name,
18821873
);
18831874

18841875
const createWorkspaceRequest = persistedRequests.at(createWorkspaceRequestChangedIndex);
@@ -1890,7 +1881,7 @@ function updateGeneralSettings(policyID: string | undefined, name: string, curre
18901881
policyName: name,
18911882
},
18921883
};
1893-
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
1884+
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, {
18941885
name,
18951886
});
18961887

src/pages/settings/Preferences/PaymentCurrencyPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function PaymentCurrencyPage() {
4848
searchInputLabel={translate('common.search')}
4949
onSelect={(option: CurrencyListItem) => {
5050
if (option.currencyCode !== paymentCurrency) {
51-
updateGeneralSettings(personalPolicyID, personalPolicy?.name ?? '', option.currencyCode);
51+
updateGeneralSettings(personalPolicy, personalPolicy?.name ?? '', option.currencyCode);
5252
}
5353
handleDismissKeyboardAndGoBack();
5454
}}

src/pages/workspace/WorkspaceNamePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function WorkspaceNamePage({policy}: Props) {
3131
return;
3232
}
3333

34-
updateGeneralSettings(policy.id, values.name.trim(), policy.outputCurrency);
34+
updateGeneralSettings(policy, values.name.trim(), policy.outputCurrency);
3535
Keyboard.dismiss();
3636
Navigation.setNavigationActionToMicrotaskQueue(() => Navigation.goBack());
3737
},

src/pages/workspace/WorkspaceOverviewCurrencyPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function WorkspaceOverviewCurrencyPage({policy}: WorkspaceOverviewCurrencyPagePr
4444
return;
4545
}
4646
clearDraftValues(ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM);
47-
updateGeneralSettings(policy.id, policy?.name ?? '', item.currencyCode);
47+
updateGeneralSettings(policy, policy?.name ?? '', item.currencyCode);
4848
clearCorpayBankAccountFields();
4949

5050
if (isForcedToChangeCurrency) {

src/pages/workspace/WorkspaceOverviewPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,13 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
591591
if (!policyID) {
592592
return;
593593
}
594-
updateWorkspaceAvatar(policyID, file as File);
594+
updateWorkspaceAvatar(policyID, policy.avatarURL, file as File);
595595
}}
596596
onImageRemoved={() => {
597-
if (!policyID) {
597+
if (!policyID || !policy.avatarURL || !policy.originalFileName) {
598598
return;
599599
}
600-
deleteWorkspaceAvatar(policyID);
600+
deleteWorkspaceAvatar(policyID, policy.avatarURL, policy.originalFileName);
601601
}}
602602
editorMaskImage={expensifyIcons.ImageCropSquareMask}
603603
sentryLabel={CONST.SENTRY_LABEL.WORKSPACE.OVERVIEW.AVATAR}

0 commit comments

Comments
 (0)