- 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
141 lines
4.4 KiB
TypeScript
141 lines
4.4 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, Save, Braces } from 'lucide-react';
|
|
import { snippets } from '../../api';
|
|
import { Breadcrumbs, Button, Card, Input, CodeEditor, useToast } from '../../ui';
|
|
import styles from '../Page.module.css';
|
|
|
|
export function CreateSnippetPage() {
|
|
const { t } = useTranslation();
|
|
usePageTitle(t('snippets.create_title'));
|
|
const navigate = useNavigate();
|
|
const toast = useToast();
|
|
|
|
const [alias, setAlias] = useState('');
|
|
const [title, setTitle] = useState('');
|
|
const [description, setDescription] = useState('');
|
|
const [code, setCode] = useState('');
|
|
const [aliasError, setAliasError] = useState('');
|
|
const [titleError, setTitleError] = useState('');
|
|
const [codeError, setCodeError] = useState('');
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const handleSubmit = async (e: SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
let valid = true;
|
|
if (!alias.trim()) {
|
|
setAliasError(t('snippets.form_alias_required'));
|
|
valid = false;
|
|
}
|
|
if (!title.trim()) {
|
|
setTitleError(t('snippets.form_title_required'));
|
|
valid = false;
|
|
}
|
|
if (!code.trim()) {
|
|
setCodeError(t('snippets.form_code_required'));
|
|
valid = false;
|
|
}
|
|
if (!valid) return;
|
|
setSaving(true);
|
|
try {
|
|
const snippet = await snippets.create({
|
|
alias: alias.trim(),
|
|
title: title.trim(),
|
|
description: description.trim() || undefined,
|
|
code: code.trim(),
|
|
});
|
|
toast.success(t('snippets.created'));
|
|
navigate(`/snippets/${snippet.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('snippets.title'),
|
|
icon: <Braces size={14} />,
|
|
onClick: () => navigate('/snippets'),
|
|
},
|
|
{ label: t('snippets.create_title') },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit} noValidate>
|
|
<Card className={styles.formCardFull}>
|
|
<div className={styles.formFields}>
|
|
<Input
|
|
label={t('snippets.form_alias')}
|
|
placeholder={t('snippets.form_alias_placeholder')}
|
|
value={alias}
|
|
onChange={(e) => {
|
|
setAlias(e.target.value);
|
|
setAliasError('');
|
|
}}
|
|
error={aliasError || undefined}
|
|
required
|
|
autoFocus
|
|
/>
|
|
<Input
|
|
label={t('snippets.form_title')}
|
|
placeholder={t('snippets.form_title_placeholder')}
|
|
value={title}
|
|
onChange={(e) => {
|
|
setTitle(e.target.value);
|
|
setTitleError('');
|
|
}}
|
|
error={titleError || undefined}
|
|
required
|
|
/>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>{t('snippets.form_description')}</label>
|
|
<CodeEditor
|
|
value={description}
|
|
onChange={setDescription}
|
|
language="markdown"
|
|
rows={10}
|
|
/>
|
|
</div>
|
|
<div className={styles.formField}>
|
|
<label className={styles.fieldLabel}>
|
|
{t('snippets.form_code')}
|
|
<span className={styles.requiredMark}> *</span>
|
|
</label>
|
|
<CodeEditor
|
|
value={code}
|
|
onChange={(v) => {
|
|
setCode(v);
|
|
setCodeError('');
|
|
}}
|
|
rows={14}
|
|
error={!!codeError}
|
|
/>
|
|
{codeError && <span className={styles.fieldError}>{codeError}</span>}
|
|
</div>
|
|
</div>
|
|
<div className={styles.formActions}>
|
|
<Button type="button" variant="secondary" onClick={() => navigate('/snippets')}>
|
|
<X size={14} />
|
|
{t('snippets.action_cancel')}
|
|
</Button>
|
|
<Button type="submit" disabled={saving}>
|
|
<Save size={14} />
|
|
{t('snippets.action_save')}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|