Files
yaak/src-web/components/FormMultipartEditor.tsx
Gregory Schier f967820f12 Model and DB refactor (#61)
- [x] Move from `sqlx` to `rusqlite`
- [x] Generate TS types from Rust models
2024-08-05 07:58:20 -07:00

50 lines
1.3 KiB
TypeScript

import { useCallback, useMemo } from 'react';
import type { HttpRequest } from '@yaakapp/api';
import type { Pair, PairEditorProps } from './core/PairEditor';
import { PairEditor } from './core/PairEditor';
type Props = {
forceUpdateKey: string;
body: HttpRequest['body'];
onChange: (body: HttpRequest['body']) => void;
};
export function FormMultipartEditor({ body, forceUpdateKey, onChange }: Props) {
const pairs = useMemo<Pair[]>(
() =>
(Array.isArray(body.form) ? body.form : []).map((p) => ({
enabled: p.enabled,
name: p.name,
value: p.file ?? p.value,
contentType: p.contentType,
isFile: !!p.file,
})),
[body.form],
);
const handleChange = useCallback<PairEditorProps['onChange']>(
(pairs) =>
onChange({
form: pairs.map((p) => ({
enabled: p.enabled,
name: p.name,
contentType: p.contentType,
file: p.isFile ? p.value : undefined,
value: p.isFile ? undefined : p.value,
})),
}),
[onChange],
);
return (
<PairEditor
valueAutocompleteVariables
nameAutocompleteVariables
allowFileValues
pairs={pairs}
onChange={handleChange}
forceUpdateKey={forceUpdateKey}
/>
);
}