Skip to content

Commit

Permalink
removed plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sharknoon committed Nov 19, 2024
1 parent cf85eba commit 50d89d1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
18 changes: 0 additions & 18 deletions plugins/favorite-tiles.client.ts

This file was deleted.

63 changes: 58 additions & 5 deletions stores/favorite-tiles.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { defineStore } from "pinia";
import type { UnsubscribeFunc } from "pocketbase";

export const useFavoriteTilesStore = defineStore(
"favorite-tiles",
() => {
const pocketBase = usePocketBase();

const favoriteTiles = ref<number[]>([]);

function isFavorite(tile: number) {
Expand All @@ -12,13 +15,63 @@ export const useFavoriteTilesStore = defineStore(
function toggleFavoriteTile(tile: number) {
const index = favoriteTiles.value.indexOf(tile);
if (index >= 0) {
favoriteTiles.value.splice(index, 1);
removeFavoriteTile(tile);
} else {
favoriteTiles.value.push(tile);
addFavoriteTile(tile);
}
}

return { favoriteTiles, isFavorite, toggleFavoriteTile };
async function getCloudFavoriteTiles(): Promise<number[] | undefined> {
if (!pocketBase.authStore.model) return;
const user = await pocketBase
.collection("users")
.getOne(pocketBase.authStore.model.id);
if (!user) return;
return user.favorite_tiles;
}

async function setCloudFavoriteTiles(favoriteTiles: number[]) {
if (!pocketBase.authStore.model) return;
await pocketBase
.collection("users")
.update(pocketBase.authStore.model.id, {
favorite_tiles: JSON.stringify(favoriteTiles),
});
console.log("Updated favorite tiles in the cloud");
}

function addFavoriteTile(tile: number) {
favoriteTiles.value.push(tile);
getCloudFavoriteTiles().then((cloudFavoriteTiles) => {
if (!cloudFavoriteTiles) return;
if (!cloudFavoriteTiles.includes(tile)) {
setCloudFavoriteTiles([...cloudFavoriteTiles, tile]);
}
});
}

function removeFavoriteTile(tile: number) {
const index = favoriteTiles.value.indexOf(tile);
if (index >= 0) {
favoriteTiles.value.splice(index, 1);
}
getCloudFavoriteTiles().then((cloudFavoriteTiles) => {
if (!cloudFavoriteTiles) return;
const cloudIndex = cloudFavoriteTiles.indexOf(tile);
if (cloudIndex >= 0) {
cloudFavoriteTiles.splice(cloudIndex, 1);
setCloudFavoriteTiles(cloudFavoriteTiles);
}
});
}

return {
favoriteTiles,
isFavorite,
toggleFavoriteTile,
getCloudFavoriteTiles,
setCloudFavoriteTiles,
};
},
{
persist: {
Expand All @@ -33,7 +86,7 @@ export const useFavoriteTilesStore = defineStore(
const newFavoriteTiles: string = JSON.parse(vuex).favoriteTiles;
localStorage.setItem(
"favorite-tiles",
JSON.stringify({ favoriteTiles: newFavoriteTiles }),
JSON.stringify({ favoriteTiles: newFavoriteTiles })
);
localStorage.removeItem("vuex");
} catch (e) {
Expand All @@ -42,5 +95,5 @@ export const useFavoriteTilesStore = defineStore(
}
},
},
},
}
);

0 comments on commit 50d89d1

Please sign in to comment.