Skip to content

Commit

Permalink
enh(api): use api for all collectives actions
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Nov 29, 2023
1 parent b5bb039 commit d2453e4
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 28 deletions.
97 changes: 94 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,38 @@ export function deleteCollective(collectiveId, removeCircle) {
return axios.delete(collectivesUrl('trash', collectiveId + query))
}

/**
* Restore a collective with the given id from trash
*
* @param {number} collectiveId Id of the colletive to be restored
*/
export function restoreCollective(collectiveId) {
return axios.patch(collectivesUrl('trash', collectiveId))
}

/**
* Update a collective with the given properties
*
* @param {object} collective Properties for the collective
*/
export function updateCollective(collective) {
return axios.put(
collectivesUrl(collective.id),
collective,
)
}

/**
* Set the permission level required for editing.
*
* @param {number} collectiveId - id of the collective to update
* @param {number} level - required level for editing
*/
export function updateCollectiveEditPermissions(collectiveId, level) {
return axios.put(collectivesUrl(collectiveId, 'editLevel'), { level })
return axios.put(
collectivesUrl(collectiveId, 'editLevel'),
{ level },
)
}

/**
Expand All @@ -81,7 +105,10 @@ export function updateCollectiveEditPermissions(collectiveId, level) {
* @param {number} level - required level for sharing
*/
export function updateCollectiveSharePermissions(collectiveId, level) {
return axios.put(collectivesUrl(collectiveId, 'shareLevel'), { level })
return axios.put(
collectivesUrl(collectiveId, 'shareLevel'),
{ level },
)
}

/**
Expand All @@ -93,7 +120,45 @@ export function updateCollectiveSharePermissions(collectiveId, level) {
* Possible modes: pageModes.MODE_VIEW or pageModes.MODE_EDIT
*/
export function updateCollectivePageMode(collectiveId, mode) {
return axios.put(collectivesUrl(collectiveId, 'pageMode'), { mode })
return axios.put(
collectivesUrl(collectiveId, 'pageMode'),
{ mode },
)
}

/**
* Create a public collective share
*
* @param {number} collectiveId Id of the colletive to be shared
*/
export function shareCollective(collectiveId) {
return axios.post(collectivesUrl(collectiveId, 'share'))
}

/**
* Update a public collective share
*
* @param {number} collectiveId Id of the colletive
* @param {string} shareToken Token of the share to be updated
* @param {boolean} shareEditable Is collective share editable
*/
export function updateShareCollective(collectiveId, shareToken, shareEditable) {
return axios.put(
collectivesUrl(collectiveId, 'share', shareToken),
{ editable: shareEditable },
)
}

/**
* Delete a public collective share
*
* @param {number} collectiveId Id of the colletive
* @param {string} shareToken Token of the share to be removed
*/
export function unshareCollective(collectiveId, shareToken) {
return axios.delete(
collectivesUrl(collectiveId, 'share', shareToken),
)
}

/**
Expand Down Expand Up @@ -129,3 +194,29 @@ export function pagesUrl(context, ...parts) {
? collectivesUrl('p', context.shareTokenParam, '_pages', ...parts)
: collectivesUrl(context.collectiveId, '_pages', ...parts)
}

/**
* Set the page order for the current user
*
* @param {number} collectiveId ID of the colletive to be updated
* @param {number} pageOrder the desired page order for the current user
*/
export function setCollectiveUserSettingPageOrder(collectiveId, pageOrder) {
return axios.put(
collectivesUrl(collectiveId, 'userSettings', 'pageOrder'),
{ pageOrder },
)
}

