Skip to content

Commit

Permalink
enh(api): add page related functions to the js api
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Nov 30, 2023
1 parent 864c667 commit 521a567
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 52 deletions.
183 changes: 183 additions & 0 deletions src/apis/collectives/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ export function getPages(context) {
return axios.get(pagesUrl(context))
}

/**
* Get all trashed pages in the given context.
*
* @param {object} context - either the current collective or a share context
*/
export function getTrashPages(context) {
return axios.get(pagesUrl(context, 'trash'))
}

/**
* Get a page in the given context (collective or public share)
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to retrieve
*/
export function getPage(context, pageId) {
return axios.get(pagesUrl(context, pageId))
}

/**
* Create a new page in the given context (collective or public share)
*
Expand All @@ -22,3 +41,167 @@ export function createPage(context, page) {
page,
)
}

/**
* Touch a page in the given context (collective or public share)
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to touch
*/
export function touchPage(context, pageId) {
return axios.get(pagesUrl(context, pageId, '/touch'))
}

/**
* Rename a page in the given context (collective or public share)
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to rename
* @param {string} title - New title for the page
*/
export function renamePage(context, pageId, title) {
return axios.put(
pagesUrl(context, pageId),
{ title },
)
}

/**
* Copy a page inside the given context (collective or public share)
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to copy
* @param {number} parentId - Id of the page to copy to
* @param {number} index - Index for subpage order of parent page
*/
export function copyPage(context, pageId, parentId, index) {
return axios.put(
pagesUrl(context, pageId),
{ parentId, index, copy: true },
)
}

/**
* Move a page inside the given context (collective or public share)
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to move
* @param {number} parentId - Id of the page to move to
* @param {number} index - Index for subpage order of parent page
*/
export function movePage(context, pageId, parentId, index) {
return axios.put(
pagesUrl(context, pageId),
{ parentId, index },
)
}

/**
* Copy page to another collective
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to move
* @param {number} collectiveId - Id of the new collective
* @param {number} parentId - Id of the page to move to
* @param {number} index - Index for subpage order of parent page
*/
export function copyPageToCollective(context, pageId, collectiveId, parentId, index) {
return axios.put(
pagesUrl(context, pageId, 'to', collectiveId),
{ parentId, index, copy: true },
)
}

/**
* Move page to another collective
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to move
* @param {number} collectiveId - Id of the new collective
* @param {number} parentId - Id of the page to move to
* @param {number} index - Index for subpage order of parent page
*/
export function movePageToCollective(context, pageId, collectiveId, parentId, index) {
return axios.put(
pagesUrl(context, pageId, 'to', collectiveId),
{ parentId, index },
)
}

/**
* Set emoji for a page
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to update
* @param {string} emoji - New emojie for the page
*/
export function setPageEmoji(context, pageId, emoji) {
return axios.put(
pagesUrl(context, pageId, 'emoji'),
{ emoji },
)
}

/**
* Set subpageOrder for a page
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to update
* @param {string} subpageOrder - New subpageOrdere for the page
*/
export function setPageSubpageOrder(context, pageId, subpageOrder) {
return axios.put(
pagesUrl(context, pageId, 'subpageOrder'),
{ subpageOrder },
)
}

/**
* Trash a page in the given context (collective or public share)
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to trash
*/
export function trashPage(context, pageId) {
return axios.delete(pagesUrl(context, pageId))
}

/**
* Restore the page with the given id from trash
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to restore
*/
export function restorePage(context, pageId) {
return axios.patch(pagesUrl(context, '/trash', pageId))
}

/**
* Delete the page with the given id from trash
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to trash
*/
export function deletePage(context, pageId) {
return axios.delete(pagesUrl(context, '/trash', pageId))
}

/**
* Get list of attachments for a page
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to list attachments for
*/
export function getPageAttachments(context, pageId) {
return axios.get(pagesUrl(context, pageId, 'attachments'))
}

