diff options
author | John Molakvoæ <skjnldsv@protonmail.com> | 2023-03-21 09:53:31 +0100 |
---|---|---|
committer | John Molakvoæ <skjnldsv@protonmail.com> | 2023-04-06 14:49:30 +0200 |
commit | 10010fc532a02958804667e1cb3acee8e9556394 (patch) | |
tree | bbb99c171e6e32b85b5aee9365ad5e9ebe68054e /apps/files/src/store | |
parent | b761039cf1946cb64898b9117d1b15dd89080451 (diff) | |
download | nextcloud-server-10010fc532a02958804667e1cb3acee8e9556394.tar.gz nextcloud-server-10010fc532a02958804667e1cb3acee8e9556394.zip |
feat(files): sorting
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/files/src/store')
-rw-r--r-- | apps/files/src/store/sorting.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/apps/files/src/store/sorting.ts b/apps/files/src/store/sorting.ts new file mode 100644 index 00000000000..b153301b76b --- /dev/null +++ b/apps/files/src/store/sorting.ts @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +/* eslint-disable */ +import { loadState } from '@nextcloud/initial-state' +import { generateUrl } from '@nextcloud/router' +import { defineStore } from 'pinia' +import Vue from 'vue' +import axios from '@nextcloud/axios' + +type direction = 'asc' | 'desc' + +const saveUserConfig = (key: string, direction: direction) => { + return axios.post(generateUrl('/apps/files/api/v1/sorting'), { + mode: key, + direction: direction as string, + }) +} + +const defaultFileSorting = loadState('files', 'defaultFileSorting', 'basename') +const defaultFileSortingDirection = loadState('files', 'defaultFileSortingDirection', 'asc') as direction + +export const useSortingStore = defineStore('sorting', { + state: () => ({ + defaultFileSorting, + defaultFileSortingDirection, + }), + + getters: { + isAscSorting: (state) => state.defaultFileSortingDirection === 'asc', + }, + + actions: { + /** + * Set the sorting key AND sort by ASC + * The key param must be a valid key of a File object + * If not found, will be searched within the File attributes + */ + setSortingBy(key: string) { + Vue.set(this, 'defaultFileSorting', key) + Vue.set(this, 'defaultFileSortingDirection', 'asc') + saveUserConfig(key, 'asc') + }, + + /** + * Toggle the sorting direction + */ + toggleSortingDirection() { + const newDirection = this.defaultFileSortingDirection === 'asc' ? 'desc' : 'asc' + Vue.set(this, 'defaultFileSortingDirection', newDirection) + saveUserConfig(this.defaultFileSorting, newDirection) + } + } +}) + |