From 33fbedacf4214964b207e441506a0b89843dbd04 Mon Sep 17 00:00:00 2001 From: behnamrhp Date: Mon, 22 May 2023 21:52:43 +0300 Subject: [PATCH] [FEAT]: add user creation completed --- src/driven/utils/constants/staticMessages.ts | 3 +++ .../core/create-user/infra/CreateUser.tsx | 12 ++++++++-- .../core/create-user/view/CreateUserView.tsx | 10 ++++++++- .../core/create-user/view/protocols.ts | 2 ++ .../create-user/viewmodel/CreateUserVM.ts | 22 ++++++++++++++++--- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/driven/utils/constants/staticMessages.ts b/src/driven/utils/constants/staticMessages.ts index c21228c..c841036 100644 --- a/src/driven/utils/constants/staticMessages.ts +++ b/src/driven/utils/constants/staticMessages.ts @@ -20,6 +20,9 @@ export const staticMessages = { enterPanel: 'Enter to Panel', enterPhoneNumber: 'Enter your phone number', enterOtpCode: 'Enter your Otp Code', + success: { + createUser: 'user created successfully', + }, }, service: { errors: { diff --git a/src/driving/application/core/create-user/infra/CreateUser.tsx b/src/driving/application/core/create-user/infra/CreateUser.tsx index b68b686..ec77918 100644 --- a/src/driving/application/core/create-user/infra/CreateUser.tsx +++ b/src/driving/application/core/create-user/infra/CreateUser.tsx @@ -29,7 +29,15 @@ export default function CreateUser() { // pass the usecase to the model const { handleSubmitForm } = createUserModel({ createUserLogic }); // pass the method to the viewmodel to call on submit - const { stateHandler, onSubmit, inputNames } = useCreateUserVM({ handleSubmitForm }); + const { stateHandler, onSubmit, inputNames, error, setError } = useCreateUserVM({ handleSubmitForm }); // get all of the needed information to the user to show - return ; + return ( + + ); } diff --git a/src/driving/application/core/create-user/view/CreateUserView.tsx b/src/driving/application/core/create-user/view/CreateUserView.tsx index cb0c09a..8fb33ad 100644 --- a/src/driving/application/core/create-user/view/CreateUserView.tsx +++ b/src/driving/application/core/create-user/view/CreateUserView.tsx @@ -2,10 +2,11 @@ import React from 'react'; import PrimaryButton from '~/driven/utils/components/buttons/primary-button/PrimaryButton'; import SimpleInput from '~/driven/utils/components/inputs/simple-input/SimpleInput'; import { staticMessages } from '~/driven/utils/constants/staticMessages'; +import Notification from '~/driven/utils/components/Notification/Notification'; import ICreateUserViewProps from './protocols'; export default function CreateUserView(props: ICreateUserViewProps) { - const { onSubmit, inputNames, stateHandler } = props; + const { onSubmit, inputNames, stateHandler, error, setError } = props; const { inputStates, inputsSetStates } = stateHandler; const inputs = inputNames.map((inputName) => { @@ -27,6 +28,13 @@ export default function CreateUserView(props: ICreateUserViewProps) { }); return (
+ {Boolean(error.message) && ( + setError({ message: '', type: 'error' })} + /> + )}
{inputs}
null} title={staticMessages.global.submit} /> diff --git a/src/driving/application/core/create-user/view/protocols.ts b/src/driving/application/core/create-user/view/protocols.ts index 6cd6c7b..05cea73 100644 --- a/src/driving/application/core/create-user/view/protocols.ts +++ b/src/driving/application/core/create-user/view/protocols.ts @@ -8,4 +8,6 @@ export default interface ICreateUserViewProps { inputsSetStates: SetStateInputMethod; }; inputNames: (keyof INewUserData)[]; + error: { message: string; type: 'error' | 'success' }; + setError: React.Dispatch>; } diff --git a/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts b/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts index 55ed5e6..2fd76be 100644 --- a/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts +++ b/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts @@ -1,6 +1,8 @@ import { useState } from 'react'; import { checkPhoneNumberInput } from '~/driven/utils/helpers/globalHelpers'; import { INewUserData } from '~/business-logic/core/users/create-user/create-account/data/dto/protocols'; +import { AxiosError } from 'axios'; +import { staticMessages } from '~/driven/utils/constants/staticMessages'; import ICreateUserViewProps from '../view/protocols'; import IUseCreateUserVm from './protocols'; @@ -13,6 +15,8 @@ const inputStateInitialValue: INewUserData = { const inputNames: (keyof INewUserData)[] = ['firstname', 'lastname', 'phonenumber']; const useCreateUserVM = (dependencies: IUseCreateUserVm): ICreateUserViewProps => { + const [error, setError] = useState<{ message: string; type: 'error' | 'success' }>({ message: '', type: 'error' }); + const { handleSubmitForm } = dependencies; const [inputsValue, setInputValues] = useState(inputStateInitialValue); @@ -24,10 +28,20 @@ const useCreateUserVM = (dependencies: IUseCreateUserVm): ICreateUserViewProps = })); }; - const onSubmitCreateUserForm = (e: React.FormEvent) => { + const onSubmitCreateUserForm = async (e: React.FormEvent) => { e.preventDefault(); - console.log('submit user', inputsValue); - handleSubmitForm(inputsValue); + try { + await handleSubmitForm(inputsValue); + setError({ message: staticMessages.global.success.createUser, type: 'success' }); + } catch (errorExc) { + console.log('herere', errorExc); + if (errorExc instanceof AxiosError) { + console.log('herere', errorExc); + setError({ message: errorExc.response?.data?.description, type: 'error' }); + } else if (errorExc instanceof Error) { + setError({ message: errorExc.message, type: 'error' }); + } + } }; const inputStates: INewUserData = { ...inputsValue }; @@ -39,6 +53,8 @@ const useCreateUserVM = (dependencies: IUseCreateUserVm): ICreateUserViewProps = }, onSubmit: onSubmitCreateUserForm, inputNames, + error, + setError, }; };