summaryrefslogtreecommitdiffstats
path: root/src/store/feed.ts
blob: 9a0da520b3ebc7c841575873be969e375fe32d33 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import axios from '@nextcloud/axios'

import { ActionParams, AppState } from '../store'
import { Feed } from '../types/Feed'
import { API_ROUTES } from '../types/ApiRoutes'
import { FOLDER_MUTATION_TYPES, FEED_MUTATION_TYPES } from '../types/MutationTypes'

export const FEED_ACTION_TYPES = {
	ADD_FEED: 'ADD_FEED',
	FETCH_FEEDS: 'FETCH_FEEDS',
}

const state = {
	feeds: [],
}

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

export const actions = {
	async [FEED_ACTION_TYPES.FETCH_FEEDS]({ commit }: ActionParams) {
		const feeds = await axios.get(API_ROUTES.FEED)

		commit(FEED_MUTATION_TYPES.SET_FEEDS, feeds.data.feeds)
	},
	async [FEED_ACTION_TYPES.ADD_FEED](
		{ commit }: ActionParams,
		{ feedReq }: {
			feedReq: {
				url: string;
				folder?: { id: number; name?: string; },
				user?: string;
				password?: string;
			}
		},
	) {
		let url = feedReq.url.trim()
		if (!url.startsWith('http')) {
			url = 'https://' + url
		}

		let folderId
		if (feedReq.folder?.id === undefined && feedReq.folder?.name && feedReq.folder?.name !== '') {
			const response = await axios.post(API_ROUTES.FOLDER, { folderName: feedReq.folder?.name })
			folderId = response.data.folders[0].id
			commit(FOLDER_MUTATION_TYPES.SET_FOLDERS, response.data.folders)
		} else {
			folderId = feedReq.folder?.id || 0
		}

		const feed: Feed = {
			url,
			folderId,
			title: undefined, // TODO: let user define feed title on create?
			unreadCount: 0,
			autoDiscover: undefined, // TODO: autodiscover?
		}

		// Check that url is resolvable
		try {
			const response = await axios.post(API_ROUTES.FEED, {
				url: feed.url,
				parentFolderId: feed.folderId,
				title: null,
				user: feedReq.user ? feedReq.user : null,
				password: feedReq.password ? feedReq.password : null,
				fullDiscover: feed.autoDiscover,
			})

			commit(FEED_MUTATION_TYPES.ADD_FEED, response.data.feeds[0])
		} catch (e) {
			// TODO: show error to user if failure
			console.log(e)

		}
	},
}

export const mutations = {
	[FEED_MUTATION_TYPES.SET_FEEDS](state: AppState, feeds: Feed[]) {
		feeds.forEach(it => {
			state.feeds.push(it)
		})
	},
	[FEED_MUTATION_TYPES.ADD_FEED](state: AppState, feed: Feed) {
		state.feeds.push(feed)
	},
}

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