-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh(api): generate pagesUrl from parts
Accept multiple arguments to `pagesUrl` and join them with `/` to build the url: Example: ```js api.pagesUrl({collectiveId: 10}, 'trash', 12) ``` results in `/apps/collectives/_api/10/_pages/trash/12` Signed-off-by: Max <[email protected]>
- Loading branch information
1 parent
cdf550b
commit a3ef90a
Showing
3 changed files
with
117 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,131 @@ | ||
import axios from '@nextcloud/axios' | ||
import { generateUrl } from '@nextcloud/router' | ||
|
||
// not used yet. | ||
function url(suffix = '') { | ||
return generateUrl('/apps/collectives/_api' + suffix) | ||
/** | ||
* Url for the collectives api | ||
* | ||
* @param {...any} parts - url parts to append - will be joined with `/` | ||
*/ | ||
function collectivesUrl(...parts) { | ||
const path = ['apps/collectives/_api', ...parts] | ||
.join('/') | ||
return generateUrl(path) | ||
} | ||
|
||
export function getCollectives(shareTokenParam = false) { | ||
return shareTokenParam | ||
? axios.get(url(`/p/${shareTokenParam}`)) | ||
: axios.get(url()) | ||
/** | ||
* Get all active (i.e. not trashed) collectives for the current user | ||
* | ||
* Will return the shared collective if a share token is given. | ||
* | ||
* @param {string} shareToken authentication token from the share | ||
*/ | ||
export function getCollectives(shareToken = false) { | ||
return shareToken | ||
? axios.get(collectivesUrl('p', shareToken)) | ||
: axios.get(collectivesUrl()) | ||
} | ||
|
||
/** | ||
* Get all trashed collectives for the current user | ||
*/ | ||
export function getTrashCollectives() { | ||
return axios.get(url('/trash')) | ||
return axios.get(collectivesUrl('trash')) | ||
} | ||
|
||
/** | ||
* Create a new collective with the given properties. | ||
* | ||
* @param {object} collective - properties for the new collective | ||
*/ | ||
export function newCollective(collective) { | ||
return axios.post( | ||
url(), | ||
collectivesUrl(), | ||
collective, | ||
) | ||
} | ||
|
||
export function trashCollective(id) { | ||
return axios.delete(url(`/${id}`)) | ||
/** | ||
* Trash the collective with the given id | ||
* | ||
* @param {number} collectiveId - Id of the collective to trash. | ||
*/ | ||
export function trashCollective(collectiveId) { | ||
return axios.delete(collectivesUrl(collectiveId)) | ||
} | ||
|
||
export function deleteCollective(id, circle) { | ||
let doCircle = '' | ||
if (circle) { | ||
doCircle = '?circle=1' | ||
} | ||
return axios.delete(url('/trash/' + id + doCircle)) | ||
/** | ||
* Delete the collective with the given id. | ||
* | ||
* @param {number} collectiveId - id of the collective to delete | ||
* @param {boolean} removeCircle - also remove the circle if true | ||
*/ | ||
export function deleteCollective(collectiveId, removeCircle) { | ||
const query = removeCircle ? '?circle=1' : '' | ||
return axios.delete(collectivesUrl('trash', collectiveId + query)) | ||
} | ||
|
||
export function updateCollectiveEditPermissions(id, level) { | ||
return axios.put(url(`/${id}/editLevel`), { level }) | ||
/** | ||
* 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 }) | ||
} | ||
|
||
export function updateCollectiveSharePermissions(id, level) { | ||
return axios.put(url(`/${id}/shareLevel`), { level }) | ||
/** | ||
* Set the permission level required for sharing. | ||
* | ||
* @param {number} collectiveId - id of the collective to update | ||
* @param {number} level - required level for sharing | ||
*/ | ||
export function updateCollectiveSharePermissions(collectiveId, level) { | ||
return axios.put(collectivesUrl(collectiveId, 'shareLevel'), { level }) | ||
} | ||
|
||
export function updateCollectivePageMode(id, mode) { | ||
return axios.put(url(`/${id}/pageMode`), { mode }) | ||
/** | ||
* Set the edit mode for the given collective | ||
* | ||
* @param {number} collectiveId - id of the collective to update | ||
* @param {number} mode - pageMode to use. | ||
* | ||
* Possible modes: pageModes.MODE_VIEW or pageModes.MODE_EDIT | ||
*/ | ||
export function updateCollectivePageMode(collectiveId, mode) { | ||
return axios.put(collectivesUrl(collectiveId, 'pageMode'), { mode }) | ||
} | ||
|
||
/** | ||
* Get all pages in the given context (collective or public share) | ||
* | ||
* @param {object} context - either the current collective or a share context | ||
*/ | ||
export function getPages(context) { | ||
return axios.get(pagesUrl(context)) | ||
} | ||
|
||
/** | ||
* Create a new page in the given context (collective or public share) | ||
* | ||
* @param {object} context - either the current collective or a share context | ||
* @param {object} page - properties of the new page | ||
*/ | ||
export function createPage(context, page) { | ||
const url = pagesUrl(context) + '/' + page.parentId | ||
return axios.post(url, page) | ||
return axios.post( | ||
pagesUrl(context, page.parentId), | ||
page, | ||
) | ||
} | ||
|
||
export function pagesUrl({ isPublic = false, shareTokenParam, collectiveId }) { | ||
return isPublic | ||
? url(`/p/${shareTokenParam}/_pages`) | ||
: url(`/${collectiveId}/_pages`) | ||
/** | ||
* Url for pages paths inside the given context. | ||
* | ||
* @param {object} context - either the current collective or a share context | ||
* @param {...any} parts - url parts to append. | ||
*/ | ||
export function pagesUrl(context, ...parts) { | ||
return context.isPublic | ||
? collectivesUrl('p', context.shareTokenParam, '_pages', ...parts) | ||
: collectivesUrl(context.collectiveId, '_pages', ...parts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters