From 4adcee4af774f6048affd580940ed08b96a56d20 Mon Sep 17 00:00:00 2001 From: Jesus Vasquez Date: Sun, 3 Mar 2024 22:34:43 -0500 Subject: [PATCH] prettier, lint cleanup --- app/page.tsx | 4 +- components/Form/ClientField.tsx | 31 +++-- components/Form/FileInput.tsx | 66 +++++------ components/Form/RadioGroup.tsx | 9 +- components/Form/ToggleField.tsx | 28 ++--- components/Form/data.tsx | 12 +- components/Form/formSchemas.ts | 44 +++---- components/Form/index.tsx | 199 +++++++++++++------------------- components/Form/types.ts | 14 +-- 9 files changed, 176 insertions(+), 231 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index 3726b4b..3f0fcf6 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,6 +1,6 @@ -import { Metadata } from "next" -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" import { faXmark } from "@fortawesome/free-solid-svg-icons" +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" +import { Metadata } from "next" import Form from "components/Form" export const metadata: Metadata = { diff --git a/components/Form/ClientField.tsx b/components/Form/ClientField.tsx index da2bc68..02ff447 100644 --- a/components/Form/ClientField.tsx +++ b/components/Form/ClientField.tsx @@ -1,13 +1,13 @@ +import { faClock } from "@fortawesome/free-regular-svg-icons" +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" import React from "react" import SelectInput from "./SelectInput" import { Utils } from "./Utils" -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" -import { faClock } from "@fortawesome/free-regular-svg-icons" // Define a type for your props to improve type-checking type ClientFieldProps = { - testingCenters: string[] - clients: string[] + testingCenters: string[] + clients: string[] } // Destructure the props in the function parameter @@ -15,20 +15,19 @@ const ClientField: React.FC = ({ testingCenters, clients }) => return (
{testingCenters.map((testingCenter) => ( -
+
{testingCenter} -
+
- - {clients.map((option: string) => ( - - ))} - - -
- + + {clients.map((option: string) => ( + + ))} + + +
))}
diff --git a/components/Form/FileInput.tsx b/components/Form/FileInput.tsx index 25b7a3b..daed5b0 100644 --- a/components/Form/FileInput.tsx +++ b/components/Form/FileInput.tsx @@ -1,44 +1,43 @@ -import { useField, useFormikContext } from "formik"; -import React, { useRef } from "react"; -import ErrorMessage from "./ErrorMessage"; -import RequiredAsterisk from "./RequiredAsterisk"; -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; -import { faFile } from "@fortawesome/free-solid-svg-icons"; - +import { faFile } from "@fortawesome/free-solid-svg-icons" +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" +import { useField, useFormikContext } from "formik" +import React, { useRef } from "react" +import ErrorMessage from "./ErrorMessage" +import RequiredAsterisk from "./RequiredAsterisk" interface FileInputProps { - label: string; - required?: boolean; - name:string; + label: string + required?: boolean + name: string } -const FileInput: React.FC = ({ label, ...props }) => { - const { required } = props; - const [field, meta] = useField(props); - const { setFieldValue } = useFormikContext(); - const fileInputRef = useRef(null); +const FileInput: React.FC = ({ label, ...props }) => { + const { required } = props + const [field, meta] = useField(props) + const { setFieldValue } = useFormikContext() + const fileInputRef = useRef(null) const handleChange = (event: React.ChangeEvent) => { - const { files } = event.target; - const file = files ? files[0] : null; - setFieldValue(props.name, file); // Directly use setFieldValue to update Formik's state - }; + const { files } = event.target + const file = files ? files[0] : null + setFieldValue(props.name, file) // Directly use setFieldValue to update Formik's state + } const handleClick = () => { if (fileInputRef.current) { - fileInputRef.current.click(); + fileInputRef.current.click() } - }; + } const handleDrop = (event: React.DragEvent) => { - event.preventDefault(); - event.stopPropagation(); - const files = event.dataTransfer.files; + event.preventDefault() + event.stopPropagation() + const files = event.dataTransfer.files if (files && files.length > 0) { - const file = files[0]; - setFieldValue(props.name, file); + const file = files[0] + setFieldValue(props.name, file) } - }; + } return (
@@ -53,15 +52,10 @@ const FileInput: React.FC = ({ label, ...props }) => {
- + - ); -}; + ) +} -export default FileInput; +export default FileInput diff --git a/components/Form/RadioGroup.tsx b/components/Form/RadioGroup.tsx index 9886d69..a356049 100644 --- a/components/Form/RadioGroup.tsx +++ b/components/Form/RadioGroup.tsx @@ -1,8 +1,8 @@ +import * as RadioGroup from "@radix-ui/react-radio-group" import { useField } from "formik" import React from "react" import ErrorMessage from "./ErrorMessage" import RequiredAsterisk from "./RequiredAsterisk" -import * as RadioGroup from "@radix-ui/react-radio-group" const RadioGroupField = ({ name, @@ -28,12 +28,7 @@ const RadioGroupField = ({ {label} - + {options.map((option) => (
) => { - const { required, ...restProps } = props; - const [field, meta] = useField({ ...(restProps as { name: string }), type: "checkbox" }); - const [checked, setChecked] = useState(false); + const { required, ...restProps } = props + const [field, meta] = useField({ ...(restProps as { name: string }), type: "checkbox" }) + const [checked, setChecked] = useState(false) return (
@@ -24,15 +24,15 @@ const ToggleField = ({ className="relative h-[25px] w-[42px] cursor-default rounded-full bg-black shadow-[0_2px_10px] outline-none focus:shadow-[0_0_0_2px] focus:shadow-black" checked={checked} onCheckedChange={(isChecked) => { - setChecked(isChecked); - field.onChange({ target: { name: field.name, value: isChecked } }); + setChecked(isChecked) + field.onChange({ target: { name: field.name, value: isChecked } }) }} >
- ); -}; + ) +} -export default ToggleField; +export default ToggleField diff --git a/components/Form/data.tsx b/components/Form/data.tsx index 2a1ab07..e4d295b 100644 --- a/components/Form/data.tsx +++ b/components/Form/data.tsx @@ -1,7 +1,7 @@ export const data = { - importNames: ["Import ABC", "Import DEF", "Import GHI"], - splitScheduleOptions: ["Yes", "No"], - clientOptions: ["Single", "Multiple"], - testingCenterOptions: ["testing center 1", "testing center 2", "testing center 3", "testing center 4"], - clientsOptions: ["client 1", "client 2", "client 3"], -}; \ No newline at end of file + importNames: ["Import ABC", "Import DEF", "Import GHI"], + splitScheduleOptions: ["Yes", "No"], + clientOptions: ["Single", "Multiple"], + testingCenterOptions: ["testing center 1", "testing center 2", "testing center 3", "testing center 4"], + clientsOptions: ["client 1", "client 2", "client 3"], +} diff --git a/components/Form/formSchemas.ts b/components/Form/formSchemas.ts index e2070e6..38ac1d7 100644 --- a/components/Form/formSchemas.ts +++ b/components/Form/formSchemas.ts @@ -1,30 +1,22 @@ -import { FormValues } from "./types"; -import * as Yup from "yup"; +import * as Yup from "yup" +import { FormValues } from "./types" export const initialValues: FormValues = { - importName: '', - manifestFile: null, - splitSchedule: '', - client: '', - testingCenter: '', - toleranceWindow: false, -}; + importName: "", + manifestFile: null, + splitSchedule: "", + client: "", + testingCenter: "", + toleranceWindow: false, +} export const validationSchema = Yup.object({ - importName: Yup.string() - .required('Import name is required') - .oneOf(["Import ABC", "Import DEF", "Import GHI"], "Invalid Import Name"), - manifestFile: Yup.mixed() - .required('A file is required') - .nullable(), // Allows for null values - splitSchedule: Yup.string() - .required('This field is required') - .oneOf(["Yes", "No"], "Invalid Option"), - client: Yup.string() - .required('Client selection is required') - .oneOf(["Single", "Multiple"], "Invalid Client Type"), - testingCenter: Yup.string() - .required('Testing center selection is required'), // Assuming a selection is required - toleranceWindow: Yup.boolean() - .required('Tolerance window selection is required'), // Assuming this is a boolean represented by a toggle -}); \ No newline at end of file + importName: Yup.string() + .required("Import name is required") + .oneOf(["Import ABC", "Import DEF", "Import GHI"], "Invalid Import Name"), + manifestFile: Yup.mixed().required("A file is required").nullable(), // Allows for null values + splitSchedule: Yup.string().required("This field is required").oneOf(["Yes", "No"], "Invalid Option"), + client: Yup.string().required("Client selection is required").oneOf(["Single", "Multiple"], "Invalid Client Type"), + testingCenter: Yup.string().required("Testing center selection is required"), // Assuming a selection is required + toleranceWindow: Yup.boolean().required("Tolerance window selection is required"), // Assuming this is a boolean represented by a toggle +}) diff --git a/components/Form/index.tsx b/components/Form/index.tsx index f789491..561cabb 100644 --- a/components/Form/index.tsx +++ b/components/Form/index.tsx @@ -1,130 +1,95 @@ "use client" -import React, { useState } from "react"; +import axios from "axios" +import { Form, Formik } from "formik" +import React, { useState } from "react" //Dependencies -import { Formik, Form } from "formik"; - -import axios from "axios"; //Input fields -import SelectInput from "./SelectInput"; -import RadioGroup from "./RadioGroup"; -import FileInput from "./FileInput"; -import ToggleField from "./ToggleField"; -import StatusField from "./StatusField"; -import ClientField from "./ClientField"; +import ClientField from "./ClientField" +import { data } from "./data" +import FileInput from "./FileInput" +import { initialValues, validationSchema } from "./formSchemas" +import RadioGroup from "./RadioGroup" +import SelectInput from "./SelectInput" +import StatusField from "./StatusField" +import ToggleField from "./ToggleField" -import { FormValues } from "./types"; -import { data } from "./data"; -import { initialValues, validationSchema } from "./formSchemas"; +import { FormValues } from "./types" const FormComponent: React.FC = () => { - const [formSubmitted, setFormSubmitted] = useState(false); + const [formSubmitted, setFormSubmitted] = useState(false) - const submitForm = (values: FormValues) => { - return axios.post( - `/api/submitForm`, - { - submissionData: values, - }, - { - // options - headers: { - "Content-Type": "application/json" - } - } - ); - }; - - return ( - { - try { - console.log(values); - await submitForm(values); - setFormSubmitted(true); - } catch (error) { - console.error("Error submitting form:", error); - } finally { - setSubmitting(false); - } - }} - > - {formik => ( -
-
-
- - - {data.importNames.map(option => ( - - ))} - - - - Tolerance Window: -
-
- - - - -
-
- -
- )} -
- + const submitForm = (values: FormValues) => { + return axios.post( + `/api/submitForm`, + { + submissionData: values, + }, + { + // options + headers: { + "Content-Type": "application/json", + }, + } ) + } + + return ( + { + try { + console.log(values) + await submitForm(values) + setFormSubmitted(true) + } catch (error) { + console.error("Error submitting form:", error) + } finally { + setSubmitting(false) + } + }} + > + {(formik) => ( +
+
+
+ + + {data.importNames.map((option) => ( + + ))} + + + + + Tolerance Window: + +
+
+ + + + +
+
+ +
+ )} +
+ ) } -export default FormComponent; - +export default FormComponent diff --git a/components/Form/types.ts b/components/Form/types.ts index 3793f42..fea6431 100644 --- a/components/Form/types.ts +++ b/components/Form/types.ts @@ -1,8 +1,8 @@ export interface FormValues { - importName: string; - manifestFile: File | null; - splitSchedule: string; - client: string; - testingCenter: string; - toleranceWindow: boolean; -} \ No newline at end of file + importName: string + manifestFile: File | null + splitSchedule: string + client: string + testingCenter: string + toleranceWindow: boolean +}