-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/pages/Dictionary/ExportWordsDialog/ExportWordsForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Button, Checkbox, FormControlLabel, Stack } from '@mui/material'; | ||
import { useGetTagList } from '@/services/tags/hooks'; | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import { orderTagsByAbc } from '@/services/tags/utils'; | ||
import { useEffectOnce } from '@/hooks/useEffectOnce'; | ||
import { exportWordsToFile } from '@/utils/file'; | ||
|
||
export const ExportWordsForm = () => { | ||
const { data: tagList } = useGetTagList({ emptyTag: true }); | ||
const allTagIds = useMemo(() => tagList?.map((it) => it.id) ?? [], [tagList]); | ||
const orderedTagList = useMemo(() => (tagList ? orderTagsByAbc(tagList) : tagList), [tagList]); | ||
const [tagIds, setTagIds] = useState<string[]>([]); | ||
const [isDisabled, setIsDisabled] = useState(false); | ||
|
||
const toggleTag = useCallback((tagId: string) => { | ||
setTagIds((state) => { | ||
if (state.includes(tagId)) { | ||
return state.filter((val) => val !== tagId); | ||
} else { | ||
return [...state, tagId]; | ||
} | ||
}); | ||
}, []); | ||
|
||
const exportWords = useCallback(async () => { | ||
setIsDisabled(true); | ||
try { | ||
await exportWordsToFile(tagIds); | ||
} finally { | ||
setIsDisabled(false); | ||
} | ||
}, [tagIds]); | ||
|
||
useEffectOnce({ | ||
effect: () => { | ||
setTagIds(allTagIds); | ||
}, | ||
condition: () => allTagIds.length > 0, | ||
deps: [allTagIds], | ||
}); | ||
|
||
return ( | ||
<Stack spacing={2} alignItems="start" marginTop={1}> | ||
<FormControlLabel | ||
label="Все слова" | ||
control={<Checkbox checked={tagIds.length === allTagIds.length} onChange={() => setTagIds(allTagIds)} />} | ||
disabled={isDisabled} | ||
/> | ||
{orderedTagList?.map((it) => { | ||
return ( | ||
<FormControlLabel | ||
key={it.id} | ||
label={`${it.name} (${it.count})`} | ||
control={<Checkbox checked={tagIds.includes(it.id)} onChange={() => toggleTag(it.id)} />} | ||
sx={{ paddingLeft: 4 }} | ||
disabled={isDisabled} | ||
/> | ||
); | ||
})} | ||
|
||
<Button variant="contained" disabled={tagIds.length === 0 || isDisabled} onClick={exportWords}> | ||
Экспорт | ||
</Button> | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Dialog, DialogContent, DialogTitle, IconButton } from '@mui/material'; | ||
import CloseIcon from '@mui/icons-material/Close'; | ||
import { DialogProps } from '@toolpad/core/useDialogs'; | ||
import { ExportWordsForm } from './ExportWordsForm'; | ||
|
||
export const ExportWordsDialog = ({ open, onClose }: DialogProps) => { | ||
return ( | ||
<Dialog fullWidth open={open} onClose={() => onClose()}> | ||
<DialogTitle sx={{ display: 'flex', alignItems: 'center' }}> | ||
Экспортировать слова | ||
<IconButton sx={{ ml: 'auto' }} onClick={() => onClose()}> | ||
<CloseIcon /> | ||
</IconButton> | ||
</DialogTitle> | ||
<DialogContent> | ||
<ExportWordsForm /> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters