Skip to content

Commit 93442de

Browse files
authored
Fix/add to collection (#178)
* fix(add-to-collection-api): added deactivate guard and clearing store
1 parent f7b3543 commit 93442de

File tree

15 files changed

+60
-25
lines changed

15 files changed

+60
-25
lines changed

src/app/features/collections/collections.routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Routes } from '@angular/router';
44

55
import { AddToCollectionState } from '@osf/features/collections/store/add-to-collection';
66
import { CollectionsState } from '@osf/features/collections/store/collections';
7+
import { ConfirmLeavingGuard } from '@shared/guards';
78
import { ContributorsState, ProjectsState } from '@shared/stores';
89

910
import { ModeratorsState } from '../moderation/store/moderation';
@@ -40,6 +41,7 @@ export const collectionsRoutes: Routes = [
4041
(mod) => mod.AddToCollectionComponent
4142
),
4243
providers: [provideStates([ProjectsState, CollectionsState, AddToCollectionState, ContributorsState])],
44+
canDeactivate: [ConfirmLeavingGuard],
4345
},
4446
{
4547
path: ':id/moderation',

src/app/features/collections/components/add-to-collection/add-to-collection.component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Button } from 'primeng/button';
66
import { DialogService } from 'primeng/dynamicdialog';
77
import { Stepper } from 'primeng/stepper';
88

