diff --git a/src/generators/NextGenerator.js b/src/generators/NextGenerator.js index 49f078d4..588853e9 100644 --- a/src/generators/NextGenerator.js +++ b/src/generators/NextGenerator.js @@ -13,9 +13,6 @@ export default class NextGenerator extends BaseGenerator { "components/foo/Show.tsx", "components/foo/Form.tsx", - // interfaces - "error/SubmissionError.ts", - // types "types/Collection.ts", "types/foo.ts", @@ -92,9 +89,6 @@ export default class NextGenerator extends BaseGenerator { // components "components/common/ReferenceLinks.tsx", - // error - "error/SubmissionError.ts", - // types "types/Collection.ts", diff --git a/src/generators/NextGenerator.test.js b/src/generators/NextGenerator.test.js index 4bdaae67..474feb78 100644 --- a/src/generators/NextGenerator.test.js +++ b/src/generators/NextGenerator.test.js @@ -44,7 +44,6 @@ describe("generate", () => { "/components/abc/Show.tsx", "/components/abc/Form.tsx", "/components/common/ReferenceLinks.tsx", - "/error/SubmissionError.ts", "/types/Abc.ts", "/types/Collection.ts", "/pages/abcs/[id]/index.tsx", diff --git a/templates/next/components/foo/Form.tsx b/templates/next/components/foo/Form.tsx index 40945da0..afc66390 100644 --- a/templates/next/components/foo/Form.tsx +++ b/templates/next/components/foo/Form.tsx @@ -2,7 +2,7 @@ import { FunctionComponent, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import Head from "next/head"; -import { Formik } from "formik"; +import { ErrorMessage, Formik } from "formik"; import { fetch } from "../../utils/dataAccess"; import { {{{ucf}}} } from '../../types/{{{ucf}}}'; @@ -21,7 +21,7 @@ export const Form: FunctionComponent = ({ {{{lc}}} }) => { await fetch({{{lc}}}['@id'], { method: "DELETE" }); router.push("/{{{name}}}"); } catch (error) { - setError("Error when deleting the resource."); + setError(`Error when deleting the resource: ${error}`); console.error(error); } }; @@ -42,7 +42,7 @@ export const Form: FunctionComponent = ({ {{{lc}}} }) => { // add your validation logic here return errors; }} - onSubmit={async (values, { setSubmitting, setStatus }) => { + onSubmit={async (values, { setSubmitting, setStatus, setErrors }) => { const isCreation = !values["@id"]; try { await fetch(isCreation ? "/{{{name}}}" : values["@id"], { @@ -57,8 +57,9 @@ export const Form: FunctionComponent = ({ {{{lc}}} }) => { } catch (error) { setStatus({ isValid: false, - msg: `Error when ${isCreation ? 'creating': 'updating'} the resource.`, + msg: `${error.defaultErrorMsg}`, }); + setErrors(error.fields); } setSubmitting(false); }} @@ -91,7 +92,11 @@ export const Form: FunctionComponent = ({ {{{lc}}} }) => { onBlur={handleBlur} /> - { errors.{{name}} && touched.{{name}} &&
{ errors.{{name}} }
} + {{/each}} {status && status.msg && ( @@ -105,12 +110,6 @@ export const Form: FunctionComponent = ({ {{{lc}}} }) => { )} - {error && ( -
- {error} -
- )} -