Skip to content

Commit 9adcf91

Browse files
committed
added modify dish
1 parent 8522a9e commit 9adcf91

File tree

11 files changed

+275
-10
lines changed

11 files changed

+275
-10
lines changed

lab6/restauracja/src/app/app-routing.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MenagerGuard } from './guard/menager.guard';
1414
import { HomeComponent } from './home/home.component';
1515
import { LoginComponent } from './login/login.component';
1616
import { MenagerdashboardComponent } from './menagerdashboard/menagerdashboard.component';
17+
import { ModifyDishComponent } from './modify-dish/modify-dish.component';
1718
import { PagenotfoundComponent } from './pagenotfound/pagenotfound.component';
1819
import { RegisterComponent } from './register/register.component';
1920
import { SingledishComponent } from './singledish/singledish.component';
@@ -38,6 +39,11 @@ const routes: Routes = [
3839
component: MenagerdashboardComponent,
3940
canActivate: [MenagerGuard],
4041
},
42+
{
43+
path: 'menager/modify/:id',
44+
component: ModifyDishComponent,
45+
canActivate: [AuthGuard, MenagerGuard],
46+
},
4147
{
4248
path: 'admin',
4349
component: AdmindashboardComponent,

lab6/restauracja/src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { AdmindashboardComponent } from './admindashboard/admindashboard.compone
2525
import { MenagerdashboardComponent } from './menagerdashboard/menagerdashboard.component';
2626
import { FooterComponent } from './footer/footer.component';
2727
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
28+
import { ModifyDishComponent } from './modify-dish/modify-dish.component';
2829
@NgModule({
2930
declarations: [
3031
AppComponent,
@@ -43,6 +44,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4344
AdmindashboardComponent,
4445
MenagerdashboardComponent,
4546
FooterComponent,
47+
ModifyDishComponent,
4648
],
4749
imports: [
4850
BrowserModule,

lab6/restauracja/src/app/fire-base-service.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,14 @@ export class FireBaseServiceService {
146146
window.alert(err)
147147
}
148148
}
149+
150+
updateDish(data: any, idS:string){
151+
this.db.list('Dishes').snapshotChanges().pipe(first()).subscribe((items: any) => {
152+
for (let i of items) {
153+
if (i.payload.val().id == idS) {
154+
this.db.list('Dishes').update(i.payload.key, data)
155+
}
156+
}
157+
});
158+
}
149159
}

lab6/restauracja/src/app/login/login.component.css

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ button, h2{
1919
label{
2020
margin-right:1em;
2121
}
22-
input{
23-
color:var(--colorPrimary);
24-
font-weight: 700;
25-
}
22+
2623
label,input,h2, button{
2724
font-size:3em;
2825
}

lab6/restauracja/src/app/menagerdashboard/menagerdashboard.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h2>Zarządzaj daniami</h2>
1616
<td>{{dish.name}}</td>
1717
<td>
1818
<button (click)="deleteDish(dish.id)" class="button-accent" >USUŃ</button>
19-
<button (click)="updateDish(dish.id)" class="button-accent">MODYFIKUJ</button>
19+
<button routerLink="modify/{{dish.id}}" class="button-accent">MODYFIKUJ</button>
2020
</td>
2121
</tr>
2222
</table>

lab6/restauracja/src/app/menagerdashboard/menagerdashboard.component.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,4 @@ export class MenagerdashboardComponent implements OnInit {
4343
deleteDish(idx: number) {
4444
this.fb.removeDish(idx);
4545
}
46-
47-
updateDish(idx: number) {
48-
window.alert('Not yet implemented');
49-
}
5046
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
.form-container {
2+
display: flex;
3+
flex-direction: column;
4+
align-items: center;
5+
margin: 1em;
6+
}
7+
form {
8+
display: flex;
9+
flex-direction: column;
10+
width: 50%;
11+
}
12+
div {
13+
display: flex;
14+
flex-direction: row;
15+
justify-content: flex-end;
16+
}
17+
label,
18+
input {
19+
font-size: 1.2em;
20+
margin: 1em;
21+
width: 20vw;
22+
}
23+
button {
24+
width: 50%;
25+
padding: 1em;
26+
align-self: center;
27+
}
28+
29+
input {
30+
min-width: 100px;
31+
width: 500px;
32+
}
33+
34+
p {
35+
text-align: center;
36+
}
37+
38+
.left-right {
39+
width: 100%;
40+
display: flex;
41+
flex-direction: row;
42+
justify-content: space-between;
43+
}
44+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<div class="form-container">
2+
<h2>Dodaj danie</h2>
3+
<form [formGroup]="dishModifyForm">
4+
<div class="left-right">
5+
<label for="name">Nazwa dania:</label>
6+
<input id="name" type="text" formControlName="Name" />
7+
</div>
8+
9+
<div class="left-right">
10+
<label for="type">Typ kuchni:</label>
11+
<input id="type" type="text" formControlName="Type" />
12+
</div>
13+
14+
<div class="left-right">
15+
<label for="category">Kategoria:</label>
16+
<input id="category" type="text" formControlName="Category" />
17+
</div>
18+
19+
<div class="left-right">
20+
<label for="ingredients">Składniki:</label>
21+
<input id="ingredients" type="text" formControlName="Ingredients" />
22+
</div>
23+
24+
<div class="left-right">
25+
<label for="maxperday">Maks na dzień:</label>
26+
<input id="maxperday" type="text" formControlName="MaxPerDay" />
27+
</div>
28+
29+
<div class="left-right">
30+
<label for="price">Cena:</label>
31+
<input id="price" type="text" formControlName="Price" />
32+
</div>
33+
34+
<div class="left-right">
35+
<label for="shortdesc">Opis:</label>
36+
<input id="shortdesc" type="text" formControlName="ShortDesc" />
37+
</div>
38+
39+
<div class="left-right">
40+
<label for="imagelink">Ścieżka do zdj:</label>
41+
<input id="imagelink" type="text" formControlName="ImageLink" />
42+
</div>
43+
44+
<button type="submit" (click)="submitForm()" class="button-accent">Modyfikuj</button>
45+
<p *ngIf="showError">Wprowadź poprawne dane!</p>
46+
<p *ngIf="showOk">Poprawnie zaktualizowano potrawę!</p>
47+
</form>
48+
49+
</div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { ModifyDishComponent } from './modify-dish.component';
4+
5+
describe('ModifyDishComponent', () => {
6+
let component: ModifyDishComponent;
7+
let fixture: ComponentFixture<ModifyDishComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ ModifyDishComponent ]
12+
})
13+
.compileComponents();
14+
});
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(ModifyDishComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms';
3+
import { ActivatedRoute, Router } from '@angular/router';
4+
import { first, Subscription } from 'rxjs';
5+
import { FireBaseServiceService } from '../fire-base-service.service';
6+
7+
@Component({
8+
selector: 'app-modify-dish',
9+
templateUrl: './modify-dish.component.html',
10+
styleUrls: ['./modify-dish.component.css'],
11+
})
12+
export class ModifyDishComponent implements OnInit {
13+
constructor(
14+
private route: ActivatedRoute,
15+
private fb: FireBaseServiceService,
16+
private router: Router
17+
) {}
18+
19+
id: any = null;
20+
dish: any = null;
21+
subscription: Subscription | undefined;
22+
23+
ngOnInit(): void {
24+
this.subscription = this.route.params.subscribe((params) => {
25+
this.id = params['id'];
26+
});
27+
this.fb
28+
.getDishes()
29+
.pipe(first())
30+
.subscribe((dishes: any[]) => {
31+
for (let d of dishes) {
32+
if (d.id == this.id) {
33+
this.dishModifyForm.patchValue(d)
34+
break;
35+
}
36+
}})
37+
}
38+
ngOnDestroy(): void {
39+
if (this.subscription) this.subscription.unsubscribe();
40+
}
41+
42+
dishModifyForm = new FormGroup({
43+
Name: new FormControl('', [
44+
Validators.required,
45+
Validators.minLength(2),
46+
]),
47+
Type: new FormControl('', [
48+
Validators.required,
49+
Validators.minLength(2),
50+
]),
51+
Category: new FormControl('', [
52+
Validators.required,
53+
Validators.minLength(2),
54+
]),
55+
Ingredients: new FormControl('', [
56+
Validators.required,
57+
Validators.minLength(2),
58+
]),
59+
MaxPerDay: new FormControl('', [
60+
Validators.required,
61+
Validators.min(1),
62+
Validators.pattern('[0-9]*'),
63+
]),
64+
Price: new FormControl('', [
65+
Validators.required,
66+
Validators.min(1),
67+
Validators.pattern('[0-9]*.?[0-9]+'),
68+
]),
69+
ShortDesc: new FormControl('', [
70+
Validators.required,
71+
Validators.minLength(2),
72+
]),
73+
ImageLink: new FormControl('', [
74+
Validators.required,
75+
Validators.minLength(1),
76+
]),
77+
});
78+
79+
showError = false;
80+
showOk = false;
81+
82+
83+
84+
getFormValidationErrors(form: any) {
85+
Object.keys(form.controls).forEach(key => {
86+
const controlErrors: ValidationErrors = form.get(key).errors;
87+
if (controlErrors != null) {
88+
Object.keys(controlErrors).forEach(keyError => {
89+
console.log('Key control: ' + key + ', keyError: ' + keyError + ', err value: ', controlErrors[keyError]);
90+
});
91+
}
92+
});
93+
}
94+
95+
submitForm() {
96+
if (!this.dishModifyForm.valid) {
97+
this.showError = true;
98+
return;
99+
}
100+
this.fb
101+
.getDishes()
102+
.pipe(first())
103+
.subscribe((dishes: any[]) => {
104+
let dish: any;
105+
for (let d of dishes) {
106+
if (d.id == this.id) {
107+
dish = d;
108+
break;
109+
}
110+
}
111+
let dataToUpdate = {
112+
Name: this.dishModifyForm.get('Name')!.value,
113+
Type: this.dishModifyForm.get('Type')!.value,
114+
Category: this.dishModifyForm.get('Category')!.value,
115+
Ingredients: this.dishModifyForm.get('Ingredients')!.value,
116+
MaxPerDay: this.dishModifyForm.get('MaxPerDay')!.value,
117+
Price: parseFloat(this.dishModifyForm.get('Price')!.value),
118+
ShortDesc: this.dishModifyForm.get('ShortDesc')!.value,
119+
ImageLink: new Array<string>(
120+
this.dishModifyForm.get('ImageLink')!.value
121+
),
122+
};
123+
try{
124+
this.fb.updateDish(dataToUpdate, this.id)
125+
}catch(err){
126+
window.alert(err)
127+
}
128+
this.showError = false;
129+
this.showOk = true;
130+
this.dishModifyForm.reset();
131+
});
132+
this.router.navigate(['/menager/'])
133+
}
134+
}

0 commit comments

Comments
 (0)