Skip to content

Commit b2b366d

Browse files
committed
refactor(files): updated interfaces and refactored code
1 parent f2dd1e4 commit b2b366d

4 files changed

Lines changed: 54 additions & 100 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SupportedFeature } from '@osf/shared/enums/addon-supported-features.enum';
2+
import { FileMenuType } from '@osf/shared/enums/file-menu-type.enum';
3+
4+
export function mapMenuActions(supportedFeatures: SupportedFeature[]): Record<FileMenuType, boolean> {
5+
return {
6+
[FileMenuType.Download]: supportedFeatures.includes(SupportedFeature.DownloadAsZip),
7+
[FileMenuType.Rename]: supportedFeatures.includes(SupportedFeature.AddUpdateFiles),
8+
[FileMenuType.Delete]: supportedFeatures.includes(SupportedFeature.DeleteFiles),
9+
[FileMenuType.Move]:
10+
supportedFeatures.includes(SupportedFeature.CopyInto) &&
11+
supportedFeatures.includes(SupportedFeature.DeleteFiles) &&
12+
supportedFeatures.includes(SupportedFeature.AddUpdateFiles),
13+
[FileMenuType.Embed]: true,
14+
[FileMenuType.Share]: true,
15+
[FileMenuType.Copy]: true,
16+
};
17+
}

src/app/features/files/pages/files/files.component.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@
1717
[(ngModel)]="currentRootFolder"
1818
(onChange)="handleRootFolderChange($event.value)"
1919
variant="filled"
20-
>
21-
<ng-template #selectedItem let-selectedOption>
22-
<p class="provider-name">{{ selectedOption.label }}</p>
23-
</ng-template>
24-
<ng-template #item let-option>
25-
<p class="provider-name">{{ option.label }}</p>
26-
</ng-template>
27-
</p-select>
20+
/>
2821
}
2922
</div>
3023
@if (filesSelection.length) {

src/app/features/files/pages/files/files.component.scss

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,3 @@
55
flex: 1;
66
overflow: hidden;
77
}
8-
9-
.blue-text {
10-
color: var(--pr-blue-1);
11-
}
12-
13-
.filename {
14-
overflow-wrap: anywhere;
15-
}
16-
17-
.upload-dialog {
18-
width: mix.rem(128px);
19-
}
20-
21-
.provider-name {
22-
text-transform: capitalize;
23-
}

src/app/features/files/pages/files/files.component.ts

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,6 @@ import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
3939
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
4040
import { ActivatedRoute, Router } from '@angular/router';
4141

42-
import { ENVIRONMENT } from '@core/provider/environment.provider';
43-
import {
44-
CreateFolder,
45-
DeleteEntry,
46-
GetConfiguredStorageAddons,
47-
GetFiles,
48-
GetRootFolders,
49-
GetStorageSupportedFeatures,
50-
RenameEntry,
51-
ResetFilesState,
52-
SetCurrentProvider,
53-
SetFilesCurrentFolder,
54-
SetMoveDialogCurrentFolder,
55-
SetSearch,
56-
SetSort,
57-
} from '@osf/features/files/store';
5842
import { FileUploadDialogComponent } from '@osf/shared/components/file-upload-dialog/file-upload-dialog.component';
5943
import { FilesTreeComponent } from '@osf/shared/components/files-tree/files-tree.component';
6044
import { FormSelectComponent } from '@osf/shared/components/form-select/form-select.component';
@@ -68,7 +52,8 @@ import { ALL_SORT_OPTIONS } from '@osf/shared/constants/sort-options.const';
6852
import { SupportedFeature } from '@osf/shared/enums/addon-supported-features.enum';
6953
import { FileMenuType } from '@osf/shared/enums/file-menu-type.enum';
7054
import { ResourceType } from '@osf/shared/enums/resource-type.enum';
71-
import { UserPermissions } from '@osf/shared/enums/user-permissions.enum';
55+
import { FilePageLinkModel } from '@osf/shared/models/files/file-page-link.model';
56+
import { RenamedFileLinkModel } from '@osf/shared/models/files/renamed-file-link.model';
7257
import { CustomConfirmationService } from '@osf/shared/services/custom-confirmation.service';
7358
import { CustomDialogService } from '@osf/shared/services/custom-dialog.service';
7459
import { FilesService } from '@osf/shared/services/files.service';
@@ -82,14 +67,28 @@ import { FileFolderModel } from '@shared/models/files/file-folder.model';
8267
import { FileLabelModel } from '@shared/models/files/file-label.model';
8368
import { DataciteService } from '@shared/services/datacite/datacite.service';
8469

