feat: ✨ added the option to overwrite or keep the existing configuration during import
This commit is contained in:
parent
c31e55560b
commit
b1feb476c0
4 changed files with 63 additions and 40 deletions
|
|
@ -231,6 +231,7 @@ return [
|
|||
"select-file" => "Drag and drop a file or click to select one",
|
||||
"allowed_format" => "Allowed format: :format",
|
||||
"rules_weight" => "Max size: :size",
|
||||
"overwrite" => "Overwrite existing data ?",
|
||||
"validation" => [
|
||||
"mimes" => "The file must be of type: :values.",
|
||||
"max_size" => "The file must not exceed :value",
|
||||
|
|
|
|||
|
|
@ -232,6 +232,7 @@ return [
|
|||
"select-file" => "Glisser-déposer un fichier ou cliquez pour en sélectionner un",
|
||||
"allowed_format" => "Format autorisé: :format",
|
||||
"rules_weight" => "Taille max: :size",
|
||||
"overwrite" => "Surcharger les données existantes ?",
|
||||
"validation" => [
|
||||
"mimes" => "Le fichier doit être de type : :values.",
|
||||
"max_size" => "Le fichier ne doit pas dépasser : :value",
|
||||
|
|
|
|||
|
|
@ -14,28 +14,51 @@ import {
|
|||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
// * Other
|
||||
import SimpleSwitch from "@/back/Components/Inputs/SimpleSwitch";
|
||||
import InputError from "@/back/Components/InputError";
|
||||
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
||||
import { cn } from "@/lib/utils";
|
||||
import str from "@/hooks/use-string";
|
||||
import useTrans from "@/hooks/use-translations";
|
||||
import InputError from "../InputError";
|
||||
|
||||
type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
|
||||
type JsonPrimitive = string | number | boolean | null;
|
||||
|
||||
type JsonObject = {
|
||||
[key: string]: JsonPrimitive | JsonObject | JsonObject[];
|
||||
};
|
||||
|
||||
type ImportConfigJsonProps = {
|
||||
onConfirm: (data: JsonValue) => void;
|
||||
onConfirm: (data: JsonObject[], overwrite: boolean) => void;
|
||||
};
|
||||
|
||||
// Zod schemas.
|
||||
const jsonObjectSchema: z.ZodType<JsonObject> = z.lazy(() =>
|
||||
z.record(
|
||||
z.string(),
|
||||
z.union([
|
||||
z.string(),
|
||||
z.number(),
|
||||
z.boolean(),
|
||||
z.null(),
|
||||
jsonObjectSchema,
|
||||
z.array(jsonObjectSchema),
|
||||
])
|
||||
)
|
||||
);
|
||||
const jsonArraySchema = z.array(jsonObjectSchema);
|
||||
|
||||
export default function ImportConfigJson({
|
||||
onConfirm,
|
||||
}: ImportConfigJsonProps) {
|
||||
// Json content.
|
||||
const [jsonText, setJsonText] = useState<JsonValue>({});
|
||||
const [jsonText, setJsonText] = useState<JsonObject[]>([]);
|
||||
|
||||
// Modale state.
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [isOpen, setIsOpen] = useState<boolean>(false);
|
||||
// Event state.
|
||||
const [isDragging, setIsDragging] = useState(false);
|
||||
const [isDragging, setIsDragging] = useState<boolean>(false);
|
||||
// Overwrite state.
|
||||
const [overwrite, setOverwrite] = useState<boolean>(false);
|
||||
|
||||
// Local errors.
|
||||
const [globalErrors, setGlobalErrors] = useState<Record<string, string>>({});
|
||||
|
|
@ -47,18 +70,6 @@ export default function ImportConfigJson({
|
|||
// Maximum file size accepted (2MB).
|
||||
const maxSize = 2 * 1024 * 1024;
|
||||
|
||||
// JSON valide: string | number | boolean | null | array | object
|
||||
const jsonValueSchema: z.ZodType<JsonValue> = z.lazy(() =>
|
||||
z.union([
|
||||
z.string(),
|
||||
z.number(),
|
||||
z.boolean(),
|
||||
z.null(),
|
||||
z.array(jsonValueSchema),
|
||||
z.record(z.string(), jsonValueSchema),
|
||||
])
|
||||
);
|
||||
|
||||
/**
|
||||
* Get translation of specific key.
|
||||
*
|
||||
|
|
@ -135,7 +146,7 @@ export default function ImportConfigJson({
|
|||
const parsed = JSON.parse(text);
|
||||
|
||||
// Validate json with zod.
|
||||
jsonValueSchema.parse(parsed);
|
||||
jsonArraySchema.parse(parsed);
|
||||
|
||||
setJsonText(parsed);
|
||||
setGlobalErrors({});
|
||||
|
|
@ -178,8 +189,8 @@ export default function ImportConfigJson({
|
|||
const handleConfirm = (): void => {
|
||||
try {
|
||||
// Validate json with zod.
|
||||
jsonValueSchema.parse(jsonText);
|
||||
onConfirm(jsonText);
|
||||
jsonArraySchema.parse(jsonText);
|
||||
onConfirm(jsonText, overwrite);
|
||||
resetComponent();
|
||||
} catch {
|
||||
setEditorErrors(prev => ({ ...prev, 'editor-error': useTrans('back.json_editor.validation.invalid_content_json') }));
|
||||
|
|
@ -194,9 +205,10 @@ export default function ImportConfigJson({
|
|||
const resetComponent = (): void => {
|
||||
setIsOpen(false);
|
||||
setTimeout(() => {
|
||||
setJsonText(null);
|
||||
setJsonText([]);
|
||||
setGlobalErrors({});
|
||||
setEditorErrors({});
|
||||
setOverwrite(false);
|
||||
}, 200);
|
||||
}
|
||||
|
||||
|
|
@ -261,7 +273,7 @@ export default function ImportConfigJson({
|
|||
{/* JSON editor */}
|
||||
<JsonEditor
|
||||
data={jsonText}
|
||||
setData={(val) => setJsonText(val as JsonValue)}
|
||||
setData={(val) => setJsonText(val as JsonObject[])}
|
||||
icons={icons}
|
||||
translations={translations}
|
||||
showIconTooltips={true}
|
||||
|
|
@ -271,22 +283,30 @@ export default function ImportConfigJson({
|
|||
/>
|
||||
</div>
|
||||
<InputError message={editorErrors["editor-error"]} />
|
||||
<DialogFooter>
|
||||
<BtnOptionnalLinkWithTooltip
|
||||
content={getTranslation('back.json_editor.cancel')}
|
||||
tooltipContent={getTranslation('back.json_editor.tooltip_cancel')}
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="default"
|
||||
onClick={resetComponent}
|
||||
/>
|
||||
<BtnOptionnalLinkWithTooltip
|
||||
content={getTranslation('back.json_editor.validate')}
|
||||
tooltipContent={getTranslation('back.json_editor.tooltip_validate')}
|
||||
type="button"
|
||||
size="default"
|
||||
onClick={handleConfirm}
|
||||
<DialogFooter className="sm:justify-between">
|
||||
<SimpleSwitch
|
||||
label={getTranslation("back.json_editor.overwrite")}
|
||||
name="overwrite"
|
||||
checked={overwrite}
|
||||
setData={(_, value) => setOverwrite(value)}
|
||||
/>
|
||||
<div className="flex justify-center items-center gap-1.5">
|
||||
<BtnOptionnalLinkWithTooltip
|
||||
content={getTranslation('back.json_editor.cancel')}
|
||||
tooltipContent={getTranslation('back.json_editor.tooltip_cancel')}
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="default"
|
||||
onClick={resetComponent}
|
||||
/>
|
||||
<BtnOptionnalLinkWithTooltip
|
||||
content={getTranslation('back.json_editor.validate')}
|
||||
tooltipContent={getTranslation('back.json_editor.tooltip_validate')}
|
||||
type="button"
|
||||
size="default"
|
||||
onClick={handleConfirm}
|
||||
/>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
|
|
|||
|
|
@ -221,8 +221,9 @@ export default function Form({
|
|||
{auth.user.role === enumUserRole.conceptor.value && route().current('back.page-configs.create') && (
|
||||
<div className="border-b pb-6 mb-6">
|
||||
<ImportConfigJson
|
||||
onConfirm={(value) => {
|
||||
setData('config', value as ConfigField[])
|
||||
onConfirm={(value, overwrite) => {
|
||||
const newConfig = (!overwrite) ? [...data.config, ...value] : value;
|
||||
setData('config', newConfig as ConfigField[])
|
||||
setResetState(prev => prev + 1)
|
||||
}}
|
||||
/>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue