2021-05-19 17:56:41 +00:00
|
|
|
import { useCallback, useEffect, useState } from "react"
|
|
|
|
import useBackendRequest from "./useBackendRequest"
|
2021-05-24 03:02:07 +00:00
|
|
|
import { ViewNotAllowedError } from "../objects/Errors"
|
2021-05-12 02:10:36 +00:00
|
|
|
|
|
|
|
|
2021-05-19 14:47:29 +00:00
|
|
|
/**
|
|
|
|
* An hook which allows access to a full REST viewset (list, create, retrieve, edit, delete).
|
|
|
|
*
|
|
|
|
* @param resourcesPath - The path of the resource directory.
|
|
|
|
* @param pkName - The name of the primary key attribute of the elements.
|
2021-05-19 17:56:41 +00:00
|
|
|
* @param allowViews - An object with maps views to a boolean detailing if they're allowed in the viewset or not.
|
2021-05-19 14:47:29 +00:00
|
|
|
*/
|
2021-05-19 17:56:41 +00:00
|
|
|
export default function useBackendViewset(resourcesPath, pkName,
|
|
|
|
{
|
|
|
|
list: allowList = true,
|
|
|
|
create: allowCreate = true,
|
|
|
|
retrieve: allowRetrieve = true,
|
|
|
|
edit: allowEdit = true,
|
|
|
|
destroy: allowDestroy = true,
|
|
|
|
command: allowCommand = false,
|
|
|
|
action: allowAction = false,
|
2021-05-20 10:16:01 +00:00
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
const { abort, running, apiRequest } = useBackendRequest()
|
2021-05-19 17:56:41 +00:00
|
|
|
|
|
|
|
const [firstLoad, setFirstLoad] = useState(false)
|
|
|
|
const [resources, setResources] = useState([])
|
|
|
|
const [error, setError] = useState(null)
|
2021-05-12 02:10:36 +00:00
|
|
|
|
|
|
|
const apiList = useCallback(
|
2021-05-19 17:56:41 +00:00
|
|
|
async (init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowList) {
|
|
|
|
throw new ViewNotAllowedError("list")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest("GET", `${resourcesPath}`, undefined, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
|
|
|
[apiRequest, allowList, resourcesPath],
|
2021-05-12 02:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const apiRetrieve = useCallback(
|
2021-05-19 17:56:41 +00:00
|
|
|
async (id, init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowRetrieve) {
|
|
|
|
throw new ViewNotAllowedError("retrieve")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest("GET", `${resourcesPath}${id}`, undefined, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
|
|
|
[apiRequest, allowRetrieve, resourcesPath],
|
2021-05-12 02:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const apiCreate = useCallback(
|
2021-05-19 17:56:41 +00:00
|
|
|
async (data, init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowCreate) {
|
|
|
|
throw new ViewNotAllowedError("create")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest("POST", `${resourcesPath}`, data, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
|
|
|
[apiRequest, allowCreate, resourcesPath],
|
2021-05-12 02:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const apiEdit = useCallback(
|
2021-05-19 17:56:41 +00:00
|
|
|
async (id, data, init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowEdit) {
|
|
|
|
throw new ViewNotAllowedError("edit")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest("PUT", `${resourcesPath}${id}`, data, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
|
|
|
[apiRequest, allowEdit, resourcesPath],
|
2021-05-12 02:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const apiDestroy = useCallback(
|
2021-05-19 17:56:41 +00:00
|
|
|
async (id, init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowDestroy) {
|
|
|
|
throw new ViewNotAllowedError("destroy")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest("DELETE", `${resourcesPath}${id}`, undefined, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
|
|
|
[apiRequest, allowDestroy, resourcesPath],
|
2021-05-12 02:10:36 +00:00
|
|
|
)
|
|
|
|
|
2021-05-19 17:56:41 +00:00
|
|
|
const apiCommand = useCallback(
|
|
|
|
async (method, command, data, init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowCommand) {
|
|
|
|
throw new ViewNotAllowedError("command")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest(method, `${resourcesPath}${command}`, data, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
2021-05-20 10:16:01 +00:00
|
|
|
[apiRequest, allowCommand, resourcesPath],
|
2021-05-19 17:56:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const apiAction = useCallback(
|
|
|
|
async (method, id, command, data, init) => {
|
2021-05-20 10:16:01 +00:00
|
|
|
if(!allowAction) {
|
|
|
|
throw new ViewNotAllowedError("action")
|
|
|
|
}
|
2021-05-23 16:23:44 +00:00
|
|
|
return apiRequest(method, `${resourcesPath}${id}/${command}`, data, init)
|
2021-05-19 17:56:41 +00:00
|
|
|
},
|
2021-05-20 10:16:01 +00:00
|
|
|
[apiRequest, allowAction, resourcesPath],
|
2021-05-19 17:56:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const listResources = useCallback(
|
2021-05-12 02:10:36 +00:00
|
|
|
async () => {
|
2021-05-24 03:02:07 +00:00
|
|
|
let res
|
2021-05-12 02:10:36 +00:00
|
|
|
try {
|
2021-05-24 03:02:07 +00:00
|
|
|
res = await apiList()
|
2021-05-12 02:10:36 +00:00
|
|
|
}
|
|
|
|
catch(e) {
|
2021-05-19 17:56:41 +00:00
|
|
|
setError(e)
|
2021-05-24 03:02:07 +00:00
|
|
|
return
|
2021-05-12 02:10:36 +00:00
|
|
|
}
|
2021-05-19 17:56:41 +00:00
|
|
|
setError(null)
|
2021-05-24 03:02:07 +00:00
|
|
|
setResources(res)
|
2021-05-12 02:10:36 +00:00
|
|
|
},
|
|
|
|
[apiList],
|
|
|
|
)
|
|
|
|
|
2021-05-19 17:56:41 +00:00
|
|
|
const retrieveResource = useCallback(
|
2021-05-12 23:07:17 +00:00
|
|
|
async (pk) => {
|
2021-05-24 03:02:07 +00:00
|
|
|
let res
|
|
|
|
try {
|
|
|
|
res = await apiRetrieve(pk)
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
setError(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setError(null)
|
|
|
|
|
|
|
|
setResources(r => r.map(resource => {
|
2021-05-19 17:56:41 +00:00
|
|
|
if(resource[pkName] === pk) {
|
2021-05-24 03:02:07 +00:00
|
|
|
return res
|
2021-05-19 17:56:41 +00:00
|
|
|
}
|
|
|
|
return resource
|
|
|
|
}))
|
2021-05-24 03:02:07 +00:00
|
|
|
|
|
|
|
return res
|
2021-05-12 23:07:17 +00:00
|
|
|
},
|
2021-05-13 15:05:33 +00:00
|
|
|
[apiRetrieve, pkName],
|
2021-05-12 23:07:17 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 02:10:36 +00:00
|
|
|
const createResource = useCallback(
|
|
|
|
async (data) => {
|
2021-05-24 03:02:07 +00:00
|
|
|
let res
|
|
|
|
try {
|
|
|
|
res = await apiCreate(data)
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
setError(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setError(null)
|
|
|
|
|
|
|
|
setResources(r => [...r, res])
|
|
|
|
return res
|
2021-05-12 02:10:36 +00:00
|
|
|
},
|
|
|
|
[apiCreate],
|
|
|
|
)
|
|
|
|
|
|
|
|
const editResource = useCallback(
|
|
|
|
async (pk, data) => {
|
2021-05-24 03:02:07 +00:00
|
|
|
let res
|
|
|
|
try {
|
|
|
|
res = await apiEdit(pk, data)
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
setError(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setError(null)
|
|
|
|
|
|
|
|
setResources(r => r.map(resource => {
|
2021-05-19 17:56:41 +00:00
|
|
|
if(resource[pkName] === pk) {
|
2021-05-24 03:02:07 +00:00
|
|
|
return res
|
2021-05-19 17:56:41 +00:00
|
|
|
}
|
|
|
|
return resource
|
|
|
|
}))
|
2021-05-24 03:02:07 +00:00
|
|
|
return res
|
2021-05-12 02:10:36 +00:00
|
|
|
},
|
|
|
|
[apiEdit, pkName],
|
|
|
|
)
|
|
|
|
|
|
|
|
const destroyResource = useCallback(
|
|
|
|
async (pk) => {
|
2021-05-24 03:02:07 +00:00
|
|
|
try {
|
|
|
|
await apiDestroy(pk)
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
setError(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setError(null)
|
|
|
|
|
|
|
|
setResources(r => r.filter(resource => resource[pkName] !== pk))
|
2021-05-19 17:56:41 +00:00
|
|
|
return null
|
2021-05-12 02:10:36 +00:00
|
|
|
},
|
|
|
|
[apiDestroy, pkName],
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(
|
2021-05-19 17:56:41 +00:00
|
|
|
async () => {
|
|
|
|
if(allowList && !firstLoad && !running) {
|
|
|
|
await listResources()
|
|
|
|
setFirstLoad(true)
|
2021-05-12 02:10:36 +00:00
|
|
|
}
|
|
|
|
},
|
2021-05-19 17:56:41 +00:00
|
|
|
[listResources, firstLoad, running, allowList],
|
2021-05-12 02:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
abort,
|
|
|
|
resources,
|
2021-05-19 17:56:41 +00:00
|
|
|
firstLoad,
|
2021-05-12 02:10:36 +00:00
|
|
|
running,
|
2021-05-19 17:56:41 +00:00
|
|
|
error,
|
2021-05-12 23:07:17 +00:00
|
|
|
apiRequest,
|
2021-05-19 17:56:41 +00:00
|
|
|
allowList,
|
2021-05-12 02:10:36 +00:00
|
|
|
apiList,
|
2021-05-19 17:56:41 +00:00
|
|
|
listResources,
|
|
|
|
allowRetrieve,
|
2021-05-12 02:10:36 +00:00
|
|
|
apiRetrieve,
|
2021-05-19 17:56:41 +00:00
|
|
|
retrieveResource,
|
|
|
|
allowCreate,
|
2021-05-12 02:10:36 +00:00
|
|
|
apiCreate,
|
|
|
|
createResource,
|
2021-05-19 17:56:41 +00:00
|
|
|
allowEdit,
|
|
|
|
apiEdit,
|
2021-05-12 02:10:36 +00:00
|
|
|
editResource,
|
2021-05-19 17:56:41 +00:00
|
|
|
allowDestroy,
|
|
|
|
apiDestroy,
|
2021-05-12 02:10:36 +00:00
|
|
|
destroyResource,
|
2021-05-19 17:56:41 +00:00
|
|
|
apiCommand,
|
|
|
|
apiAction,
|
2021-05-12 02:10:36 +00:00
|
|
|
}
|
|
|
|
}
|