diff --git a/src/business-logic/core/users/create-user/index.ts b/src/business-logic/core/users/create-user/index.ts index aa10a7d..30c0df1 100644 --- a/src/business-logic/core/users/create-user/index.ts +++ b/src/business-logic/core/users/create-user/index.ts @@ -1,3 +1,3 @@ -import createUserInfra from './infra/createUserInfra'; +import CreateUserInfra from './infra/createUserInfra'; -export default createUserInfra; +export default CreateUserInfra; diff --git a/src/business-logic/core/users/create-user/infra/createUserInfra.ts b/src/business-logic/core/users/create-user/infra/createUserInfra.ts index 8b1a472..6a032a7 100644 --- a/src/business-logic/core/users/create-user/infra/createUserInfra.ts +++ b/src/business-logic/core/users/create-user/infra/createUserInfra.ts @@ -4,7 +4,7 @@ import CreateProfileRepo from '../create-profile/data/repository/CreateRepositor import { HttpHandler as HttpProfileHandler } from '../create-profile/data/repository/protocols'; import CreateUserUsecase from '../usecase/createUserUsecase'; -class createUserInfra { +class CreateUserInfra { private httpAccountHandler: HttpAccountHandler; private httpProfileHandler: HttpProfileHandler; @@ -26,4 +26,4 @@ class createUserInfra { } } -export default createUserInfra; +export default CreateUserInfra; diff --git a/src/business-logic/core/users/create-user/ports.ts b/src/business-logic/core/users/create-user/ports.ts index 5103545..02ed3c0 100644 --- a/src/business-logic/core/users/create-user/ports.ts +++ b/src/business-logic/core/users/create-user/ports.ts @@ -1,7 +1,10 @@ import { HttpHandler as HttpProfileHandler } from './create-profile/data/repository/protocols'; import { HttpHandler as HttpAccountHandler } from './create-account/data/repository/protocols'; +import CreateUserUsecase from './usecase/createUserUsecase'; export default interface createUserPort { httpAccountHandler: HttpAccountHandler; httpProfileHandler: HttpProfileHandler; } + +export type createUserReturnType = CreateUserUsecase; diff --git a/src/driven/utils/components/inputs/simple-input/SimpleInput.tsx b/src/driven/utils/components/inputs/simple-input/SimpleInput.tsx index 7633e4d..3355e11 100644 --- a/src/driven/utils/components/inputs/simple-input/SimpleInput.tsx +++ b/src/driven/utils/components/inputs/simple-input/SimpleInput.tsx @@ -1,18 +1,41 @@ import React from 'react'; -interface ISimpleInput { - title: string; +export type SetStateInputMethod = (name: NameType, newValue: string) => void; + +interface ISimpleInput { + inputData: { + title: string; + name: string; + }; className?: string; + stateHanlder: { + state: string; + setState: SetStateInputMethod; + }; } -export default function SimpleInput(props: ISimpleInput) { - const { title, className } = props; +export default function SimpleInput(props: ISimpleInput) { + const { className, inputData, stateHanlder } = props; + const { name, title } = inputData; + const { setState, state } = stateHanlder; + + const handleInputChange = (e: React.ChangeEvent) => { + const { value, name: inputName } = e.target; + setState(inputName as NameType, value); + }; + return (
- +
); } diff --git a/src/driven/utils/constants/staticMessages.ts b/src/driven/utils/constants/staticMessages.ts index 64031a1..c4c5b90 100644 --- a/src/driven/utils/constants/staticMessages.ts +++ b/src/driven/utils/constants/staticMessages.ts @@ -5,7 +5,7 @@ export const staticMessages = { }, users: 'Users', submit: 'Submit', - fistname: 'Firstname', + firstname: 'Firstname', lastname: 'Lastname', place_id: 'Place id', title: 'title', @@ -14,7 +14,7 @@ export const staticMessages = { address: 'Address', qrCode: 'qrCode', createUser: 'Create user', - phoneNumber: 'Phone Number', + phonenumber: 'Phone Number', }, service: { errors: { diff --git a/src/driven/utils/helpers/globalHelpers.ts b/src/driven/utils/helpers/globalHelpers.ts index f99ea4b..1fc5a84 100644 --- a/src/driven/utils/helpers/globalHelpers.ts +++ b/src/driven/utils/helpers/globalHelpers.ts @@ -1,8 +1,5 @@ import StateManagementService from '~/driven/boundaries/state-management'; -import { - errorHandlingStateTypes, - UIErrorHandling, -} from './protocols/globalHelpersProtocols'; +import { errorHandlingStateTypes, UIErrorHandling } from './protocols/globalHelpersProtocols'; export const UIErrorHandlingFactory = ({ state, @@ -18,13 +15,14 @@ export const UIErrorHandlingFactory = ({ state, }); -export const prepareStateManagementForVM = ( - apiUrl: string, - model: () => Promise -) => { +export const prepareStateManagementForVM = (apiUrl: string, model: () => Promise) => { const stateManagement = StateManagementService.swr(); const useGetPlacesList = () => stateManagement.useGetQuery(apiUrl, model); return useGetPlacesList; }; + +export const checkPhoneNumberInput = (newValue: string) => { + return (Number.isFinite(+newValue) || newValue === '+') && newValue.length <= 12; +}; diff --git a/src/driving/application/core/create-user/infra/CreateUser.tsx b/src/driving/application/core/create-user/infra/CreateUser.tsx index fd4d7b7..1b626f7 100644 --- a/src/driving/application/core/create-user/infra/CreateUser.tsx +++ b/src/driving/application/core/create-user/infra/CreateUser.tsx @@ -1,6 +1,22 @@ import React from 'react'; +import createAccountAdapter from '~/driven/adapters/create-account-adapter/createAccountAdapter'; +import createProfileAdapter from '~/driven/adapters/create-profile-adapter/createProfileAdapter'; +import CreateUserInfra from '~/business-logic/core/users/create-user'; import CreateUserView from '../view/CreateUserView'; +import useCreateUserVM from '../viewmodel/CreateUserVM'; +import createUserModel from '../model/createUserModel'; export default function CreateUser() { - return ; + // get adapters from driven layer + const createAccountDrivenAdapter = createAccountAdapter(); + const createProfileDrivenAdapter = createProfileAdapter(); + // pass to the logic and get the usecase + const createUserInfra = new CreateUserInfra(createAccountDrivenAdapter, createProfileDrivenAdapter); + const createUserLogic = createUserInfra.execute(); + // 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 }); + // get all of the needed information to the user to show + return ; } diff --git a/src/driving/application/core/create-user/model/createUserModel.ts b/src/driving/application/core/create-user/model/createUserModel.ts new file mode 100644 index 0000000..6c92840 --- /dev/null +++ b/src/driving/application/core/create-user/model/createUserModel.ts @@ -0,0 +1,21 @@ +import CreateUserUsecase from '~/business-logic/core/users/create-user/usecase/createUserUsecase'; +import { INewUserData } from '~/business-logic/core/users/create-user/create-account/data/dto/protocols'; +import IUseCreateUserVm from '../viewmodel/protocols'; + +interface ICreateUserModel { + createUserLogic: CreateUserUsecase; +} + +const createUserModel = (dependencies: ICreateUserModel): IUseCreateUserVm => { + const { createUserLogic } = dependencies; + + const handleSubmitForm = async (newUserData: INewUserData) => { + await createUserLogic.execute(newUserData); + }; + + return { + handleSubmitForm, + }; +}; + +export default createUserModel; diff --git a/src/driving/application/core/create-user/view/CreateUserView.tsx b/src/driving/application/core/create-user/view/CreateUserView.tsx index 4083af8..5bd6243 100644 --- a/src/driving/application/core/create-user/view/CreateUserView.tsx +++ b/src/driving/application/core/create-user/view/CreateUserView.tsx @@ -2,18 +2,35 @@ 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 ICreateUserViewProps from './protocols'; -export default function CreateUserView() { +export default function CreateUserView(props: ICreateUserViewProps) { + const { onSubmit, inputNames, stateHandler } = props; + const { inputStates, inputsSetStates } = stateHandler; + + const inputs = inputNames.map((inputName) => { + const title = staticMessages.global[inputName] as string; + return ( + + ); + }); return ( -
-
- - - -
+
+
{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 new file mode 100644 index 0000000..6cd6c7b --- /dev/null +++ b/src/driving/application/core/create-user/view/protocols.ts @@ -0,0 +1,11 @@ +import { INewUserData } from '~/business-logic/core/users/create-user/create-account/data/dto/protocols'; +import { SetStateInputMethod } from '~/driven/utils/components/inputs/simple-input/SimpleInput'; + +export default interface ICreateUserViewProps { + onSubmit: (e: React.FormEvent) => void; + stateHandler: { + inputStates: INewUserData; + inputsSetStates: SetStateInputMethod; + }; + inputNames: (keyof INewUserData)[]; +} diff --git a/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts b/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts new file mode 100644 index 0000000..55ed5e6 --- /dev/null +++ b/src/driving/application/core/create-user/viewmodel/CreateUserVM.ts @@ -0,0 +1,45 @@ +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 ICreateUserViewProps from '../view/protocols'; +import IUseCreateUserVm from './protocols'; + +const inputStateInitialValue: INewUserData = { + firstname: '', + lastname: '', + phonenumber: '', +}; + +const inputNames: (keyof INewUserData)[] = ['firstname', 'lastname', 'phonenumber']; + +const useCreateUserVM = (dependencies: IUseCreateUserVm): ICreateUserViewProps => { + const { handleSubmitForm } = dependencies; + const [inputsValue, setInputValues] = useState(inputStateInitialValue); + + const inputsSetStates = (name: keyof INewUserData, newValue: string) => { + if (name === 'phonenumber' && !checkPhoneNumberInput(newValue)) return; + setInputValues((prev) => ({ + ...prev, + [name]: newValue, + })); + }; + + const onSubmitCreateUserForm = (e: React.FormEvent) => { + e.preventDefault(); + console.log('submit user', inputsValue); + handleSubmitForm(inputsValue); + }; + + const inputStates: INewUserData = { ...inputsValue }; + + return { + stateHandler: { + inputsSetStates, + inputStates, + }, + onSubmit: onSubmitCreateUserForm, + inputNames, + }; +}; + +export default useCreateUserVM; diff --git a/src/driving/application/core/create-user/viewmodel/protocols.ts b/src/driving/application/core/create-user/viewmodel/protocols.ts new file mode 100644 index 0000000..d6033ac --- /dev/null +++ b/src/driving/application/core/create-user/viewmodel/protocols.ts @@ -0,0 +1,5 @@ +import { INewUserData } from '~/business-logic/core/users/create-user/create-account/data/dto/protocols'; + +export default interface IUseCreateUserVm { + handleSubmitForm: (newUserData: INewUserData) => void; +} diff --git a/src/driving/application/core/users-list/view/UsersListView.tsx b/src/driving/application/core/users-list/view/UsersListView.tsx index 0535ef8..18ef379 100644 --- a/src/driving/application/core/users-list/view/UsersListView.tsx +++ b/src/driving/application/core/users-list/view/UsersListView.tsx @@ -27,7 +27,7 @@ export default function UsersListView(props: IUserListProps) { }); }, [usersList]); const tableTitles: Pick = { - firstname: staticMessages.global.fistname, + firstname: staticMessages.global.firstname, lastname: staticMessages.global.lastname, };