- add usePageTitle hook that writes '<title> | Liqa' to document.title - list pages use section name; detail pages use entity name once loaded - create/edit pages use static action title, edit includes entity name
133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
import { useState, SubmitEvent } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { usePageTitle } from '../../hooks/usePageTitle';
|
|
import { X, Globe, Save } from 'lucide-react';
|
|
import { environments } from '../../api';
|
|
import { Breadcrumbs, Button, Card, Input, CodeEditor, useToast } from '../../ui';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function CreateEnvironmentPage() {
|
|
const { t } = useTranslation();
|
|
usePageTitle(t('environments.create_title'));
|
|
const navigate = useNavigate();
|
|
const toast = useToast();
|
|
|
|
const [name, setName] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [dataJson, setDataJson] = useState('{\n "id": "https://id.example.com"\n}');
|
|
const [nameError, setNameError] = useState('');
|
|
const [dataError, setDataError] = useState('');
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const handleSubmit = async (e: SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
if (!name.trim()) {
|
|
setNameError(t('environments.form_name_required'));
|
|
return;
|
|
}
|
|
let parsedData: Record<string, string | undefined>;
|
|
try {
|
|
const raw = dataJson.trim() ? JSON.parse(dataJson.trim()) : {};
|
|
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
|
|
throw new Error('invalid');
|
|
}
|
|
parsedData = Object.fromEntries(
|
|
Object.entries(raw as Record<string, unknown>).map(([k, v]) => [
|
|
k,
|
|
v == null ? undefined : String(v),
|
|
]),
|
|
);
|
|
setDataError('');
|
|
} catch {
|
|
setDataError(t('environments.form_data_invalid_json'));
|
|
return;
|
|
}
|
|
setSaving(true);
|
|
try {
|
|
const env = await environments.create(
|
|
name.trim(),
|
|
parsedData,
|
|
description.trim() || undefined,
|
|
);
|
|
toast.success(t('environments.created'));
|
|
navigate(`/environments/${env.id}`);
|
|
} catch (err) {
|
|
const message = (err as Error).message;
|
|
toast.error(message);
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<div className={styles.pageToolbar}>
|
|
<Breadcrumbs
|
|
items={[
|
|
{
|
|
label: t('environments.title'),
|
|
icon: <Globe size={14} />,
|
|
onClick: () => navigate('/environments'),
|
|
},
|
|
{ label: t('environments.create_title') },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit} noValidate>
|
|
<Card className={styles.formCardFull}>
|
|
<div className={styles.formFields}>
|
|
<Input
|
|
label={t('environments.form_name')}
|
|
placeholder={t('environments.form_name_placeholder')}
|
|
value={name}
|
|
onChange={(e) => {
|
|
setName(e.target.value);
|
|
setNameError('');
|
|
}}
|
|
error={nameError || undefined}
|
|
required
|
|
autoFocus
|
|
/>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>{t('environments.form_description')}</label>
|
|
<CodeEditor
|
|
language="markdown"
|
|
value={description}
|
|
onChange={setDescription}
|
|
rows={6}
|
|
/>
|
|
</div>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>{t('environments.form_data')}</label>
|
|
<CodeEditor
|
|
language="json"
|
|
value={dataJson}
|
|
onChange={(v) => {
|
|
setDataJson(v);
|
|
setDataError('');
|
|
}}
|
|
rows={8}
|
|
error={!!dataError}
|
|
/>
|
|
{dataError && <span className={styles.fieldError}>{dataError}</span>}
|
|
</div>
|
|
</div>
|
|
|
|
<div className={styles.formActions}>
|
|
<Button type="button" variant="secondary" onClick={() => navigate('/environments')}>
|
|
<X size={14} />
|
|
{t('environments.action_cancel')}
|
|
</Button>
|
|
<Button type="submit" loading={saving}>
|
|
<Save size={14} />
|
|
{t('environments.action_save')}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|