aboutsummaryrefslogtreecommitdiff
path: root/src/tools/tools.store.ts
blob: 769b4d8b7429e46afcc7b5ca021a6735a39e445f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { type MaybeRef, get, useStorage } from '@vueuse/core';
import { defineStore } from 'pinia';
import type { Ref } from 'vue';
import type { Tool, ToolWithCategory } from './tools.types';
import { toolsWithCategory } from './index';

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);
    },
  },
});