Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 92 additions & 84 deletions src/components/dashboard/system_options/EntityTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
} from '@chakra-ui/react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { FiArchive, FiEdit2, FiMapPin, FiTrash2, FiType } from 'react-icons/fi';
import {
IoAdd,
IoBookOutline,
IoCheckmark,
IoCloseOutline,
IoEllipsisHorizontal,
} from 'react-icons/io5';
import { IoAdd, IoBookOutline, IoEllipsisHorizontal } from 'react-icons/io5';
import { LuInfo } from 'react-icons/lu';
import AbsenceBox from '../../absences/AbsenceBox';

Expand Down Expand Up @@ -152,7 +146,7 @@
return name.trim().substring(0, maxLength).trim();
};

const handleSaveEditedItem = () => {

Check warning on line 149 in src/components/dashboard/system_options/EntityTable.tsx

View workflow job for this annotation

GitHub Actions / lint

The 'handleSaveEditedItem' function makes the dependencies of useEffect Hook (at line 322) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of 'handleSaveEditedItem' in its own useCallback() Hook
// Only require name to be present, abbreviation is now optional
if (!editingItem || !editingItem.name) return;

Expand Down Expand Up @@ -205,7 +199,7 @@
}
};

const handleAddItem = () => {

Check warning on line 202 in src/components/dashboard/system_options/EntityTable.tsx

View workflow job for this annotation

GitHub Actions / lint

The 'handleAddItem' function makes the dependencies of useEffect Hook (at line 322) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of 'handleAddItem' in its own useCallback() Hook
// Only require name to be present, abbreviation is now optional
if (!newItem.name) return;

Expand Down Expand Up @@ -301,6 +295,32 @@
);
};

// Add click outside handler
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
editingRowRef.current &&
!editingRowRef.current.contains(event.target as Node)
) {
if (editingItem) {
handleSaveEditedItem();
} else if (isAddingItem) {
// Cancel adding if name is empty - just close the form without adding
if (!newItem.name.trim()) {
handleCancelEdit();
} else {
handleAddItem();
}
}
}
};

document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [editingItem, isAddingItem, newItem, handleAddItem, handleSaveEditedItem]);

return (
<Box borderWidth="1px" borderRadius="md" overflow="hidden">
{/* Table Header */}
Expand Down Expand Up @@ -415,7 +435,6 @@
<Box
px={4}
py={2}
borderBottomWidth="1px"
_last={{ borderBottomWidth: 0 }}
ref={editingItem?.id === item.id ? editingRowRef : undefined}
bg={item.archived ? 'neutralGray.100' : 'white'}
Expand All @@ -426,11 +445,25 @@
key={item.id}
transition="background-color 0.3s ease"
height={ROW_HEIGHT}
borderColor={
editingItem?.id === item.id
? theme.colors.primaryBlue[300]
: theme.colors.neutralGray[200]
}
borderWidth={editingItem?.id === item.id ? '1px' : '0px'}
borderBottomWidth="1px"
borderRadius={editingItem?.id === item.id ? '5px' : '0px'}
position={editingItem?.id === item.id ? 'relative' : 'static'}
>
{editingItem && editingItem.id === item.id ? (
<>
<Box width={leftColumnWidth} pr={4}>
<HStack spacing={5}>
<Box
width={leftColumnWidth}
pr={4}
display="flex"
alignItems="center"
>
<HStack spacing={2} alignItems="center">
{entityType === 'subject' && (
<Box position="relative">
<Circle
Expand Down Expand Up @@ -494,6 +527,7 @@
)}
</Box>
)}
{entityType === 'subject' && <Box width={3} />}
<Input
value={editingItem.name}
onChange={(e) => {
Expand All @@ -512,9 +546,18 @@
abbreviation: newAbbreviation,
});
}}
placeholder={`${title} name`}
size="sm"
flex="1"
maxLength={maxFullNameLength}
border="none"
_focus={{ boxShadow: 'none' }}
textStyle="cellBody"
fontSize="15px"
px="0"
borderRadius="0"
height="auto"
ml={entityType !== 'subject' ? '0' : undefined}
/>
</HStack>
</Box>
Expand All @@ -538,7 +581,7 @@

