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

Remove documents from store on collection deletion #LMR-793 #349

Merged
merged 2 commits into from
May 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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;
}
10 changes: 10 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,19 @@ 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[] {
const ids: string[] = state.ids as string[];
const entities = state.entities;
return ids.map((id: string) => entities[id])
.filter(document => document.collectionId === collectionId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we map ids to entities here? What about filtering just entities?

  return Object.values(state.entities)
    .filter(document => document.collectionId === collectionId)
    .map(document => document.id);

In the future, we will hopefully be able to remove entities by a predicate (see this NgRx issue).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I was looking for something like that, thanks!

.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