|
| 1 | +import {Component, Inject, PLATFORM_ID} from '@angular/core'; |
| 2 | +import {ClientService, defaultPageSize} from "../../api/client.service"; |
| 3 | +import {ActivatedRoute} from "@angular/router"; |
| 4 | +import {PageTitleComponent} from "../../components/ui/text/page-title.component"; |
| 5 | +import {ResponsiveGridComponent} from "../../components/ui/responsive-grid.component"; |
| 6 | +import {Level} from "../../api/types/levels/level"; |
| 7 | +import {LevelPreviewComponent} from "../../components/items/level-preview.component"; |
| 8 | +import {ContainerComponent} from "../../components/ui/container.component"; |
| 9 | +import {Scrollable} from "../../helpers/scrollable"; |
| 10 | +import {defaultListInfo, RefreshApiListInfo} from "../../api/refresh-api-list-info"; |
| 11 | +import {InfiniteScrollerComponent} from "../../components/ui/infinite-scroller.component"; |
| 12 | +import { User } from '../../api/types/users/user'; |
| 13 | +import { UserLinkComponent } from "../../components/ui/text/links/user-link.component"; |
| 14 | +import { RadioButtonComponent } from "../../components/ui/form/radio-button.component"; |
| 15 | +import { DropdownMenuComponent } from "../../components/ui/form/dropdown-menu.component"; |
| 16 | +import { ButtonComponent } from "../../components/ui/form/button.component"; |
| 17 | +import { ContainerHeaderComponent } from "../../components/ui/container-header.component"; |
| 18 | +import { FormControl, FormGroup } from '@angular/forms'; |
| 19 | +import { ListWithData } from '../../api/list-with-data'; |
| 20 | +import { BannerService } from '../../banners/banner.service'; |
| 21 | +import { faChevronDown, faChevronUp } from '@fortawesome/free-solid-svg-icons'; |
| 22 | +import { isPlatformBrowser } from '@angular/common'; |
| 23 | +import { RefreshApiError } from '../../api/refresh-api-error'; |
| 24 | +import { CachedListWithData } from '../../api/cached-list-with-data'; |
| 25 | + |
| 26 | +@Component({ |
| 27 | + selector: 'app-level-user-listing', |
| 28 | + imports: [ |
| 29 | + PageTitleComponent, |
| 30 | + ResponsiveGridComponent, |
| 31 | + LevelPreviewComponent, |
| 32 | + ContainerComponent, |
| 33 | + InfiniteScrollerComponent, |
| 34 | + UserLinkComponent, |
| 35 | + RadioButtonComponent, |
| 36 | + DropdownMenuComponent, |
| 37 | + ButtonComponent, |
| 38 | + ContainerHeaderComponent |
| 39 | +], |
| 40 | + templateUrl: './level-user-listing.component.html' |
| 41 | +}) |
| 42 | +export class LevelUserListingComponent implements Scrollable { |
| 43 | + user: User | undefined; |
| 44 | + levelsPublishedByUser: CachedListWithData<Level> | undefined; |
| 45 | + levelsHeartedByUser: CachedListWithData<Level> | undefined; |
| 46 | + currentLevels: CachedListWithData<Level> = { |
| 47 | + data: [], |
| 48 | + listInfo: defaultListInfo, |
| 49 | + totalLoads: 0, |
| 50 | + }; |
| 51 | + |
| 52 | + showLevelDropdown: boolean = false; |
| 53 | + levelSelectionString: string = ""; |
| 54 | + filterForm = new FormGroup({ |
| 55 | + selection: new FormControl(-1) |
| 56 | + }); |
| 57 | + |
| 58 | + protected readonly isBrowser: boolean; |
| 59 | + |
| 60 | + constructor(private client: ClientService, protected banner: BannerService, private route: ActivatedRoute, @Inject(PLATFORM_ID) platformId: Object) { |
| 61 | + route.params.subscribe(params => { |
| 62 | + const username: string | undefined = params['username']; |
| 63 | + const category: string | undefined = params['category']; |
| 64 | + |
| 65 | + if (username != null) { |
| 66 | + this.client.getUserByUsername(username).subscribe({ |
| 67 | + error: error => { |
| 68 | + const apiError: RefreshApiError | undefined = error.error?.error; |
| 69 | + this.banner.warn("Failed to get user", apiError == null ? error.message : apiError.message); |
| 70 | + }, |
| 71 | + next: user => { |
| 72 | + this.user = user; |
| 73 | + |
| 74 | + if (category != null) { |
| 75 | + this.levelSelectionString = category; |
| 76 | + switch (category) { |
| 77 | + case "byUser": |
| 78 | + this.setLevelSelection(0); |
| 79 | + break; |
| 80 | + case "hearted": |
| 81 | + this.setLevelSelection(1); |
| 82 | + break; |
| 83 | + default: |
| 84 | + this.banner.warn("Cannot get levels", "Selection '" + category + "' is unknown"); |
| 85 | + return; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + this.isBrowser = isPlatformBrowser(platformId); |
| 94 | + } |
| 95 | + |
| 96 | + levelSelectionButtonClick() { |
| 97 | + this.showLevelDropdown = !this.showLevelDropdown; |
| 98 | + } |
| 99 | + |
| 100 | + getLevelSelectionText(selection: number): string { |
| 101 | + switch (selection) { |
| 102 | + case 0: return "Published by"; |
| 103 | + case 1: return "Hearted by"; |
| 104 | + default: return "Something by"; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + setLevelSelection(selection: number) { |
| 109 | + if (this.user == null) return; |
| 110 | + |
| 111 | + let previousSelection: number = this.filterForm.controls.selection.getRawValue()!; |
| 112 | + if (selection === previousSelection) return; |
| 113 | + |
| 114 | + switch (previousSelection) { |
| 115 | + case 0: |
| 116 | + this.levelsPublishedByUser = this.currentLevels; |
| 117 | + break; |
| 118 | + case 1: |
| 119 | + this.levelsHeartedByUser = this.currentLevels; |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + let cachedList: CachedListWithData<Level> | undefined; |
| 124 | + switch (selection) { |
| 125 | + case 0: |
| 126 | + this.levelSelectionString = "byUser"; |
| 127 | + cachedList = this.levelsPublishedByUser; |
| 128 | + break; |
| 129 | + case 1: |
| 130 | + this.levelSelectionString = "hearted"; |
| 131 | + cachedList = this.levelsHeartedByUser; |
| 132 | + break; |
| 133 | + default: |
| 134 | + this.banner.warn("Cannot get levels", "Selection " + selection + " is unknown"); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + this.filterForm.controls.selection.setValue(selection); |
| 139 | + if(this.isBrowser) { |
| 140 | + window.history.replaceState({}, '', `/levels/${this.levelSelectionString}/user/${this.user.username}`); |
| 141 | + } |
| 142 | + |
| 143 | + if (cachedList != null) { |
| 144 | + this.currentLevels = cachedList; |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + this.currentLevels = { |
| 149 | + data: [], |
| 150 | + listInfo: defaultListInfo, |
| 151 | + totalLoads: 0, |
| 152 | + }; |
| 153 | + this.currentLevels.totalLoads++; |
| 154 | + this.loadData(); |
| 155 | + } |
| 156 | + |
| 157 | + isLoading: boolean = false; |
| 158 | + get listInfo() {return this.currentLevels.listInfo} |
| 159 | + |
| 160 | + loadData(): void { |
| 161 | + if(!this.user) return; |
| 162 | + |
| 163 | + this.isLoading = true; |
| 164 | + this.client.getLevelsInCategory(this.levelSelectionString, this.listInfo.nextPageIndex, defaultPageSize, {u: this.user.username}).subscribe({ |
| 165 | + error: error => { |
| 166 | + const apiError: RefreshApiError | undefined = error.error?.error; |
| 167 | + this.banner.warn("Failed to get levels", apiError == null ? error.message : apiError.message); |
| 168 | + }, |
| 169 | + next: list => { |
| 170 | + this.currentLevels.data = this.currentLevels.data.concat(list.data); |
| 171 | + this.currentLevels.listInfo = list.listInfo; |
| 172 | + this.isLoading = false; |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + incrementLoads() { |
| 178 | + this.currentLevels.totalLoads++; |
| 179 | + } |
| 180 | + |
| 181 | + protected readonly faChevronDown = faChevronDown; |
| 182 | + protected readonly faChevronUp = faChevronUp; |
| 183 | +} |
0 commit comments