forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#33500 from samh-nl/fix/issue-25488
Allow empty draft messages
- Loading branch information
Showing
14 changed files
with
132 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import _ from 'lodash'; | ||
import Onyx, {OnyxEntry} from 'react-native-onyx'; | ||
import Log from '@libs/Log'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import {ReportActionsDraft, ReportActionsDrafts} from '@src/types/onyx'; | ||
import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
|
||
type ReportActionsDraftsKey = `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`; | ||
|
||
/** | ||
* This migration removes empty drafts from reportActionsDrafts, which was previously used to mark a draft as being non-existent (e.g. upon cancel). | ||
*/ | ||
export default function (): Promise<void> { | ||
return new Promise<void>((resolve) => { | ||
const connectionID = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS, | ||
waitForCollectionCallback: true, | ||
callback: (allReportActionsDrafts) => { | ||
Onyx.disconnect(connectionID); | ||
|
||
if (!allReportActionsDrafts) { | ||
Log.info('[Migrate Onyx] Skipped migration RemoveEmptyReportActionsDrafts because there were no reportActionsDrafts'); | ||
return resolve(); | ||
} | ||
|
||
const newReportActionsDrafts: Record<ReportActionsDraftsKey, OnyxEntry<ReportActionsDrafts>> = {}; | ||
Object.entries(allReportActionsDrafts).forEach(([onyxKey, reportActionDrafts]) => { | ||
const newReportActionsDraftsForReport: Record<string, ReportActionsDraft> = {}; | ||
|
||
// Whether there is at least one draft in this report that has to be migrated | ||
let hasUnmigratedDraft = false; | ||
|
||
if (reportActionDrafts) { | ||
Object.entries(reportActionDrafts).forEach(([reportActionID, reportActionDraft]) => { | ||
// If the draft is a string, it means it hasn't been migrated yet | ||
if (typeof reportActionDraft === 'string') { | ||
hasUnmigratedDraft = true; | ||
Log.info(`[Migrate Onyx] Migrating draft for report action ${reportActionID}`); | ||
|
||
if (_.isEmpty(reportActionDraft)) { | ||
Log.info(`[Migrate Onyx] Removing draft for report action ${reportActionID}`); | ||
return; | ||
} | ||
|
||
newReportActionsDraftsForReport[reportActionID] = {message: reportActionDraft}; | ||
} else { | ||
// We've already migrated this draft, so keep the existing value | ||
newReportActionsDraftsForReport[reportActionID] = reportActionDraft; | ||
} | ||
}); | ||
} | ||
|
||
if (isEmptyObject(newReportActionsDraftsForReport)) { | ||
Log.info('[Migrate Onyx] NO REMAINING'); | ||
// Clear if there are no drafts remaining | ||
newReportActionsDrafts[onyxKey as ReportActionsDraftsKey] = null; | ||
} else if (hasUnmigratedDraft) { | ||
// Only migrate if there are unmigrated drafts, there's no need to overwrite this onyx key with the same data | ||
newReportActionsDrafts[onyxKey as ReportActionsDraftsKey] = newReportActionsDraftsForReport; | ||
} | ||
}); | ||
|
||
if (isEmptyObject(newReportActionsDrafts)) { | ||
Log.info('[Migrate Onyx] Skipped migration RemoveEmptyReportActionsDrafts because there are no actions drafts to migrate'); | ||
return resolve(); | ||
} | ||
|
||
Log.info(`[Migrate Onyx] Updating drafts for ${Object.keys(newReportActionsDrafts).length} reports`); | ||
Onyx.multiSet(newReportActionsDrafts).then(() => { | ||
Log.info('[Migrate Onyx] Ran migration RemoveEmptyReportActionsDrafts successfully'); | ||
resolve(); | ||
}); | ||
}, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type ReportActionsDraft = | ||
| { | ||
message: string; | ||
} | ||
| string; | ||
|
||
export default ReportActionsDraft; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
type ReportActionsDrafts = Record<string, string>; | ||
import ReportActionsDraft from './ReportActionsDraft'; | ||
|
||
type ReportActionsDrafts = Record<string, ReportActionsDraft>; | ||
|
||
export default ReportActionsDrafts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters