-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPhysicians.jsx
More file actions
102 lines (94 loc) · 2.75 KB
/
Copy pathPhysicians.jsx
File metadata and controls
102 lines (94 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
Container,
Group,
TextInput,
Divider,
Pagination,
LoadingOverlay,
Title,
Button,
} from '@mantine/core';
import { useDebouncedCallback } from '@mantine/hooks';
import { useState } from 'react';
import { useNavigate } from 'react-router';
import { StatusCodes } from 'http-status-codes';
import { TbSearch as IconSearch } from 'react-icons/tb';
import PhysiciansTable from './PhysiciansTable';
import { usePhysicians } from './usePhysicians';
import LifelineAPI from '#app/LifelineAPI';
/**
* Physicians page component
*
*/
export default function Physicians () {
const [inputValue, setInputValue] = useState('');
const [creating, setCreating] = useState(false);
const navigate = useNavigate();
const { physicians, headers, isFetching, page, pages, setPage, setSearch } = usePhysicians();
const handleSearch = useDebouncedCallback((query) => {
setSearch(query);
}, 500);
async function handleCreatePhysician () {
try {
setCreating(true);
const res = await LifelineAPI.registerPhysician({});
if (res.status === StatusCodes.CREATED) {
const physician = await res.json();
navigate(`/physicians/${physician.id}/edit`);
return;
}
let message = 'Unable to create physician';
try {
const data = await res.json();
if (data?.message) {
message = data.message;
}
} catch (error) {
// Swallow JSON parsing errors since we already have a default message
}
throw new Error(message);
} catch (err) {
console.error(err);
alert(err.message || 'Unable to create physician');
} finally {
setCreating(false);
}
}
return (
<Container>
<Group justify='space-between' wrap='nowrap' my='sm'>
<Title order={3} mr='md'>
Physicians
</Title>
<Group gap='sm' wrap='nowrap'>
<TextInput
leftSectionPointerEvents='none'
leftSection={<IconSearch />}
placeholder='Search'
onChange={(event) => {
setInputValue(event.currentTarget.value);
handleSearch(event.currentTarget.value);
}}
value={inputValue}
/>
<Button
variant='filled'
onClick={handleCreatePhysician}
loading={creating}
loaderProps={{ type: 'dots' }}
>
Create Physician
</Button>
</Group>
</Group>
<Divider mb='xl' />
<LoadingOverlay
visible={isFetching}
zIndex={1000}
overlayProps={{ radius: 'sm', blur: 2 }}
/>
<PhysiciansTable headers={headers} data={physicians} />
<Pagination total={pages} value={page} onChange={setPage} />
</Container>
);
}