Skip to content

Commit

Permalink
feat: add star col to support favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin3go committed Feb 21, 2024
1 parent 548a177 commit 9be90de
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
58 changes: 52 additions & 6 deletions components/home/List.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="list-container">
<v-card >
<v-card>
<template v-slot:text>
<v-text-field
v-model="search"
Expand All @@ -27,8 +27,7 @@
<v-data-table
v-model:expanded="expanded"
:headers="headers"
:items="data"

:items="dataList"
item-value="prompt"
>
<template v-slot:item.prompt="{ item }">
Expand All @@ -37,7 +36,15 @@
</div>
</template>
<template v-slot:item.homepage="{ item }">
<v-btn class="text-capitalize" color="primary" variant="text" :href="item.homepage" target="_blank"> Go to Page </v-btn>
<v-btn
class="text-capitalize"
color="primary"
variant="text"
:href="item.homepage"
target="_blank"
>
Go to Page
</v-btn>
</template>
<template v-slot:item.video="{ item }">
<v-card class="my-2" elevation="2" rounded>
Expand All @@ -52,6 +59,24 @@
></iframe>
</v-card>
</template>
<template v-slot:item.star="{ item }">
<ClientOnly>
<v-checkbox
v-model="item.star"
:messages="item.star ? 'starred' : 'star it'"
false-icon="mdi-star-outline"
true-icon="mdi-star"
@update:modelValue="() => starItem(item)"
></v-checkbox>
<template #placeholder>
<v-checkbox
:model-value="false"
messages="star it"
false-icon="mdi-star-outline"
></v-checkbox>
</template>
</ClientOnly>
</template>
</v-data-table>
</v-card>
</div>
Expand All @@ -67,14 +92,35 @@ const headers = ref([
key: "prompt",
},
{ title: "Author", sortable: false, key: "author" },
{ title: "HomePage", sortable: false, align: "center", key: "homepage" },
{ title: "Publish", width: "120px", key: "publish" },
{ title: "HomePage", sortable: false, align: "center", key: "homepage" },
{ title: "Star", key: "star" },
{ title: "Preview", sortable: false, key: "video" },
]);
const { data } = await useFetch("/api/sora", { query: { search } });
const { data: soraData }: { data: any } = await useFetch("/api/sora", {
query: { search },
});
const { data: topWords } = await useFetch("/api/topWords");
const store = useMyFavoritesListStore();
const dataList = computed(() => {
const favoriteSet = new Set(
store.favoritesList.map((item: any) => item.prompt)
);
return soraData?.value?.map((item: any) => ({
...item,
star: favoriteSet.has(item.prompt),
}));
});
function starItem(item: any) {
if (item.star) {
store.addFavorite(item);
} else {
store.removeFavorite(item);
}
}
</script>

<style></style>
27 changes: 25 additions & 2 deletions stores/favoritesList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import { defineStore } from 'pinia'

interface IFavoritesListItem {
prompt: string;
author: string;
homepage: string;
video: string;
publish: string;
}

export const useMyFavoritesListStore = defineStore({
id: 'myFavoritesListStore',
state: () => ({
favoritesList: [],
favoritesList: [] as IFavoritesListItem[],
}),
actions: {},
actions: {
addFavorite(item: IFavoritesListItem) {
const index = this.favoritesList.findIndex((i) => i.prompt === item.prompt)
if (index === -1) {
this.favoritesList.push(item)
return true;
}
else return false;
},
removeFavorite(item: IFavoritesListItem) {
const index = this.favoritesList.findIndex((i) => i.prompt === item.prompt)
if (index !== -1) {
this.favoritesList.splice(index, 1)
}
},
},
persist: {
storage: persistedState.localStorage,
},
Expand Down

0 comments on commit 9be90de

Please sign in to comment.