Browse Source

- Remove annoying padding, Add search function

master
Quildra 1 year ago
parent
commit
e1b7ec2b23
  1. 1
      origin-dex/src/app/app.component.ts
  2. 2
      origin-dex/src/app/app.config.ts
  3. 94
      origin-dex/src/app/features/pokemon/pokemon-carousel/pokemon-carousel.component.ts

1
origin-dex/src/app/app.component.ts

@ -77,7 +77,6 @@ import { MatListModule } from '@angular/material/list';
} }
.content { .content {
padding: 20px;
height: 100%; height: 100%;
overflow: auto; overflow: auto;
} }

2
origin-dex/src/app/app.config.ts

@ -9,7 +9,7 @@ import { provideClientHydration } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
provideZoneChangeDetection({ eventCoalescing: true }), provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes, withDebugTracing()), provideRouter(routes),
provideClientHydration(), provideClientHydration(),
provideHttpClient( provideHttpClient(
withFetch(), withFetch(),

94
origin-dex/src/app/features/pokemon/pokemon-carousel/pokemon-carousel.component.ts

@ -3,10 +3,21 @@ import { CommonModule } from '@angular/common';
import { MatCardModule } from '@angular/material/card'; import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon'; import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { PokemonService } from '../../../core/services/pokemon.service'; import { PokemonService } from '../../../core/services/pokemon.service';
import { Pokemon } from '../../../core/models/pokemon.model'; import { Pokemon } from '../../../core/models/pokemon.model';
import { PokemonCellComponent } from '../pokemon-cell/pokemon-cell.component'; import { PokemonCellComponent } from '../pokemon-cell/pokemon-cell.component';
import { PokemonDetailsComponent } from '../pokemon-details/pokemon-details.component'; import { PokemonDetailsComponent } from '../pokemon-details/pokemon-details.component';
import { map, startWith } from 'rxjs/operators';
import { Observable } from 'rxjs';
interface PokemonSearchResult {
pokemon: Pokemon;
boxNumber: number;
}
@Component({ @Component({
selector: 'app-pokemon-carousel', selector: 'app-pokemon-carousel',
@ -16,10 +27,37 @@ import { PokemonDetailsComponent } from '../pokemon-details/pokemon-details.comp
MatCardModule, MatCardModule,
MatIconModule, MatIconModule,
MatButtonModule, MatButtonModule,
MatAutocompleteModule,
MatInputModule,
MatFormFieldModule,
ReactiveFormsModule,
PokemonCellComponent, PokemonCellComponent,
PokemonDetailsComponent PokemonDetailsComponent
], ],
template: ` template: `
<div class="search-container">
<mat-form-field appearance="fill" class="search-field">
<mat-label>Search Pokémon</mat-label>
<input type="text"
matInput
[formControl]="searchControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete"
(optionSelected)="onSearchSelect($event)"
[displayWith]="displayFn">
<mat-option *ngFor="let result of filteredOptions | async" [value]="result">
{{ result.pokemon.Name }}
<span *ngIf="result.pokemon.Form">
({{ result.pokemon.Form }})
</span>
<span *ngIf="result.boxNumber !== undefined">
(Box {{ result.boxNumber + 1 }})
</span>
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
<div class="carousel-container"> <div class="carousel-container">
<button mat-icon-button class="nav-button prev" <button mat-icon-button class="nav-button prev"
[disabled]="currentBoxIndex === 0" [disabled]="currentBoxIndex === 0"
@ -60,13 +98,23 @@ import { PokemonDetailsComponent } from '../pokemon-details/pokemon-details.comp
></app-pokemon-details> ></app-pokemon-details>
`, `,
styles: [` styles: [`
.search-container {
display: flex;
justify-content: center;
}
.search-field {
width: 100%;
max-width: 400px;
}
.carousel-container { .carousel-container {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
padding: 20px;
position: relative; position: relative;
height: calc(100vh - 200px); //height: calc(100vh - 200px);
padding-top: 2em;
padding-bottom: 2em;
} }
.pokemon-box-container { .pokemon-box-container {
@ -125,6 +173,8 @@ export class PokemonCarouselComponent implements OnInit {
currentBoxIndex = 0; currentBoxIndex = 0;
selectedPokemon: Pokemon | null = null; selectedPokemon: Pokemon | null = null;
caughtPokemon = new Set<string>(); caughtPokemon = new Set<string>();
searchControl = new FormControl('');
filteredOptions: Observable<PokemonSearchResult[]>;
get currentGroup(): (Pokemon | null)[] { get currentGroup(): (Pokemon | null)[] {
return this.pokemonGroups[this.currentBoxIndex] || []; return this.pokemonGroups[this.currentBoxIndex] || [];
@ -133,7 +183,12 @@ export class PokemonCarouselComponent implements OnInit {
constructor( constructor(
private pokemonService: PokemonService, private pokemonService: PokemonService,
private cdr: ChangeDetectorRef private cdr: ChangeDetectorRef
) {} ) {
this.filteredOptions = this.searchControl.valueChanges.pipe(
startWith(''),
map(value => this.filterPokemon(value))
);
}
ngOnInit() { ngOnInit() {
this.loadPokemon(); this.loadPokemon();
@ -183,4 +238,37 @@ export class PokemonCarouselComponent implements OnInit {
this.selectedPokemon = pokemon; this.selectedPokemon = pokemon;
this.cdr.markForCheck(); this.cdr.markForCheck();
} }
private filterPokemon(value: string | PokemonSearchResult | null): PokemonSearchResult[] {
if (!value) return [];
const searchTerm = typeof value === 'string' ? value.toLowerCase() : '';
if (searchTerm.length < 2) return []; // Only search with 2 or more characters
const results: PokemonSearchResult[] = [];
this.pokemonGroups.forEach((group, boxIndex) => {
group.forEach(pokemon => {
if (pokemon && pokemon.Name.toLowerCase().includes(searchTerm)) {
results.push({
pokemon,
boxNumber: boxIndex
});
}
});
});
return results.slice(0, 10); // Limit to 10 results
}
displayFn(result: PokemonSearchResult): string {
return result?.pokemon?.Name || '';
}
onSearchSelect(event: any) {
const result: PokemonSearchResult = event.option.value;
this.currentBoxIndex = result.boxNumber;
this.searchControl.setValue('');
this.cdr.markForCheck();
}
} }
Loading…
Cancel
Save