setEditingItem({
...editingItem,
abbreviation: newValue,
abbreviation: newValue.trim(),
});
}}
onBlur={() => {
Expand All @@ -556,46 +599,18 @@
});
}
}}
placeholder="Abbr."
size="sm"
maxW="60px"
maxW="100px"
maxLength={maxAbbreviationLength}
border="none"
_focus={{ boxShadow: 'none' }}
textStyle="cellBody"
fontSize="15px"
px="0"
borderRadius="0"
height="auto"
/>
<HStack
spacing={0}
boxShadow="0px 0px 10px 0px rgba(0, 0, 0, 0.15)"
borderRadius="5px"
>
<Box
as="button"
p={1}
cursor="pointer"
bg="buttonBackground"
_hover={{ bg: 'neutralGray.100' }}
_active={{ bg: 'neutralGray.300' }}
borderRadius="5px 0 0 5px"
onClick={handleCancelEdit}
>
<IoCloseOutline
size={24}
color={theme.colors.neutralGray[600]}
/>
</Box>
<Box
as="button"
p={1}
cursor="pointer"
bg="buttonBackground"
_hover={{ bg: 'neutralGray.100' }}
_active={{ bg: 'neutralGray.300' }}
borderRadius="0 5px 5px 0"
onClick={handleSaveEditedItem}
>
<IoCheckmark
size={24}
color={theme.colors.neutralGray[600]}
/>
</Box>
</HStack>
</Box>
</>
) : (
Expand Down Expand Up @@ -845,9 +860,19 @@
},
}}
height={ROW_HEIGHT}
borderWidth="1px"
borderColor={theme.colors.primaryBlue[300]}
borderRadius="5px"
position="relative"
zIndex="1"
>
<Box width={leftColumnWidth} pr={4}>
<HStack spacing={5}>
<Box
width={leftColumnWidth}
pr={4}
display="flex"
alignItems="center"
>
<HStack spacing={2} alignItems="center">
{entityType === 'subject' && (
<Box position="relative">
<Circle
Expand Down Expand Up @@ -916,6 +941,7 @@
)}
</Box>
)}
{entityType === 'subject' && <Box width={3} />}
<Input
value={newItem.name}
onChange={(e) => {
Expand All @@ -935,6 +961,14 @@
size="sm"
flex="1"
maxLength={maxFullNameLength}
border="none"
_focus={{ boxShadow: 'none' }}
textStyle="cellBody"
fontSize="15px"
px="0"
borderRadius="0"
height="auto"
ml={entityType !== 'subject' ? '0' : undefined}
/>
</HStack>
</Box>
Expand Down Expand Up @@ -978,42 +1012,16 @@
}}
placeholder="Abbr."
size="sm"
maxW="60px"
maxW="100px"
maxLength={maxAbbreviationLength}
border="none"
_focus={{ boxShadow: 'none' }}
textStyle="cellBody"
fontSize="15px"
px="0"
borderRadius="0"
height="auto"
/>
<HStack
spacing={0}
boxShadow="0px 0px 10px 0px rgba(0, 0, 0, 0.15)"
borderRadius="5px"
>
<Box
as="button"
p={1}
cursor="pointer"
bg="buttonBackground"
_hover={{ bg: 'neutralGray.100' }}
_active={{ bg: 'neutralGray.300' }}
borderRadius="5px 0 0 5px"
onClick={handleCancelEdit}
>
<IoCloseOutline
size={24}
color={theme.colors.neutralGray[600]}
/>
</Box>
<Box
as="button"
p={1}
cursor="pointer"
bg="buttonBackground"
_hover={{ bg: 'neutralGray.100' }}
_active={{ bg: 'neutralGray.300' }}
borderRadius="0 5px 5px 0"
onClick={handleAddItem}
>
<IoCheckmark size={24} color={theme.colors.neutralGray[600]} />
</Box>
</HStack>
</Box>
</Box>
) : null}
Expand Down