/**
* Get list of backlinks for a page
*
* @param {object} context - either the current collective or a share context
* @param {number} pageId - Id of the page to list backlinks for
*/
export function getPageBacklinks(context, pageId) {
return axios.get(pagesUrl(context, pageId, 'backlinks'))
}
71 changes: 19 additions & 52 deletions src/store/pages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { set } from 'vue'
import { getCurrentUser } from '@nextcloud/auth'
import { getBuilder } from '@nextcloud/browser-storage'
import axios from '@nextcloud/axios'
import { generateRemoteUrl } from '@nextcloud/router'
/* eslint import/namespace: ['error', { allowComputed: true }] */
import * as sortOrders from '../util/sortOrders.js'
Expand Down Expand Up @@ -251,34 +250,6 @@ export default {
}
},

emojiUrl(_state, getters) {
return (pageId) => api.pagesUrl(getters.context, pageId, 'emoji')
},

subpageOrderUrl(_state, getters) {
return (pageId) => api.pagesUrl(getters.context, pageId, 'subpageOrder')
},

touchUrl(_state, getters) {
return api.pagesUrl(getters.context, getters.currentPage.id, 'touch')
},

attachmentsUrl(_state, getters) {
return (pageId) => api.pagesUrl(getters.context, pageId, 'attachments')
},

backlinksUrl(_state, getters) {
return (pageId) => api.pagesUrl(getters.context, pageId, 'backlinks')
},

trashIndexUrl(_state, getters) {
return api.pagesUrl(getters.context, 'trash')
},

trashActionUrl(_state, getters) {
return (pageId) => api.pagesUrl(getters.context, 'trash', pageId)
},

