|
2 | 2 | * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later
|
4 | 4 | */
|
5 |
| -import { defineStore } from 'pinia' |
| 5 | +import type { ViewConfigs, ViewId, ViewConfig } from '../types' |
| 6 | + |
| 7 | +import { getCurrentUser } from '@nextcloud/auth' |
6 | 8 | import { emit, subscribe } from '@nextcloud/event-bus'
|
7 |
| -import { generateUrl } from '@nextcloud/router' |
8 | 9 | import { loadState } from '@nextcloud/initial-state'
|
| 10 | +import { generateUrl } from '@nextcloud/router' |
| 11 | +import { defineStore } from 'pinia' |
| 12 | +import { ref, set } from 'vue' |
9 | 13 | import axios from '@nextcloud/axios'
|
10 |
| -import Vue from 'vue' |
11 | 14 |
|
12 |
| -import type { ViewConfigs, ViewConfigStore, ViewId, ViewConfig } from '../types' |
| 15 | +const initialViewConfig = loadState('files', 'viewConfigs', {}) as ViewConfigs |
13 | 16 |
|
14 |
| -const viewConfig = loadState('files', 'viewConfigs', {}) as ViewConfigs |
| 17 | +export const useViewConfigStore = defineStore('viewconfig', () => { |
15 | 18 |
|
16 |
| -export const useViewConfigStore = function(...args) { |
17 |
| - const store = defineStore('viewconfig', { |
18 |
| - state: () => ({ |
19 |
| - viewConfig, |
20 |
| - } as ViewConfigStore), |
| 19 | + const viewConfigs = ref({ ...initialViewConfig }) |
21 | 20 |
|
22 |
| - getters: { |
23 |
| - getConfig: (state) => (view: ViewId): ViewConfig => state.viewConfig[view] || {}, |
| 21 | + /** |
| 22 | + * Get the config for a specific view |
| 23 | + * @param viewid Id of the view to fet the config for |
| 24 | + */ |
| 25 | + function getConfig(viewid: ViewId): ViewConfig { |
| 26 | + return viewConfigs.value[viewid] || {} |
| 27 | + } |
24 | 28 |
|
25 |
| - getConfigs: (state) => (): ViewConfigs => state.viewConfig, |
26 |
| - }, |
| 29 | + /** |
| 30 | + * Update the view config local store |
| 31 | + * @param viewId The id of the view to update |
| 32 | + * @param key The config key to update |
| 33 | + * @param value The new value |
| 34 | + */ |
| 35 | + function onUpdate(viewId: ViewId, key: string, value: string | number | boolean): void { |
| 36 | + if (!(viewId in viewConfigs.value)) { |
| 37 | + set(viewConfigs.value, viewId, {}) |
| 38 | + } |
| 39 | + set(viewConfigs.value[viewId], key, value) |
| 40 | + } |
27 | 41 |
|
28 |
| - actions: { |
29 |
| - /** |
30 |
| - * Update the view config local store |
31 |
| - * @param view |
32 |
| - * @param key |
33 |
| - * @param value |
34 |
| - */ |
35 |
| - onUpdate(view: ViewId, key: string, value: string | number | boolean) { |
36 |
| - if (!this.viewConfig[view]) { |
37 |
| - Vue.set(this.viewConfig, view, {}) |
38 |
| - } |
39 |
| - Vue.set(this.viewConfig[view], key, value) |
40 |
| - }, |
| 42 | + /** |
| 43 | + * Update the view config local store AND on server side |
| 44 | + * @param view Id of the view to update |
| 45 | + * @param key Config key to update |
| 46 | + * @param value New value |
| 47 | + */ |
| 48 | + async function update(view: ViewId, key: string, value: string | number | boolean): Promise<void> { |
| 49 | + if (getCurrentUser() !== null) { |
| 50 | + await axios.put(generateUrl('/apps/files/api/v1/views'), { |
| 51 | + value, |
| 52 | + view, |
| 53 | + key, |
| 54 | + }) |
| 55 | + } |
41 | 56 |
|
42 |
| - /** |
43 |
| - * Update the view config local store AND on server side |
44 |
| - * @param view |
45 |
| - * @param key |
46 |
| - * @param value |
47 |
| - */ |
48 |
| - async update(view: ViewId, key: string, value: string | number | boolean) { |
49 |
| - axios.put(generateUrl('/apps/files/api/v1/views'), { |
50 |
| - value, |
51 |
| - view, |
52 |
| - key, |
53 |
| - }) |
| 57 | + emit('files:view-config:updated', { view, key, value }) |
| 58 | + } |
54 | 59 |
|
55 |
| - emit('files:viewconfig:updated', { view, key, value }) |
56 |
| - }, |
| 60 | + /** |
| 61 | + * Set the sorting key AND sort by ASC |
| 62 | + * The key param must be a valid key of a File object |
| 63 | + * If not found, will be searched within the File attributes |
| 64 | + * @param key Key to sort by |
| 65 | + * @param view View to set the sorting key for |
| 66 | + */ |
| 67 | + function setSortingBy(key = 'basename', view = 'files'): void { |
| 68 | + // Save new config |
| 69 | + update(view, 'sorting_mode', key) |
| 70 | + update(view, 'sorting_direction', 'asc') |
| 71 | + } |
57 | 72 |
|
58 |
| - /** |
59 |
| - * Set the sorting key AND sort by ASC |
60 |
| - * The key param must be a valid key of a File object |
61 |
| - * If not found, will be searched within the File attributes |
62 |
| - * @param key Key to sort by |
63 |
| - * @param view View to set the sorting key for |
64 |
| - */ |
65 |
| - setSortingBy(key = 'basename', view = 'files') { |
66 |
| - // Save new config |
67 |
| - this.update(view, 'sorting_mode', key) |
68 |
| - this.update(view, 'sorting_direction', 'asc') |
69 |
| - }, |
| 73 | + /** |
| 74 | + * Toggle the sorting direction |
| 75 | + * @param viewId id of the view to set the sorting order for |
| 76 | + */ |
| 77 | + function toggleSortingDirection(viewId = 'files'): void { |
| 78 | + const config = viewConfigs.value[viewId] || { sorting_direction: 'asc' } |
| 79 | + const newDirection = config.sorting_direction === 'asc' ? 'desc' : 'asc' |
70 | 80 |
|
71 |
| - /** |
72 |
| - * Toggle the sorting direction |
73 |
| - * @param view view to set the sorting order for |
74 |
| - */ |
75 |
| - toggleSortingDirection(view = 'files') { |
76 |
| - const config = this.getConfig(view) || { sorting_direction: 'asc' } |
77 |
| - const newDirection = config.sorting_direction === 'asc' ? 'desc' : 'asc' |
| 81 | + // Save new config |
| 82 | + update(viewId, 'sorting_direction', newDirection) |
| 83 | + } |
78 | 84 |
|
79 |
| - // Save new config |
80 |
| - this.update(view, 'sorting_direction', newDirection) |
81 |
| - }, |
82 |
| - }, |
83 |
| - }) |
| 85 | + // Initialize event listener |
| 86 | + subscribe('files:view-config:updated', ({ view, key, value }) => onUpdate(view, key, value)) |
84 | 87 |
|
85 |
| - const viewConfigStore = store(...args) |
| 88 | + return { |
| 89 | + viewConfigs, |
86 | 90 |
|
87 |
| - // Make sure we only register the listeners once |
88 |
| - if (!viewConfigStore._initialized) { |
89 |
| - subscribe('files:viewconfig:updated', function({ view, key, value }: { view: ViewId, key: string, value: boolean }) { |
90 |
| - viewConfigStore.onUpdate(view, key, value) |
91 |
| - }) |
92 |
| - viewConfigStore._initialized = true |
| 91 | + getConfig, |
| 92 | + setSortingBy, |
| 93 | + toggleSortingDirection, |
| 94 | + update, |
93 | 95 | }
|
94 |
| - |
95 |
| - return viewConfigStore |
96 |
| -} |
| 96 | +}) |
0 commit comments