9+
import { Observable } from 'rxjs';
10+
911
import {
1012
ChangeDetectionStrategy,
1113
Component,
@@ -27,6 +29,7 @@ import {
2729
} from '@osf/features/collections/store/add-to-collection/add-to-collection.actions';
2830
import { CollectionsSelectors, GetCollectionProvider } from '@osf/features/collections/store/collections';
2931
import { LoadingSpinnerComponent } from '@shared/components';
32+
import { CanDeactivateComponent } from '@shared/models';
3033
import { ProjectsSelectors } from '@shared/stores/projects/projects.selectors';
3134

3235
import {
@@ -55,7 +58,7 @@ import {
5558
providers: [DialogService],
5659
changeDetection: ChangeDetectionStrategy.OnPush,
5760
})
58-
export class AddToCollectionComponent {
61+
export class AddToCollectionComponent implements CanDeactivateComponent {
5962
private readonly router = inject(Router);
6063
private readonly route = inject(ActivatedRoute);
6164
private readonly destroyRef = inject(DestroyRef);
@@ -70,6 +73,7 @@ export class AddToCollectionComponent {
7073
protected selectedProject = select(ProjectsSelectors.getSelectedProject);
7174
protected currentUser = select(UserSelectors.getCurrentUser);
7275
protected providerId = signal<string>('');
76+
protected isSubmitted = signal<boolean>(false);
7377
protected projectMetadataSaved = signal<boolean>(false);
7478
protected projectContributorsSaved = signal<boolean>(false);
7579
protected collectionMetadataSaved = signal<boolean>(false);
@@ -123,6 +127,7 @@ export class AddToCollectionComponent {
123127
collectionMetadata: this.collectionMetadataForm.value || {},
124128
userId: this.currentUser()?.id || '',
125129
};
130+
this.isSubmitted.set(true);
126131

127132
const dialogRef = this.dialogService.open(AddToCollectionConfirmationDialogComponent, {
128133
width: '500px',
@@ -136,6 +141,7 @@ export class AddToCollectionComponent {
136141

137142
dialogRef.onClose.subscribe((result) => {
138143
if (result) {
144+
this.isSubmitted.set(false);
139145
this.router.navigate(['/my-projects', this.selectedProject()?.id, 'overview']);
140146
}
141147
});
@@ -165,4 +171,8 @@ export class AddToCollectionComponent {
165171
$event.preventDefault();
166172
return false;
167173
}
174+
175+
canDeactivate(): Observable<boolean> | boolean {
176+
return this.isSubmitted();
177+
}
168178
}

src/app/features/collections/components/add-to-collection/project-metadata-step/project-metadata-step.component.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { License } from '@shared/models';
4040
import { Project } from '@shared/models/projects';
4141
import { InterpolatePipe } from '@shared/pipes';
4242
import { ToastService } from '@shared/services';
43-
import { GetAllContributors, UpdateProjectMetadata } from '@shared/stores';
43+
import { ClearProjects, GetAllContributors, UpdateProjectMetadata } from '@shared/stores';
4444
import { ProjectsSelectors } from '@shared/stores/projects/projects.selectors';
4545

4646
@Component({
@@ -95,6 +95,7 @@ export class ProjectMetadataStepComponent {
9595
updateCollectionSubmissionMetadata: UpdateProjectMetadata,
9696
getAllContributors: GetAllContributors,
9797
getCollectionLicenses: GetCollectionLicenses,
98+
clearProjects: ClearProjects,
9899
});
99100

100101
protected readonly projectMetadataForm: FormGroup<ProjectMetadataForm> = this.formService.createForm();
@@ -231,5 +232,11 @@ export class ProjectMetadataStepComponent {
231232
this.initialProjectMetadataFormValues.set(JSON.stringify(formValue));
232233
}
233234
});
235+
236+
effect(() => {
237+
this.destroyRef.onDestroy(() => {
238+
this.actions.clearProjects();
239+
});
240+
});
234241
}
235242
}

src/app/features/collections/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { AddToCollectionComponent } from './add-to-collection/add-to-collection.component';
12
export { CollectionsFilterChipsComponent } from './collections-filter-chips/collections-filter-chips.component';
23
export { CollectionsFiltersComponent } from './collections-filters/collections-filters.component';
34
export { CollectionsHelpDialogComponent } from './collections-help-dialog/collections-help-dialog.component';

src/app/features/preprints/guards/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/features/preprints/models/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './can-deactivate.interface';
21
export * from './preprint.models';
32
export * from './preprint-json-api.models';
43
export * from './preprint-licenses-json-api.models';

src/app/features/preprints/pages/submit-preprint-stepper/submit-preprint-stepper.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ import {
3131
} from '@osf/features/preprints/components';
3232
import { submitPreprintSteps } from '@osf/features/preprints/constants';
3333
import { PreprintSteps } from '@osf/features/preprints/enums';
34-
import { CanDeactivateComponent } from '@osf/features/preprints/models';
3534
import { GetPreprintProviderById, PreprintProvidersSelectors } from '@osf/features/preprints/store/preprint-providers';
3635
import {
3736
DeletePreprint,
3837
PreprintStepperSelectors,
3938
ResetState,
4039
SetSelectedPreprintProviderId,
4140
} from '@osf/features/preprints/store/preprint-stepper';
42-
import { StepOption } from '@osf/shared/models';
41+
import { CanDeactivateComponent, StepOption } from '@osf/shared/models';
4342
import { StepperComponent } from '@shared/components';
4443
import { BrandService } from '@shared/services';
4544
import { BrowserTabHelper, HeaderStyleHelper, IS_WEB } from '@shared/utils';

src/app/features/preprints/pages/update-preprint-stepper/update-preprint-stepper.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
} from '@osf/features/preprints/components';
3131
import { updatePreprintSteps } from '@osf/features/preprints/constants';
3232
import { PreprintSteps } from '@osf/features/preprints/enums';
33-
import { CanDeactivateComponent } from '@osf/features/preprints/models';
3433
import { GetPreprintProviderById, PreprintProvidersSelectors } from '@osf/features/preprints/store/preprint-providers';
3534
import {
3635
FetchPreprintById,
@@ -39,7 +38,7 @@ import {
3938
SetSelectedPreprintProviderId,
4039
} from '@osf/features/preprints/store/preprint-stepper';
4140
import { StepperComponent } from '@shared/components';
42-
import { StepOption } from '@shared/models';
41+
import { CanDeactivateComponent, StepOption } from '@shared/models';
4342
import { BrandService } from '@shared/services';
4443
import { BrowserTabHelper, HeaderStyleHelper, IS_WEB } from '@shared/utils';
4544

src/app/features/preprints/preprints.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { provideStates } from '@ngxs/store';
22

33
import { Routes } from '@angular/router';
44

5-
import { ConfirmLeavingGuard } from '@osf/features/preprints/guards';
65
import { PreprintsComponent } from '@osf/features/preprints/preprints.component';
76
import { PreprintProvidersState } from '@osf/features/preprints/store/preprint-providers';
87
import { PreprintStepperState } from '@osf/features/preprints/store/preprint-stepper';
98
import { PreprintsDiscoverState } from '@osf/features/preprints/store/preprints-discover';
109
import { PreprintsResourcesFiltersState } from '@osf/features/preprints/store/preprints-resources-filters';
1110
import { PreprintsResourcesFiltersOptionsState } from '@osf/features/preprints/store/preprints-resources-filters-options';
11+
import { ConfirmLeavingGuard } from '@shared/guards';
1212
import { ResourceType } from '@shared/enums';
1313
import { ContributorsState, SubjectsState } from '@shared/stores';
1414

src/app/features/preprints/guards/confirm-leaving.guard.ts renamed to src/app/shared/guards/confirm-leaving.guard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { Subject } from 'rxjs';
33
import { inject } from '@angular/core';
44
import { CanDeactivateFn } from '@angular/router';
55

6-
import { SubmitPreprintStepperComponent } from '@osf/features/preprints/pages/submit-preprint-stepper/submit-preprint-stepper.component';
6+
import { CanDeactivateComponent } from '@shared/models';
77
import { CustomConfirmationService } from '@shared/services';
88

9-
export const ConfirmLeavingGuard: CanDeactivateFn<SubmitPreprintStepperComponent> = (component) => {
9+
export const ConfirmLeavingGuard: CanDeactivateFn<CanDeactivateComponent> = (component) => {
1010
const confirmationService = inject(CustomConfirmationService);
1111
const confirmationResultSubject = new Subject<boolean>();
1212
const confirmationResultObservable = confirmationResultSubject.asObservable();

0 commit comments

Comments
 (0)