Skip to content

Commit

Permalink
Remove documents from store on collection deletion #LMR-793
Browse files Browse the repository at this point in the history
  • Loading branch information
kubedo8 authored and livthomas committed May 5, 2018
1 parent 48c9929 commit 5f2e184
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/app/core/store/collections/collections.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {AppState} from "../app.state";
import {HttpErrorResponse} from "@angular/common/http";
import {RouterAction} from "../router/router.action";
import {selectOrganizationByWorkspace} from "../organizations/organizations.state";
import {DocumentsAction} from '../documents/documents.action';

@Injectable()
export class CollectionsEffects {
Expand Down Expand Up @@ -112,18 +113,19 @@ export class CollectionsEffects {
ofType<CollectionsAction.CreateFailure>(CollectionsActionType.CREATE_FAILURE),
tap(action => console.error(action.payload.error)),
withLatestFrom(this.store$.select(selectOrganizationByWorkspace)),
map( ([action, organization]) => {
map(([action, organization]) => {
if (action.payload.error instanceof HttpErrorResponse && action.payload.error.status == 402) {
const title = this.i18n({ id: 'serviceLimits.trial', value: 'Trial Service' });
const title = this.i18n({id: 'serviceLimits.trial', value: 'Trial Service'});
const message = this.i18n({
id: 'collection.create.serviceLimits',
value: 'You are currently on the Trial plan which allows you to have only limited number of files. Do you want to upgrade to Business now?' });
value: 'You are currently on the Trial plan which allows you to have only limited number of files. Do you want to upgrade to Business now?'
});
return new NotificationsAction.Confirm({
title,
message,
action: new RouterAction.Go({
path: ['/organization', organization.code, 'detail'],
extras: { fragment: 'orderService' }
extras: {fragment: 'orderService'}
})
});
}
Expand Down Expand Up @@ -151,16 +153,17 @@ export class CollectionsEffects {
withLatestFrom(this.store$.select(selectOrganizationByWorkspace)),
map(([action, organization]) => {
if (action.payload.error instanceof HttpErrorResponse && action.payload.error.status == 402) {
const title = this.i18n({ id: 'serviceLimits.trial', value: 'Trial Service' });
const title = this.i18n({id: 'serviceLimits.trial', value: 'Trial Service'});
const message = this.i18n({
id: 'collection.create.serviceLimits',
value: 'You are currently on the Trial plan which allows you to have only limited number of files. Do you want to upgrade to Business now?' });
value: 'You are currently on the Trial plan which allows you to have only limited number of files. Do you want to upgrade to Business now?'
});
return new NotificationsAction.Confirm({
title,
message,
action: new RouterAction.Go({
path: ['/organization', organization.code, 'detail'],
extras: { fragment: 'orderService' }
extras: {fragment: 'orderService'}
})
});
}
Expand Down Expand Up @@ -197,7 +200,9 @@ export class CollectionsEffects {
public delete$: Observable<Action> = this.actions$.pipe(
ofType<CollectionsAction.Delete>(CollectionsActionType.DELETE),
mergeMap(action => this.collectionService.removeCollection(action.payload.collectionId).pipe(
map(collectionId => new CollectionsAction.DeleteSuccess({collectionId})),
flatMap(collectionId => [new CollectionsAction.DeleteSuccess({collectionId}),
new DocumentsAction.ClearByCollection({collectionId})
]),
catchError((error) => Observable.of(new CollectionsAction.DeleteFailure({error: error})))
))
);
Expand Down
12 changes: 10 additions & 2 deletions src/app/core/store/documents/documents.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export enum DocumentsActionType {
DELETE_SUCCESS = '[Documents] Delete :: Success',
DELETE_FAILURE = '[Documents] Delete :: Failure',

CLEAR = '[Documents] Clear'
CLEAR = '[Documents] Clear',
CLEAR_BY_COLLECTION = '[Documents] Clear by collection'

}

Expand Down Expand Up @@ -161,10 +162,17 @@ export namespace DocumentsAction {
}
}

export class ClearByCollection implements Action {
public readonly type = DocumentsActionType.CLEAR_BY_COLLECTION;

public constructor(public payload: { collectionId: string }) {
}
}

export type All =
Get | GetSuccess | GetFailure |
Create | CreateSuccess | CreateFailure |
Update | UpdateData | PatchData | UpdateSuccess | UpdateFailure |
Delete | DeleteSuccess | DeleteFailure | DeleteConfirm |
Clear;
Clear | ClearByCollection;
}
8 changes: 8 additions & 0 deletions src/app/core/store/documents/documents.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ export function documentsReducer(state: DocumentsState = initialDocumentsState,
return documentsAdapter.updateOne({id: action.payload.document.id, changes: action.payload.document}, state);
case DocumentsActionType.DELETE_SUCCESS:
return documentsAdapter.removeOne(action.payload.documentId, state);
case DocumentsActionType.CLEAR_BY_COLLECTION:
return documentsAdapter.removeMany(findCollectionDocumentIds(state, action.payload.collectionId), state);
case DocumentsActionType.CLEAR:
return initialDocumentsState;
default:
return state;
}
}

function findCollectionDocumentIds(state: DocumentsState, collectionId: string): string[] {
return Object.values(state.entities)
.filter(document => document.collectionId === collectionId)
.map(document => document.id);
}
2 changes: 1 addition & 1 deletion src/app/core/store/documents/documents.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface DocumentsState extends EntityState<DocumentModel> {
queries: QueryModel[];
}

export const documentsAdapter = createEntityAdapter<DocumentModel>();
export const documentsAdapter = createEntityAdapter<DocumentModel>({selectId: document => document.id});

export const initialDocumentsState: DocumentsState = documentsAdapter.getInitialState({
queries: []
Expand Down

0 comments on commit 5f2e184

Please sign in to comment.