Skip to content

Commit

Permalink
fix: make acceptable file types for attachments configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Sep 10, 2024
1 parent 607778a commit 59e3d0b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,10 @@ export const defaultJsonConfig = {
},
"birth_certificate": {
"dataType": "file",
"label": $localize`:Label for a child attribute:Birth certificate`
"label": $localize`:Label for a child attribute:Birth certificate`,
"additional": {
"acceptedFileTypes": ".pdf"
}
}
},
} as EntityConfig,
Expand Down
1 change: 1 addition & 0 deletions src/app/features/file/edit-file/edit-file.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<input
type="file"
[accept]="acceptedFileTypes"
style="display: none"
(change)="onFileSelected($event.target['files'][0])"
#fileUpload
Expand Down
14 changes: 14 additions & 0 deletions src/app/features/file/edit-file/edit-file.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FileService } from "../file.service";
import { EntitySchemaService } from "../../../core/entity/schema/entity-schema.service";
import { EntityMapperService } from "../../../core/entity/entity-mapper/entity-mapper.service";
import { NAVIGATOR_TOKEN } from "../../../utils/di-tokens";
import { FileFieldConfig } from "../file.datatype";

describe("EditFileComponent", () => {
let component: EditFileComponent;
Expand Down Expand Up @@ -57,6 +58,19 @@ describe("EditFileComponent", () => {
expect(component).toBeTruthy();
});

it("should use acceptedFileTypes from field config", () => {
setupComponent();
component.formFieldConfig = {
id: "testProp",
dataType: "file",
additional: { acceptedFileTypes: ".png" } as FileFieldConfig,
};

component.ngOnInit();

expect(component.acceptedFileTypes).toBe(".png");
});

it("should not upload a file that was selected but the process was canceled", () => {
setupComponent();
component.formControl.enable();
Expand Down
17 changes: 17 additions & 0 deletions src/app/features/file/edit-file/edit-file.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
import { ErrorHintComponent } from "../../../core/common-components/error-hint/error-hint.component";
import { NotAvailableOfflineError } from "../../../core/session/not-available-offline.error";
import { NAVIGATOR_TOKEN } from "../../../utils/di-tokens";
import { FileFieldConfig } from "../file.datatype";

/**
* This component should be used as a `editComponent` when a property should store files.
Expand Down Expand Up @@ -51,6 +52,18 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
private removeClicked = false;
initialValue: string;

/**
* config for the given form field / entity attribute, containing special settings for this component.
* (re-declared here for better typing)
*/
declare additional: FileFieldConfig;

/**
* The accepted file types for file selection dialog.
* If not defined, allows any file.
*/
acceptedFileTypes: string = "*";

constructor(
protected fileService: FileService,
private alertService: AlertService,
Expand All @@ -63,6 +76,10 @@ export class EditFileComponent extends EditComponent<string> implements OnInit {
override ngOnInit() {
super.ngOnInit();
this.initialValue = this.formControl.value;

this.acceptedFileTypes =
this.additional?.acceptedFileTypes ?? this.acceptedFileTypes;

this.formControl.statusChanges
.pipe(
distinctUntilChanged(),
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/file/edit-photo/edit-photo.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
type="file"
style="display: none"
(change)="onFileSelected($event.target['files'][0])"
accept="image/*"
[accept]="acceptedFileTypes"
#fileUpload
/>
</div>
3 changes: 2 additions & 1 deletion src/app/features/file/edit-photo/edit-photo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class EditPhotoComponent extends EditFileComponent implements OnInit {

override ngOnInit() {
super.ngOnInit();
this.compression = this.additional ?? this.compression;
this.compression = this.additional?.imageCompression ?? this.compression;
this.acceptedFileTypes = this.additional?.acceptedFileTypes ?? "image/*";
if (this.formControl.value) {
this.fileService
.loadFile(this.entity, this.formControlName)
Expand Down
16 changes: 16 additions & 0 deletions src/app/features/file/file.datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ export class FileDatatype extends StringDatatype {
);
}
}

/**
* (Optional) "additional" object to configure details of a "file" datatype / form field.
*/
export interface FileFieldConfig {
/**
* The accepted file types for file selection dialog.
* If not defined, allows any file.
*/
acceptedFileTypes?: string;

/**
* The maxSize to which the image will be automatically resized before upload.
*/
imageCompression?: number;
}

0 comments on commit 59e3d0b

Please sign in to comment.