Skip to content

Commit

Permalink
fix: sorted daily story base on id
Browse files Browse the repository at this point in the history
  • Loading branch information
antheiz committed Aug 29, 2024
1 parent f125e4b commit d4d4297
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/lib/mops.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { Story } from "../types/index"


export class StoryService {
private endpoint = 'https://raw.githubusercontent.com/papua-opensource/mops/dataset/data.json';
private stories: Story[] = [];
private dailyStory: Story | null = null;
private lastUpdated: Date | null = null;
private lastUsedIndex: number = -1;

constructor() {
this.fetchStories();
Expand All @@ -17,6 +15,8 @@ export class StoryService {
const response = await fetch(this.endpoint);
const data = await response.json();
this.stories = data.stories;
// Sort stories by id
this.stories.sort((a, b) => a.id - b.id);
} catch (error) {
console.error('Failed to fetch stories:', error);
}
Expand All @@ -32,11 +32,17 @@ export class StoryService {
return this.stories.find(story => story.slug === slug);
}

// Get a random slug (used for the random story each night)
getRandomSlug(): string | undefined {
// Get the next story in sequence
getNextStory(): Story | undefined {
if (this.stories.length === 0) return undefined;

const randomIndex = Math.floor(Math.random() * this.stories.length);
return this.stories[randomIndex].slug;
this.lastUsedIndex = (this.lastUsedIndex + 1) % this.stories.length;
return this.stories[this.lastUsedIndex];
}

// Get the current story's slug
getCurrentStorySlug(): string | undefined {
if (this.lastUsedIndex === -1 || this.stories.length === 0) return undefined;
return this.stories[this.lastUsedIndex].slug;
}
}
9 changes: 5 additions & 4 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ let storedSlug = Astro.cookies.get('dailyStorySlug')?.value;
// Cek apakah ini hari baru atau tidak ada slug yang tersimpan
if (storedDate !== today || !storedSlug) {
// Jika setelah jam 9 pagi, ambil cerita acak baru
// Jika setelah jam 9 pagi, ambil cerita berikutnya dalam urutan
if (now.getHours() >= 9) {
storedSlug = storyService.getRandomSlug();
dailyStory = storyService.getNextStory();
storedSlug = dailyStory?.slug;
Astro.cookies.set('dailyStoryDate', today, { path: '/' });
Astro.cookies.set('dailyStorySlug', storedSlug || '', { path: '/' });
} else if (!storedSlug) {
// Jika sebelum jam 9 pagi dan tidak ada slug tersimpan, ambil yang acak
storedSlug = storyService.getRandomSlug();
// Jika sebelum jam 9 pagi dan tidak ada slug tersimpan, gunakan cerita saat ini
storedSlug = storyService.getCurrentStorySlug();
Astro.cookies.set('dailyStorySlug', storedSlug || '', { path: '/' });
}
// Jika sebelum jam 9 pagi dan ada slug tersimpan, gunakan yang tersimpan
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Scene = {
}

export type Story = {
id: number;
title: string;
slug: string;
description: string;
Expand Down

0 comments on commit d4d4297

Please sign in to comment.