pageTitle(state, getters) {
return pageId => {
const page = state.pages.find(p => p.id === pageId)
Expand Down Expand Up @@ -519,7 +490,7 @@ export default {
*/
async [GET_TRASH_PAGES]({ commit, getters }) {
commit('load', 'pageTrash')
const response = await axios.get(getters.trashIndexUrl)
const response = await api.getTrashPages(getters.context)
commit(SET_TRASH_PAGES, response.data.data)
commit('done', 'pageTrash')
},
Expand All @@ -534,7 +505,7 @@ export default {
* @param {number} pageId Page ID
*/
async [GET_PAGE]({ commit, getters, state }, pageId) {
const response = await axios.get(api.pagesUrl(getters.context, pageId))
const response = await api.getPage(getters.context, pageId)
commit(UPDATE_PAGE, response.data.data)
},

Expand All @@ -558,7 +529,7 @@ export default {
},

/**
* Create a new page
* Create a new template page
*
* @param {object} store the vuex store
* @param {Function} store.commit commit changes
Expand Down Expand Up @@ -587,7 +558,7 @@ export default {
* @param {object} store.getters getters of the store
*/
async [TOUCH_PAGE]({ commit, getters }) {
const response = await axios.get(getters.touchUrl)
const response = await api.touchPage(getters.context, getters.currentPage.id)
commit(UPDATE_PAGE, response.data.data)
},

Expand All @@ -600,8 +571,7 @@ export default {
* @param {string} newTitle new title for the page
*/
async [RENAME_PAGE]({ commit, getters }, newTitle) {
const url = api.pagesUrl(getters.context, getters.currentPage.id)
const response = await axios.put(url, { title: newTitle })
const response = await api.renamePage(getters.context, getters.currentPage.id, newTitle)
await commit(UPDATE_PAGE, response.data.data)
},

Expand Down Expand Up @@ -630,9 +600,8 @@ export default {
index += 1
}

const url = api.pagesUrl(getters.context, pageId)
try {
await axios.put(url, { index, parentId: newParentId, copy: true })
await api.copyPage(getters.context, pageId, newParentId, index)
// Reload the page list to make new page appear
await dispatch(GET_PAGES, false)
} finally {
Expand Down Expand Up @@ -669,9 +638,8 @@ export default {
page.parentId = newParentId
commit(UPDATE_PAGE, page)

const url = api.pagesUrl(getters.context, pageId)
try {
const response = await axios.put(url, { index, parentId: newParentId })
const response = api.movePage(getters.context, pageId, newParentId, index)
commit(UPDATE_PAGE, response.data.data)
} catch (e) {
commit(UPDATE_PAGE, pageClone)
Expand Down Expand Up @@ -704,8 +672,7 @@ export default {
async [COPY_PAGE_TO_COLLECTIVE]({ commit, getters, state, dispatch }, { collectiveId, newParentId, pageId, index }) {
commit('load', 'pagelist')

const url = api.pagesUrl(getters.context, pageId, 'to', collectiveId)
await axios.put(url, { index, parentId: newParentId, copy: true })
await api.copyPageToCollective(getters.context, pageId, collectiveId, newParentId, index)
commit('done', 'pagelist')
},

Expand All @@ -728,8 +695,7 @@ export default {
const page = { ...state.pages.find(p => p.id === pageId) }
const hasSubpages = getters.visibleSubpages(pageId).length > 0

const url = api.pagesUrl(getters.context, pageId, 'to', collectiveId)
await axios.put(url, { index, parentId: newParentId })
await api.movePageToCollective(getters.context, pageId, collectiveId, newParentId, index)
commit(REMOVE_PAGE, page)
commit('done', 'pagelist')

Expand All @@ -752,7 +718,7 @@ export default {
*/
async [SET_PAGE_EMOJI]({ commit, getters }, { pageId, emoji }) {
commit('load', `pageEmoji-${pageId}`)
const response = await axios.put(getters.emojiUrl(pageId), { emoji })
const response = await api.setPageEmoji(getters.context, pageId, emoji)
commit(UPDATE_PAGE, response.data.data)
commit('done', `pageEmoji-${pageId}`)
},
Expand Down Expand Up @@ -781,9 +747,10 @@ export default {
commit(UPDATE_PAGE, page)

try {
const response = await axios.put(
getters.subpageOrderUrl(pageId),
{ subpageOrder: JSON.stringify(subpageOrder) },
const response = await api.setPageSubpageOrder(
getters.context,
pageId,
JSON.stringify(subpageOrder),
)
commit(UPDATE_PAGE, response.data.data)
} catch (e) {
Expand All @@ -804,7 +771,7 @@ export default {
* @param {number} page.pageId ID of the page
*/
async [TRASH_PAGE]({ commit, getters }, { pageId }) {
const response = await axios.delete(api.pagesUrl(getters.context, pageId))
const response = await api.trashPage(getters.context, pageId)
commit(MOVE_PAGE_INTO_TRASH, response.data.data)
},

Expand All @@ -818,7 +785,7 @@ export default {
* @param {number} page.pageId ID of the page to restore
*/
async [RESTORE_PAGE]({ commit, getters }, { pageId }) {
const response = await axios.patch(getters.trashActionUrl(pageId))
const response = await api.restorePage(getters.context, pageId)
commit(RESTORE_PAGE_FROM_TRASH, response.data.data)
},

Expand All @@ -832,7 +799,7 @@ export default {
* @param {number} page.pageId ID of the page to delete
*/
async [DELETE_PAGE]({ commit, getters }, { pageId }) {
axios.delete(getters.trashActionUrl(pageId))
await api.deletePage(getters.context, pageId)
commit(DELETE_PAGE_FROM_TRASH_BY_ID, pageId)
},

Expand All @@ -845,7 +812,7 @@ export default {
* @param {object} page Page to get attachments for
*/
async [GET_ATTACHMENTS]({ commit, getters }, page) {
const response = await axios.get(getters.attachmentsUrl(page.id))
const response = await api.getPageAttachments(getters.context, page.id)
commit(SET_ATTACHMENTS, { attachments: response.data.data })
},

Expand All @@ -858,7 +825,7 @@ export default {
* @param {object} page Page to get backlinks for
*/
async [GET_BACKLINKS]({ commit, getters }, page) {
const response = await axios.get(getters.backlinksUrl(page.id))
const response = await api.getPageBacklinks(getters.context, page.id)
commit(SET_BACKLINKS, { pages: response.data.data })
},

Expand Down

0 comments on commit 521a567

Please sign in to comment.