From 63f5ae0f6fb1cdd697d42401a223520ba6e59b05 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 5 Sep 2023 14:55:41 +0200 Subject: [PATCH 01/18] ref: migrate to Typescript --- src/libs/actions/FormActions.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/libs/actions/FormActions.ts diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts new file mode 100644 index 000000000000..1245ddc69779 --- /dev/null +++ b/src/libs/actions/FormActions.ts @@ -0,0 +1,21 @@ +import Onyx from 'react-native-onyx'; +import * as OnyxCommon from '../../types/onyx/OnyxCommon'; +import {OnyxKey} from '../../ONYXKEYS'; + +function setIsLoading(formID: OnyxKey, isLoading: boolean) { + Onyx.merge(formID, {isLoading}); +} + +function setErrors(formID: OnyxKey, errors: OnyxCommon.Errors) { + Onyx.merge(formID, {errors}); +} + +function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { + Onyx.merge(formID, {errorFields}); +} + +function setDraftValues(formID: OnyxKey, draftValues: unknown) { + Onyx.merge(`${formID}Draft`, draftValues); +} + +export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From fcf10551bdec064daae2f52ef238990c490b8afd Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Wed, 6 Sep 2023 08:13:54 +0200 Subject: [PATCH 02/18] ref: added utility types --- src/libs/actions/FormActions.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 1245ddc69779..41e2c230ef11 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,6 +1,6 @@ import Onyx from 'react-native-onyx'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -import {OnyxKey} from '../../ONYXKEYS'; +import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); @@ -14,8 +14,13 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues(formID: OnyxKey, draftValues: unknown) { +type KeysWhichCouldBeDraft = T extends `${infer Prefix}Draft` ? Prefix : never; +type MapUnionToValue = T extends keyof OnyxValues ? OnyxValues[T] : never; +type KeysWithDraftSuffix = T extends `${infer Prefix}Draft` ? T : never; + +function setDraftValues>(formID: KeysWhichCouldBeDraft, draftValues: MapUnionToValue) { Onyx.merge(`${formID}Draft`, draftValues); } +setDraftValues('', {}); export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From e4351925e3f1fe4edad325652e6e7047dff93524 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Wed, 6 Sep 2023 16:56:37 +0200 Subject: [PATCH 03/18] ref: added TODO comments --- src/libs/actions/FormActions.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 41e2c230ef11..b67d72cb6541 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,6 +1,8 @@ import Onyx from 'react-native-onyx'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; +// TODO: Ask where to put this type +type MapUnionToValue = T extends keyof OnyxValues ? OnyxValues[T] : never; function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); @@ -14,13 +16,9 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -type KeysWhichCouldBeDraft = T extends `${infer Prefix}Draft` ? Prefix : never; -type MapUnionToValue = T extends keyof OnyxValues ? OnyxValues[T] : never; -type KeysWithDraftSuffix = T extends `${infer Prefix}Draft` ? T : never; - -function setDraftValues>(formID: KeysWhichCouldBeDraft, draftValues: MapUnionToValue) { +function setDraftValues(formID: T, draftValues: MapUnionToValue) { + // TODO: Ask about what should we do here ? Onyx.merge(`${formID}Draft`, draftValues); } -setDraftValues('', {}); export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From 7d3bf5bbd10deab0a3e9c7031eddedf72962807c Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Wed, 6 Sep 2023 17:05:35 +0200 Subject: [PATCH 04/18] fix: removed JS file --- src/libs/actions/FormActions.js | 35 --------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 src/libs/actions/FormActions.js diff --git a/src/libs/actions/FormActions.js b/src/libs/actions/FormActions.js deleted file mode 100644 index 2a4e1dbd5d27..000000000000 --- a/src/libs/actions/FormActions.js +++ /dev/null @@ -1,35 +0,0 @@ -import Onyx from 'react-native-onyx'; - -/** - * @param {String} formID - * @param {Boolean} isLoading - */ -function setIsLoading(formID, isLoading) { - Onyx.merge(formID, {isLoading}); -} - -/** - * @param {String} formID - * @param {Object} errors - */ -function setErrors(formID, errors) { - Onyx.merge(formID, {errors}); -} - -/** - * @param {String} formID - * @param {Object} errorFields - */ -function setErrorFields(formID, errorFields) { - Onyx.merge(formID, {errorFields}); -} - -/** - * @param {String} formID - * @param {Object} draftValues - */ -function setDraftValues(formID, draftValues) { - Onyx.merge(`${formID}Draft`, draftValues); -} - -export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From b7ae41913d9c5ba04f37557f3a24039c3784e0ab Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 14 Sep 2023 15:20:42 +0200 Subject: [PATCH 05/18] fix: fix setDraftValues type --- src/libs/actions/FormActions.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index b67d72cb6541..c50ba6f2d4cb 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,8 +1,11 @@ import Onyx from 'react-native-onyx'; +import {PartialDeep} from 'type-fest'; +import {KeyValueMapping} from 'react-native-onyx/lib/types'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; -// TODO: Ask where to put this type -type MapUnionToValue = T extends keyof OnyxValues ? OnyxValues[T] : never; +import {OnyxKey} from '../../ONYXKEYS'; + +type KeysWhichCouldBeDraft = T extends `${infer Prefix}Draft` ? Prefix : never; +type KeysWithDraftSuffix = T extends `${infer Prefix}Draft` ? T : never; function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); @@ -16,8 +19,7 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues(formID: T, draftValues: MapUnionToValue) { - // TODO: Ask about what should we do here ? +function setDraftValues>(formID: KeysWhichCouldBeDraft, draftValues: PartialDeep}Draft`], {}>) { Onyx.merge(`${formID}Draft`, draftValues); } From 3bf1d01023e3d29aa248e87ae9e7ab6b614223ea Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 14 Sep 2023 15:23:43 +0200 Subject: [PATCH 06/18] fix: removed unnecessary type --- src/libs/actions/FormActions.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index c50ba6f2d4cb..5ca814328d8d 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -5,7 +5,6 @@ import * as OnyxCommon from '../../types/onyx/OnyxCommon'; import {OnyxKey} from '../../ONYXKEYS'; type KeysWhichCouldBeDraft = T extends `${infer Prefix}Draft` ? Prefix : never; -type KeysWithDraftSuffix = T extends `${infer Prefix}Draft` ? T : never; function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); @@ -19,8 +18,8 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues>(formID: KeysWhichCouldBeDraft, draftValues: PartialDeep}Draft`], {}>) { +function setDraftValues(formID: KeysWhichCouldBeDraft, draftValues: PartialDeep}Draft`], {}>) { Onyx.merge(`${formID}Draft`, draftValues); } - +setDraftValues('customStatus', {}); export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From 3488e8415f5d62bd6ed28bc416cd849bf77e277d Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Fri, 15 Sep 2023 09:22:23 +0200 Subject: [PATCH 07/18] fix: fixed draftValues type --- src/libs/actions/FormActions.ts | 10 +++--- .../migrations/RenameExpensifyNewsStatus.ts | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/libs/migrations/RenameExpensifyNewsStatus.ts diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 5ca814328d8d..5ec5af66e47b 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,10 +1,8 @@ import Onyx from 'react-native-onyx'; -import {PartialDeep} from 'type-fest'; -import {KeyValueMapping} from 'react-native-onyx/lib/types'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -import {OnyxKey} from '../../ONYXKEYS'; +import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; -type KeysWhichCouldBeDraft = T extends `${infer Prefix}Draft` ? Prefix : never; +type ExtractValueByKey = T[K]; function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); @@ -18,8 +16,8 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues(formID: KeysWhichCouldBeDraft, draftValues: PartialDeep}Draft`], {}>) { +function setDraftValues(formID: T, draftValues: ExtractValueByKey) { Onyx.merge(`${formID}Draft`, draftValues); } -setDraftValues('customStatus', {}); +setDraftValues('reimbursementAccount', {}); export {setIsLoading, setErrors, setErrorFields, setDraftValues}; diff --git a/src/libs/migrations/RenameExpensifyNewsStatus.ts b/src/libs/migrations/RenameExpensifyNewsStatus.ts new file mode 100644 index 000000000000..d386f3ca2e03 --- /dev/null +++ b/src/libs/migrations/RenameExpensifyNewsStatus.ts @@ -0,0 +1,32 @@ +import Onyx from 'react-native-onyx'; +import ONYXKEYS from '../../ONYXKEYS'; +import Log from '../Log'; + +// This migration changes the name of the Onyx key user.expensifyNewsStatus from expensifyNewsStatus to isSubscribedToNewsletter +export default function () { + return new Promise((resolve) => { + // Connect to the USER key in Onyx to get the value of expensifyNewsStatus + // then set that value as isSubscribedToNewsletter + // finally remove expensifyNewsStatus by setting the value to null + const connectionID = Onyx.connect({ + key: ONYXKEYS.USER, + callback: (user) => { + Onyx.disconnect(connectionID); + + // Fail early here because there is nothing to migrate + if (!user || !user?.expensifyNewsStatus) { + Log.info('[Migrate Onyx] Skipped migration RenameExpensifyNewsStatus'); + return resolve(user); + } + + // eslint-disable-next-line rulesdir/prefer-actions-set-data + Onyx.merge(ONYXKEYS.USER, { + expensifyNewsStatus: null, + isSubscribedToNewsletter: user.expensifyNewsStatus, + }).then(() => { + resolve(user); + }); + }, + }); + }); +} From e4fdf7cfcb899ae46acf1f6b4a7010842129fdf5 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Fri, 15 Sep 2023 13:29:19 +0200 Subject: [PATCH 08/18] fix: fixed TS errors --- src/libs/actions/FormActions.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 5ec5af66e47b..6986d170774d 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -2,8 +2,11 @@ import Onyx from 'react-native-onyx'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; -type ExtractValueByKey = T[K]; - +type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; +// type DraftKey = T extends DraftKeysWithoutDraftSuffix ? keyof OnyxValues[`${T}Draft`] : never; +type GetDraftValue = T extends keyof OnyxValues ? OnyxValues[T] : never; +type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; +type GetDraftKey = T extends `${infer Prefix}Draft` ? T : `${T}Draft`; function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); } @@ -16,8 +19,8 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues(formID: T, draftValues: ExtractValueByKey) { +function setDraftValues(formID: DraftKeysWithoutDraftSuffix, draftValues: GetDraftValue>) { Onyx.merge(`${formID}Draft`, draftValues); } -setDraftValues('reimbursementAccount', {}); +setDraftValues('customStatus', {}); export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From 94e14b15627d27186fae5986f5e89d77213e0232 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 19 Sep 2023 14:01:13 +0200 Subject: [PATCH 09/18] fix: added custom status key to onyx --- src/ONYXKEYS.ts | 4 ++++ src/libs/actions/FormActions.ts | 9 ++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 2e0b75910bae..9b0d1706e828 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -47,6 +47,9 @@ const ONYXKEYS = { /** Has information about the network status (offline/online) */ NETWORK: 'network', + // status + CUSTOM_STATUS: 'customStatus', + // draft status CUSTOM_STATUS_DRAFT: 'customStatusDraft', @@ -314,6 +317,7 @@ type OnyxValues = { [ONYXKEYS.IOU]: OnyxTypes.IOU; [ONYXKEYS.MODAL]: OnyxTypes.Modal; [ONYXKEYS.NETWORK]: OnyxTypes.Network; + [ONYXKEYS.CUSTOM_STATUS]: OnyxTypes.CustomStatusDraft; [ONYXKEYS.CUSTOM_STATUS_DRAFT]: OnyxTypes.CustomStatusDraft; [ONYXKEYS.PERSONAL_DETAILS_LIST]: Record; [ONYXKEYS.PRIVATE_PERSONAL_DETAILS]: OnyxTypes.PrivatePersonalDetails; diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 6986d170774d..799b501bf798 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -3,10 +3,9 @@ import * as OnyxCommon from '../../types/onyx/OnyxCommon'; import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; -// type DraftKey = T extends DraftKeysWithoutDraftSuffix ? keyof OnyxValues[`${T}Draft`] : never; -type GetDraftValue = T extends keyof OnyxValues ? OnyxValues[T] : never; +type GetDraftValue = T extends keyof OnyxValues ? OnyxValues[`${T}Draft`] : never; type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; -type GetDraftKey = T extends `${infer Prefix}Draft` ? T : `${T}Draft`; + function setIsLoading(formID: OnyxKey, isLoading: boolean) { Onyx.merge(formID, {isLoading}); } @@ -19,8 +18,8 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues(formID: DraftKeysWithoutDraftSuffix, draftValues: GetDraftValue>) { +function setDraftValues(formID: T, draftValues: GetDraftValue) { Onyx.merge(`${formID}Draft`, draftValues); } -setDraftValues('customStatus', {}); + export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From e17e39a6c580f34eeb7b4b6943baf6fe91cae313 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 19 Sep 2023 14:08:45 +0200 Subject: [PATCH 10/18] fix: finnaly add correct types --- src/ONYXKEYS.ts | 4 ---- src/libs/actions/FormActions.ts | 5 +++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 9b0d1706e828..2e0b75910bae 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -47,9 +47,6 @@ const ONYXKEYS = { /** Has information about the network status (offline/online) */ NETWORK: 'network', - // status - CUSTOM_STATUS: 'customStatus', - // draft status CUSTOM_STATUS_DRAFT: 'customStatusDraft', @@ -317,7 +314,6 @@ type OnyxValues = { [ONYXKEYS.IOU]: OnyxTypes.IOU; [ONYXKEYS.MODAL]: OnyxTypes.Modal; [ONYXKEYS.NETWORK]: OnyxTypes.Network; - [ONYXKEYS.CUSTOM_STATUS]: OnyxTypes.CustomStatusDraft; [ONYXKEYS.CUSTOM_STATUS_DRAFT]: OnyxTypes.CustomStatusDraft; [ONYXKEYS.PERSONAL_DETAILS_LIST]: Record; [ONYXKEYS.PRIVATE_PERSONAL_DETAILS]: OnyxTypes.PrivatePersonalDetails; diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 799b501bf798..b2788ade242e 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,9 +1,10 @@ import Onyx from 'react-native-onyx'; +import {PartialDeep} from 'type-fest'; +import {KeyValueMapping} from 'react-native-onyx/lib/types'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; -type GetDraftValue = T extends keyof OnyxValues ? OnyxValues[`${T}Draft`] : never; type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; function setIsLoading(formID: OnyxKey, isLoading: boolean) { @@ -18,7 +19,7 @@ function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { Onyx.merge(formID, {errorFields}); } -function setDraftValues(formID: T, draftValues: GetDraftValue) { +function setDraftValues(formID: T, draftValues: PartialDeep) { Onyx.merge(`${formID}Draft`, draftValues); } From e8f65c51aaf3a4b3be8f4f9eb44bbca82febf70d Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 19 Sep 2023 14:10:39 +0200 Subject: [PATCH 11/18] fix: removed unwanted file --- .../migrations/RenameExpensifyNewsStatus.ts | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/libs/migrations/RenameExpensifyNewsStatus.ts diff --git a/src/libs/migrations/RenameExpensifyNewsStatus.ts b/src/libs/migrations/RenameExpensifyNewsStatus.ts deleted file mode 100644 index d386f3ca2e03..000000000000 --- a/src/libs/migrations/RenameExpensifyNewsStatus.ts +++ /dev/null @@ -1,32 +0,0 @@ -import Onyx from 'react-native-onyx'; -import ONYXKEYS from '../../ONYXKEYS'; -import Log from '../Log'; - -// This migration changes the name of the Onyx key user.expensifyNewsStatus from expensifyNewsStatus to isSubscribedToNewsletter -export default function () { - return new Promise((resolve) => { - // Connect to the USER key in Onyx to get the value of expensifyNewsStatus - // then set that value as isSubscribedToNewsletter - // finally remove expensifyNewsStatus by setting the value to null - const connectionID = Onyx.connect({ - key: ONYXKEYS.USER, - callback: (user) => { - Onyx.disconnect(connectionID); - - // Fail early here because there is nothing to migrate - if (!user || !user?.expensifyNewsStatus) { - Log.info('[Migrate Onyx] Skipped migration RenameExpensifyNewsStatus'); - return resolve(user); - } - - // eslint-disable-next-line rulesdir/prefer-actions-set-data - Onyx.merge(ONYXKEYS.USER, { - expensifyNewsStatus: null, - isSubscribedToNewsletter: user.expensifyNewsStatus, - }).then(() => { - resolve(user); - }); - }, - }); - }); -} From 0c585ea5c33560bc684d67daec1651149d642834 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Thu, 21 Sep 2023 16:14:57 +0200 Subject: [PATCH 12/18] fix: added draft eqivalents of form keys --- src/ONYXKEYS.ts | 55 +++++++++++++++++++++++++++++++-- src/libs/actions/FormActions.ts | 26 +++++++++------- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 05256f2b806c..e2153d95fddc 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -262,40 +262,68 @@ const ONYXKEYS = { /** List of Form ids */ FORMS: { ADD_DEBIT_CARD_FORM: 'addDebitCardForm', + ADD_DEBIT_CARD_FORM_DRAFT: 'addDebitCardFormDraft', WORKSPACE_SETTINGS_FORM: 'workspaceSettingsForm', + WORKSPACE_SETTINGS_FORM_DRAFT: 'workspaceSettingsFormDraft', WORKSPACE_RATE_AND_UNIT_FORM: 'workspaceRateAndUnitForm', + WORKSPACE_RATE_AND_UNIT_FORM_DRAFT: 'workspaceRateAndUnitFormDraft', CLOSE_ACCOUNT_FORM: 'closeAccount', + CLOSE_ACCOUNT_FORM_DRAFT: 'closeAccountDraft', PROFILE_SETTINGS_FORM: 'profileSettingsForm', + PROFILE_SETTINGS_FORM_DRAFT: 'profileSettingsFormDraft', DISPLAY_NAME_FORM: 'displayNameForm', + DISPLAY_NAME_FORM_DRAFT: 'displayNameFormDraft', ROOM_NAME_FORM: 'roomNameForm', + ROOM_NAME_FORM_DRAFT: 'roomNameFormDraft', WELCOME_MESSAGE_FORM: 'welcomeMessageForm', + WELCOME_MESSAGE_FORM_DRAFT: 'welcomeMessageFormDraft', LEGAL_NAME_FORM: 'legalNameForm', + LEGAL_NAME_FORM_DRAFT: 'legalNameFormDraft', WORKSPACE_INVITE_MESSAGE_FORM: 'workspaceInviteMessageForm', + WORKSPACE_INVITE_MESSAGE_FORM_DRAFT: 'workspaceInviteMessageFormDraft', DATE_OF_BIRTH_FORM: 'dateOfBirthForm', + DATE_OF_BIRTH_FORM_DRAFT: 'dateOfBirthFormDraft', HOME_ADDRESS_FORM: 'homeAddressForm', + HOME_ADDRESS_FORM_DRAFT: 'homeAddressFormDraft', NEW_ROOM_FORM: 'newRoomForm', + NEW_ROOM_FORM_DRAFT: 'newRoomFormDraft', ROOM_SETTINGS_FORM: 'roomSettingsForm', + ROOM_SETTINGS_FORM_DRAFT: 'roomSettingsFormDraft', NEW_TASK_FORM: 'newTaskForm', + NEW_TASK_FORM_DRAFT: 'newTaskFormDraft', EDIT_TASK_FORM: 'editTaskForm', + EDIT_TASK_FORM_DRAFT: 'editTaskFormDraft', MONEY_REQUEST_DESCRIPTION_FORM: 'moneyRequestDescriptionForm', + MONEY_REQUEST_DESCRIPTION_FORM_DRAFT: 'moneyRequestDescriptionFormDraft', MONEY_REQUEST_MERCHANT_FORM: 'moneyRequestMerchantForm', + MONEY_REQUEST_MERCHANT_FORM_DRAFT: 'moneyRequestMerchantFormDraft', MONEY_REQUEST_AMOUNT_FORM: 'moneyRequestAmountForm', + MONEY_REQUEST_AMOUNT_FORM_DRAFT: 'moneyRequestAmountFormDraft', MONEY_REQUEST_DATE_FORM: 'moneyRequestCreatedForm', + MONEY_REQUEST_DATE_FORM_DRAFT: 'moneyRequestCreatedFormDraft', NEW_CONTACT_METHOD_FORM: 'newContactMethodForm', + NEW_CONTACT_METHOD_FORM_DRAFT: 'newContactMethodFormDraft', WAYPOINT_FORM: 'waypointForm', WAYPOINT_FORM_DRAFT: 'waypointFormDraft', SETTINGS_STATUS_SET_FORM: 'settingsStatusSetForm', + SETTINGS_STATUS_SET_FORM_DRAFT: 'settingsStatusSetFormDraft', SETTINGS_STATUS_CLEAR_AFTER_FORM: 'settingsStatusClearAfterForm', + SETTINGS_STATUS_CLEAR_AFTER_FORM_DRAFT: 'settingsStatusClearAfterFormDraft', SETTINGS_STATUS_SET_CLEAR_AFTER_FORM: 'settingsStatusSetClearAfterForm', + SETTINGS_STATUS_SET_CLEAR_AFTER_FORM_DRAFT: 'settingsStatusSetClearAfterFormDraft', PRIVATE_NOTES_FORM: 'privateNotesForm', + PRIVATE_NOTES_FORM_DRAFT: 'privateNotesFormDraft', I_KNOW_A_TEACHER_FORM: 'iKnowTeacherForm', + I_KNOW_A_TEACHER_FORM_DRAFT: 'iKnowTeacherFormDraft', INTRO_SCHOOL_PRINCIPAL_FORM: 'introSchoolPrincipalForm', + INTRO_SCHOOL_PRINCIPAL_FORM_DRAFT: 'introSchoolPrincipalFormDraft', }, } as const; type OnyxKeysMap = typeof ONYXKEYS; type OnyxCollectionKey = ValueOf; type OnyxKey = DeepValueOf>; +type OnyxFormKey = ValueOf; type OnyxValues = { [ONYXKEYS.ACCOUNT]: OnyxTypes.Account; @@ -393,33 +421,56 @@ type OnyxValues = { // Forms [ONYXKEYS.FORMS.ADD_DEBIT_CARD_FORM]: OnyxTypes.AddDebitCardForm; + [ONYXKEYS.FORMS.ADD_DEBIT_CARD_FORM_DRAFT]: OnyxTypes.AddDebitCardForm; [ONYXKEYS.FORMS.WORKSPACE_SETTINGS_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.WORKSPACE_SETTINGS_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.CLOSE_ACCOUNT_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.PROFILE_SETTINGS_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.PROFILE_SETTINGS_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.DISPLAY_NAME_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.DISPLAY_NAME_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.ROOM_NAME_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.ROOM_NAME_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.WELCOME_MESSAGE_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.WELCOME_MESSAGE_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.LEGAL_NAME_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.LEGAL_NAME_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.WORKSPACE_INVITE_MESSAGE_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.WORKSPACE_INVITE_MESSAGE_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.DATE_OF_BIRTH_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.DATE_OF_BIRTH_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.HOME_ADDRESS_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.HOME_ADDRESS_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.NEW_ROOM_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.NEW_ROOM_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.ROOM_SETTINGS_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.ROOM_SETTINGS_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.NEW_TASK_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.NEW_TASK_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.EDIT_TASK_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.EDIT_TASK_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.MONEY_REQUEST_DESCRIPTION_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.MONEY_REQUEST_MERCHANT_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.MONEY_REQUEST_MERCHANT_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.MONEY_REQUEST_AMOUNT_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.MONEY_REQUEST_AMOUNT_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.MONEY_REQUEST_DATE_FORM]: OnyxTypes.Form; - [ONYXKEYS.FORMS.MONEY_REQUEST_DATE_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.MONEY_REQUEST_DATE_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.NEW_CONTACT_METHOD_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.NEW_CONTACT_METHOD_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.WAYPOINT_FORM]: OnyxTypes.Form; [ONYXKEYS.FORMS.WAYPOINT_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.SETTINGS_STATUS_SET_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.SETTINGS_STATUS_SET_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.SETTINGS_STATUS_CLEAR_AFTER_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.SETTINGS_STATUS_CLEAR_AFTER_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.SETTINGS_STATUS_SET_CLEAR_AFTER_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.SETTINGS_STATUS_SET_CLEAR_AFTER_FORM_DRAFT]: OnyxTypes.Form; }; export default ONYXKEYS; -export type {OnyxKey, OnyxCollectionKey, OnyxValues}; +export type {OnyxKey, OnyxCollectionKey, OnyxValues, OnyxFormKey, OnyxKeysMap}; diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index b2788ade242e..80c03a9e796b 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -2,25 +2,29 @@ import Onyx from 'react-native-onyx'; import {PartialDeep} from 'type-fest'; import {KeyValueMapping} from 'react-native-onyx/lib/types'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -import {OnyxKey, OnyxValues} from '../../ONYXKEYS'; +import {OnyxFormKey, OnyxKey, OnyxKeysMap, OnyxValues} from '../../ONYXKEYS'; +import {Form} from '../../types/onyx'; -type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; -type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; +type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; +type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; +type GetValueForKey = T extends keyof OnyxValues ? OnyxValues[T] : never; -function setIsLoading(formID: OnyxKey, isLoading: boolean) { - Onyx.merge(formID, {isLoading}); +function setIsLoading(formID: OnyxFormKey, isLoading: boolean) { + Onyx.merge(formID, {isLoading} satisfies Form); } -function setErrors(formID: OnyxKey, errors: OnyxCommon.Errors) { - Onyx.merge(formID, {errors}); +function setErrors(formID: OnyxFormKey, errors: OnyxCommon.Errors) { + Onyx.merge(formID, {errors} satisfies Form); } -function setErrorFields(formID: OnyxKey, errorFields: OnyxCommon.ErrorFields) { - Onyx.merge(formID, {errorFields}); +function setErrorFields(formID: OnyxFormKey, errorFields: OnyxCommon.ErrorFields) { + Onyx.merge(formID, {errorFields} satisfies Form); } -function setDraftValues(formID: T, draftValues: PartialDeep) { - Onyx.merge(`${formID}Draft`, draftValues); +function setDraftValues(formID: T, draftValues: PartialDeep) { + Onyx.merge(`${formID}Draft` as OnyxKey, draftValues); } +setDraftValues('reimbursementAccount', {}); + export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From e4b4256c0013d549e3b46600355c4fa1c62eccd1 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Fri, 22 Sep 2023 17:35:04 +0200 Subject: [PATCH 13/18] fix: wip --- src/libs/actions/FormActions.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 80c03a9e796b..b03240151257 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -2,12 +2,11 @@ import Onyx from 'react-native-onyx'; import {PartialDeep} from 'type-fest'; import {KeyValueMapping} from 'react-native-onyx/lib/types'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -import {OnyxFormKey, OnyxKey, OnyxKeysMap, OnyxValues} from '../../ONYXKEYS'; +import {OnyxFormKey, OnyxKey, OnyxKeysMap} from '../../ONYXKEYS'; import {Form} from '../../types/onyx'; type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; -type GetValueForKey = T extends keyof OnyxValues ? OnyxValues[T] : never; function setIsLoading(formID: OnyxFormKey, isLoading: boolean) { Onyx.merge(formID, {isLoading} satisfies Form); @@ -24,7 +23,7 @@ function setErrorFields(formID: OnyxFormKey, errorFields: OnyxCommon.ErrorFields function setDraftValues(formID: T, draftValues: PartialDeep) { Onyx.merge(`${formID}Draft` as OnyxKey, draftValues); } - +// TODO: Remove this once we have fix for a types setDraftValues('reimbursementAccount', {}); export {setIsLoading, setErrors, setErrorFields, setDraftValues}; From e7fbcb3b08f3250e10f425c682070bb700df493b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Henriques?= Date: Tue, 26 Sep 2023 11:12:54 +0100 Subject: [PATCH 14/18] Fix setDraftValues types --- src/ONYXKEYS.ts | 8 +++++++- src/libs/actions/FormActions.ts | 19 +++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index e2153d95fddc..4e1d13abc2d4 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -323,7 +323,7 @@ const ONYXKEYS = { type OnyxKeysMap = typeof ONYXKEYS; type OnyxCollectionKey = ValueOf; type OnyxKey = DeepValueOf>; -type OnyxFormKey = ValueOf; +type OnyxFormKey = ValueOf | OnyxKeysMap['REIMBURSEMENT_ACCOUNT'] | OnyxKeysMap['REIMBURSEMENT_ACCOUNT_DRAFT']; type OnyxValues = { [ONYXKEYS.ACCOUNT]: OnyxTypes.Account; @@ -470,6 +470,12 @@ type OnyxValues = { [ONYXKEYS.FORMS.SETTINGS_STATUS_CLEAR_AFTER_FORM_DRAFT]: OnyxTypes.Form; [ONYXKEYS.FORMS.SETTINGS_STATUS_SET_CLEAR_AFTER_FORM]: OnyxTypes.Form; [ONYXKEYS.FORMS.SETTINGS_STATUS_SET_CLEAR_AFTER_FORM_DRAFT]: OnyxTypes.Form; + [ONYXKEYS.FORMS.PRIVATE_NOTES_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.PRIVATE_NOTES_FORM_DRAFT]: OnyxTypes.Form; + [ONYXKEYS.FORMS.I_KNOW_A_TEACHER_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.I_KNOW_A_TEACHER_FORM_DRAFT]: OnyxTypes.Form; + [ONYXKEYS.FORMS.INTRO_SCHOOL_PRINCIPAL_FORM]: OnyxTypes.Form; + [ONYXKEYS.FORMS.INTRO_SCHOOL_PRINCIPAL_FORM_DRAFT]: OnyxTypes.Form; }; export default ONYXKEYS; diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index b03240151257..dc2c884165c1 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,12 +1,13 @@ import Onyx from 'react-native-onyx'; -import {PartialDeep} from 'type-fest'; import {KeyValueMapping} from 'react-native-onyx/lib/types'; -import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -import {OnyxFormKey, OnyxKey, OnyxKeysMap} from '../../ONYXKEYS'; +import {PartialDeep} from 'type-fest'; +import {OnyxFormKey} from '../../ONYXKEYS'; import {Form} from '../../types/onyx'; +import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -type KeysWhichCouldBeDraft = T extends `${infer U}Draft` ? U : never; -type DraftKeysWithoutDraftSuffix = KeysWhichCouldBeDraft; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +type ExcludeDraft = T extends `${infer R}Draft` ? never : T; +type OnyxFormKeyWithoutDraft = ExcludeDraft; function setIsLoading(formID: OnyxFormKey, isLoading: boolean) { Onyx.merge(formID, {isLoading} satisfies Form); @@ -20,10 +21,8 @@ function setErrorFields(formID: OnyxFormKey, errorFields: OnyxCommon.ErrorFields Onyx.merge(formID, {errorFields} satisfies Form); } -function setDraftValues(formID: T, draftValues: PartialDeep) { - Onyx.merge(`${formID}Draft` as OnyxKey, draftValues); +function setDraftValues(formID: T, draftValues: PartialDeep) { + Onyx.merge(`${formID}Draft`, draftValues); } -// TODO: Remove this once we have fix for a types -setDraftValues('reimbursementAccount', {}); -export {setIsLoading, setErrors, setErrorFields, setDraftValues}; +export {setDraftValues, setErrorFields, setErrors, setIsLoading}; From cbb57cc5a724f5849e3a920e0c41093f7db94010 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 26 Sep 2023 15:12:52 +0200 Subject: [PATCH 15/18] chore: fix tests --- src/ONYXKEYS.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 4e1d13abc2d4..bde6788b0916 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -136,7 +136,6 @@ const ONYXKEYS = { /** The user's bank accounts */ BANK_ACCOUNT_LIST: 'bankAccountList', - /** The user's payment and P2P cards */ FUND_LIST: 'fundList', From 872783568998ada0a0d163fa6bda3172201a41db Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 26 Sep 2023 15:13:09 +0200 Subject: [PATCH 16/18] chore: fix tests --- src/ONYXKEYS.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index bde6788b0916..4e1d13abc2d4 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -136,6 +136,7 @@ const ONYXKEYS = { /** The user's bank accounts */ BANK_ACCOUNT_LIST: 'bankAccountList', + /** The user's payment and P2P cards */ FUND_LIST: 'fundList', From c1737454e4ef51f4a18cccfd002416def03009df Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 26 Sep 2023 15:19:37 +0200 Subject: [PATCH 17/18] fix: resolve comments --- src/libs/actions/FormActions.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index dc2c884165c1..8d59173853ea 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -5,8 +5,7 @@ import {OnyxFormKey} from '../../ONYXKEYS'; import {Form} from '../../types/onyx'; import * as OnyxCommon from '../../types/onyx/OnyxCommon'; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -type ExcludeDraft = T extends `${infer R}Draft` ? never : T; +type ExcludeDraft = T extends `${string}Draft` ? never : T; type OnyxFormKeyWithoutDraft = ExcludeDraft; function setIsLoading(formID: OnyxFormKey, isLoading: boolean) { From d5daf2d8f38bfc962aebf37bd902655477cc7821 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Mon, 30 Oct 2023 12:13:09 +0100 Subject: [PATCH 18/18] fix: linter --- src/libs/actions/FormActions.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/FormActions.ts b/src/libs/actions/FormActions.ts index 39d64b7d37f9..ecaf38dc44f2 100644 --- a/src/libs/actions/FormActions.ts +++ b/src/libs/actions/FormActions.ts @@ -1,8 +1,8 @@ import Onyx from 'react-native-onyx'; import {KeyValueMapping, NullishDeep} from 'react-native-onyx/lib/types'; -import {OnyxFormKey} from '../../ONYXKEYS'; -import {Form} from '../../types/onyx'; -import * as OnyxCommon from '../../types/onyx/OnyxCommon'; +import {OnyxFormKey} from '@src/ONYXKEYS'; +import {Form} from '@src/types/onyx'; +import * as OnyxCommon from '@src/types/onyx/OnyxCommon'; type ExcludeDraft = T extends `${string}Draft` ? never : T; type OnyxFormKeyWithoutDraft = ExcludeDraft;