Skip to content

Commit c4511f9

Browse files
committed
pagination and validation message
1 parent 8026020 commit c4511f9

7 files changed

+139
-12
lines changed

package-lock.json

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@angular/router": "^14.1.0",
2121
"bootstrap": "^5.2.0",
2222
"ckeditor4-angular": "^3.1.1",
23+
"ngx-pagination": "^6.0.2",
2324
"ngx-toastr": "^15.0.0",
2425
"rxjs": "~7.5.0",
2526
"tslib": "^2.3.0",

src/app/app.module.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { CKEditorModule } from 'ckeditor4-angular';
1515
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
1616
import { SafeHtmlPipe } from './safe-html.pipe';
1717
import { ToastrModule } from 'ngx-toastr';
18+
import { SortByDatePipe } from './sort-by-date.pipe';
19+
import { NgxPaginationModule } from 'ngx-pagination';
20+
1821

1922

2023
@NgModule({
@@ -27,6 +30,7 @@ import { ToastrModule } from 'ngx-toastr';
2730
DiaryComponent,
2831
IdeaComponent,
2932
SafeHtmlPipe,
33+
SortByDatePipe,
3034

3135
],
3236
imports: [
@@ -36,7 +40,8 @@ import { ToastrModule } from 'ngx-toastr';
3640
HttpClientModule,
3741
CKEditorModule,
3842
ToastrModule.forRoot(),
39-
BrowserAnimationsModule
43+
BrowserAnimationsModule,
44+
NgxPaginationModule
4045
],
4146
providers: [],
4247
bootstrap: [AppComponent]

src/app/sort-by-date.pipe.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { SortByDatePipe } from './sort-by-date.pipe';
2+
3+
describe('SortByDatePipe', () => {
4+
it('create an instance', () => {
5+
const pipe = new SortByDatePipe();
6+
expect(pipe).toBeTruthy();
7+
});
8+
});

src/app/sort-by-date.pipe.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'sortByDate'
5+
})
6+
export class SortByDatePipe implements PipeTransform {
7+
8+
transform(value) {
9+
const sortedValues = value.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
10+
11+
return sortedValues;
12+
}
13+
14+
}

src/app/story/story.component.html

+61-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@
77

88
<div class="row">
99
<span class="fs-3 ">Your Stories
10-
<button class="btn btn-secondary float-end" (click)="listing=false;createStory=true;">Add
10+
<button class="btn btn-secondary float-end" (click)="openCreateForm()">Add
1111
New</button></span>
1212
</div>
13-
<div class="mx-5 py-3" *ngFor="let story of stories">
13+
<div class="mx-5 py-3" *ngFor="
14+
let story of stories | sortByDate
15+
| paginate
16+
: {
17+
itemsPerPage: tableSize,
18+
currentPage: page,
19+
totalItems: count
20+
};
21+
let i = index
22+
23+
">
1424

1525
<div class="row pb-2">
1626
<div>
1727
<span class="float-start fs-3"> {{story.title}}</span>
18-
<span class="float-end text-primary"> Date :- {{story.created_at}}</span>
28+
<span class="float-end text-primary"> Date :- {{story.created_at | date:'mediumDate'}}</span>
1929

