108 lines
5 KiB
JavaScript
108 lines
5 KiB
JavaScript
// Rôle : choisit la destination et le contenu lors de la copie d'un exemple de la librairie.
|
|
import { useId, useState } from "react";
|
|
import { Modal } from "./Modal.jsx";
|
|
|
|
export function LibraryCopyModal({ content, toolTitle, toolboxes, hasDependency, onClose, onConfirm }) {
|
|
const titleId = useId();
|
|
const descriptionId = useId();
|
|
const [toolboxId, setToolboxId] = useState(toolboxes[0]?.id || "");
|
|
const initialToolbox = toolboxes.find((toolbox) => toolbox.id === toolboxId);
|
|
const [spaceId, setSpaceId] = useState(initialToolbox?.activeSpaceId || initialToolbox?.defaultSpaceId || "");
|
|
const [newToolboxName, setNewToolboxName] = useState("");
|
|
const [copyData, setCopyData] = useState(true);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState("");
|
|
const selectedToolbox = toolboxes.find((toolbox) => toolbox.id === toolboxId);
|
|
const createsToolbox = toolboxes.length === 0;
|
|
|
|
async function submit(event) {
|
|
event.preventDefault();
|
|
setBusy(true);
|
|
setError("");
|
|
const success = await onConfirm({ toolboxId, spaceId, newToolboxName: newToolboxName.trim(), copyData });
|
|
if (!success) {
|
|
setError(content.failure);
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Modal
|
|
rootClassName="confirm-modal-root library-copy-modal-root"
|
|
dialogClassName="confirm-modal nds-modal library-copy-modal"
|
|
labelledBy={titleId}
|
|
describedBy={descriptionId}
|
|
onRequestClose={onClose}
|
|
canClose={!busy}
|
|
>
|
|
<header>
|
|
<p className="eyebrow">{content.eyebrow}</p>
|
|
<h2 id={titleId}>{content.title.replace("{tool}", toolTitle)}</h2>
|
|
</header>
|
|
<p id={descriptionId} className="library-copy-description">{content.description}</p>
|
|
<form onSubmit={submit}>
|
|
{createsToolbox ? (
|
|
<label className="field library-copy-field">
|
|
<span>{content.newToolboxLabel}</span>
|
|
<input
|
|
className="nds-input"
|
|
data-size="sm"
|
|
type="text"
|
|
autoComplete="off"
|
|
placeholder={content.newToolboxPlaceholder}
|
|
required
|
|
value={newToolboxName}
|
|
onChange={(event) => setNewToolboxName(event.target.value)}
|
|
data-modal-initial-focus=""
|
|
/>
|
|
<small>{content.newToolboxHelp}</small>
|
|
</label>
|
|
) : (
|
|
<div className="library-copy-destination">
|
|
<label className="field library-copy-field">
|
|
<span>{content.toolboxLabel}</span>
|
|
<select
|
|
className="nds-select"
|
|
data-size="sm"
|
|
value={toolboxId}
|
|
onChange={(event) => {
|
|
const nextToolbox = toolboxes.find((toolbox) => toolbox.id === event.target.value);
|
|
setToolboxId(event.target.value);
|
|
setSpaceId(nextToolbox?.activeSpaceId || nextToolbox?.defaultSpaceId || "");
|
|
}}
|
|
data-modal-initial-focus=""
|
|
>
|
|
{toolboxes.map((toolbox) => <option key={toolbox.id} value={toolbox.id}>{toolbox.name}</option>)}
|
|
</select>
|
|
</label>
|
|
<label className="field library-copy-field">
|
|
<span>{content.spaceLabel}</span>
|
|
<select className="nds-select" data-size="sm" value={spaceId} onChange={(event) => setSpaceId(event.target.value)}>
|
|
{(selectedToolbox?.spaces || []).map((space) => <option key={space.id} value={space.id}>{space.name}</option>)}
|
|
</select>
|
|
</label>
|
|
</div>
|
|
)}
|
|
<fieldset className="library-copy-mode">
|
|
<legend>{content.modeLabel}</legend>
|
|
<label className={copyData ? "is-selected" : ""}>
|
|
<input type="radio" name="copy-mode" value="current" checked={copyData} onChange={() => setCopyData(true)} />
|
|
<span><strong>{content.currentLabel}</strong><small>{content.currentHelp}</small></span>
|
|
</label>
|
|
<label className={!copyData ? "is-selected" : ""}>
|
|
<input type="radio" name="copy-mode" value="empty" checked={!copyData} onChange={() => setCopyData(false)} />
|
|
<span><strong>{content.emptyLabel}</strong><small>{content.emptyHelp}</small></span>
|
|
</label>
|
|
</fieldset>
|
|
{copyData && hasDependency && <p className="nds-inline-notice" data-variant="info">{content.dependencyNotice}</p>}
|
|
{error && <p className="nds-inline-notice" data-variant="danger" role="alert">{error}</p>}
|
|
<footer className="nds-modal-footer">
|
|
<button className="nds-button" data-size="sm" type="button" disabled={busy} onClick={onClose}>{content.cancelLabel}</button>
|
|
<button className="nds-button" data-size="sm" data-variant="primary" type="submit" disabled={busy || (createsToolbox ? !newToolboxName.trim() : !toolboxId || !spaceId)}>
|
|
{busy ? content.savingLabel : createsToolbox ? content.createAndAddLabel : content.addLabel}
|
|
</button>
|
|
</footer>
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|