Skip to content

Commit a709e0a

Browse files
committed
fix(frontend): refresh list on create/update
1 parent 6cf4e9b commit a709e0a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

frontend/src/app/components/SampleDialog.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type SampleDialogProps = {
1919
open: boolean;
2020
/** Setter to change modal state */
2121
setOpen: (open: boolean) => void;
22+
/** triggered when sample is created or updated */
23+
onChange: (sample: Sample) => void;
2224
}
2325

2426
type FormType =
@@ -36,7 +38,7 @@ const DEFAULT_VALUES: FormType = {
3638
/**
3739
* Sample dialog to create or edit a sample.
3840
*/
39-
export default function SampleDialog({ sample, open, setOpen }: SampleDialogProps) {
41+
export default function SampleDialog({ sample, open, setOpen, onChange }: SampleDialogProps) {
4042
const { createSample, updateSample } = useAPI()
4143
/** Check if sample is being edited, ensure at compile time sample is not undefined when return true */
4244
const isEditing = (sample: Sample|undefined): sample is Sample => {
@@ -73,6 +75,7 @@ export default function SampleDialog({ sample, open, setOpen }: SampleDialogProp
7375

7476
if (result) {
7577
setOpen(false)
78+
onChange(result)
7679
} else {
7780
alert(`Unable to ${sample ? "update" : "create"} sample, check console.`)
7881
}

frontend/src/app/page.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function Home() {
2525

2626
/** On mount */
2727
useEffect(() => {
28-
api.getPage()
28+
api.getPage(0)
2929
// Load sample from sample-id URLParam
3030
if (urlSampleId) {
3131
api.getSample(parseInt(urlSampleId, 10))
@@ -46,7 +46,7 @@ export default function Home() {
4646
}
4747

4848
const handleRefresh = async () => {
49-
await api.getPage();
49+
await api.refreshPage();
5050

5151
if (sample !== null) {
5252
api.getSample(sample.id);
@@ -74,6 +74,10 @@ export default function Home() {
7474
setCurrentSample(null)
7575
}
7676

77+
function handleSampleChange(): void {
78+
api.refreshPage()
79+
}
80+
7781
/**
7882
* Handle onClose event from Delete Dialog
7983
*/
@@ -136,12 +140,14 @@ export default function Home() {
136140
<SampleDialog
137141
open={dialogCreateOpen}
138142
setOpen={setDialogCreateOpen}
143+
onChange={handleSampleChange}
139144
/>
140145
{/** Edit Sample Dialog */}
141146
{sample && <SampleDialog
142147
sample={sample}
143148
open={dialogEditOpen}
144149
setOpen={setDialogEditOpen}
150+
onChange={handleSampleChange}
145151
/>}
146152
{/** Delete Sample Dialog */}
147153
{sample && <ConfirmationDialog

frontend/src/app/utilities/useApi.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const api = axios.create({
1616
* Hook wrapping several callbacks to interact with Biostack's API while updating the AppContext
1717
*/
1818
export function useAPI() {
19-
const state = useAppContext();
19+
const { page } = useAppContext();
2020
const dispatch = useAppDispatchContext();
2121

2222
/**
@@ -35,8 +35,8 @@ export function useAPI() {
3535
* @param limit the page item limit (default is current page.limit)
3636
*/
3737
const getPage = useCallback(async (
38-
index: number = state.page.index,
39-
limit: number = state.page.limit
38+
index: number,
39+
limit: number = page.limit
4040
) => {
4141
dispatch({ type: 'setStatus', payload: { status: 'loading' } })
4242
try {
@@ -48,7 +48,7 @@ export function useAPI() {
4848
} catch (error: any) {
4949
return handleError(error)
5050
}
51-
}, [state, dispatch, handleError])
51+
}, [page, dispatch, handleError])
5252

5353
/**
5454
* Get a sample from a given id.
@@ -108,12 +108,16 @@ export function useAPI() {
108108
}
109109
};
110110

111+
/** Refresh the current page */
112+
const refreshPage = async () => getPage(page.index)
113+
111114
return {
112115
getPage,
113116
getSample,
114117
createSample,
115118
updateSample,
116119
deleteSample,
120+
refreshPage,
117121
}
118122
}
119123

0 commit comments

Comments
 (0)