-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#383] Handle load case detail in edit case page
- Loading branch information
1 parent
c201909
commit 3ca338f
Showing
2 changed files
with
67 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,66 @@ | ||
import React from "react"; | ||
import React, { useState, useEffect } from "react"; | ||
import { Spin } from "antd"; | ||
import { CaseWrapper } from "./layout"; | ||
import { useParams } from "react-router-dom"; | ||
import { useParams, useNavigate } from "react-router-dom"; | ||
import { api } from "../../lib"; | ||
import { CurrentCaseState } from "./store"; | ||
|
||
const Case = () => { | ||
const navigate = useNavigate(); | ||
const { caseId, stepId } = useParams(); | ||
|
||
return <CaseWrapper stepId={stepId}>Case</CaseWrapper>; | ||
const [loading, setLoading] = useState(true); | ||
const currentCase = CurrentCaseState.useState((s) => s); | ||
|
||
const page = (key) => { | ||
switch (parseInt(key)) { | ||
case 1: | ||
return <>Step 1 {currentCase.name}</>; | ||
case 2: | ||
return <>Step 2</>; | ||
case 3: | ||
return <>Step 3</>; | ||
case 4: | ||
return <>Step 4</>; | ||
case 5: | ||
return <>Step 5</>; | ||
default: | ||
return navigate("/not-found"); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (caseId && !currentCase.id) { | ||
// prevent fetch the data when it's already defined | ||
api | ||
.get(`case/${caseId}`) | ||
.then((res) => { | ||
const { data } = res; | ||
CurrentCaseState.update((s) => ({ ...s, ...data })); | ||
}) | ||
.catch((e) => { | ||
console.error("Error fetching case data", e); | ||
navigate("/not-found"); | ||
}) | ||
.finally(() => { | ||
setTimeout(() => { | ||
setLoading(false); | ||
}, 100); | ||
}); | ||
} | ||
}, [caseId, currentCase, navigate]); | ||
|
||
return ( | ||
<CaseWrapper caseId={caseId} stepId={stepId}> | ||
{loading ? ( | ||
<div className="loading-container"> | ||
<Spin /> | ||
</div> | ||
) : ( | ||
page(stepId) | ||
)} | ||
</CaseWrapper> | ||
); | ||
}; | ||
|
||
export default Case; |
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