import * as React from 'react'; import {EncryptStorage} from 'storage-encryption'; import {Button, Stack, TextField} from "@mui/material"; import MDEditor from '@uiw/react-md-editor'; let encryptStorage = new EncryptStorage('test'); // TODO la clef doit venir de l'utilisateur export default function PageForm({setListPages, csrf, url, passphrase}) { const isPassphraseSet = passphrase !== null; const [content, setContent] = React.useState(""); const onSubmit = async (event: React.FormEvent) => { event.preventDefault(); encryptStorage = new EncryptStorage(passphrase); const HTMLForm : HTMLFormElement = event.currentTarget; const decryptedFormData = new FormData(HTMLForm); const encryptedFormData = new FormData(); for (const [key, value] of decryptedFormData.entries()) { encryptStorage.encrypt('uuid'+key, value); const newEncryptedString = localStorage.getItem('uuid'+key); if (newEncryptedString) { encryptedFormData.append(key, newEncryptedString); } } encryptStorage.encrypt('uuidtext', content); encryptedFormData.append("text", ""+localStorage.getItem('uuidtext')) encryptedFormData.append('_token', csrf); const response = await fetch(url, { method: 'POST', body: encryptedFormData }); const json = await response.json(); const uuid = json.uuid; for (const key of decryptedFormData.keys()) { const newEncryptedString = localStorage.getItem('uuid'+key); localStorage.setItem(uuid+key, ""+newEncryptedString); localStorage.removeItem("uuid"+key); } if (json.success) { HTMLForm.reset(); setListPages(previousList => [ { id: uuid, date: json.date, title: decryptedFormData.get("title"), content: decryptedFormData.get("text"), }, ...previousList ]); } } if (isPassphraseSet) { return (
); } return (
); }