Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPT: Display notice when permanent deletion succeeds or fails #6536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions client/state/notices/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { translate } from 'i18n-calypso';
import {
NOTICE_CREATE,
NOTICE_REMOVE,
POST_DELETE_FAILURE,
POST_DELETE_SUCCESS,
POST_SAVE_SUCCESS,
ROUTE_SET
} from 'state/action-types';
Expand Down Expand Up @@ -89,6 +91,48 @@ export const items = createReducer( {}, {
text
}
};
},
[ POST_DELETE_SUCCESS ]: ( state, action ) => {
// A request to permanently delete a post has completed successfully
const count = get( state, [ action.type, 'count' ], 0 ) + 1;

let text;
if ( 1 === count ) {
text = translate( 'Post successfully deleted' );
} else {
text = translate(
'%d post successfully deleted',
'%d posts successfully deleted',
{ count, args: [ count ] }
);
}

return {
...state,
[ action.type ]: {
showDismiss: true,
isPersistent: false,
displayOnNextPage: false,
status: 'is-success',
noticeId: action.type,
count,
text
}
};
},
[ POST_DELETE_FAILURE ]: ( state, action ) => {
// A request to permanently delete a post has failed
return {
...state,
[ action.type ]: {
showDismiss: true,
isPersistent: false,
displayOnNextPage: false,
status: 'is-error',
noticeId: action.type,
text: translate( 'An error occurred while deleting the post' )
}
};
}
} );

Expand Down
72 changes: 72 additions & 0 deletions client/state/notices/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import deepFreeze from 'deep-freeze';
import {
NOTICE_CREATE,
NOTICE_REMOVE,
POST_DELETE_FAILURE,
POST_DELETE_SUCCESS,
POST_SAVE_SUCCESS,
ROUTE_SET
} from 'state/action-types';
Expand Down Expand Up @@ -184,5 +186,75 @@ describe( 'reducer', () => {
} );
} );
} );

context( 'POST_DELETE_SUCCESS', () => {
it( 'should return state with single delete success', () => {
const original = deepFreeze( {} );
const state = items( original, {
type: POST_DELETE_SUCCESS
} );

expect( state ).to.eql( {
POST_DELETE_SUCCESS: {
showDismiss: true,
isPersistent: false,
displayOnNextPage: false,
status: 'is-success',
noticeId: 'POST_DELETE_SUCCESS',
count: 1,
text: 'Post successfully deleted'
}
} );
} );

it( 'should return state with multiple delete success', () => {
const original = deepFreeze( {
POST_DELETE_SUCCESS: {
showDismiss: true,
isPersistent: false,
displayOnNextPage: false,
status: 'is-success',
noticeId: 'POST_DELETE_SUCCESS',
count: 1,
text: 'Post successfully deleted'
}
} );
const state = items( original, {
type: POST_DELETE_SUCCESS
} );

expect( state ).to.eql( {
POST_DELETE_SUCCESS: {
showDismiss: true,
isPersistent: false,
displayOnNextPage: false,
status: 'is-success',
noticeId: 'POST_DELETE_SUCCESS',
count: 2,
text: '2 posts successfully deleted'
}
} );
} );
} );

context( 'POST_DELETE_FAILURE', () => {
it( 'should return state with delete failure notice', () => {
const original = deepFreeze( {} );
const state = items( original, {
type: POST_DELETE_FAILURE
} );

expect( state ).to.eql( {
POST_DELETE_FAILURE: {
showDismiss: true,
isPersistent: false,
displayOnNextPage: false,
status: 'is-error',
noticeId: 'POST_DELETE_FAILURE',
text: 'An error occurred while deleting the post'
}
} );
} );
} );
} );
} );