mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
fix: transform to formdata helper
This commit is contained in:
parent
81786bf7da
commit
ac7a8a0872
@ -47,12 +47,14 @@ export function transformToFormData(data: object): FormData {
|
|||||||
* Append value to form-data format
|
* Append value to form-data format
|
||||||
*/
|
*/
|
||||||
Object.entries(data).forEach(([key, value]) => {
|
Object.entries(data).forEach(([key, value]) => {
|
||||||
|
if (value) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
value.forEach((v, i) => formData.append(`${key}[${i}]`, v));
|
value.forEach((v, i) => formData.append(`${key}[${i}]`, v));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
formData.append(key, value);
|
formData.append(key, value);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return formData;
|
return formData;
|
||||||
|
|||||||
@ -1,23 +1,43 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Button, Menu, MenuButton, MenuList, MenuItem } from '@chakra-ui/react';
|
import { Button, Menu, MenuButton, MenuList, MenuItem, useToast } from '@chakra-ui/react';
|
||||||
import { ChevronDownIcon, DeleteIcon } from '@chakra-ui/icons';
|
import { ChevronDownIcon, DeleteIcon } from '@chakra-ui/icons';
|
||||||
import { MdRemove, MdImportExport } from 'react-icons/md';
|
import { MdRemove, MdImportExport } from 'react-icons/md';
|
||||||
|
|
||||||
import type { ICourse } from '@educt/interfaces';
|
import type { ICourse } from '@educt/interfaces';
|
||||||
|
import { CourseServiceInstance } from '@educt/services';
|
||||||
|
|
||||||
type BulkActionsMenuPropsType = {
|
type BulkActionsMenuPropsType = {
|
||||||
|
courseId: string;
|
||||||
selected: ICourse['students'];
|
selected: ICourse['students'];
|
||||||
rows: ICourse['students'];
|
rows: ICourse['students'];
|
||||||
setRows: React.Dispatch<React.SetStateAction<ICourse['students']>>;
|
setRows: React.Dispatch<React.SetStateAction<ICourse['students']>>;
|
||||||
|
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
|
||||||
|
onRemoved: (removed: ICourse['students']) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const BulkActionsMenu: React.FC<BulkActionsMenuPropsType> = ({ selected, rows, setRows }) => {
|
const BulkActionsMenu: React.FC<BulkActionsMenuPropsType> = ({
|
||||||
|
courseId,
|
||||||
|
selected,
|
||||||
|
rows,
|
||||||
|
setRows,
|
||||||
|
setIsLoading,
|
||||||
|
onRemoved,
|
||||||
|
}) => {
|
||||||
|
const toast = useToast();
|
||||||
|
|
||||||
const handleDetach = async () => {
|
const handleDetach = async () => {
|
||||||
console.log(
|
try {
|
||||||
`Detaching from course: `,
|
setIsLoading(true);
|
||||||
selected.map(s => s.id)
|
const ids = selected.map(s => s.id);
|
||||||
);
|
await CourseServiceInstance.detachStudentsList(courseId, ids);
|
||||||
|
toast({ title: 'Students removed form course.', status: 'info' });
|
||||||
setRows(rows.filter(r => selected.every(s => s.id !== r.id)));
|
setRows(rows.filter(r => selected.every(s => s.id !== r.id)));
|
||||||
|
onRemoved(selected);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
const handleDelete = async () => undefined;
|
const handleDelete = async () => undefined;
|
||||||
const handleExport = async () => undefined;
|
const handleExport = async () => undefined;
|
||||||
|
|||||||
@ -151,35 +151,43 @@ const StudentTableList: React.FC<StudentTableListPropsType> = props => {
|
|||||||
onAdded={users => setRows(prev => [...prev, ...users])}
|
onAdded={users => setRows(prev => [...prev, ...users])}
|
||||||
/>
|
/>
|
||||||
<Flex justifyContent='space-between' flexDir={{ base: 'column', lg: 'row' }}>
|
<Flex justifyContent='space-between' flexDir={{ base: 'column', lg: 'row' }}>
|
||||||
<Flex mb='2'>
|
<Flex mb='2' alignItems='center'>
|
||||||
<InputGroup mr='2' borderRadius='lg'>
|
<Box>
|
||||||
|
<InputGroup mr='2' borderRadius='lg' h='full'>
|
||||||
<InputLeftElement pointerEvents='none'>
|
<InputLeftElement pointerEvents='none'>
|
||||||
<MdSearch color='gray.300' />
|
<MdSearch color='gray.300' />
|
||||||
</InputLeftElement>
|
</InputLeftElement>
|
||||||
<Input value={search} size='sm' onChange={handleSearch} type='text' placeholder='Search for student...' />
|
<Input value={search} size='sm' onChange={handleSearch} type='text' placeholder='Search for student...' />
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
|
</Box>
|
||||||
|
{isLoading && (
|
||||||
|
<Flex alignItems='center'>
|
||||||
|
<Text fontSize='sm' ml='3' mr='3' mt='2' mb='2'>
|
||||||
|
Loading...
|
||||||
|
</Text>
|
||||||
|
<BeatLoader size={4} color='gray' />
|
||||||
|
</Flex>
|
||||||
|
)}
|
||||||
</Flex>
|
</Flex>
|
||||||
|
|
||||||
<Flex alignItems='center' justifyContent='space-between' pr={{ base: '8px', lg: '0' }}>
|
<Flex alignItems='center' justifyContent='space-between' pr={{ base: '8px', lg: '0' }}>
|
||||||
{selected.length !== 0 && (
|
{selected.length !== 0 && (
|
||||||
<>
|
<>
|
||||||
<Text mr='4'>
|
<Text mr='4'>
|
||||||
You have selected <b>{selected.length}</b> student(s).
|
You have selected <b>{selected.length}</b> student(s).
|
||||||
</Text>
|
</Text>
|
||||||
<BulkActionsMenu selected={selected} rows={rows} setRows={setRows} />
|
<BulkActionsMenu
|
||||||
|
courseId={course.id}
|
||||||
|
selected={selected}
|
||||||
|
rows={rows}
|
||||||
|
setRows={setRows}
|
||||||
|
setIsLoading={setIsLoading}
|
||||||
|
onRemoved={removed => setSelected(prev => prev.filter(s => removed.every(r => s.id !== r.id)))}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<AddButton onClick={onOpenAddStudentModal} />
|
<AddButton onClick={onOpenAddStudentModal} />
|
||||||
</Flex>
|
</Flex>
|
||||||
</Flex>
|
</Flex>
|
||||||
{isLoading && (
|
|
||||||
<Flex alignItems='center' justifyContent={'flex-end'}>
|
|
||||||
<BeatLoader size={6} color='gray' />
|
|
||||||
<Text fontSize='sm' ml='3' mt='2' mb='2'>
|
|
||||||
Loading...
|
|
||||||
</Text>
|
|
||||||
</Flex>
|
|
||||||
)}
|
|
||||||
<Table overflow='hidden' borderRadius='lg' mt='2'>
|
<Table overflow='hidden' borderRadius='lg' mt='2'>
|
||||||
<StudentTableHead onSelectAll={handleSelectAll} />
|
<StudentTableHead onSelectAll={handleSelectAll} />
|
||||||
<Tbody>
|
<Tbody>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user