1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-24 05:54:18 +00:00
pds-2021-g2-nest/nest_frontend/hooks/useBackendResource.js

150 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-05-20 10:16:01 +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"
/**
* An hook which allows access to a full REST resource (retrieve, edit, delete).
*
* @param resourcePath - The path of the resource file.
* @param allowViews - An object with maps views to a boolean detailing if they're allowed in the viewset or not.
*/
2021-05-20 10:16:01 +00:00
export default function useBackendResource(
resourcePath,
{
retrieve: allowRetrieve = true,
edit: allowEdit = true,
destroy: allowDestroy = true,
action: allowAction = false,
} = {},
) {
2021-05-20 10:16:01 +00:00
const { abort, running, apiRequest } = useBackendRequest()
const [firstLoad, setFirstLoad] = useState(false)
const [resource, setResource] = useState(null)
const [error, setError] = useState(null)
const apiRetrieve = useCallback(
async (init) => {
2021-05-20 10:16:01 +00:00
if(!allowRetrieve) {
throw new ViewNotAllowedError("retrieve")
}
2021-05-23 16:22:30 +00:00
return apiRequest("GET", `${resourcePath}`, undefined, init)
},
[apiRequest, allowRetrieve, resourcePath],
)
const apiEdit = useCallback(
async (data, init) => {
2021-05-20 10:16:01 +00:00
if(!allowEdit) {
throw new ViewNotAllowedError("edit")
}
2021-05-23 16:22:30 +00:00
return apiRequest("PUT", `${resourcePath}`, data, init)
},
[apiRequest, allowEdit, resourcePath],
)
const apiDestroy = useCallback(
async (init) => {
2021-05-20 10:16:01 +00:00
if(!allowDestroy) {
throw new ViewNotAllowedError("destroy")
}
2021-05-23 16:22:30 +00:00
return apiRequest("DELETE", `${resourcePath}`, undefined, init)
},
[apiRequest, allowDestroy, resourcePath],
)
const apiAction = useCallback(
async (method, command, data, init) => {
2021-05-20 10:16:01 +00:00
if(!allowAction) {
throw new ViewNotAllowedError("action")
}
2021-05-23 16:22:30 +00:00
return apiRequest(method, `${resourcePath}/${command}`, data, init)
},
2021-05-20 10:16:01 +00:00
[apiRequest, allowAction, resourcePath],
)
const retrieveResource = useCallback(
async (pk) => {
let refreshedResource
try {
refreshedResource = await apiRetrieve(pk)
}
catch(e) {
setError(e)
2021-05-24 03:02:07 +00:00
return
}
setError(null)
2021-05-24 03:02:07 +00:00
setResource(refreshedResource)
return refreshedResource
},
[apiRetrieve],
)
const editResource = useCallback(
async (pk, data) => {
let editedResource
try {
editedResource = await apiEdit(pk, data)
}
catch(e) {
setError(e)
2021-05-24 03:02:07 +00:00
return
}
setError(null)
2021-05-24 03:02:07 +00:00
setResource(editedResource)
return editedResource
},
[apiEdit],
)
const destroyResource = useCallback(
async (pk) => {
try {
await apiDestroy(pk)
}
catch(e) {
setError(e)
2021-05-24 03:02:07 +00:00
return
}
setError(null)
2021-05-24 03:02:07 +00:00
setResource(null)
return null
},
[apiDestroy],
)
useEffect(
async () => {
if(allowRetrieve && !firstLoad && !running) {
// noinspection JSIgnoredPromiseFromCall
await retrieveResource()
setFirstLoad(true)
}
},
[retrieveResource, firstLoad, running, allowRetrieve],
)
return {
abort,
resource,
running,
firstLoad,
error,
apiRequest,
allowRetrieve,
apiRetrieve,
retrieveResource,
allowEdit,
apiEdit,
editResource,
allowDestroy,
apiDestroy,
destroyResource,
apiAction,
}
}