-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAdminCard.tsx
More file actions
83 lines (75 loc) · 2.72 KB
/
AdminCard.tsx
File metadata and controls
83 lines (75 loc) · 2.72 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
import { useState } from 'react';
import {
Flex,
Text,
IconButton,
Switch,
useColorModeValue,
ButtonGroup,
useEditableControls,
Editable,
EditablePreview,
Input,
EditableInput,
} from '@chakra-ui/react';
import { CheckIcon, CloseIcon, DeleteIcon, EditIcon } from '@chakra-ui/icons';
import { Assignment, Location } from '@prisma/client';
import { UseTRPCMutationResult } from '@trpc/react/shared';
interface AdminCardProps {
assignmentOrLocation: Assignment | Location;
editMutation: UseTRPCMutationResult<any, any, any, any>;
handleDelete: (id: number) => Promise<void>;
}
const EditableControls = () => {
const { isEditing, getSubmitButtonProps, getCancelButtonProps, getEditButtonProps } = useEditableControls();
return isEditing ? (
<ButtonGroup ml={4} mt={1} justifyContent='center' size='sm'>
<IconButton aria-label='Confirm' icon={<CheckIcon />} {...getSubmitButtonProps()} />
<IconButton aria-label='Cancel' icon={<CloseIcon />} {...getCancelButtonProps()} />
</ButtonGroup>
) : (
<Flex ml={2} mt={1} justifyContent='center'>
<IconButton aria-label='Edit' size='sm' icon={<EditIcon />} {...getEditButtonProps()} />
</Flex>
);
};
/**
* Component which represents a single assignment or location
*/
const AdminCard = (props: AdminCardProps) => {
const { assignmentOrLocation, editMutation, handleDelete } = props;
const boxColor = useColorModeValue('gray.100', 'gray.700');
const [isChecked, setIsChecked] = useState(assignmentOrLocation.active);
const handleNameChange = (newName: string) => {
editMutation.mutateAsync({ id: assignmentOrLocation.id, name: newName, active: assignmentOrLocation.active });
};
const handleActiveChange = () => {
setIsChecked(!isChecked);
editMutation.mutateAsync({ id: assignmentOrLocation.id, name: assignmentOrLocation.name, active: !isChecked });
};
return (
<>
<Flex borderRadius={4} mb={2} flexDirection='row' p={2} backgroundColor={boxColor}>
<Editable
onSubmit={handleNameChange}
textAlign='center'
fontWeight='semibold'
display='flex'
defaultValue={assignmentOrLocation.name}
fontSize='xl'
isPreviewFocusable={false}
>
<EditablePreview />
<Input as={EditableInput} />
<EditableControls />
</Editable>
<Text fontSize='large' mt={1.5} ml={5}>
Active?
</Text>
<Switch onChange={handleActiveChange} mt={2.5} ml={3} isChecked={isChecked} />
<DeleteIcon ml={5} mt={2} onClick={() => handleDelete(assignmentOrLocation.id)} />
</Flex>
</>
);
};
export default AdminCard;