aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/need-list
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-ui/src/app/components/need-list')
-rw-r--r--ufund-ui/src/app/components/need-list/need-list.component.css74
-rw-r--r--ufund-ui/src/app/components/need-list/need-list.component.html73
-rw-r--r--ufund-ui/src/app/components/need-list/need-list.component.ts306
3 files changed, 312 insertions, 141 deletions
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.css b/ufund-ui/src/app/components/need-list/need-list.component.css
index bbc3f2c..345326f 100644
--- a/ufund-ui/src/app/components/need-list/need-list.component.css
+++ b/ufund-ui/src/app/components/need-list/need-list.component.css
@@ -1,24 +1,82 @@
:host {
- list-style-type:circle;
+ --list-background-color: lightgray;
+ list-style-type: none;
border: 2px solid #000;
display: block;
- width: 30%;
border-radius: 5px;
-
}
-li, div {
+div {
border: 2px solid #000;
border-radius: 5px;
padding: 5px;
margin: 5px;
+}
+
+ul {
+ list-style-type: none;
+ padding-inline-start: 0px;
+}
+
+li {
+ background-color: var(--list-background-color);
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ transition: all 0.3s ease;
+ font-weight: bold;
+ border: 2px solid #000;
+ border-radius: 5px;
+ margin: 5px;
+ > button {
+ background-color: transparent;
+ width: 88%;
+ transition: all 0.3s ease;
+ font-weight: bold;
+ border: none;
+ border-radius: 5px;
+ padding-left: 1.5%;
+ > section {
+ width: 100%;
+ flex: none;
+ display: inline-block;
+ background-color: magenta;
+ > progress {
+ width: 25%;
+ float: none;
+ }
+ }
+ }
+
+ > section {
+ width: 12%;
+ }
+}
+section button{
+ margin: 4%;
}
-#search-form {
+li > button span {
+ font-style: italic;
+ font-weight: normal;
+}
+
+li > button:hover p {
+ text-decoration: underline;
+}
+
+
+.icon {
+ width: 18px;
+ margin: 3px -3px -1px -3px;
+}
+
+#search-container {
background-color: #d9d9d9;
- padding: 10px 20px 20px 20px;
border: 2px solid #000;
- border-radius: 5px;
- visibility: visible;
+ border-radius: 5px;
+ .wide-input {
+ width: 60%;
+ }
} \ No newline at end of file
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.html b/ufund-ui/src/app/components/need-list/need-list.component.html
index 36c12d0..866e5e4 100644
--- a/ufund-ui/src/app/components/need-list/need-list.component.html
+++ b/ufund-ui/src/app/components/need-list/need-list.component.html
@@ -1,28 +1,51 @@
<h1>Needs List</h1>
-<input id="search-button" type="button" value="Search" (click)="open()">
-<div id="search-form">
- <form #searchForm="ngForm">
- <label>Search:</label><br>
- <input type="text" name="search" (input)="search(searchForm.value)" ngModel>
- <input type="button" value="Clear" (click)="searchForm.reset()"> <br>
- </form>
- <button (click)="close()">Close</button>
- <div>
- <h2 id="search-status">Search Results:</h2>
- <div *ngFor="let need of searchResults">
- <a routerLink="/need/{{need.id}}">
- {{need.name}}
- </a>
- <button (click)="delete(need.id)" *ngIf="isManager()">Delete</button>
- <!-- <button (click)="add(need)" *ngIf="isHelper()">Add To Basket</button> -->
- </div>
- </div>
+<div id="search-container">
+ <section>
+ <label for="sort">Sort by: </label>
+ <select [(ngModel)] = "sortSelection" class="wide-input" (change)="search(searchForm.value)" [value]="sortSelection">
+ <option *ngFor="let algorithm of SortingAlgoArrays" value="{{algorithm.name}}">
+ {{algorithm.display[sortMode === 'Ascending' ? 0 : 1]}}
+ </option>
+ </select>
+ <button (click)="changeSortMode(searchForm.value)">
+ {{sortMode}}
+ </button>
+ </section>
+ <section>
+ <form id="search-form" #searchForm="ngForm">
+ <input type="text" name="search" class="wide-input" placeholder="Search in {{needs.length}} needs..." (input)="search(searchForm.value)" ngModel>
+ <input type="reset" value="Clear" (click)="search(null)"> <br>
+ </form>
+ </section>
+ <!--<button (click)="close()">Close</button>-->
</div>
-<li *ngFor="let need of needs">
- <a routerLink="/need/{{need.id}}">
- {{need.name}}
- </a>
- <button (click)="delete(need.id)" *ngIf="isManager()">Delete</button>
- <button (click)="add(need)" *ngIf="isHelper()">Add To Basket</button>
-</li>
+<!-- display for when results are present and filtered-->
+<h2 *ngIf="searchResults.length < needs.length && searchResults.length != 0"> Search Results({{needs.length - searchResults.length}} needs filtered): </h2>
+<h2 *ngIf="searchResults.length == needs.length"> All Needs </h2>
+<h2 *ngIf="searchResults.length == 0"> No Results Found </h2>
+
+<ul>
+ <li *ngFor="let need of searchResults" id="need-button-{{need.id}}">
+ <button [routerLink]="'/need/' + need.id" (mouseenter) ="changeText(need.id, '(details)')" (mouseleave)="changeText(need.id, '')">
+ <section> <p> {{need.name}} | {{need.location}} <span> {{need.urgent ? "URGENT" : ""}} </span> <span id="hover-status-label-{{need.id}}"> </span> </section>
+ <section>
+ <progress value="need.current" max="need.maxGoal"></progress>
+ <progress value="need.current" max="need.maxGoal"></progress>
+ <progress value="need.current" max="need.maxGoal"></progress>
+ <progress value="need.current" max="need.maxGoal"></progress>
+ </section>
+ <section>{{need.current}}/{{need.maxGoal}} {{(need.current / need.maxGoal) * 100}}% <span>{{need.type}}</span></section>
+ </button>
+
+ <button (click)="add(need)" *ngIf="isHelper()">Add To Basket</button>
+ <section *ngIf="isManager()">
+ <button (click)="select(need)" id="need-edit-button-{{need.id}}">
+ <img class="icon" src="/edit.png" alt="Select">
+ </button>
+ <button (click)="delete(need.id)" *ngIf="isManager()">
+ <img class="icon" src="/delete.png" alt="Delete">
+ </button>
+ </section>
+ </li>
+</ul> \ No newline at end of file
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.ts b/ufund-ui/src/app/components/need-list/need-list.component.ts
index 3a89a20..66e53e8 100644
--- a/ufund-ui/src/app/components/need-list/need-list.component.ts
+++ b/ufund-ui/src/app/components/need-list/need-list.component.ts
@@ -1,138 +1,228 @@
-import {Component} from '@angular/core';
+import {Component , Output, EventEmitter} from '@angular/core';
import {Need} from '../../models/Need';
import {CupboardService} from '../../services/cupboard.service';
-import {UsersService} from '../../services/users.service';
-import {userType} from '../../models/User';
-import {catchError, of} from 'rxjs';
+import { UsersService } from '../../services/users.service';
+import { userType } from '../../models/User';
import {AuthService} from '../../services/auth.service';
+import {catchError, of} from 'rxjs';
-@Component({
- selector: 'app-need-list',
- standalone: false,
- templateUrl: './need-list.component.html',
- styleUrl: './need-list.component.css'
-})
-export class NeedListComponent {
- needs: Need[] = [];
- searchResults: Need[] = [];
+interface sortAlgo {
+ (a: Need,b: Need): number;
+}
- constructor(
- private cupboardService: CupboardService,
- private usersService: UsersService,
- private authService: AuthService
- ) {}
+// sort functions
+const sortByName: sortAlgo = (a: Need, b: Need): number => {
+ if(a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
+ return -1;
+ }
+ return 1;
+}
- refresh() {
- this.cupboardService.getNeeds().subscribe(n => this.needs = n)
- }
+const sortByGoal: sortAlgo = (a: Need, b: Need): number => {
+ if(a.maxGoal == b.maxGoal) {
+ return sortByName(a,b);
+ }
+ else if(a.maxGoal > b.maxGoal) {
+ return -1;
+ }
+ return 1;
+}
- ngOnInit(): void {
- this.refresh()
+const sortByCompletion: sortAlgo = (a: Need, b: Need): number => {
+ if(a.current == b.current) {
+ return sortByGoal(a,b);
+ }
+ else if(a.current > b.current) {
+ return -1;
+ }
+ return 1;
+}
- this.close();
- }
+const sortByPriority: sortAlgo = (a: Need, b: Need): number => {
+ if(a.urgent == b.urgent) {
+ return sortByGoal(a,b);
+ }
+ else if(a.urgent && !b.urgent) {
+ return -1;
+ }
+ return 1;
+}
- private showElement(element: any) {
- if (element) {
- element.style.visibility = 'visible';
- element.style.position = 'relative';
- }
- }
+@Component({
+ selector: 'app-need-list',
+ standalone: false,
+ templateUrl: './need-list.component.html',
+ styleUrl: './need-list.component.css'
+})
+export class NeedListComponent {
+ selectedNeed: Need | null = null;
+ needs: Need[] = [];
+ searchResults: Need[] = [];
+ sortMode = 'Ascending'
- private hideElement(element: any) {
- if (element) {
- element.style.visibility = 'hidden';
- element.style.position = 'absolute';
- }
- }
+ currentSortAlgo: sortAlgo = sortByPriority;
+ sortSelection: string = 'sortByPriority';
- private updateSearchStatus(text: string) {
- let element = document.getElementById('search-status');
- if (element) {
- element.innerHTML = text;
- }
- }
+ SortingAlgoArrays: {func:sortAlgo,name:string, display:string[]}[] = [
+ {func:sortByName,name:"sortByName", display:["Name (A to Z)", "Name (Z to A)"]},
+ {func:sortByGoal,name:"sortByGoal", display:["Largest Maximum Goal", "Smallest Maximum Goal"]},
+ {func:sortByCompletion,name:"sortByCompletion", display:["Most Completed", "Least Completed"]},
+ {func:sortByPriority,name:"sortByPriority", display:["Highest Priority", "Lowest Priority"]},
+ ];
- open() {
- this.hideElement(document.getElementById('search-button'));
- this.showElement(document.getElementById('search-form'));
- }
+ @Output() currentNeed = new EventEmitter<Need>();
- close() {
- this.hideElement(document.getElementById('search-form'));
- this.showElement(document.getElementById('search-button'));
- this.hideElement(document.getElementById('search-status'));
- }
+ constructor(
+ private cupboardService: CupboardService,
+ private usersService: UsersService,
+ private authService: AuthService
+ ) {}
- private searchDelay: any;
-
- 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);
- }
-
- this.searchDelay = setTimeout(() => {
- const currentSearchValue = form.search; //latest value of the search
- this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => {
- this.searchResults = n;
- console.log(currentSearchValue, this.searchResults);
- this.showElement(document.getElementById('search-results'));
- this.showElement(document.getElementById('search-status'));
- if (this.searchResults.length === this.needs.length) {
- this.updateSearchStatus("Please refine your search");
- this.searchResults = [];
- } else if (this.searchResults.length === 0) {
- this.updateSearchStatus("No results found");
- } else {
- this.updateSearchStatus("Search results:");
- }
- });
- }, 250);
+ refresh() {
+ this.cupboardService.getNeeds().subscribe(n => {
+ if (this.sortMode == 'Ascending') {
+ this.needs = n.sort(this.currentSortAlgo);
+ } else {
+ this.needs = n.sort(this.currentSortAlgo).reverse();
+ }
+ this.searchResults = this.needs;
+ });
}
- delete(id: number) {
- this.cupboardService.deleteNeed(id).subscribe(() => {
- this.needs = this.needs.filter(n => n.id !== id)
- })
- }
+ ngOnInit(): void {
+ this.refresh()
+ }
- isManager() {
- const type = this.authService.getCurrentUser()?.type;
- return type === ("MANAGER" as unknown as userType);
+ changeSortMode(form : any) {
+ if (this.sortMode == 'Ascending'){
+ this.sortMode = 'Descending'
+ } else {
+ this.sortMode = 'Ascending'
}
+ this.search(form)
+ }
+
+ private searchDelay: any;
- isHelper() {
- const type = this.authService.getCurrentUser()?.type;
- return type === ("HELPER" as unknown as userType);
+ 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
+ this.SortingAlgoArrays.forEach(algo => {
+ if(algo.name === this.sortSelection) {
+ this.currentSortAlgo = algo.func;
+ console.log("changed sorting algorithm to: ", algo.name + this.sortMode)
+ return
+ }
+ });
- 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: any, _) => {
- console.error(err);
- return of();
- }))
- .subscribe(() => {
- this.usersService.refreshBasket();
- });
+ 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);
} else {
- window.alert("This need is already in your basket!")
+ this.searchResults = n.sort(this.currentSortAlgo).reverse();
}
+ console.log(currentSearchValue, this.searchResults);
+ });
+ }
+ }, 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;
+ }
+ }
+
+ delete(id : number) {
+ this.cupboardService.deleteNeed(id).subscribe(() => {
+ this.needs = this.needs.filter(n => n.id !== id)
+ })
+ }
+
+ isManager() {
+ const type = this.authService.getCurrentUser()?.type;
+ return type === ("MANAGER" as unknown as userType);
+ }
+
+ isHelper() {
+ const type = this.authService.getCurrentUser()?.type;
+ return type === ("HELPER" as unknown as userType);
+ }
+
+ changeText(id : number, text : string) {
+ const span = document.getElementById('hover-status-label-' + id);
+ if (span) {
+ span.innerHTML = ' ' + text;
+ }
+ }
+
+ 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 {
+ window.alert("This need is already in your basket!")
+ }
- }
}
- back() {
- this.searchResults = [];
+ }
+
+ back() {
+ this.searchResults = this.needs;
+ }
+
+ 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';
+ }
+ }
}