85-
import {
86-
CreateFolderDialogComponent,
87-
FileBrowserInfoComponent,
88-
FilesSelectionActionsComponent,
89-
MoveFileDialogComponent,
90-
} from '../../components';
70+
import { CreateFolderDialogComponent } from '../../components/create-folder-dialog/create-folder-dialog.component';
71+
import { FileBrowserInfoComponent } from '../../components/file-browser-info/file-browser-info.component';
72+
import { FilesSelectionActionsComponent } from '../../components/files-selection-actions/files-selection-actions.component';
73+
import { MoveFileDialogComponent } from '../../components/move-file-dialog/move-file-dialog.component';
9174
import { FileProvider } from '../../constants';
92-
import { FilesSelectors } from '../../store';
75+
import { mapMenuActions } from '../../mappers/file-menu-actions.mapper';
76+
import {
77+
CreateFolder,
78+
DeleteEntry,
79+
FilesSelectors,
80+
GetConfiguredStorageAddons,
81+
GetFiles,
82+
GetRootFolders,
83+
GetStorageSupportedFeatures,
84+
RenameEntry,
85+
ResetFilesState,
86+
SetCurrentProvider,
87+
SetFilesCurrentFolder,
88+
SetMoveDialogCurrentFolder,
89+
SetSearch,
90+
SetSort,
91+
} from '../../store';
9392

