Skip to content

Commit b34c830

Browse files
committed
improve code
1 parent 1016ef8 commit b34c830

File tree

1 file changed

+36
-65
lines changed

1 file changed

+36
-65
lines changed

packages/fields/src/mutation/index.ts

+36-65
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ import { store as coreStore } from '@wordpress/core-data';
1010
import type { CoreDataError, Post } from '../types';
1111
import { dispatch } from '@wordpress/data';
1212

13+
const getErrorMessagesFromPromises = < T >(
14+
allSettledResults: PromiseSettledResult< T >[]
15+
) => {
16+
const errorMessages = new Set< string >();
17+
// If there was at lease one failure.
18+
if ( allSettledResults.length === 1 ) {
19+
const typedError = allSettledResults[ 0 ] as {
20+
reason?: CoreDataError;
21+
};
22+
if ( typedError.reason?.message ) {
23+
errorMessages.add( typedError.reason.message );
24+
}
25+
} else {
26+
const failedPromises = allSettledResults.filter(
27+
( { status } ) => status === 'rejected'
28+
);
29+
for ( const failedPromise of failedPromises ) {
30+
const typedError = failedPromise as {
31+
reason?: CoreDataError;
32+
};
33+
if ( typedError.reason?.message ) {
34+
errorMessages.add( typedError.reason.message );
35+
}
36+
}
37+
}
38+
return errorMessages;
39+
};
40+
1341
export type NoticeSettings< T extends Post > = {
1442
success: {
1543
id?: string;
@@ -64,38 +92,9 @@ export const deletePostWithNotices = async < T extends Post >(
6492
} );
6593
callbacks.onActionPerformed?.( posts );
6694
} else {
67-
const errorMessages = new Set< string >();
68-
// If there was at lease one failure.
69-
let errorMessage;
70-
// If we were trying to permanently delete a single post.
71-
if ( promiseResult.length === 1 ) {
72-
const typedError = promiseResult[ 0 ] as {
73-
reason?: CoreDataError;
74-
};
75-
if ( typedError.reason?.message ) {
76-
errorMessage = typedError.reason.message;
77-
errorMessages.add( typedError.reason.message );
78-
} else {
79-
errorMessage =
80-
notice.error.messages.getMessage( errorMessages );
81-
}
82-
// If we were trying to permanently delete multiple posts
83-
} else {
84-
const failedPromises = promiseResult.filter(
85-
( { status } ) => status === 'rejected'
86-
);
87-
for ( const failedPromise of failedPromises ) {
88-
const typedError = failedPromise as {
89-
reason?: CoreDataError;
90-
};
91-
if ( typedError.reason?.message ) {
92-
errorMessages.add( typedError.reason.message );
93-
}
94-
}
95+
const errorMessages = getErrorMessagesFromPromises( promiseResult );
96+
const errorMessage = notice.error.messages.getMessage( errorMessages );
9597

96-
errorMessage =
97-
notice.error.messages.getBatchMessage( errorMessages );
98-
}
9998
createErrorNotice( errorMessage, {
10099
type: notice.error.type ?? 'snackbar',
101100
id: notice.error.id,
@@ -129,7 +128,7 @@ export const editPostWithNotices = async < T extends Post >(
129128
);
130129
} )
131130
);
132-
const promiseResult = await Promise.allSettled(
131+
const allSettledResults = await Promise.allSettled(
133132
postsWithUpdates.map( ( post ) => {
134133
return saveEditedEntityRecord(
135134
'postType',
@@ -142,9 +141,9 @@ export const editPostWithNotices = async < T extends Post >(
142141
} )
143142
);
144143
// If all the promises were fulfilled with success.
145-
if ( promiseResult.every( ( { status } ) => status === 'fulfilled' ) ) {
144+
if ( allSettledResults.every( ( { status } ) => status === 'fulfilled' ) ) {
146145
let successMessage;
147-
if ( promiseResult.length === 1 ) {
146+
if ( allSettledResults.length === 1 ) {
148147
successMessage = notice.success.messages.getMessage(
149148
postsWithUpdates[ 0 ].originalPost
150149
);
@@ -161,38 +160,10 @@ export const editPostWithNotices = async < T extends Post >(
161160
postsWithUpdates.map( ( post ) => post.originalPost )
162161
);
163162
} else {
164-
const errorMessages = new Set< string >();
165-
// If there was at lease one failure.
166-
let errorMessage;
167-
// If we were trying to permanently delete a single post.
168-
if ( promiseResult.length === 1 ) {
169-
const typedError = promiseResult[ 0 ] as {
170-
reason?: CoreDataError;
171-
};
172-
if ( typedError.reason?.message ) {
173-
errorMessage = typedError.reason.message;
174-
errorMessages.add( typedError.reason.message );
175-
} else {
176-
errorMessage =
177-
notice.error.messages.getMessage( errorMessages );
178-
}
179-
// If we were trying to permanently delete multiple posts
180-
} else {
181-
const failedPromises = promiseResult.filter(
182-
( { status } ) => status === 'rejected'
183-
);
184-
for ( const failedPromise of failedPromises ) {
185-
const typedError = failedPromise as {
186-
reason?: CoreDataError;
187-
};
188-
if ( typedError.reason?.message ) {
189-
errorMessages.add( typedError.reason.message );
190-
}
191-
}
163+
const errorMessages = getErrorMessagesFromPromises( allSettledResults );
164+
const errorMessage =
165+
notice.error.messages.getBatchMessage( errorMessages );
192166

193-
errorMessage =
194-
notice.error.messages.getBatchMessage( errorMessages );
195-
}
196167
createErrorNotice( errorMessage, {
197168
type: notice.error.type ?? 'snackbar',
198169
id: notice.error.id,

0 commit comments

Comments
 (0)