149 lines
4.5 KiB
TypeScript
149 lines
4.5 KiB
TypeScript
import { useEffect, useState, useRef } from 'react'
|
|
import { FileText } from 'lucide-react'
|
|
import { api } from './api'
|
|
import { Field, Select } from './App'
|
|
import { Button } from './components/ui/button'
|
|
import { Input } from './components/ui/input'
|
|
import { Textarea } from './components/ui/textarea'
|
|
|
|
export function ReplyTemplates({ queue: q, ticketId, from, onInsert }: any) {
|
|
const [selected, setSelected] = useState(''),
|
|
[busy, setBusy] = useState(false),
|
|
[error, setError] = useState('')
|
|
const context = useRef(''),
|
|
alive = useRef(true)
|
|
context.current = `${q.id}/${ticketId}`
|
|
useEffect(() => {
|
|
alive.current = true
|
|
return () => {
|
|
alive.current = false
|
|
}
|
|
}, [])
|
|
useEffect(() => {
|
|
setSelected('')
|
|
setError('')
|
|
setBusy(false)
|
|
}, [q.id, ticketId])
|
|
if (!q.templates?.length) return null
|
|
return (
|
|
<div className="reply-template-picker">
|
|
<div className="reply-template-row">
|
|
<Field label="Antwortvorlage">
|
|
<Select
|
|
aria-label="Antwortvorlage"
|
|
value={selected}
|
|
disabled={busy}
|
|
onChange={setSelected}
|
|
>
|
|
<option value="">Vorlage auswählen …</option>
|
|
{q.templates.map((t: any) => (
|
|
<option key={t.id} value={t.id}>
|
|
{t.name}
|
|
</option>
|
|
))}
|
|
</Select>
|
|
</Field>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={!selected || busy}
|
|
onClick={async () => {
|
|
const current = context.current
|
|
setBusy(true)
|
|
setError('')
|
|
try {
|
|
const result = await api(
|
|
`/tickets/${ticketId}/templates/${encodeURIComponent(selected)}`,
|
|
'POST',
|
|
{ from },
|
|
)
|
|
if (alive.current && context.current === current) onInsert(result.text)
|
|
} catch (e) {
|
|
if (alive.current && context.current === current) setError((e as Error).message)
|
|
} finally {
|
|
if (alive.current && context.current === current) setBusy(false)
|
|
}
|
|
}}
|
|
>
|
|
<FileText size={15} />
|
|
{busy ? 'Wird eingefügt …' : 'Vorlage einfügen'}
|
|
</Button>
|
|
</div>
|
|
{error && (
|
|
<div className="error" role="alert">
|
|
{error}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
export function ReplyTemplateEditor({ templates, onChange, disabled }: any) {
|
|
return (
|
|
<>
|
|
<h3>Antwortvorlagen</h3>
|
|
<p className="muted">
|
|
Vordefinierte Texte für diese Queue. Beim Einfügen werden Platzhalter wie {'{kunde.name}'},{' '}
|
|
{'{ticket.nummer}'} und {'{benutzer.name}'} aufgelöst. Die Signatur wird weiterhin separat
|
|
angehängt.
|
|
</p>
|
|
{!templates.length && <p className="empty">Noch keine Antwortvorlagen für diese Queue.</p>}
|
|
{templates.map((t: any, i: number) => (
|
|
<div className="signature-editor" key={t.id || `new-${i}`}>
|
|
<Field label="Vorlagenname">
|
|
<Input
|
|
aria-label={`Vorlagenname ${i + 1}`}
|
|
required
|
|
maxLength={100}
|
|
disabled={disabled}
|
|
value={t.name}
|
|
onChange={(e) =>
|
|
onChange(
|
|
templates.map((v: any, j: number) =>
|
|
j === i ? { ...v, name: e.target.value } : v,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Field label="Antworttext">
|
|
<Textarea
|
|
aria-label={`Vorlagentext ${i + 1}`}
|
|
required
|
|
rows={7}
|
|
maxLength={20000}
|
|
disabled={disabled}
|
|
value={t.body}
|
|
onChange={(e) =>
|
|
onChange(
|
|
templates.map((v: any, j: number) =>
|
|
j === i ? { ...v, body: e.target.value } : v,
|
|
),
|
|
)
|
|
}
|
|
/>
|
|
</Field>
|
|
{!disabled && (
|
|
<Button
|
|
type="button"
|
|
variant="plain"
|
|
onClick={() => onChange(templates.filter((_: any, j: number) => j !== i))}
|
|
>
|
|
Vorlage entfernen
|
|
</Button>
|
|
)}
|
|
</div>
|
|
))}
|
|
{!disabled && (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={templates.length >= 100}
|
|
onClick={() => onChange([...templates, { name: '', body: '' }])}
|
|
>
|
|
Antwortvorlage hinzufügen
|
|
</Button>
|
|
)}
|
|
</>
|
|
)
|
|
}
|