/**
* Set the the `show recent pages` toggle for the current user
*
* @param {number} collectiveId ID of the colletive to be updated
* @param {boolean} showRecentPages the desired value
*/
export function setCollectiveUserSettingShowRecentPages(collectiveId, showRecentPages) {
return axios.put(
collectivesUrl(collectiveId, 'userSettings', 'pageOrder'),
{ showRecentPages },
)
}
43 changes: 18 additions & 25 deletions src/store/collectives.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { byName } from '../util/sortOrders.js'
import randomEmoji from '../util/randomEmoji.js'
Expand Down Expand Up @@ -283,10 +282,7 @@ export default {
* @param {object} collective Properties for the collective
*/
async [UPDATE_COLLECTIVE]({ commit }, collective) {
const response = await axios.put(
generateUrl('/apps/collectives/_api/' + collective.id),
collective,
)
const response = await api.updateCollective(collective)
commit(ADD_OR_UPDATE_COLLECTIVE, response.data.data)
},

Expand All @@ -312,7 +308,7 @@ export default {
* @param {number} collective.id ID of the colletive to be restored
*/
async [RESTORE_COLLECTIVE]({ commit }, { id }) {
const response = await axios.patch(generateUrl('/apps/collectives/_api/trash/' + id))
const response = await api.restoreCollective(id)
commit(RESTORE_COLLECTIVE_FROM_TRASH, response.data.data)
},

Expand Down Expand Up @@ -343,13 +339,13 @@ export default {
*/
async [SHARE_COLLECTIVE]({ commit }, { id }) {
commit('load', 'share')
const response = await axios.post(generateUrl('/apps/collectives/_api/' + id + '/share'))
const response = await api.shareCollective(id)
commit(ADD_OR_UPDATE_COLLECTIVE, response.data.data)
commit('done', 'share')
},

/**
* Create a public collective share
* Update a public collective share
*
* @param {object} store the vuex store
* @param {Function} store.commit commit changes
Expand All @@ -360,11 +356,7 @@ export default {
*/
async [UPDATE_SHARE_COLLECTIVE]({ commit }, { id, shareToken, shareEditable }) {
commit('load', 'shareEditable')
const response = await axios.put(
generateUrl('/apps/collectives/_api/' + id + '/share/' + shareToken),
{ editable: shareEditable },

)
const response = await api.updateShareCollective(id, shareToken, shareEditable)
commit(ADD_OR_UPDATE_COLLECTIVE, response.data.data)
commit('done', 'shareEditable')
},
Expand All @@ -375,13 +367,11 @@ export default {
* @param {object} store the vuex store
* @param {Function} store.commit commit changes
* @param {object} store.getters getters of the store
* @param {object} collective the collective with id
* @param {object} collective the collective with id and shareToken
*/
async [UNSHARE_COLLECTIVE]({ commit, getters }, collective) {
commit('load', 'unshare')
const response = await axios.delete(
generateUrl('/apps/collectives/_api/' + collective.id + '/share/' + collective.shareToken),
)
const response = await api.unshareCollective(collective.id, collective.shareToken)
commit(ADD_OR_UPDATE_COLLECTIVE, response.data.data)
commit('done', 'unshare')
},
Expand Down Expand Up @@ -422,20 +412,23 @@ export default {
commit(ADD_OR_UPDATE_COLLECTIVE, response.data.data)
},

/**
* Set the page order for the current user
*
* @param {object} store the vuex store
* @param {Function} store.commit commit changes
* @param {object} data the data object
* @param {number} data.id ID of the colletive to be updated
* @param {number} data.pageOrder the desired page order for the current user
*/
async [SET_COLLECTIVE_USER_SETTING_PAGE_ORDER]({ commit }, { id, pageOrder }) {
await axios.put(
generateUrl('/apps/collectives/_api/' + id + '/_userSettings/pageOrder'),
{ pageOrder },
)
await api.setCollectiveUserSettingPageOrder(id, pageOrder)
commit(PATCH_COLLECTIVE_WITH_PROPERTY, { id, property: 'userPageOrder', value: pageOrder })
},

async [SET_COLLECTIVE_USER_SETTING_SHOW_RECENT_PAGES]({ commit, getters }, { id, showRecentPages }) {
commit(PATCH_COLLECTIVE_WITH_PROPERTY, { id, property: 'userShowRecentPages', value: showRecentPages })
await axios.put(
generateUrl('/apps/collectives/_api/' + id + '/_userSettings/showRecentPages'),
{ showRecentPages },
)
await api.setCollectiveUserSettingShowRecentPages(id, showRecentPages)
},

[MARK_COLLECTIVE_DELETED]({ commit }, collective) {
Expand Down

0 comments on commit d2453e4

Please sign in to comment.