From 350fecc996cb6b015478acaab28aecf6fcab779b Mon Sep 17 00:00:00 2001 From: SevicheCC <91365763+Sevichecc@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:50:50 +0800 Subject: [PATCH] feat: add loader --- .../{InputForm.tsx => CreatAppForm.tsx} | 40 ++++++++++++++----- components/FormContainer.tsx | 10 ++--- hooks/useCreateApp.ts | 6 +-- 3 files changed, 37 insertions(+), 19 deletions(-) rename components/{InputForm.tsx => CreatAppForm.tsx} (83%) diff --git a/components/InputForm.tsx b/components/CreatAppForm.tsx similarity index 83% rename from components/InputForm.tsx rename to components/CreatAppForm.tsx index 58fb861..1d9cf90 100644 --- a/components/InputForm.tsx +++ b/components/CreatAppForm.tsx @@ -1,7 +1,9 @@ "use client"; import * as z from "zod"; -import { zodResolver } from "@hookform/resolvers/zod"; +import { Dispatch, SetStateAction, useState } from "react"; import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -14,11 +16,11 @@ import { FormLabel, FormMessage, } from "@/components/ui/form"; - import ScopeSection from "@/components/scopes/ScopeSection"; +import { AppInfo } from "@/components/FormContainer"; + import { scopesInfo } from "@/lib/scopes"; -import { Dispatch, SetStateAction } from "react"; -import { AppInfo } from "./FormContainer"; +import { Credentials } from "@/lib/types"; const formSchema = z.object({ instanceUrl: z.string().url().trim(), @@ -29,12 +31,18 @@ const formSchema = z.object({ }); export type FormSchema = z.infer; -interface InputFormProps { +interface CreateAppFormProps { createApp: ({}: FormSchema) => Promise; setAppInfo: Dispatch>; + credentials: Credentials | undefined; } -const InputForm: React.FC = ({ createApp, setAppInfo }) => { +const CreateAppForm: React.FC = ({ + createApp, + setAppInfo, + credentials, +}) => { + const [isSubmitted, setIsSubmitted] = useState(false); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { @@ -42,11 +50,13 @@ const InputForm: React.FC = ({ createApp, setAppInfo }) => { clientName: "", redirectUris: "urn:ietf:wg:oauth:2.0:oob", scopes: ["read"], + website: "" }, }); const onSubmit = async (values: FormSchema) => { setAppInfo(values); + setIsSubmitted(true); await createApp(values); }; @@ -115,7 +125,7 @@ const InputForm: React.FC = ({ createApp, setAppInfo }) => { urn:ietf:wg:oauth:2.0:oob {" "} - for local tests or for getting access token + for local tests or to obtaining an access token. @@ -150,11 +160,19 @@ const InputForm: React.FC = ({ createApp, setAppInfo }) => { )} /> - + + {!credentials && isSubmitted ? ( + + ) : ( + + )} ); }; -export default InputForm; +export default CreateAppForm; diff --git a/components/FormContainer.tsx b/components/FormContainer.tsx index a80719f..064e2fe 100644 --- a/components/FormContainer.tsx +++ b/components/FormContainer.tsx @@ -1,6 +1,7 @@ "use client"; -import { useState, useEffect, useRef } from "react"; -import InputForm from "@/components/InputForm"; + +import { useState } from "react"; +import CreateAppForm from "@/components/CreatAppForm"; import ResultTable from "@/components/tables/ResultTable"; import useCreateApp from "@/hooks/useCreateApp"; import { @@ -10,7 +11,7 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; -import { FormSchema } from "@/components/InputForm"; +import { FormSchema } from "@/components/CreatAppForm"; export type AppInfo = Pick; @@ -22,7 +23,6 @@ const FormContainer = () => { scopes: [""], }); - return ( <> @@ -31,7 +31,7 @@ const FormContainer = () => { Card Description - + {credentials && ( diff --git a/hooks/useCreateApp.ts b/hooks/useCreateApp.ts index 203ea39..f59be96 100644 --- a/hooks/useCreateApp.ts +++ b/hooks/useCreateApp.ts @@ -1,9 +1,9 @@ -import { FormSchema } from "@/components/InputForm"; +import { FormSchema } from "@/components/CreatAppForm"; import { useCallback, useState } from "react"; -import { AppEntry, MError } from "@/lib/types"; +import { Credentials, MError } from "@/lib/types"; const useCreateApp = () => { - const [credentials, setCredentials] = useState(); + const [credentials, setCredentials] = useState(); const createApp = useCallback( async ({