summaryrefslogtreecommitdiffstats
path: root/src/store/folder.ts
blob: 77d8a3b158d76e93ebc558ce752e9c4dfd763585 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'

import { AppState, ActionParams } from '../store'
import { Folder } from '../types/Folder'

export const FOLDER_ACTION_TYPES = {
	FETCH_FOLDERS: 'FETCH_FOLDERS',
	ADD_FOLDERS: 'ADD_FOLDER',
	DELETE_FOLDER: 'DELETE_FOLDER',
}

export const FOLDER_MUTATION_TYPES = {
	SET_FOLDERS: 'SET_FOLDERS',
	DELETE_FOLDER: 'DELETE_FOLDER',
}

const state = {
	folders: [],
}

const getters = {
	folders(state: AppState) {
		return state.folders
	},
}

const folderUrl = generateUrl('/apps/news/folders')

export const actions = {
	async [FOLDER_ACTION_TYPES.FETCH_FOLDERS]({ commit }: ActionParams) {
		const folders = await axios.get(
			generateUrl('/apps/news/folders'),
		)

		commit(FOLDER_MUTATION_TYPES.SET_FOLDERS, folders.data.folders)
	},
	async [FOLDER_ACTION_TYPES.ADD_FOLDERS]({ commit }: ActionParams, { folder }: { folder: Folder}) {
		const response = await axios.post(folderUrl, { folderName: folder.name })
		commit(FOLDER_MUTATION_TYPES.SET_FOLDERS, response.data.folders)
	},
	async [FOLDER_ACTION_TYPES.DELETE_FOLDER]({ commit }: ActionParams, { folder }: { folder: Folder}) {
		/**
      this.getByFolderId(folderId).forEach(function (feed) {
          promises.push(self.reversiblyDelete(feed.id, false, true));
      });
      this.updateUnreadCache();
		 */
		await axios.delete(folderUrl + '/' + folder.id)
		commit(FOLDER_MUTATION_TYPES.DELETE_FOLDER, folder)
	},
	// loadFolder({commit}) {
	//     console.log('loading folders')
	//     axios.get(folderUrl).then(
	//         response => {
	//             commit('addFolders', response.data.folders);
	//             axios.get(feedUrl).then(
	//                 response => commit('addFeeds', response.data.feeds)
	//             )
	//         }
	//     )
	// },
}

export const mutations = {
	[FOLDER_MUTATION_TYPES.SET_FOLDERS](state: AppState, folders: Folder[]) {
		folders.forEach(it => {
			it.feedCount = 0
			it.feeds = []
			state.folders.push(it)
		})
	},
	[FOLDER_MUTATION_TYPES.DELETE_FOLDER](state: AppState, folder: Folder) {
		const index = state.folders.indexOf(folder)
		state.folders.splice(index, 1)
	},
}

export default {
	state,
	getters,
	actions,
	mutations,
}