1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
import {Component, Input, OnInit, ViewChild, TemplateRef} from '@angular/core';
import {GoalType, Need} from '../../models/Need';
import {ActivatedRoute, Router} from "@angular/router";
import {CupboardService} from "../../services/cupboard.service";
import {AuthService} from '../../services/auth.service';
import {catchError, of} from 'rxjs';
import {ToastsService, ToastType} from '../../services/toasts.service';
import {UsersService} from '../../services/users.service';
import {ModalService} from '../../services/modal.service';
@Component({
selector: 'app-need-page',
standalone: false,
templateUrl: './need-page.component.html',
styleUrl: './need-page.component.css'
})
export class NeedPageComponent implements OnInit {
constructor(
private route: ActivatedRoute,
private cupboardService: CupboardService,
private authService: AuthService,
protected usersService: UsersService,
private toastService: ToastsService,
private router: Router,
protected modalService: ModalService
) {}
@Input() need!: Need;
warned: boolean = false;
ngOnInit(): void {
const id = Number(this.route.snapshot.paramMap.get('id'));
this.cupboardService.getNeed(id).subscribe(n => this.need = n);
}
add(need: Need) {
const currentUser = this.authService.getCurrentUser();
if (currentUser) {
if (!currentUser.basket.includes(need.id)) {
currentUser.basket.push(need.id);
this.usersService.updateUser(currentUser)
.pipe(catchError((err, _) => {
let action = {label: "View Basket", onAction: () => this.router.navigate(['/basket'])}
this.toastService.sendToast(ToastType.INFO, `"${need.name}" Added to basket`, action)
return of();
}))
.subscribe(() => {
this.usersService.refreshBasket();
});
} else {
this.toastService.sendToast(ToastType.ERROR, "This need is already in your basket!")
}
}
}
delete(id : number) {
this.cupboardService.deleteNeed(id)
.pipe(catchError((ex, _) => {
this.toastService.sendToast(ToastType.ERROR, ex.error)
return of()
}))
.subscribe(() => {
this.toastService.sendToast(ToastType.INFO, "Need deleted.")
this.router.navigate(['/cupboard'])
})
// this.refresh();
}
readonly GoalType = GoalType
}
|