@@ -49,7 +49,7 @@ import CONST from '@src/CONST';
4949import ONYXKEYS from '@src/ONYXKEYS' ;
5050import ROUTES from '@src/ROUTES' ;
5151import type { ExpenseRuleForm , MerchantRuleForm , SpendRuleForm } from '@src/types/form' ;
52- import type { AppReview , BlockedFromConcierge , CustomStatusDraft , ExpenseRule , Policy , ReportAttributesDerivedValue } from '@src/types/onyx' ;
52+ import type { AppReview , BlockedFromConcierge , CustomStatusDraft , ExpenseRule , ReportAttributesDerivedValue } from '@src/types/onyx' ;
5353import type Login from '@src/types/onyx/Login' ;
5454import type { Errors } from '@src/types/onyx/OnyxCommon' ;
5555import type { AnyOnyxServerUpdate , OnyxServerUpdate , OnyxUpdateEvent } from '@src/types/onyx/OnyxUpdatesFromServer' ;
@@ -1053,20 +1053,37 @@ function generateStatementPDF(period: string) {
10531053
10541054/**
10551055 * Sets a contact method / secondary login as the user's "Default" contact method.
1056+ * This uses no offline support (Pattern C) — state is only updated after the server confirms success.
10561057 * @param skipNavigation - When true, do not navigate (caller handles navigation, e.g. via useEffect when primaryContactMethod updates).
10571058 */
10581059function setContactMethodAsDefault (
10591060 currentUserPersonalDetails : OnyxEntry < OnyxPersonalDetails > ,
1060- policies : OnyxCollection < Policy > ,
10611061 newDefaultContactMethod : string ,
10621062 formatPhoneNumber : LocaleContextProps [ 'formatPhoneNumber' ] ,
1063- backTo ?: string ,
1064- skipNavigation ?: boolean ,
1063+ backTo : string | undefined ,
1064+ skipNavigation : boolean ,
1065+ validateCode : string ,
10651066) {
1066- const oldDefaultContactMethod = currentEmail ;
1067- const optimisticData : Array <
1068- OnyxUpdate < typeof ONYXKEYS . ACCOUNT | typeof ONYXKEYS . SESSION | typeof ONYXKEYS . LOGIN_LIST | typeof ONYXKEYS . PERSONAL_DETAILS_LIST | typeof ONYXKEYS . COLLECTION . POLICY >
1069- > = [
1067+ // Pattern C: only set a pending indicator optimistically, no actual data changes
1068+ const optimisticData : Array < OnyxUpdate < typeof ONYXKEYS . LOGIN_LIST > > = [
1069+ {
1070+ onyxMethod : Onyx . METHOD . MERGE ,
1071+ key : ONYXKEYS . LOGIN_LIST ,
1072+ value : {
1073+ [ newDefaultContactMethod ] : {
1074+ pendingFields : {
1075+ defaultLogin : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ,
1076+ } ,
1077+ errorFields : {
1078+ defaultLogin : null ,
1079+ } ,
1080+ } ,
1081+ } ,
1082+ } ,
1083+ ] ;
1084+
1085+ // Pattern C: apply all actual data changes only after server confirms success
1086+ const successData : Array < OnyxUpdate < typeof ONYXKEYS . ACCOUNT | typeof ONYXKEYS . SESSION | typeof ONYXKEYS . LOGIN_LIST | typeof ONYXKEYS . PERSONAL_DETAILS_LIST > > = [
10701087 {
10711088 onyxMethod : Onyx . METHOD . MERGE ,
10721089 key : ONYXKEYS . ACCOUNT ,
@@ -1087,9 +1104,6 @@ function setContactMethodAsDefault(
10871104 value : {
10881105 [ newDefaultContactMethod ] : {
10891106 pendingFields : {
1090- defaultLogin : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ,
1091- } ,
1092- errorFields : {
10931107 defaultLogin : null ,
10941108 } ,
10951109 } ,
@@ -1106,36 +1120,8 @@ function setContactMethodAsDefault(
11061120 } ,
11071121 } ,
11081122 ] ;
1109- const successData : Array < OnyxUpdate < typeof ONYXKEYS . LOGIN_LIST > > = [
1110- {
1111- onyxMethod : Onyx . METHOD . MERGE ,
1112- key : ONYXKEYS . LOGIN_LIST ,
1113- value : {
1114- [ newDefaultContactMethod ] : {
1115- pendingFields : {
1116- defaultLogin : null ,
1117- } ,
1118- } ,
1119- } ,
1120- } ,
1121- ] ;
1122- const failureData : Array <
1123- OnyxUpdate < typeof ONYXKEYS . ACCOUNT | typeof ONYXKEYS . SESSION | typeof ONYXKEYS . LOGIN_LIST | typeof ONYXKEYS . PERSONAL_DETAILS_LIST | typeof ONYXKEYS . COLLECTION . POLICY >
1124- > = [
1125- {
1126- onyxMethod : Onyx . METHOD . MERGE ,
1127- key : ONYXKEYS . ACCOUNT ,
1128- value : {
1129- primaryLogin : oldDefaultContactMethod ,
1130- } ,
1131- } ,
1132- {
1133- onyxMethod : Onyx . METHOD . MERGE ,
1134- key : ONYXKEYS . SESSION ,
1135- value : {
1136- email : oldDefaultContactMethod ,
1137- } ,
1138- } ,
1123+
1124+ const failureData : Array < OnyxUpdate < typeof ONYXKEYS . LOGIN_LIST > > = [
11391125 {
11401126 onyxMethod : Onyx . METHOD . MERGE ,
11411127 key : ONYXKEYS . LOGIN_LIST ,
@@ -1150,65 +1136,11 @@ function setContactMethodAsDefault(
11501136 } ,
11511137 } ,
11521138 } ,
1153- {
1154- onyxMethod : Onyx . METHOD . MERGE ,
1155- key : ONYXKEYS . PERSONAL_DETAILS_LIST ,
1156- value : {
1157- [ currentUserAccountID ] : { ...currentUserPersonalDetails } ,
1158- } ,
1159- } ,
11601139 ] ;
11611140
1162- for ( const policy of Object . values ( policies ?? { } ) ) {
1163- if ( ! policy ) {
1164- continue ;
1165- }
1166-
1167- let optimisticPolicyDataValue ;
1168- let failurePolicyDataValue ;
1169-
1170- if ( policy . employeeList ) {
1171- const currentEmployee = policy . employeeList [ oldDefaultContactMethod ] ;
1172- optimisticPolicyDataValue = {
1173- employeeList : {
1174- [ oldDefaultContactMethod ] : null ,
1175- [ newDefaultContactMethod ] : currentEmployee ,
1176- } ,
1177- } ;
1178- failurePolicyDataValue = {
1179- employeeList : {
1180- [ oldDefaultContactMethod ] : currentEmployee ,
1181- [ newDefaultContactMethod ] : null ,
1182- } ,
1183- } ;
1184- }
1185-
1186- if ( policy . ownerAccountID === currentUserAccountID ) {
1187- optimisticPolicyDataValue = {
1188- ...optimisticPolicyDataValue ,
1189- owner : newDefaultContactMethod ,
1190- } ;
1191- failurePolicyDataValue = {
1192- ...failurePolicyDataValue ,
1193- owner : policy . owner ,
1194- } ;
1195- }
1196-
1197- if ( optimisticPolicyDataValue && failurePolicyDataValue ) {
1198- optimisticData . push ( {
1199- onyxMethod : Onyx . METHOD . MERGE ,
1200- key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policy . id } ` ,
1201- value : optimisticPolicyDataValue ,
1202- } ) ;
1203- failureData . push ( {
1204- onyxMethod : Onyx . METHOD . MERGE ,
1205- key : `${ ONYXKEYS . COLLECTION . POLICY } ${ policy . id } ` ,
1206- value : failurePolicyDataValue ,
1207- } ) ;
1208- }
1209- }
12101141 const parameters : SetContactMethodAsDefaultParams = {
12111142 partnerUserID : newDefaultContactMethod ,
1143+ validateCode,
12121144 } ;
12131145
12141146 API . write ( WRITE_COMMANDS . SET_CONTACT_METHOD_AS_DEFAULT , parameters , {
0 commit comments