-
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.
feat: allow import/export dictionary
- Loading branch information
1 parent
9525da4
commit c3bc955
Showing
12 changed files
with
438 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true | ||
"trailingComma": "all", | ||
"singleQuote": true, | ||
"plugins": [ | ||
"prettier-plugin-tailwindcss" | ||
], | ||
"tailwindConfig": "./tailwind.config.ts" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useWordService } from '@/hooks/useWordService' | ||
import { ArrowDownToLine } from 'lucide-react' | ||
import { useCallback } from 'react' | ||
|
||
export default function ButtonExportDictionary() { | ||
// #region Services | ||
const wordService = useWordService() | ||
// #endregion | ||
|
||
// #region Callbacks | ||
const onExport = useCallback(() => { | ||
wordService.export().then((exportResponse) => { | ||
const element = document.createElement('a') | ||
const file = new Blob([exportResponse.data], { type: 'text/plain' }) | ||
element.href = URL.createObjectURL(file) | ||
element.download = `${new Date().toISOString()}+export.txt` | ||
document.body.appendChild(element) | ||
element.click() | ||
}) | ||
}, [wordService]) | ||
|
||
// #endregion | ||
|
||
return ( | ||
<button | ||
className="my-2 rounded-lg bg-white p-2 hover:bg-white/50" | ||
onClick={() => { | ||
onExport() | ||
}} | ||
> | ||
<ArrowDownToLine className="text-gray-800" /> | ||
</button> | ||
) | ||
} |
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,67 @@ | ||
import { useWordDispatch } from '@/hooks/useWord' | ||
import { useWordService } from '@/hooks/useWordService' | ||
import { ArrowUpFromLine } from 'lucide-react' | ||
import { ChangeEvent, useCallback } from 'react' | ||
|
||
export default function ButtonImportDictionary() { | ||
// #region Contexts | ||
const wordDispatch = useWordDispatch() | ||
// #endregion | ||
|
||
// #region Services | ||
const wordService = useWordService() | ||
// #endregion | ||
|
||
// #region Callbacks | ||
const onImport = useCallback( | ||
(event: ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0] | ||
if (!file) { | ||
alert('No dictionary has been added.') | ||
return | ||
} | ||
|
||
const reader = new FileReader() | ||
reader.onload = function (event) { | ||
const eventTarget = event.target | ||
if (!eventTarget?.result || typeof eventTarget.result !== 'string') { | ||
alert('It has not been possible to read the dictionary.') | ||
return | ||
} | ||
|
||
wordService | ||
.import({ data: eventTarget.result }) | ||
.then((importResponse) => { | ||
if (!importResponse.isSuccessful) { | ||
alert(importResponse.message) | ||
return | ||
} | ||
|
||
wordDispatch({ type: 'clear' }) | ||
alert('New dictionary imported successfully.') | ||
}) | ||
} | ||
|
||
reader.readAsText(file) | ||
}, | ||
[wordService, wordDispatch], | ||
) | ||
// #endregion | ||
|
||
return ( | ||
<div className="my-2 rounded-lg p-2 hover:bg-gray-300"> | ||
<label htmlFor="input--import-data"> | ||
<ArrowUpFromLine className="text-gray-500" /> | ||
</label> | ||
<input | ||
id="input--import-data" | ||
type="file" | ||
className="hidden" | ||
accept="text/plain" | ||
onChange={(ev) => { | ||
onImport(ev) | ||
}} | ||
></input> | ||
</div> | ||
) | ||
} |
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
Oops, something went wrong.