mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-25 09:16:15 -04:00
feat(tools): added favorite tool handling
This commit is contained in:
parent
8d09086e78
commit
4cd809bd0c
10 changed files with 181 additions and 51 deletions
44
src/tools/tools.store.ts
Normal file
44
src/tools/tools.store.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { get, useStorage, type MaybeRef } from '@vueuse/core';
|
||||
import { defineStore } from 'pinia';
|
||||
import type { Ref } from 'vue';
|
||||
import { toolsWithCategory } from './index';
|
||||
import type { Tool, ToolWithCategory } from './tools.types';
|
||||
|
||||
export const useToolStore = defineStore('tools', {
|
||||
state: () => ({
|
||||
favoriteToolsName: useStorage('favoriteToolsName', []) as Ref<string[]>,
|
||||
}),
|
||||
getters: {
|
||||
favoriteTools(state) {
|
||||
return state.favoriteToolsName
|
||||
.map((favoriteName) => toolsWithCategory.find(({ name }) => name === favoriteName))
|
||||
.filter(Boolean) as ToolWithCategory[]; // cast because .filter(Boolean) does not remove undefined from type
|
||||
},
|
||||
|
||||
notFavoriteTools(state): ToolWithCategory[] {
|
||||
return toolsWithCategory.filter((tool) => !state.favoriteToolsName.includes(tool.name));
|
||||
},
|
||||
|
||||
tools(): ToolWithCategory[] {
|
||||
return toolsWithCategory;
|
||||
},
|
||||
|
||||
newTools(): ToolWithCategory[] {
|
||||
return this.tools.filter(({ isNew }) => isNew);
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
addToolToFavorites({ tool }: { tool: MaybeRef<Tool> }) {
|
||||
this.favoriteToolsName.push(get(tool).name);
|
||||
},
|
||||
|
||||
removeToolFromFavorites({ tool }: { tool: MaybeRef<Tool> }) {
|
||||
this.favoriteToolsName = this.favoriteToolsName.filter((name) => get(tool).name !== name);
|
||||
},
|
||||
|
||||
isToolFavorite({ tool }: { tool: MaybeRef<Tool> }) {
|
||||
return this.favoriteToolsName.includes(get(tool).name);
|
||||
},
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue