Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Sep 13, 2024
1 parent 1016ef8 commit b34c830
Showing 1 changed file with 36 additions and 65 deletions.
101 changes: 36 additions & 65 deletions packages/fields/src/mutation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ import { store as coreStore } from '@wordpress/core-data';
import type { CoreDataError, Post } from '../types';
import { dispatch } from '@wordpress/data';

const getErrorMessagesFromPromises = < T >(
allSettledResults: PromiseSettledResult< T >[]
) => {
const errorMessages = new Set< string >();
// If there was at lease one failure.
if ( allSettledResults.length === 1 ) {
const typedError = allSettledResults[ 0 ] as {
reason?: CoreDataError;
};
if ( typedError.reason?.message ) {
errorMessages.add( typedError.reason.message );
}
} else {
const failedPromises = allSettledResults.filter(
( { status } ) => status === 'rejected'
);
for ( const failedPromise of failedPromises ) {
const typedError = failedPromise as {
reason?: CoreDataError;
};
if ( typedError.reason?.message ) {
errorMessages.add( typedError.reason.message );
}
}
}
return errorMessages;
};

export type NoticeSettings< T extends Post > = {
success: {
id?: string;
Expand Down Expand Up @@ -64,38 +92,9 @@ export const deletePostWithNotices = async < T extends Post >(
} );
callbacks.onActionPerformed?.( posts );
} else {
const errorMessages = new Set< string >();
// If there was at lease one failure.
let errorMessage;
// If we were trying to permanently delete a single post.
if ( promiseResult.length === 1 ) {
const typedError = promiseResult[ 0 ] as {
reason?: CoreDataError;
};
if ( typedError.reason?.message ) {
errorMessage = typedError.reason.message;
errorMessages.add( typedError.reason.message );
} else {
errorMessage =
notice.error.messages.getMessage( errorMessages );
}
// If we were trying to permanently delete multiple posts
} else {
const failedPromises = promiseResult.filter(
( { status } ) => status === 'rejected'
);
for ( const failedPromise of failedPromises ) {
const typedError = failedPromise as {
reason?: CoreDataError;
};
if ( typedError.reason?.message ) {
errorMessages.add( typedError.reason.message );
}
}
const errorMessages = getErrorMessagesFromPromises( promiseResult );
const errorMessage = notice.error.messages.getMessage( errorMessages );

errorMessage =
notice.error.messages.getBatchMessage( errorMessages );
}
createErrorNotice( errorMessage, {
type: notice.error.type ?? 'snackbar',
id: notice.error.id,
Expand Down Expand Up @@ -129,7 +128,7 @@ export const editPostWithNotices = async < T extends Post >(
);
} )
);
const promiseResult = await Promise.allSettled(
const allSettledResults = await Promise.allSettled(
postsWithUpdates.map( ( post ) => {
return saveEditedEntityRecord(
'postType',
Expand All @@ -142,9 +141,9 @@ export const editPostWithNotices = async < T extends Post >(
} )
);
// If all the promises were fulfilled with success.
if ( promiseResult.every( ( { status } ) => status === 'fulfilled' ) ) {
if ( allSettledResults.every( ( { status } ) => status === 'fulfilled' ) ) {
let successMessage;
if ( promiseResult.length === 1 ) {
if ( allSettledResults.length === 1 ) {
successMessage = notice.success.messages.getMessage(
postsWithUpdates[ 0 ].originalPost
);
Expand All @@ -161,38 +160,10 @@ export const editPostWithNotices = async < T extends Post >(
postsWithUpdates.map( ( post ) => post.originalPost )
);
} else {
const errorMessages = new Set< string >();
// If there was at lease one failure.
let errorMessage;
// If we were trying to permanently delete a single post.
if ( promiseResult.length === 1 ) {
const typedError = promiseResult[ 0 ] as {
reason?: CoreDataError;
};
if ( typedError.reason?.message ) {
errorMessage = typedError.reason.message;
errorMessages.add( typedError.reason.message );
} else {
errorMessage =
notice.error.messages.getMessage( errorMessages );
}
// If we were trying to permanently delete multiple posts
} else {
const failedPromises = promiseResult.filter(
( { status } ) => status === 'rejected'
);
for ( const failedPromise of failedPromises ) {
const typedError = failedPromise as {
reason?: CoreDataError;
};
if ( typedError.reason?.message ) {
errorMessages.add( typedError.reason.message );
}
}
const errorMessages = getErrorMessagesFromPromises( allSettledResults );
const errorMessage =
notice.error.messages.getBatchMessage( errorMessages );

errorMessage =
notice.error.messages.getBatchMessage( errorMessages );
}
createErrorNotice( errorMessage, {
type: notice.error.type ?? 'snackbar',
id: notice.error.id,
Expand Down

0 comments on commit b34c830

Please sign in to comment.