aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/cupboard/cupboard.component.ts
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-04-03 14:55:55 -0400
committersowgro <tpoke.ferrari@gmail.com>2025-04-03 14:55:55 -0400
commit0652eb445728806d7fd6a50021a763051503ab36 (patch)
tree6036fd6901529ce217ec89456e2ee9a46655483a /ufund-ui/src/app/components/cupboard/cupboard.component.ts
parent6b7c830eeefb6a6a28136a3faacf7713953a6138 (diff)
downloadJellySolutions-0652eb445728806d7fd6a50021a763051503ab36.tar.gz
JellySolutions-0652eb445728806d7fd6a50021a763051503ab36.tar.bz2
JellySolutions-0652eb445728806d7fd6a50021a763051503ab36.zip
get checkout abstraction kinda working
Diffstat (limited to 'ufund-ui/src/app/components/cupboard/cupboard.component.ts')
-rw-r--r--ufund-ui/src/app/components/cupboard/cupboard.component.ts184
1 files changed, 160 insertions, 24 deletions
diff --git a/ufund-ui/src/app/components/cupboard/cupboard.component.ts b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
index 2230cd3..5139738 100644
--- a/ufund-ui/src/app/components/cupboard/cupboard.component.ts
+++ b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
@@ -6,6 +6,8 @@ import { catchError, of } from 'rxjs';
import { NeedListComponent } from '../need-list/need-list.component';
import {AuthService} from '../../services/auth.service';
import {ToastsService, ToastType} from '../../services/toasts.service';
+import {UsersService} from '../../services/users.service';
+import {SortingAlgoArrays} from './sorting';
@Component({
selector: 'app-cupboard',
@@ -13,43 +15,54 @@ import {ToastsService, ToastType} from '../../services/toasts.service';
templateUrl: './cupboard.component.html',
styleUrl: './cupboard.component.css'
})
-
export class CupboardComponent implements OnInit {
selectedForm?: string = undefined;
- needs: any;
+ // needs: any;
@ViewChild("needList") needList?: NeedListComponent
+ private searchDelay: any;
+ selectedNeed: Need | null = null;
+ needs: Need[] = [];
+ searchResults: Need[] = [];
+ sortMode: 'Ascending' | 'Descending' = 'Ascending'
+
+ currentSortAlgo = SortingAlgoArrays.sortByPriority;
+
constructor(
private cupboardService: CupboardService,
private authService: AuthService,
- private toastService: ToastsService
+ private toastService: ToastsService,
+ private usersService: UsersService
) {}
ngOnInit(): void {
- this.cupboardService.getNeeds().subscribe(n => this.needs = n);
- if (this.isManager()) {
- console.log("Admin view of Cupboard");
- } else {
- console.log("Limited helper view of Cupboard");
- }
+ this.cupboardService.getNeeds().subscribe(n => {
+ this.needs = n;
+ this.refresh()
+ });
}
- selectedNeed: any = {
- name: '',
- location:'',
- id: null,
- maxGoal: null,
- type: '',
- urgent: false
- };
- selectedNeedId: number | null = null;
- searchResults: any[] = [];
+ refresh() {
+ this.cupboardService.getNeeds().subscribe(n => {
+ if (this.sortMode == 'Ascending') {
+ this.needs = n.sort(this.currentSortAlgo.func);
+ } else {
+ this.needs = n.sort(this.currentSortAlgo.func).reverse();
+ }
+ this.searchResults = this.needs;
+ // this.updateVisibleNeeds();
+ });
+
+ const form = document.getElementById('search-form') as HTMLFormElement;
+ form.reset();
+ this.search(null);
+ }
selectForm(name: string) {
//get search results from the need list
if (this.needList) {
- this.searchResults = this.needList.searchResults;
+ // this.searchResults = this.needList.searchResults;
}
console.log(this.searchResults)
this.selectedForm = name;
@@ -63,10 +76,65 @@ export class CupboardComponent implements OnInit {
}
}
+ async search(form: any) {
+ //wait .25 seconds before searching but cancel if another search is made during the wait to prevent too many api calls
+
+ //remove previous search if it exists
+ if (this.searchDelay) {
+ clearTimeout(this.searchDelay);
+ }
+ if (form) {
+ this.searchDelay = setTimeout(() => {
+
+ if (form) {
+ //sorting based on algo selected
+ // SortingAlgoArrays.forEach(algo => {
+ // if(algo.name === this.sortSelection) {
+ // this.currentSortAlgo = SortingAlgoArrays[this.sort];
+ // console.log("changed sorting algorithm to: ", algo.name + this.sortMode)
+ // return
+ // }
+ // });
+
+ const currentSearchValue = form.search; //latest value of the search
+ this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => {
+ if (this.sortMode == 'Ascending') {
+ this.searchResults = n.sort(this.currentSortAlgo.func);
+ } else {
+ this.searchResults = n.sort(this.currentSortAlgo.func).reverse();
+ }
+ // this.updateVisibleNeeds();
+ });
+ }
+ }, 250);
+ } else {
+ //user has cleared the search bar, we can skip the timeout for a 1/4 second faster response
+ //clear timeout to stop pending search
+ clearTimeout(this.searchDelay);
+ this.searchResults = this.needs;
+ }
+ }
+
+ changeSortMode(form : any) {
+ if (this.sortMode == 'Ascending'){
+ this.sortMode = 'Descending'
+ } else {
+ this.sortMode = 'Ascending'
+ }
+ this.search(form)
+ }
+
+ changeText(id : number, text : string) {
+ const span = document.getElementById('hover-status-label-' + id);
+ if (span) {
+ span.innerHTML = ' ' + text;
+ }
+ }
+
async updateSearchResults() {
if (this.needList) {
while (this.selectedForm == 'update') {
- this.searchResults = this.needList.searchResults
+ // this.searchResults = this.needList.searchResults
await new Promise(resolve => setTimeout(resolve, 100));
}
}
@@ -82,6 +150,45 @@ export class CupboardComponent implements OnInit {
return type === ("MANAGER" as unknown as userType);
}
+ isHelper() {
+ const type = this.authService.getCurrentUser()?.type;
+ return type === ("HELPER" as unknown as userType);
+ }
+
+ // -------------- DISPLAY FUNCTIONS -------------- //
+
+ select(need : Need) {
+ //emit value
+ // this.currentNeed.emit(need);
+ if (this.selectedNeed) {
+ //revert already selected need to previous style
+ console.log(need.id);
+ let button = document.getElementById('need-button-' + this.selectedNeed.id);
+ if (button) {
+ console.log(button)
+ button.style.background = 'lightgray';
+ button.style.marginLeft = '0%';
+ button.style.width = '98%';
+ }
+ button = document.getElementById('need-edit-button-' + this.selectedNeed.id);
+ if (button) {
+ button.style.visibility = 'visible';
+ }
+ }
+ //change selected need to selected style
+ this.selectedNeed = need;
+ let button = document.getElementById('need-button-' + need.id);
+ if (button) {
+ button.style.background = 'white';
+ button.style.marginLeft = '4%';
+ button.style.width = '100%';
+ }
+ button = document.getElementById('need-edit-button-' + need.id);
+ if (button) {
+ button.style.visibility = 'hidden';
+ }
+ }
+
submit(form: any) {
const need: Need = {
name: form.name,
@@ -90,7 +197,7 @@ export class CupboardComponent implements OnInit {
id: 0,
maxGoal: form.maxGoal,
type: form.type,
- urgent: form.urgent ? true : false,
+ urgent: !!form.urgent,
filterAttributes: [],
current: 0,
description: form.description
@@ -112,7 +219,7 @@ export class CupboardComponent implements OnInit {
(result) => {
if (result) {
console.log("need created successfully");
- this.needList?.refresh()
+ // this.needList?.refresh()
} else {
console.log("need creation failed");
}
@@ -121,7 +228,36 @@ export class CupboardComponent implements OnInit {
);
}
- destroy() {
+ // -------------- CRUD OPERATIONS -------------- //
+ delete(id : number) {
+ this.cupboardService.deleteNeed(id).subscribe(() => {
+ this.toastService.sendToast(ToastType.INFO, "Need deleted.")
+ this.needs = this.needs.filter(n => n.id !== id)
+ })
+ this.refresh();
}
+
+ add(need: Need) {
+ const currentUser = this.authService.getCurrentUser();
+ //console.log("get current user in angular:", currentUser)
+ if (currentUser) {
+ if (!currentUser.basket.includes(need.id)) {
+ currentUser.basket.push(need.id);
+ this.usersService.updateUser(currentUser)
+ .pipe(catchError((err, _) => {
+ console.error(err);
+ return of();
+ }))
+ .subscribe(() => {
+ this.usersService.refreshBasket();
+ });
+ } else {
+ this.toastService.sendToast(ToastType.ERROR, "This need is already in your basket!")
+ }
+ }
+ }
+
+ protected readonly SortingAlgoArrays = SortingAlgoArrays;
+ protected readonly Object = Object;
}