Can import family data from UI
This commit is contained in:
@ -9,6 +9,7 @@ import getCroppedImg from "../../utils/crop_image";
|
||||
import UploadIcon from "@mui/icons-material/Upload";
|
||||
import LinkIcon from "@mui/icons-material/Link";
|
||||
import { isDebug } from "../../utils/debug_utils";
|
||||
import { selectFileToUpload } from "../../utils/files_utils";
|
||||
|
||||
export function UploadPhotoButton(p: {
|
||||
label: string;
|
||||
@ -22,39 +23,20 @@ export function UploadPhotoButton(p: {
|
||||
|
||||
const uploadPhoto = async () => {
|
||||
try {
|
||||
// Create file element
|
||||
const fileEl = document.createElement("input");
|
||||
fileEl.type = "file";
|
||||
fileEl.accept =
|
||||
ServerApi.Config.constraints.photo_allowed_types.join(",");
|
||||
fileEl.click();
|
||||
const file = await selectFileToUpload({
|
||||
allowedTypes: ServerApi.Config.constraints.photo_allowed_types,
|
||||
maxSize: ServerApi.Config.constraints.photo_max_size,
|
||||
});
|
||||
|
||||
// Wait for a file to be chosen
|
||||
await new Promise((res, _rej) =>
|
||||
fileEl.addEventListener("change", () => res(null))
|
||||
);
|
||||
if (file === null) return;
|
||||
|
||||
if ((fileEl.files?.length ?? 0) === 0) return null;
|
||||
const file = fileEl.files![0];
|
||||
|
||||
// Check file size
|
||||
if (file.size > ServerApi.Config.constraints.photo_max_size) {
|
||||
await alert(
|
||||
`Le fichier sélectionné est trop lourd ! (taille maximale acceptée : ${filesize(
|
||||
ServerApi.Config.constraints.photo_max_size
|
||||
)})`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const tempURL = URL.createObjectURL(fileEl.files![0]);
|
||||
const tempURL = URL.createObjectURL(file);
|
||||
|
||||
setImageBlob(file);
|
||||
setImageURL(tempURL);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
alert("Failed to upload custom account image!");
|
||||
return null;
|
||||
alert(`Échec de l'envoi de l'image ! (${e})`);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user