diff options
author | Devlin Junker <devlin.junker@gmail.com> | 2023-08-08 16:00:51 -0700 |
---|---|---|
committer | Benjamin Brahmer <info@b-brahmer.de> | 2023-08-11 09:22:24 +0200 |
commit | 3f1fb5b1d657cc695955736101cc949572b655a6 (patch) | |
tree | 56cd5d4e0e6e1873cdf570e97f1de7923dbe2374 | |
parent | 2287835894a4552c8247c6be85934ff9eb00ed07 (diff) |
fix add + create folder
Signed-off-by: Devlin Junker <devlin.junker@gmail.com>
-rw-r--r-- | src/store/feed.ts | 43 | ||||
-rw-r--r-- | src/store/folder.ts | 19 | ||||
-rw-r--r-- | src/store/index.ts | 5 | ||||
-rw-r--r-- | src/types/ApiRoutes.ts | 6 | ||||
-rw-r--r-- | src/types/MutationTypes.ts | 10 | ||||
-rw-r--r-- | tests/javascript/unit/store/feed.spec.ts | 4 | ||||
-rw-r--r-- | tests/javascript/unit/store/folder.spec.ts | 3 |
7 files changed, 52 insertions, 38 deletions
diff --git a/src/store/feed.ts b/src/store/feed.ts index 737b90c5a..b103b7707 100644 --- a/src/store/feed.ts +++ b/src/store/feed.ts @@ -1,19 +1,15 @@ import axios from '@nextcloud/axios' -import { generateUrl } from '@nextcloud/router' 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', } -export const FEED_MUTATION_TYPES = { - ADD_FEED: 'ADD_FEED', - SET_FEEDS: 'SET_FEEDS', -} - const state = { feeds: [], } @@ -24,41 +20,48 @@ const getters = { }, } -const feedUrl = generateUrl('/apps/news/feeds') - export const actions = { async [FEED_ACTION_TYPES.FETCH_FEEDS]({ commit }: ActionParams) { - const feeds = await axios.get( - generateUrl('/apps/news/feeds'), - ) + 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 }, user?: string; password?: string; } }) { + { 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 } - /** - if (title !== undefined) { - title = title.trim(); - } - */ + 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: feedReq.folder?.id || 0, - title: undefined, + 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(feedUrl, { + const response = await axios.post(API_ROUTES.FEED, { url: feed.url, parentFolderId: feed.folderId, title: null, diff --git a/src/store/folder.ts b/src/store/folder.ts index e6ef70341..b1ae2e59d 100644 --- a/src/store/folder.ts +++ b/src/store/folder.ts @@ -1,10 +1,10 @@ import axios from '@nextcloud/axios' -import { generateUrl } from '@nextcloud/router' import { AppState, ActionParams } from '../store' import { Folder } from '../types/Folder' import { Feed } from '../types/Feed' -import { FEED_MUTATION_TYPES } from './feed' +import { FOLDER_MUTATION_TYPES, FEED_MUTATION_TYPES } from '../types/MutationTypes' +import { API_ROUTES } from '../types/ApiRoutes' export const FOLDER_ACTION_TYPES = { FETCH_FOLDERS: 'FETCH_FOLDERS', @@ -12,11 +12,6 @@ export const FOLDER_ACTION_TYPES = { DELETE_FOLDER: 'DELETE_FOLDER', } -export const FOLDER_MUTATION_TYPES = { - SET_FOLDERS: 'SET_FOLDERS', - DELETE_FOLDER: 'DELETE_FOLDER', -} - const state = { folders: [], } @@ -27,18 +22,14 @@ const getters = { }, } -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'), - ) + const folders = await axios.get(API_ROUTES.FOLDER) 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 }) + const response = await axios.post(API_ROUTES.FOLDER, { folderName: folder.name }) commit(FOLDER_MUTATION_TYPES.SET_FOLDERS, response.data.folders) }, async [FOLDER_ACTION_TYPES.DELETE_FOLDER]({ commit }: ActionParams, { folder }: { folder: Folder}) { @@ -48,7 +39,7 @@ export const actions = { promises.push(self.reversiblyDelete(feed.id, false, true)); }); */ - await axios.delete(folderUrl + '/' + folder.id) + await axios.delete(API_ROUTES.FOLDER + '/' + folder.id) commit(FOLDER_MUTATION_TYPES.DELETE_FOLDER, folder) }, } diff --git a/src/store/index.ts b/src/store/index.ts index 1a6dfc1c9..539a1b2de 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,7 +1,8 @@ import { Folder } from '../types/Folder' import { Feed } from '../types/Feed' -import feeds, { FEED_MUTATION_TYPES, FEED_ACTION_TYPES } from './feed' -import folders, { FOLDER_MUTATION_TYPES, FOLDER_ACTION_TYPES } from './folder' +import feeds, { FEED_ACTION_TYPES } from './feed' +import folders, { FOLDER_ACTION_TYPES } from './folder' +import { FOLDER_MUTATION_TYPES, FEED_MUTATION_TYPES } from '../types/MutationTypes' export const MUTATIONS = { ...FEED_MUTATION_TYPES, diff --git a/src/types/ApiRoutes.ts b/src/types/ApiRoutes.ts new file mode 100644 index 000000000..a82c66dc1 --- /dev/null +++ b/src/types/ApiRoutes.ts @@ -0,0 +1,6 @@ +import { generateUrl } from '@nextcloud/router' + +export const API_ROUTES = { + FOLDER: generateUrl('/apps/news/folders'), + FEED: generateUrl('/apps/news/feeds') +}
\ No newline at end of file diff --git a/src/types/MutationTypes.ts b/src/types/MutationTypes.ts new file mode 100644 index 000000000..d561d1d77 --- /dev/null +++ b/src/types/MutationTypes.ts @@ -0,0 +1,10 @@ + +export const FEED_MUTATION_TYPES = { + ADD_FEED: 'ADD_FEED', + SET_FEEDS: 'SET_FEEDS', +} + +export const FOLDER_MUTATION_TYPES = { + SET_FOLDERS: 'SET_FOLDERS', + DELETE_FOLDER: 'DELETE_FOLDER', +} diff --git a/tests/javascript/unit/store/feed.spec.ts b/tests/javascript/unit/store/feed.spec.ts index 928820085..ba15c9410 100644 --- a/tests/javascript/unit/store/feed.spec.ts +++ b/tests/javascript/unit/store/feed.spec.ts @@ -1,7 +1,9 @@ import axios from '@nextcloud/axios' import { Feed } from '../../../../src/types/Feed' import { AppState } from '../../../../src/store' -import { FEED_ACTION_TYPES, FEED_MUTATION_TYPES, mutations, actions } from '../../../../src/store/feed' +import { FEED_ACTION_TYPES, mutations, actions } from '../../../../src/store/feed' + +import { FEED_MUTATION_TYPES } from '../../../../src/types/MutationTypes' jest.mock('@nextcloud/axios') diff --git a/tests/javascript/unit/store/folder.spec.ts b/tests/javascript/unit/store/folder.spec.ts index 68cc04da7..eacfd18cb 100644 --- a/tests/javascript/unit/store/folder.spec.ts +++ b/tests/javascript/unit/store/folder.spec.ts @@ -2,7 +2,8 @@ import axios from '@nextcloud/axios' import { generateUrl } from '@nextcloud/router' import { Folder } from '../../../../src/types/Folder' import { AppState } from '../../../../src/store' -import { FOLDER_ACTION_TYPES, FOLDER_MUTATION_TYPES, mutations, actions } from '../../../../src/store/folder' +import { FOLDER_ACTION_TYPES, mutations, actions } from '../../../../src/store/folder' +import { FOLDER_MUTATION_TYPES } from '../../../../src/types/MutationTypes' jest.mock('@nextcloud/axios') jest.mock('@nextcloud/router') |