Skip to content

Commit d307c5e

Browse files
committed
date format
1 parent 55d1cbe commit d307c5e

File tree

9 files changed

+80
-10
lines changed

9 files changed

+80
-10
lines changed

src/angular/src/app/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { CustomAdapter } from './shared/custom-adapter';
12
import { BrowserModule } from '@angular/platform-browser';
23
import { NgModule } from '@angular/core';
34
import { HttpClientModule } from '@angular/common/http';
45

56
import { AppComponent } from './app.component';
67
import { AppStoreModule } from './store/app-store.module';
78
import { ServicesModule } from './services/services.module';
8-
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
9+
import { NgbModule, NgbDateAdapter } from '@ng-bootstrap/ng-bootstrap';
910
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
1011
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
1112
import { CompModule } from './comp/comp.module';
@@ -33,7 +34,11 @@ import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
3334
ToastrModule.forRoot(),
3435
AppRoutingModule
3536
],
36-
providers: [],
37+
providers: [
38+
{
39+
provide: NgbDateAdapter, useClass: CustomAdapter
40+
}
41+
],
3742
bootstrap: [AppComponent]
3843
})
3944
export class AppModule {}

src/angular/src/app/dto/todo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ export interface Todo {
1111
description: string;
1212
/** active : booléen gérant le statut */
1313
active: boolean;
14+
15+
dateTodo: Date;
1416
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Injectable } from '@angular/core';
2+
import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
3+
/**
4+
* This Service handles how the date is represented in scripts i.e. ngModel.
5+
*/
6+
@Injectable()
7+
export class CustomAdapter extends NgbDateAdapter<string> {
8+
9+
readonly DELIMITER = '-';
10+
11+
fromModel(value: string): NgbDateStruct {
12+
let result: NgbDateStruct = null;
13+
if (value) {
14+
const date = value.split(this.DELIMITER);
15+
result = {
16+
day : parseInt(date[0], 10),
17+
month : parseInt(date[1], 10),
18+
year : parseInt(date[2], 10)
19+
};
20+
}
21+
return result;
22+
}
23+
24+
toModel(date: NgbDateStruct): string {
25+
let result: string = null;
26+
if (date) {
27+
result = date.day + this.DELIMITER + date.month + this.DELIMITER + date.year;
28+
}
29+
return result;
30+
}
31+
}

src/angular/src/app/todo/todo-edit/todo-edit.component.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
<label for="todoDescription">Todo description</label>
1111
<input type="text" class="form-control" id="todoDescription" formControlName="description" placeholder="Enter description">
1212
</div>
13+
<div class="form-group">
14+
<label for="">Date due</label>
15+
<div class="input-group">
16+
17+
<input class="form-control" placeholder="yyyy-mm-dd"
18+
name="dp" formControlName="dateTodo" ngbDatepicker #d="ngbDatepicker">
19+
<div class="input-group-append">
20+
<button class="btn btn-outline-secondary calendar" (click)="d.toggle()" type="button"></button>
21+
</div>
22+
</div>
23+
</div>
1324
<div class="form-group">
1425
<div class="form-check abc-checkbox abc-checkbox-primary">
1526
<input class="form-check-input" id="todoActive" formControlName="active" type="checkbox">
@@ -19,7 +30,7 @@
1930
</div>
2031
</div>
2132
<div class="alert alert-warning" *ngIf="todoForm.invalid">
22-
{{todoForm.errors.validateForm.msg}}
33+
{{todoForm.errors?.validateForm.msg}}
2334
</div>
2435

2536
<button type="submit" class="btn btn-primary" [disabled]="todoForm.invalid || todoForm.pristine">

