Skip to content

Commit b26074e

Browse files
committed
refactor(desk): add useItemStore
1 parent 00a4f73 commit b26074e

File tree

10 files changed

+205
-159
lines changed

10 files changed

+205
-159
lines changed

packages/desktop/modules/collection/components/CActions.vue

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useView } from '@/modules/view/composables/use-view'
1111
import CActionsColumns from './CActionsColumns.vue'
1212
import CActionsOrder from './CActionsOrder.vue'
1313
import CActionsFilters from './CActionsFilters.vue'
14+
import { useItemStore } from '@/modules/item/store'
1415
1516
const CActionsGallery = defineAsyncComponent(() => import('./CActionsGallery.vue'))
1617
const CActionsTable = defineAsyncComponent(() => import('./CActionsTable.vue'))
@@ -63,8 +64,22 @@ watch(
6364
6465
// refresh
6566
67+
// items
68+
69+
let itemsStore = useItemStore(props.collectionId)
70+
71+
watch(
72+
() => props.collectionId,
73+
async (id) => {
74+
itemsStore = useItemStore(id)
75+
76+
await itemsStore.load()
77+
},
78+
{ immediate: true }
79+
)
80+
6681
async function refresh() {
67-
await store.item.setItems(props.collectionId, true)
82+
await itemsStore.load(true)
6883
6984
await store.column.set(props.collectionId, true)
7085
}
@@ -107,7 +122,7 @@ function isCommon(v: View): v is ViewCommon {
107122
<v-icon name="search" />
108123
</v-btn>
109124

110-
<v-btn text size="sm" @click="refresh">
125+
<v-btn text size="sm" :loading="itemsStore.loading" @click="refresh">
111126
<v-icon name="rotate" />
112127
</v-btn>
113128

packages/desktop/modules/collection/components/CFilterRelation.vue

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import get from 'lodash/get'
33
import set from 'lodash/set'
44
55
import Column from '@core/entities/column'
6-
import Item from '@core/entities/item'
76
import { ViewFilter } from '@core/entities/view-common'
87
9-
import { computed, ref } from 'vue'
8+
import { computed } from 'vue'
109
import { useVModel } from 'vue-wind/composables/v-model'
1110
import { operations } from '../composables/filter'
12-
import { useStore } from '@/store/global'
11+
import { useItemStore } from '@/modules/item/store'
1312
1413
const props = defineProps({
1514
modelValue: {
@@ -44,18 +43,7 @@ const options = Object.keys(operations.relation).map((key) => ({
4443
}))
4544
4645
// Relation
47-
const store = useStore()
48-
49-
const relation = ref({
50-
items: [] as Item[],
51-
})
52-
53-
store.item
54-
.list({
55-
collectionId: props.column.collectionId,
56-
})
57-
.then((r) => (relation.value.items = r.data))
58-
.catch(() => (relation.value.items = []))
46+
const store = useItemStore(props.column.collectionId)
5947
</script>
6048
<template>
6149
<div class="flex w-full">
@@ -71,7 +59,7 @@ store.item
7159
<v-select
7260
v-model="model.value"
7361
:placeholder="$t('value')"
74-
:options="relation.items"
62+
:options="store.items"
7563
:label-key="column.displayField || 'id'"
7664
class="w-full"
7765
value-key="id"

packages/desktop/modules/collection/components/CGallery.vue

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import { debounce } from 'lodash'
32
import { computed, onUnmounted, ref, useAttrs, watch } from 'vue'
43
import { useRouter } from 'vue-router'
54
@@ -18,7 +17,8 @@ import { createPayload } from '../composables/filter'
1817
import { withView } from '@/modules/collection-column/composables/with-view'
1918
import { useView } from '@/modules/view/composables/use-view'
2019
import { useHooks, Events } from '@/plugins/hooks'
21-
import { useItems } from '@/modules/item/composables/items'
20+
import { useItemStore } from '@/modules/item/store'
21+
import { withViewIterations } from '@/modules/view/composables'
2222
2323
// Props & Emits
2424
const props = defineProps({
@@ -62,7 +62,21 @@ const visibleColumns = computed(() => view.value.columns.filter((c) => !c.hide).
6262
6363
// items
6464
65-
const { items } = useItems(props.collectionId, view)
65+
let itemsStore = useItemStore(props.collectionId)
66+
67+
const items = computed(() => withViewIterations(itemsStore.items, view.value))
68+
69+
watch(
70+
() => props.collectionId,
71+
async (id) => {
72+
itemsStore = useItemStore(id)
73+
74+
if (!itemsStore.items.length) {
75+
await itemsStore.load()
76+
}
77+
},
78+
{ immediate: true }
79+
)
6680
6781
// thumbnail
6882
const hooks = useHooks()
@@ -96,7 +110,7 @@ onUnmounted(() => hooks.off('item:updated', onItemUpdated))
96110
async function create() {
97111
const item = new Item(createPayload(view.value?.filters, collection?.columns))
98112
99-
await store.item.create(props.collectionId, item)
113+
await itemsStore.create(item)
100114
}
101115
102116
// on item click
@@ -124,6 +138,7 @@ function onClick(item: Item) {
124138
:items="items"
125139
:columns="columns"
126140
:sizes="view.sizes"
141+
:limit="view.limit"
127142
item-key="id"
128143
class="py-4"
129144
v-bind="bindings.gallery"
@@ -162,7 +177,7 @@ function onClick(item: Item) {
162177
size="xs"
163178
color="danger"
164179
dark
165-
@click="store.item.destroy(collectionId, data.item.id)"
180+
@click="itemsStore.destroy(data.item.id)"
166181
>
167182
<v-icon name="trash" class="mr-2" />
168183
{{ $t('deleteEntity', [$t('item')]) }}

packages/desktop/modules/collection/components/CTable.vue

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import { withOnlyView, withView } from '@/modules/collection-column/composables/
1818
1919
import { withViewIterations } from '@/modules/view/composables'
2020
import { useView } from '@/modules/view/composables/use-view'
21-
import { Events, useHooks } from '@/plugins/hooks'
2221
23-
import { useItems } from '@/modules/item/composables/items'
24-
import { createViewStore } from '@/modules/view/store'
22+
import { useItemStore } from '@/modules/item/store'
2523
2624
const IValue = defineAsyncComponent(() => import('@/modules/item/components/IValue.vue'))
2725
const CActions = defineAsyncComponent(() => import('./CActions.vue'))
@@ -123,14 +121,29 @@ function resizeColumn(id: string, width: number) {
123121
}
124122
125123
// items
126-
const { items } = useItems(props.collectionId, view)
124+
125+
let itemsStore = useItemStore(props.collectionId)
126+
127+
const items = computed(() => withViewIterations(itemsStore.items, view.value))
128+
129+
watch(
130+
() => props.collectionId,
131+
async (id) => {
132+
itemsStore = useItemStore(id)
133+
134+
if (!itemsStore.items.length) {
135+
await itemsStore.load()
136+
}
137+
},
138+
{ immediate: true }
139+
)
127140
128141
// create item
129142
130143
async function create() {
131144
const item = new Item(createPayload(view.value?.filters, collection.value?.columns))
132145
133-
await store.item.create(props.collectionId, item)
146+
await itemsStore.create(item)
134147
}
135148
</script>
136149

@@ -247,7 +260,7 @@ async function create() {
247260
size="xs"
248261
color="danger"
249262
dark
250-
@click="store.item.destroy(collectionId, data.item.id)"
263+
@click="itemsStore.destroy(data.item.id)"
251264
>
252265
<v-icon name="trash" class="mr-2" />
253266
{{ $t('deleteEntity', [$t('item')]) }}

packages/desktop/modules/entry/pages/ESingle.vue

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,32 @@ async function setView() {
6565
6666
const { path, type } = entry.value
6767
68+
store.layout.toolbar = true
69+
70+
if (type === 'directory') {
71+
store.layout.toolbar = false
72+
current.value = 'folder'
73+
return
74+
}
75+
6876
if (/.(md)/.test(path)) {
6977
current.value = 'markdown'
78+
return
7079
}
7180
7281
if (/.(jpg|jpeg)/.test(path)) {
7382
current.value = 'image'
83+
return
7484
}
7585
7686
if (/.(json|txt|csv|html)/.test(path)) {
7787
current.value = 'editor'
88+
return
7889
}
7990
if (/.(js|ts)/.test(path)) {
8091
current.value = 'code'
92+
return
8193
}
82-
83-
if (type === 'directory') {
84-
current.value = 'folder'
85-
}
86-
87-
store.layout.toolbar = current.value !== 'folder'
8894
}
8995
9096
watch(() => entry.value?.path, setView, { deep: true })

packages/desktop/modules/item/components/IValueRelation.vue

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<script setup lang="ts">
2-
import { watch, ref } from 'vue'
3-
4-
import Item from '@core/entities/item'
2+
import { watch } from 'vue'
53
64
import { createValue } from '@/modules/item/composables/value'
7-
import { useStore } from '@/store/global'
5+
import { useItemStore } from '../store'
86
97
const props = defineProps({
108
collectionId: {
@@ -25,26 +23,28 @@ const props = defineProps({
2523
2624
const { payload, column, onLoaded, save } = createValue(props)
2725
28-
// options
29-
const store = useStore()
30-
31-
const options = ref<Item[]>([])
26+
await new Promise<void>((resolve) => onLoaded(resolve))
3227
33-
async function setOptions() {
34-
if (!column.value) return
28+
// options
29+
let store = useItemStore(column.value!.collectionId)
3530
36-
options.value = await store.item.list(column.value.collectionId)
37-
}
31+
watch(
32+
column,
33+
async () => {
34+
if (!column.value) return
3835
39-
watch(column, setOptions)
36+
store = useItemStore(column.value.collectionId)
4037
41-
await new Promise<void>((resolve) => onLoaded(resolve))
38+
await store.load()
39+
},
40+
{ immediate: true }
41+
)
4242
</script>
4343

4444
<template>
4545
<v-select
4646
v-model="payload"
47-
:options="options"
47+
:options="store.items"
4848
:clear-value="() => null"
4949
:label-key="column?.displayField"
5050
value-key="id"

packages/desktop/modules/item/composables/value.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { computed, ref, watch } from 'vue'
2-
import debounce from 'lodash/debounce'
3-
4-
import Column from '@core/entities/column'
5-
import Item from '@core/entities/item'
62

73
import { useStore } from '@/store/global'
4+
import { useItemStore } from '../store'
85

96
interface Params {
107
collectionId: string
@@ -13,26 +10,24 @@ interface Params {
1310
}
1411

1512
export function createValue<T = string | undefined>({ collectionId, columnId, itemId }: Params) {
16-
// column
13+
const store = useStore()
1714

18-
const column = ref<Column | null>(null)
15+
const column = computed(() => store.column.get(collectionId, columnId))
1916

2017
async function setColumn() {
21-
await store.column
22-
.show(collectionId, columnId)
23-
.then((r) => (column.value = r))
24-
.catch(() => (column.value = null))
18+
await store.column.set(collectionId)
2519
}
2620

2721
// item
2822

29-
const item = ref<Item | null>(null)
23+
const itemStore = useItemStore(collectionId)
24+
25+
const item = computed(() => itemStore.get(itemId))
3026

3127
async function setItem() {
32-
await store.item
33-
.show(collectionId, itemId)
34-
.then((r) => (item.value = r))
35-
.catch(() => (item.value = null))
28+
if (!itemStore.items.length) {
29+
await itemStore.load()
30+
}
3631
}
3732

3833
// payload
@@ -45,20 +40,24 @@ export function createValue<T = string | undefined>({ collectionId, columnId, it
4540
payload.value = item.value[column.value.field]
4641
}
4742

43+
watch(() => {
44+
if (!column.value || !item.value) return
45+
46+
return item.value[column.value.field]
47+
}, setPayload)
48+
4849
async function save() {
4950
if (!column.value || !item.value) return
5051

5152
const data = {
5253
[column.value.field]: payload.value,
5354
}
5455

55-
await store.item.update(collectionId, itemId, data)
56+
await itemStore.update(itemId, data)
5657
}
5758

5859
// load
5960

60-
const store = useStore()
61-
6261
const loading = ref(false)
6362

6463
loading.value = true

0 commit comments

Comments
 (0)