aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
blob: 42015fe5354b579a9ebe4980ea73e9ab2caf625d (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {Component, OnInit} from '@angular/core';
import {User} from '../../models/User';
import { UsersService } from '../../services/users.service';
import { Need } from '../../models/Need';
import { NeedListComponent } from '../need-list/need-list.component';
import { Router } from '@angular/router';
import { CupboardService } from '../../services/cupboard.service';

@Component({
    selector: 'app-funding-basket',
    standalone: false,
    templateUrl: './funding-basket.component.html',
    styleUrl: './funding-basket.component.css'
})
export class FundingBasketComponent implements
  OnInit {
  user!: User;
  needs: Need[] = [];
  basket: Need[] = [];
  needCount = 0;
  need_quantity: {[key: number]: number} = {};

  constructor(
    private router: Router,
      private cupboardService: CupboardService, private usersService: UsersService
  ) {}

  ngOnInit(): void {
    if (!this.usersService.getCurrentUser()) {
      this.router.navigate(['/login'], {queryParams: {redir: this.router.url}});
      return;
    }
    this.cupboardService.getNeeds().subscribe(n => this.needs = n)
    const currentUser = this.usersService.getCurrentUser();
    if (currentUser) {
      this.user = currentUser;
    }
    this.getBasketNeeds();
  }

    getBasketNeeds(): void {
        if (this.user && this.user.basket) {
            this.user.basket.forEach(need => {
                if (this.isInBasket(need)) {
                    this.basket.push(need);
                }
            });
        }
    }

    isInBasket(need: Need): boolean {
        return this.basket.some(n => n.id == need.id);
    }

    addNeed(need: Need, quantity: number = 1): void {
        if (this.user && !this.isInBasket(need)) {
            this.basket.push(need);
            this.need_quantity[need.id] = quantity;
        }
        if (this.isInBasket(need)) {
            this.need_quantity[need.id] += quantity;
        }
        this.needCount++;
    }

    removeNeed(need: Need, quantity: number = 1): void {
        if (this.user && this.isInBasket(need)) {
            this.need_quantity[need.id] -= quantity;
            if (this.need_quantity[need.id] === 0) {
                this.basket = this.basket.filter(n => need.id !== n.id);
            }
            this.needCount--;
        }
    }

    removeAllNeeds(): void {
        this.basket.forEach(need => {
            this.need_quantity = [];
        });
        this.basket = [];
        this.needCount = 0;
    }

    isBasketEmpty(): boolean {
        return this.needCount === 0;
    }

    checkout(): void {
        this.removeAllNeeds();
    }
}