2019-09-24 15:18:29 +00:00
|
|
|
import axios from 'nextcloud-axios'
|
|
|
|
import { generateUrl } from 'nextcloud-router'
|
|
|
|
import AppGlobal from '../mixins/AppGlobal'
|
|
|
|
import { mutations } from './mutations'
|
2019-08-29 09:05:29 +00:00
|
|
|
|
2019-09-24 15:18:29 +00:00
|
|
|
const BATCH_SIZE = 42
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
ADD_ALL_BOOKMARKS: 'ADD_ALL_BOOKMARKS',
|
|
|
|
CREATE_BOOKMARK: 'CREATE_BOOKMARK',
|
|
|
|
FIND_BOOKMARK: 'FIND_BOOKMARK',
|
|
|
|
DELETE_BOOKMARK: 'DELETE_BOOKMARK',
|
|
|
|
OPEN_BOOKMARK: 'OPEN_BOOKMARK',
|
|
|
|
SAVE_BOOKMARK: 'SAVE_BOOKMARK',
|
|
|
|
MOVE_BOOKMARK: 'MOVE_BOOKMARK',
|
|
|
|
IMPORT_BOOKMARKS: 'IMPORT_BOOKMARKS',
|
2019-10-05 21:24:45 +00:00
|
|
|
DELETE_BOOKMARKS: 'DELETE_BOOKMARKS',
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
LOAD_TAGS: 'LOAD_TAGS',
|
|
|
|
RENAME_TAG: 'RENAME_TAG',
|
|
|
|
DELETE_TAG: 'DELETE_TAG',
|
|
|
|
|
|
|
|
LOAD_FOLDERS: 'LOAD_FOLDERS',
|
|
|
|
CREATE_FOLDER: 'CREATE_FOLDER',
|
|
|
|
SAVE_FOLDER: 'SAVE_FOLDER',
|
|
|
|
DELETE_FOLDER: 'DELETE_FOLDER',
|
|
|
|
|
|
|
|
MOVE_SELECTION: 'MOVE_SELECTION',
|
|
|
|
|
|
|
|
RELOAD_VIEW: 'RELOAD_VIEW',
|
|
|
|
|
|
|
|
NO_FILTER: 'NO_FILTER',
|
|
|
|
FILTER_BY_RECENT: 'FILTER_BY_RECENT',
|
|
|
|
FILTER_BY_UNTAGGED: 'FILTER_BY_UNTAGGED',
|
|
|
|
FILTER_BY_TAGS: 'FILTER_BY_TAGS',
|
|
|
|
FILTER_BY_FOLDER: 'FILTER_BY_FOLDER',
|
|
|
|
FILTER_BY_SEARCH: 'FILTER_BY_SEARCH',
|
|
|
|
FETCH_PAGE: 'FETCH_PAGE',
|
|
|
|
|
|
|
|
SET_SETTING: 'SET_SETTING',
|
|
|
|
LOAD_SETTING: 'LOAD_SETTING',
|
|
|
|
LOAD_SETTINGS: 'SLOAD_SETTINGS'
|
2019-09-24 15:18:29 +00:00
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
[actions.ADD_ALL_BOOKMARKS]({ commit }, bookmarks) {
|
|
|
|
for (const bookmark of bookmarks) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.ADD_BOOKMARK, bookmark)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async [actions.FIND_BOOKMARK]({ commit, dispatch, state }, link) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-08-29 09:05:29 +00:00
|
|
|
try {
|
2019-09-09 16:17:02 +00:00
|
|
|
const response = await axios.get(url('/bookmark'), {
|
|
|
|
params: {
|
2019-08-29 09:05:29 +00:00
|
|
|
url: link
|
2019-09-09 16:17:02 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
const {
|
|
|
|
data: { data: bookmarks, status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
if (!bookmarks.length) return
|
|
|
|
commit(mutations.ADD_BOOKMARK, bookmarks[0])
|
|
|
|
return bookmarks[0]
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to find existing bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[actions.CREATE_BOOKMARK]({ commit, dispatch, state }, data) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'createBookmark' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.post(url('/bookmark'), {
|
|
|
|
url: data.url,
|
|
|
|
title: data.title,
|
|
|
|
description: data.description,
|
|
|
|
folders: data.folders,
|
|
|
|
tags: data.tags
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { item: bookmark, status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-10-05 21:41:30 +00:00
|
|
|
throw new Error(response.data.data.join('\n'))
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.DISPLAY_NEW_BOOKMARK, false)
|
|
|
|
commit(mutations.ADD_BOOKMARK, bookmark)
|
|
|
|
return dispatch(actions.OPEN_BOOKMARK, bookmark.id)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'createBookmark')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.SAVE_BOOKMARK]({ commit, dispatch, state }, id) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'saveBookmark' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.put(url(`/bookmark/${id}`), this.getters.getBookmark(id))
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to save bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'saveBookmark')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
async [actions.MOVE_BOOKMARK](
|
|
|
|
{ commit, dispatch, state },
|
|
|
|
{ bookmark, oldFolder, newFolder }
|
|
|
|
) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'moveBookmark' })
|
2019-08-29 09:05:29 +00:00
|
|
|
try {
|
|
|
|
let response = await axios.post(
|
|
|
|
url(`/folder/${newFolder}/bookmarks/${bookmark}`)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
let response2 = await axios.delete(
|
|
|
|
url(`/folder/${oldFolder}/bookmarks/${bookmark}`)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response2.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response2.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to move bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
} finally {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'moveBookmark')
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[actions.OPEN_BOOKMARK]({ commit }, id) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_SIDEBAR, { type: 'bookmark', id })
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2019-09-09 16:17:02 +00:00
|
|
|
async [actions.DELETE_BOOKMARK]({ commit, dispatch, state }, { id, folder }) {
|
2019-08-29 09:05:29 +00:00
|
|
|
if (folder) {
|
|
|
|
try {
|
|
|
|
const response = await axios.delete(
|
|
|
|
url(`/folder/${folder}/bookmarks/${id}`)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.REMOVE_BOOKMARK, id)
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
return
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
try {
|
2019-09-24 15:18:29 +00:00
|
|
|
const response = await axios.delete(url(`/bookmark/${id}`))
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.REMOVE_BOOKMARK, id)
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[actions.IMPORT_BOOKMARKS]({ commit, dispatch, state }, file) {
|
2019-09-24 15:18:29 +00:00
|
|
|
var data = new FormData()
|
|
|
|
data.append('bm_import', file)
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.post(url(`/bookmark/import`), data)
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
if (response.status === 413) {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error('Selected file is too large')
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-10-05 21:24:45 +00:00
|
|
|
throw new Error(response.data.data.join('\n'))
|
2019-08-29 09:05:29 +00:00
|
|
|
} else {
|
2019-10-05 21:24:45 +00:00
|
|
|
const status = response.data.status
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', err.message)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.DELETE_BOOKMARKS]({ commit, dispatch, state }) {
|
|
|
|
return axios
|
|
|
|
.delete(url(`/bookmark`))
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
return dispatch(actions.LOAD_FOLDERS)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', err.message)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.RENAME_TAG]({ commit, dispatch, state }, { oldName, newName }) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'tag' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.put(url(`/tag/${oldName}`), {
|
|
|
|
name: newName
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
return dispatch(actions.LOAD_TAGS)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'tag')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.LOAD_TAGS]({ commit, dispatch, state }, link) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'tags' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.get(url('/tag'), { params: { count: true } })
|
|
|
|
.then(response => {
|
2019-09-24 15:18:29 +00:00
|
|
|
const { data: tags } = response
|
|
|
|
return commit(mutations.SET_TAGS, tags)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load tags')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'tags')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.DELETE_TAG]({ commit, dispatch, state }, tag) {
|
|
|
|
return axios
|
|
|
|
.delete(url(`/tag/${tag}`))
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
dispatch(actions.LOAD_TAGS)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.LOAD_FOLDERS]({ commit, dispatch, state }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'folders' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.get(url('/folder'), { params: {} })
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { data, status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const folders = data
|
|
|
|
return commit(mutations.SET_FOLDERS, folders)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load folders')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'folders')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.DELETE_FOLDER]({ commit, dispatch, state }, id) {
|
|
|
|
return axios
|
|
|
|
.delete(url(`/folder/${id}`))
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
dispatch(actions.LOAD_FOLDERS)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete folder')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.CREATE_FOLDER](
|
|
|
|
{ commit, dispatch, state },
|
|
|
|
{ parentFolder, title }
|
|
|
|
) {
|
|
|
|
return axios
|
|
|
|
.post(url(`/folder`), {
|
|
|
|
parent_folder: parentFolder,
|
|
|
|
title
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.DISPLAY_NEW_FOLDER, false)
|
|
|
|
dispatch(actions.LOAD_FOLDERS)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create folder')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.SAVE_FOLDER]({ commit, dispatch, state }, id) {
|
2019-09-24 15:18:29 +00:00
|
|
|
const folder = this.getters.getFolder(id)[0]
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'saveFolder' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.put(url(`/folder/${id}`), {
|
|
|
|
parent_folder: folder.parent_folder,
|
|
|
|
title: folder.title
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create folder')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'saveFolder')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async [actions.MOVE_SELECTION]({ commit, dispatch, state }, folderId) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'moveSelection' })
|
2019-08-29 09:05:29 +00:00
|
|
|
try {
|
|
|
|
for (const folder of state.selection.folders) {
|
|
|
|
if (folderId === folder.id) {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error('Cannot move folder into itself')
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
folder.parent_folder = folderId
|
|
|
|
await dispatch(actions.SAVE_FOLDER, folder.id)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const bookmark of state.selection.bookmarks) {
|
|
|
|
await dispatch(actions.MOVE_BOOKMARK, {
|
|
|
|
oldFolder: bookmark.folders[bookmark.folders.length - 1], // FIXME This is veeeery ugly and will cause issues. Inevitably.
|
|
|
|
newFolder: folderId,
|
|
|
|
bookmark: bookmark.id
|
2019-09-24 15:18:29 +00:00
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to move parts of selection')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
} finally {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'moveSelection')
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.RELOAD_VIEW]({ state, dispatch, commit }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, state.fetchState.query)
|
|
|
|
dispatch(actions.FETCH_PAGE)
|
|
|
|
dispatch(actions.LOAD_FOLDERS)
|
|
|
|
dispatch(actions.LOAD_TAGS)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.NO_FILTER]({ dispatch, commit }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, {})
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_RECENT]({ dispatch, commit }, search) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { sortby: 'lastmodified' })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_SEARCH]({ dispatch, commit }, search) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { search: search.split(' ') })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_TAGS]({ dispatch, commit }, tags) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { tags, conjunction: 'and' })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_UNTAGGED]({ dispatch, commit }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { untagged: true })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_FOLDER]({ dispatch, commit }, folder) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { folder })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FETCH_PAGE]({ dispatch, commit, state }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.fetchState.reachedEnd) return
|
2019-10-05 21:13:26 +00:00
|
|
|
let canceled = false
|
|
|
|
commit(mutations.FETCH_START, {
|
|
|
|
type: 'bookmarks',
|
|
|
|
cancel() {
|
|
|
|
canceled = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
axios
|
2019-08-29 09:05:29 +00:00
|
|
|
.get(url('/bookmark'), {
|
|
|
|
params: {
|
|
|
|
limit: BATCH_SIZE,
|
|
|
|
page: state.fetchState.page,
|
|
|
|
sortby: state.settings.sorting,
|
|
|
|
...state.fetchState.query
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(response => {
|
2019-10-05 21:13:26 +00:00
|
|
|
if (canceled) return
|
2019-08-29 09:05:29 +00:00
|
|
|
const {
|
|
|
|
data: { data, status }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const bookmarks = data
|
|
|
|
commit(mutations.INCREMENT_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (bookmarks.length < BATCH_SIZE) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.REACHED_END)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
return dispatch(actions.ADD_ALL_BOOKMARKS, bookmarks)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.t('bookmarks', 'Failed to fetch bookmarks.')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-10-05 21:13:26 +00:00
|
|
|
if (canceled) return
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.FETCH_END, 'bookmarks')
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.SET_SETTING]({ commit, dispatch, state }, { key, value }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_SETTING, key, value)
|
2019-09-09 16:17:02 +00:00
|
|
|
if (key === 'viewMode' && state.viewMode !== value) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_VIEW_MODE, value)
|
2019-09-09 16:17:02 +00:00
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
|
|
|
.post(url(`/settings/${key}`), {
|
|
|
|
[key]: value
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to change setting')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.LOAD_SETTING]({ commit, dispatch, state }, key) {
|
|
|
|
return axios
|
|
|
|
.get(url(`/settings/${key}`))
|
|
|
|
.then(response => {
|
|
|
|
const {
|
|
|
|
data: { [key]: value }
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
|
|
|
commit(mutations.SET_SETTING, { key, value })
|
2019-08-29 09:05:29 +00:00
|
|
|
if (key === 'viewMode' && state.viewMode !== value) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_VIEW_MODE, value)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load setting ' + key)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.LOAD_SETTINGS]({ commit, dispatch, state }) {
|
|
|
|
return Promise.all(
|
|
|
|
['sorting', 'viewMode'].map(key => dispatch(actions.LOAD_SETTING, key))
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
function url(url) {
|
2019-09-24 15:18:29 +00:00
|
|
|
url = `/apps/bookmarks${url}`
|
|
|
|
return generateUrl(url)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|