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] [TS Migration] Standardize approach to Onyx pendingFields #34799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3d3aeaa
Create OfflineFeedback type
VickyStash Jan 19, 2024
3a42ec7
Use OfflineFeedback type instead of pendingAction
VickyStash Jan 19, 2024
4a1c14e
Fix lint errors
VickyStash Jan 19, 2024
284d119
Fix lint error in SidebarUtils
VickyStash Jan 19, 2024
3bce79c
Update PendingFields type and add OnyxItemWithOfflineFeedback type
VickyStash Jan 22, 2024
4aaad12
Use OfflineFeedback where possible
VickyStash Jan 23, 2024
abed5f1
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Jan 23, 2024
25552df
Update PolicyReportField import
VickyStash Jan 23, 2024
60fe816
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Jan 25, 2024
809707a
Update OnyxValueWithOfflineFeedback type and reuse it
VickyStash Jan 25, 2024
8682a4d
Remove extra export
VickyStash Jan 25, 2024
66808bf
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Jan 26, 2024
aa6e0e3
Lint fix after conflicts resolution
VickyStash Jan 26, 2024
c028794
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Jan 29, 2024
648e293
Lint fix
VickyStash Jan 29, 2024
a8e53ff
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Jan 30, 2024
5886dcb
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Jan 31, 2024
97bba27
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 1, 2024
98925a5
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 6, 2024
187ffd9
TS fixes after merging main
VickyStash Feb 6, 2024
d559b2e
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 9, 2024
4578ca8
TS fix
VickyStash Feb 9, 2024
f0bb6ed
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 20, 2024
3b5834f
Minor code improvement
VickyStash Feb 21, 2024
939a88c
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 22, 2024
fc6c412
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 23, 2024
8163d2c
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 26, 2024
7d5e4e8
Fix TS error
VickyStash Feb 26, 2024
f52baac
Merge branch 'main' into ts-migration/standardize-pendingFields
VickyStash Feb 26, 2024
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
2 changes: 1 addition & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function getOptionData({
result.isExpenseRequest = ReportUtils.isExpenseRequest(report);
result.isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
result.shouldShowSubscript = ReportUtils.shouldReportShowSubscript(report);
result.pendingAction = report.pendingFields ? report.pendingFields.addWorkspaceRoom || report.pendingFields.createChat : undefined;
result.pendingAction = report.pendingFields?.addWorkspaceRoom ?? report.pendingFields?.createChat;
result.brickRoadIndicator = hasErrors || hasViolations ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : '';
result.ownerAccountID = report.ownerAccountID;
result.managerID = report.managerID;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import type {Participant, Split} from '@src/types/onyx/IOU';
import type {ErrorFields, Errors, PendingFields} from '@src/types/onyx/OnyxCommon';
import type {ErrorFields, Errors} from '@src/types/onyx/OnyxCommon';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type ReportAction from '@src/types/onyx/ReportAction';
import type {OnyxData} from '@src/types/onyx/Request';
Expand Down Expand Up @@ -302,7 +302,7 @@ function setMoneyRequestMerchant(transactionID: string, merchant: string, isDraf
Onyx.merge(`${isDraft ? ONYXKEYS.COLLECTION.TRANSACTION_DRAFT : ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {merchant});
}

function setMoneyRequestPendingFields(transactionID: string, pendingFields: PendingFields) {
function setMoneyRequestPendingFields(transactionID: string, pendingFields: OnyxTypes.Transaction['pendingFields']) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {pendingFields});
}

Expand Down
7 changes: 2 additions & 5 deletions src/types/onyx/BankAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AdditionalData = {
country?: string;
};

type BankAccount = {
type BankAccount = OnyxCommon.OnyxValueWithOfflineFeedback<{
/** The bank account type */
accountType?: typeof CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT;

Expand Down Expand Up @@ -40,10 +40,7 @@ type BankAccount = {

/** Any additional error message to show */
errors?: OnyxCommon.Errors;

/** Indicates the type of change made to the bank account that hasn't been synced with the server yet */
pendingAction?: OnyxCommon.PendingAction;
};
}>;

type BankAccountList = Record<string, BankAccount>;

Expand Down
5 changes: 2 additions & 3 deletions src/types/onyx/Fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type AccountData = {
bank?: BankName;
};

type Fund = {
type Fund = OnyxCommon.OnyxValueWithOfflineFeedback<{
accountData?: AccountData;
accountType?: typeof CONST.PAYMENT_METHODS.DEBIT_CARD;
description?: string;
Expand All @@ -34,8 +34,7 @@ type Fund = {
title?: string;
isDefault?: boolean;
errors?: OnyxCommon.Errors;
pendingAction?: OnyxCommon.PendingAction;
};
}>;

type FundList = Record<string, Fund>;

Expand Down
30 changes: 15 additions & 15 deletions src/types/onyx/Login.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import type * as OnyxCommon from './OnyxCommon';

type Login = {
/** Phone/Email associated with user */
partnerUserID?: string;
type Login = OnyxCommon.OnyxValueWithOfflineFeedback<
{
/** Phone/Email associated with user */
partnerUserID?: string;

/** Value of partner name */
partnerName?: string;
/** Value of partner name */
partnerName?: string;

/** Date login was validated, used to show info indicator status */
validatedDate?: string;
/** Date login was validated, used to show info indicator status */
validatedDate?: string;

/** Whether the user validation code was sent */
validateCodeSent?: boolean;
/** Whether the user validation code was sent */
validateCodeSent?: boolean;

/** Field-specific server side errors keyed by microtime */
errorFields?: OnyxCommon.ErrorFields;

/** Field-specific pending states for offline UI status */
pendingFields?: OnyxCommon.PendingFields;
};
/** Field-specific server side errors keyed by microtime */
errorFields?: OnyxCommon.ErrorFields;
},
'defaultLogin' | 'validateLogin' | 'addedLogin' | 'deletedLogin'
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm confused what the 2nd template argument for OnyxValueWithOfflineFeedback is for. Could you help explain? Maybe a README example here could be helpful too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

2nd template argument for OnyxValueWithOfflineFeedback are additional keys to be provided as keys for pendingFields. So these keys 'defaultLogin' | 'validateLogin' | 'addedLogin' | 'deletedLogin' are outside of the Login model, but can be in pendingFields keys.

So for example, validateLogin can be used in pendingFields keys, but it doesn't exist in the Login model

pendingFields: {
validateLogin: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},

That's also was explained in this comment in 1st variant of implementation section.

Maybe it will be better to add a clarifying comment right next to the OnyxValueWithOfflineFeedback type, this way it should be easy to find and helpful.

Copy link
Contributor

Choose a reason for hiding this comment

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

@roryabraham Bump for reviewing

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for explaining

>;

type LoginList = Record<string, Login>;

Expand Down
16 changes: 13 additions & 3 deletions src/types/onyx/OnyxCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ import type {MaybePhraseKey} from '@libs/Localize';
import type {AvatarSource} from '@libs/UserUtils';
import type CONST from '@src/CONST';

type PendingAction = ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION>;
type PendingAction = ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION> | null;

type PendingFields<TKey extends string = string> = Record<TKey, PendingAction | null | undefined>;
type PendingFields<TKey extends string> = {[key in Exclude<TKey, 'pendingAction' | 'pendingFields' | 'errorFields'>]?: PendingAction};

type OfflineFeedback<TKey extends string> = {
/** The type of action that's pending */
pendingAction?: PendingAction;

/** Field-specific pending states for offline updates */
pendingFields?: PendingFields<TKey>;
};

type OnyxValueWithOfflineFeedback<TOnyx, TKey extends string = never> = keyof TOnyx extends string ? TOnyx & OfflineFeedback<keyof TOnyx | TKey> : never;

type ErrorFields<TKey extends string = string> = Record<TKey, Errors | null | undefined>;

Expand Down Expand Up @@ -33,4 +43,4 @@ type Icon = {
fill?: string;
};

export type {Icon, PendingAction, PendingFields, ErrorFields, Errors, AvatarType};
export type {Icon, PendingAction, ErrorFields, Errors, AvatarType, OnyxValueWithOfflineFeedback};
7 changes: 2 additions & 5 deletions src/types/onyx/PersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Status = {
clearAfter: string; // ISO 8601 format;
};

type PersonalDetails = {
type PersonalDetails = OnyxCommon.OnyxValueWithOfflineFeedback<{
/** ID of the current user from their personal details */
accountID: number;

Expand Down Expand Up @@ -74,15 +74,12 @@ type PersonalDetails = {
/** Field-specific server side errors keyed by microtime */
errorFields?: OnyxCommon.ErrorFields<'avatar'>;

/** Field-specific pending states for offline UI status */
pendingFields?: OnyxCommon.PendingFields<'avatar' | 'originalFileName'>;

/** A fallback avatar icon to display when there is an error on loading avatar from remote URL. */
fallbackIcon?: string;

/** Status of the current user from their personal details */
status?: Status;
};
}>;

type PersonalDetailsList = Record<string, PersonalDetails | null>;

Expand Down
Loading
Loading