Skip to content

Commit

Permalink
state managment
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitPalAP committed Jun 18, 2021
1 parent 3b0c809 commit 63461ed
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 88 deletions.
9 changes: 8 additions & 1 deletion server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
"courses": [
{
"id": 1,
"title": "Angular 9 Fundamentals",
"title": "Learn the fundamentals of ",
"description": "Learn the fundamentals of Angular 9",
"percentComplete": 26,
"favorite": true
},
{
"id": 4,
"title": "Worship will sentence",
"description": "Learn the fundamentals of Angular 9",
"percentComplete": 69,
"favorite": true
}
]
}
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { CoursesComponent } from './courses/courses.component';
import { FormsModule } from '@angular/forms';
import { CoursesService } from './shared/services/courses.service';
import { LessonsService } from './shared/services/lessons.service';
import { CoursesListComponent } from './courses/courses-list/courses-list.component';
import { CoursesDetailsComponent } from './courses/courses-details/courses-details.component';

@NgModule({
imports: [
Expand All @@ -21,7 +23,7 @@ import { LessonsService } from './shared/services/lessons.service';
MaterialModule,
HttpClientModule,
],
declarations: [AppComponent, HomeComponent, CoursesComponent],
declarations: [AppComponent, HomeComponent, CoursesComponent, CoursesListComponent, CoursesDetailsComponent],
providers: [CoursesService, LessonsService],
bootstrap: [AppComponent],
})
Expand Down
37 changes: 37 additions & 0 deletions src/app/courses/courses-details/courses-details.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<mat-card>
<mat-card-header>
<mat-card-title><span *ngIf="course.id; else noSelection">{{course.title}}</span>
<ng-template #noSelection>Select Course</ng-template>
Select Course
</mat-card-title>
</mat-card-header>
<form #form="ngForm" (submit)="saved.emit(course)">
<mat-card-content>
<mat-form-field>
<input matInput placeholder="title"
type="text" name="title"required
[(ngModel)]="course.title">
</mat-form-field>
<mat-form-field>
<input matInput placeholder="Description"
type="text" name="title"
[(ngModel)]="course.description">
</mat-form-field>
<section>
<h4>{{course.percentComplete}}% Complete</h4>
<mat-slider class="full-width"
min="0" max="100"
name="percentComplete"
thumbLabel
[(ngModel)]="course.percentComplete"></mat-slider>
</section>
<mat-checkbox name="favorite" [(ngModel)]="course.favorite" ></mat-checkbox> //Na
</mat-card-content>
<mat-card-actions>
<button [disabled]="form.invalid" type="submit" mat-button color="primary">Submit</button>
<button type="button" mat-button (click)="cancelled.emit()">CANCEL</button>
</mat-card-actions>
</form>
<pre>{{form.value | json}}</pre>
<pre>{{form.valid | json}}</pre>
</mat-card>
Empty file.
25 changes: 25 additions & 0 deletions src/app/courses/courses-details/courses-details.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { CoursesDetailsComponent } from './courses-details.component';

