Skip to content
Snippets Groups Projects
Commit 391b72b1 authored by Alexander Madsen's avatar Alexander Madsen
Browse files

CE-2081: Convert useRecord

parent 4892bca3
No related branches found
No related tags found
2 merge requests!407CE-2141: 3.0.0,!363CE-2081: Convert useRecord
...@@ -372,13 +372,6 @@ const emptyRecordListResponse = { ...@@ -372,13 +372,6 @@ const emptyRecordListResponse = {
recordList: [] recordList: []
}; };
export function useRecord(name, onError) {
const api = useContext(apiContext);
const method = useCallAndUnpack(api.apis.Records.getRecord, unpackRecord);
const boundMethod = useCallback(method.bind(null, { name: name }), [name]);
return useAsync({ fcn: boundMethod, onError: onError });
}
export function unpackLogin(loginResponse) { export function unpackLogin(loginResponse) {
return { ...loginResponse }; return { ...loginResponse };
} }
......
import React, { useEffect, useCallback, useState, useContext } from "react"; import React, {
import { useRecord } from "../../api/SwaggerApi"; useEffect,
useCallback,
useState,
useContext,
useMemo
} from "react";
import { import {
Paper, Paper,
IconButton, IconButton,
...@@ -22,17 +27,39 @@ import { useNavigate } from "react-router-dom"; ...@@ -22,17 +27,39 @@ import { useNavigate } from "react-router-dom";
import { onFetchEntityError } from "../../components/common/Helper"; import { onFetchEntityError } from "../../components/common/Helper";
import NotFoundView from "../../components/navigation/NotFoundView/NotFoundView"; import NotFoundView from "../../components/navigation/NotFoundView/NotFoundView";
import { apiContext } from "../../api/DeployApi";
import { useAPIMethod } from "@ess-ics/ce-ui-common";
export function RecordDetailsView() { export function RecordDetailsView() {
const { name } = useParams(); const { name } = useParams();
const decodedName = decodeURIComponent(name); const decodedName = decodeURIComponent(name);
const [notFoundError, setNotFoundError] = useState(); const [error, setError] = useState(null);
const [record /* getRecord*/ /* reset*/, , , recordLoading] = useRecord(
decodedName, const client = useContext(apiContext);
(m, s) => {
// handle error code 404, and define custom message on the UI because BE sends back generic error message const params = useMemo(
onFetchEntityError(m, s, setNotFoundError); () => ({
} name: decodedName
}),
[decodedName]
); );
const {
value: record,
error: fetchError,
loading: recordLoading,
dataReady
} = useAPIMethod({
fcn: client.apis.Records.getRecord,
params
});
useEffect(() => {
if (fetchError) {
onFetchEntityError(fetchError?.message, fetchError?.status, setError);
}
}, [fetchError]);
const navigate = useNavigate(); const navigate = useNavigate();
const { setTitle } = useContext(GlobalAppBarContext); const { setTitle } = useContext(GlobalAppBarContext);
...@@ -112,9 +139,9 @@ export function RecordDetailsView() { ...@@ -112,9 +139,9 @@ export function RecordDetailsView() {
return ( return (
<RootContainer> <RootContainer>
{notFoundError ? ( {error ? (
<NotFoundView /> <NotFoundView />
) : recordLoading ? ( ) : recordLoading || !dataReady ? (
<LinearProgress color="primary" /> <LinearProgress color="primary" />
) : ( ) : (
<Paper> <Paper>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment