UNPKG

@sparklink-pro/apant

Version:

Apollo & Antd tools

46 lines 2.03 kB
import { __awaiter, __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { forwardRef } from 'react'; import { OPERATION_CREATE, OPERATION_UPDATE } from '../definitions'; import { Form } from './Form'; import { useTypeForm } from '../hooks/useTypeForm'; import { useTypeMutation } from '../hooks/useTypeMutation'; /** * Render a form for the given type and perform related mutation */ export const FormType = forwardRef((_a, ref) => { var { type, name = 'default', onFailed, onSuccess, mutationOptions, mutateOptions, variables = {} } = _a, formProps = __rest(_a, ["type", "name", "onFailed", "onSuccess", "mutationOptions", "mutateOptions", "variables"]); const config = useTypeForm({ type, name }); const { object } = formProps.context; const operation = object ? OPERATION_UPDATE : OPERATION_CREATE; const mutation = useTypeMutation(Object.assign({ type, operation }, mutationOptions)); if (!config) { return (_jsxs("div", { children: ["Form ", _jsx("strong", { children: name }), " for type ", _jsx("strong", { children: type }), " not found"] })); } const onFinish = (values) => __awaiter(void 0, void 0, void 0, function* () { const vars = Object.assign(Object.assign({}, variables), { input: values }); if (operation === OPERATION_UPDATE) { vars.item = object.id; } try { const result = yield mutation.mutate(Object.assign({ variables: vars }, mutateOptions)); if (onSuccess) { onSuccess({ type, operation, result }); } } catch (e) { if (onFailed) { return onFailed({ type, operation, error: e, }); } throw e; } return undefined; }); return _jsx(Form, Object.assign({ config: config, onFinish: onFinish, ref: ref }, formProps)); }); //# sourceMappingURL=FormType.js.map