Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shuffling: store randoms on reload #74

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/components/activity-card/activity-card.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ActivityCardComponent implements OnInit {
this.getBookmarked(this.activity?.osm_id)
}

onBookmark(osm_id: string | null | undefined) {
onBookmark(osm_id: number | null | undefined) {
this.isBookmarked = !this.isBookmarked
const item = localStorage.getItem("savedLocations")
if (item) {
Expand All @@ -38,7 +38,7 @@ export class ActivityCardComponent implements OnInit {

}

getBookmarked(osm_id: string | null | undefined) {
getBookmarked(osm_id: number | null | undefined) {
const item = localStorage.getItem("savedLocations")
if(item) {
const savedLocations = JSON.parse(item)
Expand Down
5 changes: 3 additions & 2 deletions src/app/components/detail/detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export class DetailComponent implements OnInit {
}

ngOnInit(): void {
const osmId = this.route.snapshot.paramMap.get('osm_id');
if (osmId) {
const osmIdString = this.route.snapshot.paramMap.get('osm_id');
if (osmIdString) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OSM IDs können jetzt nur noch numbers, null oder undefined sein, ist das nicht unreachable?

const osmId = parseInt(osmIdString, 10);
this.activityService.getActivitiesByOsmId(osmId).subscribe((activities) => {
this.activity = activities[0];
});
Expand Down
46 changes: 38 additions & 8 deletions src/app/components/for-you-page/for-you-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Activity } from '../../types/activity.interface';
import { AirtableService } from '../../services/airtable.service';
import { ActivityCardComponent } from '../activity-card/activity-card.component';
import { NgIf } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
selector: 'app-for-you-page',
Expand All @@ -20,7 +21,11 @@ export class ForYouPageComponent implements OnInit{
isLoading = true;
activities: Activity[] = [];

constructor(private airtable: AirtableService){}
constructor(
private airtable: AirtableService,
private route: ActivatedRoute,
private router: Router
){}

ngOnInit(): void {
this.airtable.getActivityList().subscribe(
Expand All @@ -35,16 +40,41 @@ export class ForYouPageComponent implements OnInit{
}

shuffle(activities: Activity[], count: number): Activity[] {
const shuffledArray = activities.slice();
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
let result: Activity[];
// Prüfen, ob URL-Parameter vorhanden sind
const osmIds = this.route.snapshot.queryParamMap.get('osm_ids');

return shuffledArray.slice(0, count);
if (osmIds) {
const idsArray: number[] = osmIds.split(',').map((id: string) => parseInt(id, 10));
result = activities.filter((activity: Activity) => idsArray.includes(activity.osm_id));
} else {
const shuffledArray = activities.slice();
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
result = shuffledArray.slice(0, count);
this.updateUrlWithIds(result);
}
return result
}

doReshuffle(): void {
this.shuffledActivities = this.shuffle(this.allActivities, 3);
this.router.navigate([], {
relativeTo: this.route,
queryParams: { osm_ids: null },
queryParamsHandling: 'merge'
}).then(() => {
this.shuffledActivities = this.shuffle(this.allActivities, 3);
});
}

updateUrlWithIds(randomItems: Activity[]): void {
const osmIds = randomItems.map((activity: Activity) => activity.osm_id).join(',');
this.router.navigate([], {
relativeTo: this.route,
queryParams: { osm_ids: osmIds },
queryParamsHandling: 'merge'
});
}
}
2 changes: 1 addition & 1 deletion src/app/components/map/map.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class MapComponent implements OnInit {

constructor(private router: Router, private airtableService: AirtableService) {}

getBookmarked(osm_id: string | null | undefined) {
getBookmarked(osm_id: number | null | undefined) {
const item = localStorage.getItem("savedLocations")
if(item) {
const savedLocations = JSON.parse(item)
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/favorites/favorites.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class FavoritesComponent {

constructor(private airtable: AirtableService) { }

getActivityByOsm(osm_id: string) {
getActivityByOsm(osm_id: number) {
return this.airtable.getActivitiesByOsmId(osm_id);
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/services/airtable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AirtableService {
}
}

getActivitiesByOsmId(osmId: string): Observable<Activity[]> {
getActivitiesByOsmId(osmId: number): Observable<Activity[]> {
return this.getActivityList().pipe(map(activities => {
return Array.from(activities).filter(activity => activity.osm_id == osmId)
}))
Expand Down Expand Up @@ -77,7 +77,7 @@ export class AirtableService {
latitude: record.fields['latitude'] as number,
longitude: record.fields['longitude'] as number,
website: record.fields['website'] as string,
osm_id: record.fields['osm_id'] as string,
osm_id: record.fields['osm_id'] as number,
media: medias.find((media: any) => media.id === (record.fields?.['media']?.[0])) as any,
age_restriction: record.fields['age_restriction'] as number,
barrier_free: record.fields['barrier_free'] as boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/app/types/activity.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Activity {
name: string;
description?: string;
type: TypesInterface;
osm_id: string;
osm_id: number;
street: string;
number?: string;
zip: string;
Expand Down
Loading