From 450a96d2de4eaca89727e32a386b9fee46be29b1 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Sun, 7 Apr 2024 13:14:32 +0200 Subject: [PATCH] validate date input --- .../add-task/add-task.component.html | 259 ++++++++++-------- .../add-task/add-task.component.scss | 53 ++++ .../components/add-task/add-task.component.ts | 37 ++- 3 files changed, 234 insertions(+), 115 deletions(-) diff --git a/src/app/components/add-task/add-task.component.html b/src/app/components/add-task/add-task.component.html index b2d9504..f32c12e 100644 --- a/src/app/components/add-task/add-task.component.html +++ b/src/app/components/add-task/add-task.component.html @@ -7,125 +7,158 @@ (ngSubmit)="onSubmit(taskForm)" #taskForm="ngForm" onsubmit="return false" - class="content" > -
-
-

Title*

- -
- @if (!title.valid && title.touched) { -

You must enter a title.

- } +
+
+
+

Title*

+ +
+ @if (!title.valid && title.touched) { +

You must enter a title.

+ } +
+
+
+

Description*

+ +
+ @if (!description.valid && description.touched) { +

You must enter a description. (min. 10 letters)

+ } +
-
-

Description*

- -
- @if (!description.valid && description.touched) { -

You must enter a description. (min. 10 letters)

- } +
+
+
+

Due date*

+ +
+ @if (!date.valid && date.touched) { +

You must enter a date.

+ } @if (dateInPast && date.valid) { +

The date must be in the future

+ } +
+
+
+

Prio*

+
+ + + +
-
-
-
-

Due date*

- -
- @if (!date.valid && date.touched) { -

You must enter a date.

- } -
-
-
-

Prio*

-
- - - -
-
+
+ +
diff --git a/src/app/components/add-task/add-task.component.scss b/src/app/components/add-task/add-task.component.scss index f87eefe..ea449b2 100644 --- a/src/app/components/add-task/add-task.component.scss +++ b/src/app/components/add-task/add-task.component.scss @@ -111,3 +111,56 @@ p { .btn:hover { box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3); } + +/*------------- SUBMIT BUTTON -------------*/ + +.form-buttons { + display: flex; + justify-content: end; +} + +.btn-submit { + width: 183px; + height: 56px; + padding: 12px; + margin: 12px 12px; + border-radius: 10px; + border: 1px solid var(--black) !important; + background-color: var(--light-gray); + font-size: 23px; + font-weight: 400; + transition: 125ms ease-in-out; + &:hover:not(:disabled) { + box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3); + background-color: var(--light-blue) !important; + border: 1px solid var(--light-blue) !important; + cursor: pointer; + } + &:not(:disabled) { + background-color: var(--very-dark-blue) !important; + color: var(--white); + } + &:hover { + cursor: default; + } +} + +.btn-clear { + width: 110px; + height: 56px; + padding: 12px; + margin: 12px 12px; + border-radius: 10px; + color: var(--black); + background-color: var(--white); + border: 1px solid var(--black); + font-size: 23px; + font-weight: 400; + transition: 125ms ease-in-out; + cursor: pointer; + &:hover { + color: var(--light-blue); + border-color: var(--light-bluek); + box-shadow: 2px 2px 2px 0px rgba(0, 0, 0, 0.3); + } +} diff --git a/src/app/components/add-task/add-task.component.ts b/src/app/components/add-task/add-task.component.ts index 0bb9d09..586c70a 100644 --- a/src/app/components/add-task/add-task.component.ts +++ b/src/app/components/add-task/add-task.component.ts @@ -1,6 +1,6 @@ import { CommonModule } from '@angular/common'; -import { Component } from '@angular/core'; -import { FormsModule, NgForm } from '@angular/forms'; +import { Component, ViewChild } from '@angular/core'; +import { FormsModule, NgForm, NgModel } from '@angular/forms'; @Component({ selector: 'app-add-task', @@ -10,7 +10,11 @@ import { FormsModule, NgForm } from '@angular/forms'; styleUrl: './add-task.component.scss', }) export class AddTaskComponent { + @ViewChild('title', { static: true }) titleField!: NgModel; + @ViewChild('description', { static: true }) descriptionField!: NgModel; + currentDate: string = new Date().toISOString().split('T')[0]; + dateInPast: boolean = false; taskData = { title: '', @@ -26,6 +30,17 @@ export class AddTaskComponent { } } + checkDateInput() { + const currentDateForm = this.taskData.date.replaceAll('-', ''); + const currentDate = new Date() + .toISOString() + .split('T')[0] + .replaceAll('-', ''); + currentDateForm < currentDate + ? (this.dateInPast = true) + : (this.dateInPast = false); + } + tooglePriority(prio: string) { this.taskData.priority !== prio ? (this.taskData.priority = prio) @@ -37,9 +52,27 @@ export class AddTaskComponent { localStorage.setItem('taskData', JSON.stringify(this.taskData)); } + removeTaskData() { + localStorage.removeItem('taskData'); + this.untouchedFormFields(); + this.clearFormData(); + } + + untouchedFormFields() { + this.titleField.control.markAsUntouched(); + this.descriptionField.control.markAsUntouched(); + } + + clearFormData() { + this.taskData.title = ''; + this.taskData.description = ''; + this.taskData.date = this.currentDate; + } + onSubmit(ngForm: NgForm) { if (ngForm.submitted && ngForm.form.valid) { console.log('Send completed'); + this.removeTaskData(); } } }