Skip to content

Commit 23b9cae

Browse files
authored
Merge pull request #1008 from adlius/fix-only-public-project-collection
Only show public projects for collection submission workflow.
2 parents e751ae8 + bed1c91 commit 23b9cae

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/app/features/collections/components/add-to-collection/select-project-step/select-project-step.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ <h3>{{ 'collections.addToCollection.selectProject' | translate }}</h3>
2626
<div class="mt-4 w-full lg:w-6">
2727
<osf-project-selector
2828
[excludeProjectIds]="excludedProjectIds()"
29+
[publicOnly]="true"
2930
[(selectedProject)]="currentSelectedProject"
3031
(projectChange)="handleProjectChange($event)"
3132
(projectsLoaded)="handleProjectsLoaded($event)"

src/app/shared/components/project-selector/project-selector.component.spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
import { provideStore } from '@ngxs/store';
1+
import { provideStore, Store } from '@ngxs/store';
22

33
import { MockProvider } from 'ng-mocks';
44

55
import { ComponentFixture, TestBed } from '@angular/core/testing';
66

77
import { UserState } from '@core/store/user';
8+
import { ProjectModel } from '@osf/shared/models/projects/projects.model';
89
import { ToastService } from '@osf/shared/services/toast.service';
910
import { ProjectsState } from '@shared/stores/projects';
1011

1112
import { provideOSFCore } from '@testing/osf.testing.provider';
1213

1314
import { ProjectSelectorComponent } from './project-selector.component';
1415

16+
const makeProject = (id: string, isPublic: boolean): ProjectModel =>
17+
({ id, title: `Project ${id}`, isPublic }) as ProjectModel;
18+
1519
describe('ProjectSelectorComponent', () => {
1620
let component: ProjectSelectorComponent;
1721
let fixture: ComponentFixture<ProjectSelectorComponent>;
22+
let store: Store;
1823

1924
beforeEach(() => {
2025
TestBed.configureTestingModule({
2126
imports: [ProjectSelectorComponent],
2227
providers: [provideOSFCore(), MockProvider(ToastService), provideStore([ProjectsState, UserState])],
2328
});
2429

30+
store = TestBed.inject(Store);
2531
fixture = TestBed.createComponent(ProjectSelectorComponent);
2632
component = fixture.componentInstance;
2733
fixture.detectChanges();
@@ -51,4 +57,36 @@ describe('ProjectSelectorComponent', () => {
5157

5258
expect(mockEvent.originalEvent.preventDefault).toHaveBeenCalled();
5359
});
60+
61+
describe('publicOnly filtering', () => {
62+
const publicProject = makeProject('1', true);
63+
const privateProject = makeProject('2', false);
64+
65+
const setProjects = (projects: ProjectModel[]) => {
66+
store.reset({
67+
...store.snapshot(),
68+
projects: { projects: { data: projects, isLoading: false, error: null } },
69+
});
70+
};
71+
72+
it('should show all projects when publicOnly is false', () => {
73+
fixture.componentRef.setInput('publicOnly', false);
74+
setProjects([publicProject, privateProject]);
75+
fixture.detectChanges();
76+
77+
const ids = component.projectsOptions().map((o) => o.value.id);
78+
expect(ids).toContain('1');
79+
expect(ids).toContain('2');
80+
});
81+
82+
it('should only show public projects when publicOnly is true', () => {
83+
fixture.componentRef.setInput('publicOnly', true);
84+
setProjects([publicProject, privateProject]);
85+
fixture.detectChanges();
86+
87+
const ids = component.projectsOptions().map((o) => o.value.id);
88+
expect(ids).toContain('1');
89+
expect(ids).not.toContain('2');
90+
});
91+
});
5492
});

src/app/shared/components/project-selector/project-selector.component.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class ProjectSelectorComponent {
4444
placeholder = input<string>('common.buttons.select');
4545
showClear = input<boolean>(true);
4646
excludeProjectIds = input<string[]>([]);
47+
publicOnly = input<boolean>(false);
4748
selectedProject = model<ProjectModel | null>(null);
4849

4950
projectChange = output<ProjectModel | null>();
@@ -105,7 +106,9 @@ export class ProjectSelectorComponent {
105106
}
106107

107108
const excludeSet = new Set(excludeIds);
108-
const availableProjects = projects.filter((project) => !excludeSet.has(project.id));
109+
const availableProjects = projects.filter(
110+
(project) => !excludeSet.has(project.id) && (!this.publicOnly() || project.isPublic)
111+
);
109112

110113
const options = availableProjects.map((project) => ({
111114
label: project.title,
@@ -132,6 +135,10 @@ export class ProjectSelectorComponent {
132135
'filter[current_user_permissions]': 'admin',
133136
};
134137

138+
if (this.publicOnly()) {
139+
params['filter[public]'] = 'true';
140+
}
141+
135142
if (filterTitle && filterTitle.trim()) {
136143
params['filter[title]'] = filterTitle;
137144
}

0 commit comments

Comments
 (0)