src/angular/src/app/todo/todo-edit/todo-edit.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class TodoEditComponent implements OnInit, OnDestroy {
4343
*/
4444
ngOnInit() {
4545
this.todo$ = combineLatest(
46-
this.route.paramMap.pipe(map(paramMap => paramMap.get('id'))),
47-
this.todosService.entityMap$
46+
[this.route.paramMap.pipe(map(paramMap => paramMap.get('id'))),
47+
this.todosService.entityMap$]
4848
).pipe(
4949
map(([id, entityMap]) => {
5050
const todo = entityMap[id];
@@ -61,6 +61,7 @@ export class TodoEditComponent implements OnInit, OnDestroy {
6161
title: new FormControl(),
6262
description: new FormControl(),
6363
active: new FormControl(),
64+
dateTodo: new FormControl(),
6465
id: new FormControl()
6566
}, (formGroup: FormGroup) => {
6667
return this.validateForm(formGroup);

src/angular/src/app/todo/todo.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class TodoComponent implements OnInit {
1818
}
1919

2020
ngOnInit() {
21-
this.todo = { title: '', description: '', active: true };
21+
this.todo = { title: '', description: '', active: true, dateTodo: new Date() };
2222
this.loading$.subscribe(isLoading => {
2323
console.log('loading : ' + isLoading);
2424
});

src/angular/src/app/todo/todo.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { CounterComponent } from '../counter/counter.component';
1010
import { CompModule } from '../comp/comp.module';
1111
import { TodoEditComponent } from './todo-edit/todo-edit.component';
1212
import { RouterModule } from '@angular/router';
13+
import { NgbDatepicker, NgbDatepickerModule } from '@ng-bootstrap/ng-bootstrap';
1314

1415
@NgModule({
1516
imports: [
@@ -19,7 +20,8 @@ import { RouterModule } from '@angular/router';
1920
ReactiveFormsModule,
2021
CompModule,
2122
RouterModule,
22-
FontAwesomeModule
23+
FontAwesomeModule,
24+
NgbDatepickerModule
2325
],
2426
declarations: [
2527
TodosComponent,

src/main/java/ru/coco/todo/TodoApplication.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.coco.todo;
22

3+
import java.util.Date;
4+
35
import org.springframework.beans.factory.InitializingBean;
46
import org.springframework.beans.factory.annotation.Autowired;
57
import org.springframework.boot.SpringApplication;
@@ -22,8 +24,8 @@ public static void main(String[] args) {
2224
@Bean
2325
InitializingBean sendDatabase() {
2426
return () -> {
25-
todoRepository.save(new Todo("John", "the life of Rambo", true, new TodoCategory("Recettes")));
26-
todoRepository.save(new Todo("Billy", "the Kid", true, new TodoCategory("Western")));
27+
todoRepository.save(new Todo("John", "the life of Rambo", true, new TodoCategory("Recettes"), new Date()));
28+
todoRepository.save(new Todo("Billy", "the Kid", true, new TodoCategory("Western"), new Date()));
2729
};
2830
}
2931
}

src/main/java/ru/coco/todo/model/Todo.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package ru.coco.todo.model;
22

3+
import java.util.Date;
4+
35
import javax.persistence.*;
46

7+
import com.fasterxml.jackson.annotation.JsonFormat;
8+
59
/**
610
* Created by a10282 on 11/04/2018.
711
*/
@@ -18,14 +22,18 @@ public class Todo {
1822

1923
private TodoCategory todoCategory;
2024

25+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
26+
private Date dateTodo;
27+
2128
public Todo() {
2229
}
2330

24-
public Todo(String title, String description, Boolean active, TodoCategory todoCategory) {
31+
public Todo(String title, String description, Boolean active, TodoCategory todoCategory, Date dateTodo) {
2532
this.title = title;
2633
this.description = description;
2734
this.active = active;
2835
this.todoCategory = todoCategory;
36+
this.dateTodo = dateTodo;
2937
}
3038

3139
public Boolean getActive() {
@@ -70,4 +78,12 @@ public TodoCategory getTodoCategory() {
7078
public void setTodoCategory(TodoCategory todoCategory) {
7179
this.todoCategory = todoCategory;
7280
}
81+
82+
public Date getDateTodo() {
83+
return dateTodo;
84+
}
85+
86+
public void setDateTodo(Date dateTodo) {
87+
this.dateTodo = dateTodo;
88+
}
7389
}

0 commit comments

Comments
 (0)