9493
@Component({
9594
selector: 'osf-files',
@@ -107,7 +106,6 @@ import { FilesSelectors } from '../../store';
107106
SubHeaderComponent,
108107
FileUploadDialogComponent,
109108
ViewOnlyLinkMessageComponent,
110-
GoogleFilePickerComponent,
111109
FilesSelectionActionsComponent,
112110
TranslatePipe,
113111
],
@@ -128,15 +126,10 @@ export class FilesComponent {
128126
private readonly translateService = inject(TranslateService);
129127
private readonly router = inject(Router);
130128
private readonly dataciteService = inject(DataciteService);
131-
private readonly environment = inject(ENVIRONMENT);
132129
private readonly customConfirmationService = inject(CustomConfirmationService);
133130
private readonly toastService = inject(ToastService);
134131
private readonly viewOnlyService = inject(ViewOnlyLinkHelperService);
135-
private readonly platformId = inject(PLATFORM_ID);
136-
private readonly isBrowser = isPlatformBrowser(this.platformId);
137-
138-
private readonly webUrl = this.environment.webUrl;
139-
private readonly apiDomainUrl = this.environment.apiDomainUrl;
132+
private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
140133

141134
private readonly actions = createDispatchMap({
142135
createFolder: CreateFolder,
@@ -167,6 +160,8 @@ export class FilesComponent {
167160
readonly configuredStorageAddons = select(FilesSelectors.getConfiguredStorageAddons);
168161
readonly isConfiguredStorageAddonsLoading = select(FilesSelectors.isConfiguredStorageAddonsLoading);
169162
readonly supportedFeatures = select(FilesSelectors.getStorageSupportedFeatures);
163+
readonly hasWriteAccess = select(CurrentResourceSelectors.hasResourceWriteAccess);
164+
readonly hasAdminAccess = select(CurrentResourceSelectors.hasResourceAdminAccess);
170165

171166
readonly isGoogleDrive = signal<boolean>(false);
172167
readonly accountId = signal<string>('');
@@ -193,17 +188,12 @@ export class FilesComponent {
193188
allowRevisions = false;
194189
filesSelection: FileModel[] = [];
195190

196-
private readonly urlMap = new Map<ResourceType, string>([
197-
[ResourceType.Project, 'nodes'],
198-
[ResourceType.Registration, 'registrations'],
199-
]);
200-
201191
readonly allowedMenuActions = computed(() => {
202192
const provider = this.provider();
203193
const supportedFeatures = this.supportedFeatures()[provider] || [];
204194
const hasViewOnly = this.hasViewOnly();
205195
const isRegistration = this.resourceType() === ResourceType.Registration;
206-
const menuMap = this.mapMenuActions(supportedFeatures);
196+
const menuMap = mapMenuActions(supportedFeatures);
207197

208198
const result: Record<FileMenuType, boolean> = { ...menuMap };
209199

@@ -235,16 +225,7 @@ export class FilesComponent {
235225
);
236226

237227
readonly hasViewOnly = computed(() => this.viewOnlyService.hasViewOnlyParam(this.router));
238-
239-
readonly canEdit = computed(() => {
240-
const details = this.resourceDetails();
241-
const hasAdminOrWrite = details.currentUserPermissions?.some(
242-
(permission) => permission === UserPermissions.Admin || permission === UserPermissions.Write
243-
);
244-
245-
return hasAdminOrWrite;
246-
});
247-
228+
readonly canEdit = computed(() => this.hasWriteAccess() || this.hasAdminAccess());
248229
readonly isRegistration = computed(() => this.resourceType() === ResourceType.Registration);
249230

250231
canUploadFiles = computed(
@@ -260,9 +241,8 @@ export class FilesComponent {
260241
() => this.isButtonDisabled() || (this.googleFilePickerComponent()?.isGFPDisabled() ?? false)
261242
);
262243

263-
private route = inject(ActivatedRoute);
264244
readonly providerName = toSignal(
265-
this.route?.params?.pipe(map((params) => params['fileProvider'])) ?? of('osfstorage')
245+
this.activeRoute?.params?.pipe(map((params) => params['fileProvider'])) ?? of('osfstorage')
266246
);
267247

268248
constructor() {
@@ -274,14 +254,11 @@ export class FilesComponent {
274254

275255
effect(() => {
276256
const resourceId = this.resourceId();
257+
if (!resourceId) return;
277258

278-
const resourcePath = this.urlMap.get(this.resourceType()!);
279-
const folderLink = `${this.apiDomainUrl}/v2/${resourcePath}/${resourceId}/files/`;
280-
const iriLink = `${this.webUrl}/${resourceId}`;
281-
282-
this.actions.getResourceDetails(resourceId, this.resourceType()!);
283-
this.actions.getRootFolders(folderLink);
284-
this.actions.getConfiguredStorageAddons(iriLink);
259+
this.actions.getResourceDetails(resourceId, this.resourceType());
260+
this.actions.getRootFolders(resourceId, this.resourceType());
261+
this.actions.getConfiguredStorageAddons(resourceId);
285262
});
286263

287264
effect(() => {
@@ -358,7 +335,7 @@ export class FilesComponent {
358335
});
359336
}
360337

361-
onLoadFiles(event: { link: string; page: number }) {
338+
onLoadFiles(event: FilePageLinkModel) {
362339
this.actions.getFiles(event.link, event.page);
363340
}
364341

@@ -427,9 +404,7 @@ export class FilesComponent {
427404
this.customConfirmationService.confirmDelete({
428405
headerKey: conflictFiles.length > 1 ? 'files.dialogs.replaceFile.multiple' : 'files.dialogs.replaceFile.single',
429406
messageKey: 'files.dialogs.replaceFile.message',
430-
messageParams: {
431-
name: conflictFiles.map((c) => c.file.name).join(', '),
432-
},
407+
messageParams: { name: conflictFiles.map((c) => c.file.name).join(', ') },
433408
acceptLabelKey: 'common.buttons.replace',
434409
onConfirm: () => {
435410
const replaceRequests$ = conflictFiles.map(({ file, link }) =>
@@ -626,7 +601,7 @@ export class FilesComponent {
626601
});
627602
}
628603

629-
renameEntry(event: { newName: string; link: string }) {
604+
renameEntry(event: RenamedFileLinkModel) {
630605
const { newName, link } = event;
631606
this.actions.renameEntry(link, newName).subscribe(() => {
632607
this.toastService.showSuccess('files.dialogs.renameFile.success');
@@ -663,21 +638,6 @@ export class FilesComponent {
663638
}
664639
}
665640

666-
private mapMenuActions(supportedFeatures: SupportedFeature[]): Record<FileMenuType, boolean> {
667-
return {
668-
[FileMenuType.Download]: supportedFeatures.includes(SupportedFeature.DownloadAsZip),
669-
[FileMenuType.Rename]: supportedFeatures.includes(SupportedFeature.AddUpdateFiles),
670-
[FileMenuType.Delete]: supportedFeatures.includes(SupportedFeature.DeleteFiles),
671-
[FileMenuType.Move]:
672-
supportedFeatures.includes(SupportedFeature.CopyInto) &&
673-
supportedFeatures.includes(SupportedFeature.DeleteFiles) &&
674-
supportedFeatures.includes(SupportedFeature.AddUpdateFiles),
675-
[FileMenuType.Embed]: true,
676-
[FileMenuType.Share]: true,
677-
[FileMenuType.Copy]: true,
678-
};
679-
}
680-
681641
openGoogleFilePicker(): void {
682642
this.googleFilePickerComponent()?.createPicker();
683643
this.updateFilesList();

0 commit comments

Comments
 (0)