Skip to content

Commit

Permalink
feat(snippets): get all snipptes, set snippets by folder id or alias
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov committed Apr 1, 2022
1 parent ad19f12 commit 313301c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/renderer/components/sidebar/TheSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
:key="i.name"
:icon="i.icon"
:system="true"
:is-selected="i.alias === folderStore.selectedAlias"
@click="onClickSystemFolder(i.alias)"
>
{{ i.name }}
</SidebarListItem>
Expand Down Expand Up @@ -51,7 +53,7 @@

<script setup lang="ts">
import { computed, ref } from 'vue'
import type { SidebarSystemFolder, Tab, Tabs } from './types'
import type { SidebarSystemFolder, SystemFolderAlias, Tab, Tabs } from './types'
import Inbox from '~icons/unicons/inbox'
import Favorite from '~icons/unicons/favorite'
import Archive from '~icons/unicons/archive'
Expand Down Expand Up @@ -93,9 +95,11 @@ const activeTab = ref<Tab>('library')
const onClickFolder = async (id: string) => {
folderStore.selectId(id)
await snippetStore.getSnippetsByFolderIds(folderStore.selectedIds as string[])
const firstSnippetId = snippetStore.snippetsNonDeleted[0]?.id
snippetStore.getSnippetsById(firstSnippetId)
snippetStore.setSnippetsByFolderIds()
}
const onClickSystemFolder = (alias: SystemFolderAlias) => {
snippetStore.setSnippetsByAlias(alias)
}
const onUpdate = () => {
Expand Down
56 changes: 51 additions & 5 deletions src/renderer/store/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import type { Language } from '@/components/editor/types'
import type { SystemFolderAlias } from '@/components/sidebar/types'
import { useApi } from '@/composable'
import { store } from '@/electron'
import type { Snippet, SnippetContent } from '@@/types/db'
import type { Folder, Snippet, SnippetContent } from '@@/types/db'
import { defineStore } from 'pinia'
import { useFolderStore } from './folders'
import type { SnippetWithFolder, State } from './types/snippets'

export const useSnippetStore = defineStore('snippets', {
state: () =>
({
all: [],
snippets: [],
snippet: undefined,
fragment: 0
} as State),

getters: {
snippetsNonDeleted: state =>
state.snippets.filter(i => !i.isDeleted) as SnippetWithFolder[],
snippetsDeleted: state =>
state.snippets.filter(i => !i.isDeleted) as SnippetWithFolder[],
selectedId: state => state.snippet?.id,
currentContent: state =>
state.snippet?.content?.[state.fragment]?.value || undefined,
Expand All @@ -32,6 +30,18 @@ export const useSnippetStore = defineStore('snippets', {
},

actions: {
async getSnippets () {
// Почему то не работает _expand
const { data } = await useApi('/snippets?_expand=folder`').get().json()
const { data: folderData } = await useApi('/folders').get().json()

// Поэтому добавляем folder самостоятельно
this.all = data.value.map((i: SnippetWithFolder) => {
const folder = folderData.value.find((f: Folder) => f.id === i.folderId)
if (folder) i.folder = folder
return i
})
},
async getSnippetsByFolderIds (ids: string[]) {
const snippets: SnippetWithFolder[] = []

Expand Down Expand Up @@ -146,6 +156,42 @@ export const useSnippetStore = defineStore('snippets', {

await this.patchSnippetsById(this.selectedId!, body)
await this.getSnippetsById(this.selectedId!)
},
async setSnippetsByFolderIds () {
const folderStore = useFolderStore()
await this.getSnippetsByFolderIds(folderStore.selectedIds!)
this.snippet = this.snippets[0]
},
setSnippetsByAlias (alias: SystemFolderAlias) {
const folderStore = useFolderStore()

let snippets: SnippetWithFolder[] = []

if (alias === 'inbox') {
snippets = this.all.filter(i => !i.folderId && !i.isDeleted)
}

if (alias === 'all') {
snippets = this.all.filter(i => !i.isDeleted)
}

if (alias === 'favorites') {
snippets = this.all.filter(i => i.isFavorites && !i.isDeleted)
}

if (alias === 'trash') {
snippets = this.all.filter(i => i.isDeleted)
}

this.snippets = snippets
this.snippet = snippets[0]

folderStore.selectedId = undefined
folderStore.selectedAlias = alias

store.app.set('selectedFolderAlias', alias)
store.app.delete('selectedFolderId')
store.app.delete('selectedFolderIds')
}
}
})
1 change: 1 addition & 0 deletions src/renderer/store/types/snippets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface SnippetWithFolder extends Snippet {
}

export interface State {
all: SnippetWithFolder[]
snippets: SnippetWithFolder[]
snippet: Snippet | undefined
fragment: number
Expand Down

0 comments on commit 313301c

Please sign in to comment.