describe('CoursesDetailsComponent', () => {
let component: CoursesDetailsComponent;
let fixture: ComponentFixture<CoursesDetailsComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ CoursesDetailsComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(CoursesDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/courses/courses-details/courses-details.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'app-courses-details',
templateUrl: './courses-details.component.html',
styleUrls: ['./courses-details.component.scss'],
})
export class CoursesDetailsComponent implements OnInit {
@Input() course;
@Output() saved = new EventEmitter();
@Output() cancelled = new EventEmitter();
constructor() {}

ngOnInit(): void {}
}
19 changes: 19 additions & 0 deletions src/app/courses/courses-list/courses-list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<mat-card>
<mat-card-header>
<mat-card-title>
Course List
</mat-card-title>
</mat-card-header>
<mat-card-content>
<mat-list>
<mat-list-item *ngFor="let course of courses" (click)="selected.emit(course)">
<h3 matLine>{{course.title}}</h3>
<p matLine>{{course.description}}</p>
<button mat-icon-button color="warn" (click)="deleted.emit(course.id);$event.stopImmediatePropagation()">
<mat-icon>delete</mat-icon>
</button>
</mat-list-item>
</mat-list>

</mat-card-content>
</mat-card>
Empty file.
25 changes: 25 additions & 0 deletions src/app/courses/courses-list/courses-list.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { CoursesListComponent } from './courses-list.component';

describe('CoursesListComponent', () => {
let component: CoursesListComponent;
let fixture: ComponentFixture<CoursesListComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ CoursesListComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(CoursesListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
16 changes: 16 additions & 0 deletions src/app/courses/courses-list/courses-list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'app-courses-list',
templateUrl: './courses-list.component.html',
styleUrls: ['./courses-list.component.scss'],
})
export class CoursesListComponent implements OnInit {
@Input() courses;
@Output() selected = new EventEmitter();
@Output() deleted = new EventEmitter();

constructor() {}

ngOnInit(): void {}
}
69 changes: 10 additions & 59 deletions src/app/courses/courses.component.html
Original file line number Diff line number Diff line change
@@ -1,66 +1,17 @@
<div class="courses-containers">
<div class="courses-list">
<mat-card>
<mat-card-header>
<mat-card-title>
Course List
</mat-card-title>
</mat-card-header>
<mat-card-content>
<mat-list>
<mat-list-item *ngFor="let course of courses" (click)="onClick(course)">
<h3 matLine>{{course.title}}</h3>
<p matLine>{{course.description}}</p>

<button mat-icon-button color="warn" (click)="deleteCourse(course.id); $event.stopImmediatePropagation()">
<mat-icon>delete</mat-icon>
</button>
</mat-list-item>
</mat-list>

</mat-card-content>
</mat-card>
<app-courses-list [courses]="courses"
(selected)="onClick($event)"
(deleted)="deleteCourse($event)"
></app-courses-list>
</div>

<div class="course-details">
<mat-card>
<mat-card-header>
<mat-card-title>
<span *ngIf="tempData; else noSelection">{{tempData.title}}</span>
<ng-template #noSelection>Select Course</ng-template>
Select Course
</mat-card-title>
</mat-card-header>
<form #form="ngForm" (submit)="saveCourse(tempData)">
<mat-card-content>
<mat-form-field>
<input matInput placeholder="title"
type="text" name="title"required
[(ngModel)]="tempData.title">
</mat-form-field>
<mat-form-field>
<input matInput placeholder="Description"
type="text" name="title"
[(ngModel)]="tempData.description">
</mat-form-field>
<section>
<h4>{{tempData.percentComplete}}% Complete</h4>
<mat-slider class="full-width"
min="0" max="100"
name="percentComplete"
thumbLabel
[(ngModel)]="tempData.percentComplete"></mat-slider>
</section>
<mat-checkbox name="favorite" [(ngModel)]="tempData.favorite" ></mat-checkbox> //Na
</mat-card-content>
<mat-card-actions>
<!-- COURSE ACTIONS -->
<button [disabled]="form.invalid" type="submit" mat-button color="primary">Submit</button>
<button type="button" mat-button (click)="cancel()">CANCEL</button>
</mat-card-actions>
</form>
<pre>{{form.value | json}}</pre>
<pre>{{form.valid | json}}</pre>
</mat-card>
<app-courses-details
[course]="tempData"
(saved)="saveCourse($event)"
(cancelled)="cancel()"
></app-courses-details>

</div>
</div>
31 changes: 25 additions & 6 deletions src/app/courses/courses.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export class CoursesComponent implements OnInit {
ngOnInit(): void {
//It is like a construtor and it gets called initially when all hte binding are satisfied
this.resetSelectedCourse();
this.courseService
.all() //we do All instead of courses is becasue js dosent have concept of Private
.subscribe((courses) => (this.courses = courses));
this.loadCourses();
// this.courseService
// .all() //we do All instead of courses is becasue js dosent have concept of Private
// .subscribe((courses) => (this.courses = courses));
//We are doing this to call the EmptyObject first because
//during compiletTime we are binding an itself empty object instead of null Object
}
Expand All @@ -32,21 +33,39 @@ export class CoursesComponent implements OnInit {
};
this.tempData = emptyCourse;
}

onClick(course) {
//Select COurse
console.log(course);
this.tempData = course;
}

loadCourses() {
this.courseService.all().subscribe((courses) => (this.courses = courses));
}

refreshCourses() {
this.resetSelectedCourse();
this.loadCourses();
}

saveCourse(course) {
if (course.id) {
//If the ID exists update
this.courseService.update(course);
this.courseService
.update(course)
.subscribe((result) => this.refreshCourses());
} else {
//if not create
this.courseService.create(course);
this.courseService
.create(course)
.subscribe((res) => this.refreshCourses());
}
}
deleteCourse(courseId) {
this.courseService.deleteCourse(courseId);
this.courseService
.deleteCourse(courseId)
.subscribe((result) => this.refreshCourses());
}
cancel() {
this.resetSelectedCourse();
Expand Down
35 changes: 14 additions & 21 deletions src/app/shared/services/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,35 @@ const BASE_URL = 'http://localhost:3000/';
})
export class CoursesService {
private model = 'courses';
courses = [
{
id: 1,
title: 'Angular 9 Fundamentals',
description: 'Learn the fundamentals of Angular 9',
percentComplete: 26,
favorite: true,
},
{
id: 2,
title: 'JAvaScript The really Hard parts',
description: 'Worship will sentence',
percentComplete: 56,
favorite: true,
},
];

constructor(private http: HttpClient) {}

all() {
return this.http.get(`${BASE_URL}${this.model}`);
return this.http.get(this.getUrl());
}

find(courseId) {
console.log(courseId);
return this.http.get(this.getUrlById(courseId));
}

create(course) {
console.log('CREATE COURSE', course);
return this.http.post(this.getUrl(), course);
}

update(course) {
console.log('CREATE COURSE', course);
return this.http.put(this.getUrlById(course.id), course);
console.log('UPDATED COURSE', course);
}

deleteCourse(course) {
console.log('CREATE COURSE', course);
return this.http.delete(this.getUrlById(course));
}

private getUrl() {
return `${BASE_URL}${this.model}`;
}

private getUrlById(id) {
return `${this.getUrl()}/${id}`;
}
}

0 comments on commit 63461ed

Please sign in to comment.