Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
feat(#22): show error message and prevent creation on faulty file names
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanie S committed Jun 12, 2020
1 parent ec2bc5b commit cb1247b
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 77 deletions.
2 changes: 0 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CreateNewLabelComponent } from './components/_labels/createNewLabel/cre
import { LabelTreeComponent } from './components/_labels/labelTree/labelTree.component';
import { DocumentTreeComponent } from './components/_documents/documentTree/documentTree.component';
import { UpsertNoteComponent } from './components/_notes/upsertNote/upsertNote.component';
import { CheckForNameSafetyDirective } from './directives/checkForNameSafety.directive';
import { TagDialogComponent } from './components/_snapshots/tagDialog/tagDialog.component';
import { LastModifiedComponent } from './components/lastModified/lastModified.component';
import { CreateNewItemDialogComponent } from './components/createNewItemDialog/createNewItemDialog.component';
Expand Down Expand Up @@ -111,7 +110,6 @@ const matModules = [
TagDialogComponent,
LastModifiedComponent,
UpsertNoteComponent,
CheckForNameSafetyDirective,
DocumentTreeComponent,
LabelTreeComponent,
CreateNewLabelComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1 mat-dialog-title>{{ t('title') }}</h1>
<p>{{ t('message', { date: data.lastSnapshotDate }) }}</p>
<mat-form-field>
<mat-label>{{ t('input') }}</mat-label>
<input matInput wyCheckForNameSafety="tag" [(ngModel)]="data.tagname" />
<input matInput [(ngModel)]="data.tagname" />
<mat-error *ngIf="!isTagNameValid">BOOOOH!</mat-error>
</mat-form-field>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@

<span *transloco="let t; read: typeOfDialog">
<h1 mat-dialog-title>{{ t('title') }}</h1>
<div mat-dialog-content>
<p class="message">{{ t('message') }}</p>
<mat-form-field>
<mat-label>{{ t('input') }}</mat-label>
<input (keyup.enter)="submit()" matInput [(ngModel)]="data.filename" />
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="cancel()">{{ t('cancel') }}</button>
<button mat-raised-button color="primary" (click)="submit()">{{ t('submit') }}</button>
</div>
<form [formGroup]="form" (ngSubmit)="submit(form.value)">
<div mat-dialog-content>
<p class="message">{{ t('message') }}</p>
<mat-form-field>
<mat-label>{{ t('input') }}</mat-label>
<input matInput (keyup.enter)="submit(form.value)" required formControlName="name" />
</mat-form-field>
<mat-error *ngIf="form.get('name').errors?.forbiddenName"
>{{ 'createDirOrFile.forbiddenName' | transloco }}
</mat-error>
<mat-error *ngIf="form.get('name').errors?.required">{{ 'createDirOrFile.required' | transloco }} </mat-error>
</div>
<div mat-dialog-actions>
<button mat-button type="button" (click)="cancel()">{{ t('cancel') }}</button>
<button mat-raised-button color="primary" type="submit" [disabled]="form.invalid">{{ t('submit') }}</button>
</div>
</form>
</span>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020 s-blu
//
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand All @@ -12,3 +12,8 @@ input {
.message {
padding: 1em 0;
}

mat-error {
margin-top: -1.8em;
font-size: 10px;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
// Copyright (c) 2020 s-blu
//
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { Component, OnInit, Inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { FormControl, FormBuilder } from '@angular/forms';
import { NameSafetyValidator } from 'src/app/directives/nameSafetyValidator';
import { slideInDownAnimation, slideInDownOnEnterAnimation } from 'angular-animations';

@Component({
selector: 'wy-createNewFileDialog',
templateUrl: './createNewItemDialog.component.html',
styleUrls: ['./createNewItemDialog.component.scss'],
animations: [slideInDownOnEnterAnimation()],
})
export class CreateNewItemDialogComponent implements OnInit {
typeOfDialog = '';
form;

constructor(
public dialogRef: MatDialogRef<CreateNewItemDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data,
private formBuilder: FormBuilder
) {}

ngOnInit() {
switch (this.data.typeOfDialog) {
Expand All @@ -30,15 +41,17 @@ export class CreateNewItemDialogComponent implements OnInit {
this.typeOfDialog = 'createUnkownDialog';
break;
}
}

constructor(public dialogRef: MatDialogRef<CreateNewItemDialogComponent>, @Inject(MAT_DIALOG_DATA) public data) {}
this.form = this.formBuilder.group({
name: new FormControl('', [NameSafetyValidator()]),
});
}

cancel(): void {
this.dialogRef.close();
this.dialogRef.close(null);
}

submit() {
this.dialogRef.close(this.data.filename);
submit(values) {
this.dialogRef.close(values.name);
}
}
17 changes: 0 additions & 17 deletions src/app/directives/checkForNameSafety.directive.spec.ts

This file was deleted.

37 changes: 0 additions & 37 deletions src/app/directives/checkForNameSafety.directive.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/app/directives/nameSafetyValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2020 s-blu
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import { AbstractControl, ValidatorFn } from '@angular/forms';

export function NameSafetyValidator(typeToCheck = 'explorer') {
const nameValidator = (control: AbstractControl): { [key: string]: boolean } | null => {
const forbidden = regex.test(control.value);
return forbidden ? { forbiddenName: true } : null;
};

const forbiddenCharatersRe = {
explorer: /([/\\<>\*\?:\'"])+/,
tag: /([\s\.~\^:\?\*\[@\\])+/,
};
const regex = forbiddenCharatersRe[typeToCheck];
return regex ? nameValidator : null;
}
4 changes: 3 additions & 1 deletion src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
"lastSnapshotted": "Letzte Sicherung vom"
},
"createDirOrFile": {
"alreadyInUse": "Dieser Name ist bereits vergeben. Bitte wähle einen anderen."
"alreadyInUse": "Dieser Name ist bereits vergeben. Bitte wähle einen anderen.",
"forbiddenName": "Folgende Zeichen dürfen nicht verwendet werden: < > : / \\ * ? \" | ",
"required": "Bitte gib einen Namen ein."
},
"createFileDialog": {
"title": "Neues Dokument erstellen",
Expand Down
4 changes: 3 additions & 1 deletion src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
"lastSnapshotted": "Last snapshotted on"
},
"createDirOrFile": {
"alreadyInUse": "This name is already in use. Please choose a different one."
"alreadyInUse": "This name is already in use. Please choose a different one.",
"forbiddenName": "The following characters cannot be used: < > : / \\ * ? \" | ",
"required": "Please input a name"
},
"createFileDialog": {
"title": "Create new document",
Expand Down

0 comments on commit cb1247b

Please sign in to comment.