2022-07-27 23:16:50 +00:00
|
|
|
import { collectionStore } from '$lib/stores/collectionStore';
|
|
|
|
|
import { toast } from '$lib/components/toast/toast';
|
2022-08-08 19:07:44 +00:00
|
|
|
import { ToastType, type GameType, type SavedGameType } from '$lib/types';
|
2022-11-03 05:16:52 +00:00
|
|
|
import { convertToSavedGame } from './gameMapper';
|
2022-12-07 07:00:48 +00:00
|
|
|
import { saved_game_schema } from '../zodValidation';
|
2022-07-27 23:16:50 +00:00
|
|
|
|
2023-01-23 06:39:32 +00:00
|
|
|
export function addToCollection(game: GameType | SavedGameType, index: number) {
|
2023-01-08 08:35:40 +00:00
|
|
|
try {
|
|
|
|
|
console.log(`Saving game: ${JSON.stringify(game)}`);
|
|
|
|
|
saved_game_schema.parse(game);
|
2023-01-23 06:39:32 +00:00
|
|
|
if (index === -1) {
|
|
|
|
|
collectionStore.add(convertToSavedGame(game));
|
|
|
|
|
} else {
|
|
|
|
|
collectionStore.addSorted(convertToSavedGame(game), index);
|
|
|
|
|
}
|
2023-01-08 08:35:40 +00:00
|
|
|
toast.send('Added to collection', { duration: 3000, type: ToastType.INFO });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
toast.send('Error adding to collection', { duration: 3000, type: ToastType.ERROR });
|
|
|
|
|
}
|
2022-07-27 23:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-29 21:41:11 +00:00
|
|
|
export function removeFromCollection(game: GameType | SavedGameType) {
|
2023-01-08 08:35:40 +00:00
|
|
|
try {
|
|
|
|
|
saved_game_schema.parse(game);
|
|
|
|
|
collectionStore.remove(game.id);
|
|
|
|
|
toast.send('Removed from collection', { duration: 3000, type: ToastType.INFO });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
toast.send('Error removing from collection', { duration: 3000, type: ToastType.ERROR });
|
|
|
|
|
}
|
2022-07-28 00:05:54 +00:00
|
|
|
}
|