2030
</div>
2131
</div>
@@ -26,10 +36,56 @@
2636
<button class="btn btn-outline-secondary me-2" (click)="listing=!listing">Edit</button>
2737
<button class="btn btn-outline-danger">Delete</button>
2838
</div>
29-
39+
<div class="d-flex justify-content-center">
40+
<pagination-controls previousLabel="Prev" nextLabel="Next" (pageChange)="onTableDataChange($event)">
41+
</pagination-controls>
42+
</div>
3043
</div>
3144
<div *ngIf="createStory" class="mx-5 my-4">
32-
<p class="fs-3 text-center">Add Your Story</p>
45+
<p class="fs-3 text-center">
46+
<button class="btn btn-warning float-start" (click)="listing=true;createStory=false;">Go Back</button>
47+
Add Your Story
48+
</p>
49+
<form [formGroup]="storyForm" (ngSubmit)="addStory()">
50+
<div class="form-floating mb-3">
51+
<input type="text" class="form-control" id="floatingInputValue" formControlName="title">
52+
<label for="floatingInputValue">Title</label>
53+
<p *ngIf="(
54+
storyForm.controls['title'].dirty ||
55+
storyForm.controls['title'].touched ||
56+
storyForm.controls['title'].pristine) &&
57+
storyForm.controls['title'].errors
58+
" class="alert alert-danger">This Field is Required</p>
59+
</div>
60+
<ckeditor #editor formControlName="content">
61+
</ckeditor>
62+
<p *ngIf="(
63+
storyForm.controls['content'].dirty ||
64+
storyForm.controls['content'].touched ||
65+
storyForm.controls['content'].pristine) &&
66+
storyForm.controls['content'].errors
67+
" class="alert alert-danger">This Field is Required</p>
68+
<div class="form-floating my-3">
69+
<input type="date" class="form-control" id="floatingInputValue" formControlName="created_at">
70+
<label for="floatingInputValue">Completion Date</label>
71+
<p *ngIf="(
72+
storyForm.controls['created_at'].dirty ||
73+
storyForm.controls['created_at'].touched ||
74+
storyForm.controls['created_at'].pristine) &&
75+
storyForm.controls['created_at'].errors
76+
" class="alert alert-danger">This Field is Required</p>
77+
</div>
78+
79+
<button type="submit" class="btn btn-primary" [disabled]="!storyForm.valid">Submit
80+
</button>
81+
82+
</form>
83+
</div>
84+
<div *ngIf="editStory" class="mx-5 my-4">
85+
<p class="fs-3 text-center">
86+
<button class="btn btn-warning float-start" (click)="listing=true;editStory=false;">Go Back</button>
87+
Edit Your Story
88+
</p>
3389
<form [formGroup]="storyForm">
3490
<div class="form-floating mb-3">
3591
<input type="text" class="form-control" id="floatingInputValue" formControlName="title">

src/app/story/story.component.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,30 @@ export class StoryComponent implements OnInit {
1414

1515

1616
listing: boolean = true;
17+
editStory = false;
1718
createStory: boolean = false;
1819
storyForm!: FormGroup;
1920
stories!: Story[];
2021
successMessage: String = "ss";
2122
errorMessage!: String;
2223

24+
25+
//pagination configuration
26+
page: number = 1;
27+
count: number = 0;
28+
tableSize: number = 4;
29+
tableSizes: any = [3, 6, 9, 12];
30+
2331
constructor(private formbuilder: FormBuilder, private storyService: StoryService, private toastr: ToastrService) { }
2432

2533
ngOnInit(): void {
2634

2735
this.getStories();
28-
this.storyForm = this.formbuilder.group({
29-
title: ['', [Validators.required, Validators.minLength(255)]],
30-
content: ['', Validators.required],
31-
created_at: ['', [Validators.required]],
32-
});
36+
3337

3438
}
3539
addStory() {
40+
this.createStory = false; this.listing = true
3641
var story = this.storyForm.getRawValue();
3742
this.storyService.addStories(story).subscribe({
3843
next: (stories) => {
@@ -44,7 +49,15 @@ export class StoryComponent implements OnInit {
4449
});
4550

4651
}
47-
52+
onTableDataChange(event: any) {
53+
this.page = event;
54+
this.getStories();
55+
}
56+
onTableSizeChange(event: any): void {
57+
this.tableSize = event.target.value;
58+
this.page = 1;
59+
this.getStories();
60+
}
4861
getStories() {
4962

5063
this.storyService.getStories().subscribe({
@@ -55,4 +68,13 @@ export class StoryComponent implements OnInit {
5568
error: errror => console.log(errror),
5669
});
5770
}
71+
72+
openCreateForm() {
73+
this.storyForm = this.formbuilder.group({
74+
title: ['', [Validators.required, Validators.maxLength(255)]],
75+
content: ['', Validators.required],
76+
created_at: [Date.now().toLocaleString, [Validators.required]],
77+
});
78+
this.listing = false; this.createStory = true;
79+
}
5880
}

0 commit comments

Comments
 (0)