-
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.
Merge pull request #61 from ArthurLobopro/total-backup
Total backup
- Loading branch information
Showing
20 changed files
with
818 additions
and
385 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
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,92 @@ | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { ipcRenderer } from "electron" | ||
import { useForm } from "react-hook-form" | ||
import { z } from "zod" | ||
import { SuccessModal } from "." | ||
import { api } from "../../../store/Api" | ||
import { defaultErrorHandler, hookformOnErrorFactory } from "../../ErrorHandler" | ||
import { useModal } from "../../hooks/useModal" | ||
import { FormModal } from "./FormModal" | ||
|
||
interface ExportDataModalProps { | ||
onClose: () => void | ||
} | ||
|
||
const dataSchema = z.object({ | ||
path: z.string().refine((value) => value !== "", { | ||
message: "Escolha um local para exportar o dicionário", | ||
}), | ||
compress: z.boolean().default(true), | ||
}) | ||
|
||
type data = z.infer<typeof dataSchema> | ||
|
||
function getDefaulPath(): string { | ||
return ipcRenderer.sendSync("get-path", "documents") | ||
} | ||
|
||
export function ExportDataModal(props: ExportDataModalProps) { | ||
const { setValue, watch, handleSubmit, register } = useForm({ | ||
resolver: zodResolver(dataSchema), | ||
defaultValues: { | ||
path: getDefaulPath(), | ||
compress: true, | ||
}, | ||
}) | ||
|
||
const path = watch("path") | ||
const modal = useModal() | ||
|
||
function handleExport(data: data) { | ||
try { | ||
api.exporter.exportData(data.path, data.compress) | ||
|
||
modal.open( | ||
<SuccessModal | ||
onClose={props.onClose} | ||
message="Dados exportados com sucesso!" | ||
/>, | ||
) | ||
} catch (error) { | ||
defaultErrorHandler(error, modal) | ||
} | ||
} | ||
|
||
const handleExportError = hookformOnErrorFactory(modal) | ||
|
||
function handlePickFolder() { | ||
const folder = ipcRenderer.sendSync("get-folder") | ||
if (folder === "canceled") return | ||
setValue("path", folder) | ||
} | ||
|
||
return ( | ||
<FormModal | ||
disableSubmit={path === ""} | ||
title="Exportar dicionário" | ||
onSubmit={handleSubmit(handleExport, handleExportError)} | ||
onClose={props.onClose} | ||
submitText="Exportar" | ||
> | ||
{modal.content} | ||
<div className="flex-column gap-10"> | ||
<div className="grid-column-fill-center align-center gap-10"> | ||
<span>Salvar em: </span> | ||
<input | ||
type="text" | ||
className="simple" | ||
readOnly | ||
value={path} | ||
title={path} | ||
/> | ||
<button className="simple" type="button" onClick={handlePickFolder}> | ||
Escolher pasta | ||
</button> | ||
|
||
<span>Comprimir arquivo</span> | ||
<input type="checkbox" {...register("compress")} /> | ||
</div> | ||
</div> | ||
</FormModal> | ||
) | ||
} |
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,170 @@ | ||
import { ipcRenderer } from "electron/renderer" | ||
import fs from "node:fs" | ||
import { FormEvent, useState } from "react" | ||
import { ErrorModal, SuccessModal, WarningModal } from "." | ||
import { api } from "../../../store/Api" | ||
import { | ||
backupData, | ||
backupDataSchema, | ||
} from "../../../store/ZodSchemas/exportdata" | ||
import { defaultErrorHandler } from "../../ErrorHandler" | ||
import { useModal } from "../../hooks/useModal" | ||
import { If } from "../base" | ||
import { WarningIcon } from "../icons" | ||
import { FormModal } from "./FormModal" | ||
|
||
function CheckFileContent(filePath: string) { | ||
const fileContent = fs.readFileSync(filePath, "utf-8") | ||
const json = JSON.parse(fileContent) | ||
const isValid = backupDataSchema.safeParse(json) | ||
|
||
if (isValid.success) { | ||
return { isValid: true, data: isValid.data } | ||
} | ||
|
||
return { isValid: false } | ||
} | ||
|
||
interface ImportDataModalProps { | ||
onClose: () => void | ||
} | ||
|
||
export function ImportDataModal(props: ImportDataModalProps) { | ||
const [selectedFile, setSelectedFile] = useState({ | ||
path: "", | ||
content: {} as backupData, | ||
}) | ||
|
||
const [action, setAction] = useState<"merge" | "replace">("merge") | ||
|
||
const modal = useModal() | ||
|
||
function HandlePickFile() { | ||
setSelectedFile({ | ||
path: "", | ||
content: {} as backupData, | ||
}) | ||
|
||
const file = ipcRenderer.sendSync("get-file") | ||
|
||
if (file === "canceled") { | ||
return | ||
} | ||
|
||
const check = CheckFileContent(file) | ||
|
||
if (!check.isValid) { | ||
return modal.open( | ||
<ErrorModal onClose={modal.close} message="Arquivo inválido" />, | ||
) | ||
} | ||
|
||
setSelectedFile({ | ||
path: file, | ||
content: check.data as backupData, | ||
}) | ||
|
||
setAction("merge") | ||
} | ||
|
||
const readyToImport = !!selectedFile.path | ||
|
||
function HandleSubmit(event: FormEvent) { | ||
event.preventDefault() | ||
event.stopPropagation() | ||
|
||
if (!readyToImport) { | ||
return | ||
} | ||
|
||
try { | ||
if (action === "merge") { | ||
api.importer.importData(selectedFile.content, action) | ||
|
||
modal.open( | ||
<SuccessModal | ||
message="Dados mesclados com sucesso" | ||
onClose={props.onClose} | ||
/>, | ||
) | ||
} | ||
|
||
if (action === "replace") { | ||
modal.open( | ||
<WarningModal | ||
title="Você tem certeza?" | ||
children="Você irá perder todos os dados atuais ao realizar essa ação" | ||
onClose={(res) => { | ||
modal.close() | ||
|
||
if (res) { | ||
api.importer.importData(selectedFile.content, action) | ||
|
||
modal.open( | ||
<SuccessModal | ||
message="Dados substituídos com sucesso" | ||
onClose={props.onClose} | ||
/>, | ||
) | ||
} | ||
}} | ||
/>, | ||
) | ||
} | ||
} catch (error) { | ||
defaultErrorHandler(error, modal) | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
{modal.content} | ||
<FormModal | ||
onClose={props.onClose} | ||
title="Importar Dados" | ||
submitText="Importar" | ||
disableSubmit={!readyToImport} | ||
onSubmit={HandleSubmit} | ||
> | ||
<div className="flex-column gap-10"> | ||
<span>Arquivo:</span> | ||
<div className="flex gap-10"> | ||
<input | ||
type="text" | ||
className="simple" | ||
readOnly | ||
value={selectedFile.path} | ||
/> | ||
|
||
<button className="simple" onClick={HandlePickFile} type="button"> | ||
Selecionar | ||
</button> | ||
</div> | ||
|
||
<If condition={readyToImport}> | ||
<div className="lines"> | ||
<span>Tipo de importação:</span> | ||
<select | ||
onChange={(event) => { | ||
setAction(event.target.value as "merge" | "replace") | ||
}} | ||
value={action} | ||
> | ||
<option value="merge">Mesclar</option> | ||
<option value="replace">Substituir</option> | ||
</select> | ||
</div> | ||
|
||
<If condition={action === "replace"}> | ||
<span className="warning flex gap-10 align-center"> | ||
<WarningIcon /> | ||
Ao substituir os dados, todos seus dados anteriores serão | ||
apagados. | ||
</span> | ||
</If> | ||
</If> | ||
</div> | ||
</FormModal> | ||
</> | ||
) | ||
} |
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
Oops, something went wrong.