From 3cbfb9767dbc8aa2c7c80521bce2f5926b4e2985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ruiz?= Date: Tue, 21 May 2024 07:22:52 +0200 Subject: [PATCH 1/2] Created model and provider for sign up --- intranet/src/app.component.tsx | 11 +++-- intranet/src/core/model/index.ts | 11 +++++ intranet/src/core/providers/index.ts | 1 + intranet/src/core/providers/sign-up/index.ts | 2 + .../providers/sign-up/sign-up.context.tsx | 4 ++ .../core/providers/sign-up/sign-up.model.ts | 26 ++++++++++ .../providers/sign-up/sign-up.provider.tsx | 48 +++++++++++++++++++ intranet/src/pods/sign-up/sign-up.pod.tsx | 5 +- 8 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 intranet/src/core/model/index.ts create mode 100644 intranet/src/core/providers/index.ts create mode 100644 intranet/src/core/providers/sign-up/index.ts create mode 100644 intranet/src/core/providers/sign-up/sign-up.context.tsx create mode 100644 intranet/src/core/providers/sign-up/sign-up.model.ts create mode 100644 intranet/src/core/providers/sign-up/sign-up.provider.tsx diff --git a/intranet/src/app.component.tsx b/intranet/src/app.component.tsx index 628822f..e3c4eee 100644 --- a/intranet/src/app.component.tsx +++ b/intranet/src/app.component.tsx @@ -9,12 +9,15 @@ import { CssBaseline, ThemeProvider } from '@mui/material'; import { AppRouter } from '#core/router'; import { mainTheme } from '#core/theme'; +import { SignUpProvider } from '#core/providers/'; export const App = () => { return ( - - - - + + + + + + ); }; diff --git a/intranet/src/core/model/index.ts b/intranet/src/core/model/index.ts new file mode 100644 index 0000000..c3ac11e --- /dev/null +++ b/intranet/src/core/model/index.ts @@ -0,0 +1,11 @@ +export interface Restaurant { + name: string; + address: string; + city: string; + phoneNumber: string; +} + +export interface Beer { + beerId: string; + beerName: string; +} diff --git a/intranet/src/core/providers/index.ts b/intranet/src/core/providers/index.ts new file mode 100644 index 0000000..7ab4f54 --- /dev/null +++ b/intranet/src/core/providers/index.ts @@ -0,0 +1 @@ +export * from './sign-up'; diff --git a/intranet/src/core/providers/sign-up/index.ts b/intranet/src/core/providers/sign-up/index.ts new file mode 100644 index 0000000..074dc22 --- /dev/null +++ b/intranet/src/core/providers/sign-up/index.ts @@ -0,0 +1,2 @@ +export * from './sign-up.context'; +export * from './sign-up.provider'; diff --git a/intranet/src/core/providers/sign-up/sign-up.context.tsx b/intranet/src/core/providers/sign-up/sign-up.context.tsx new file mode 100644 index 0000000..a8c17d6 --- /dev/null +++ b/intranet/src/core/providers/sign-up/sign-up.context.tsx @@ -0,0 +1,4 @@ +import React from 'react'; +import { SignUpContextModel } from './sign-up.model'; + +export const SignUpContext = React.createContext(null); diff --git a/intranet/src/core/providers/sign-up/sign-up.model.ts b/intranet/src/core/providers/sign-up/sign-up.model.ts new file mode 100644 index 0000000..6bb4bf9 --- /dev/null +++ b/intranet/src/core/providers/sign-up/sign-up.model.ts @@ -0,0 +1,26 @@ +import { Restaurant, Beer } from '#core/model'; + +export interface SignUpModel { + userName: string; + password: string; + email: string; + restaurants: Restaurant[]; + beers: Beer[]; +} + +export const createInitialSignUp = (): SignUpModel => ({ + userName: '', + password: '', + email: '', + restaurants: [], + beers: [], +}); + +export interface SignUpContextModel { + signUpData: SignUpModel; + setUserName: (userName: string) => void; + setPassword: (password: string) => void; + setEmail: (email: string) => void; + setRestaurants: (restaurants: Restaurant[]) => void; + setBeers: (beers: Beer[]) => void; +} diff --git a/intranet/src/core/providers/sign-up/sign-up.provider.tsx b/intranet/src/core/providers/sign-up/sign-up.provider.tsx new file mode 100644 index 0000000..af3ab73 --- /dev/null +++ b/intranet/src/core/providers/sign-up/sign-up.provider.tsx @@ -0,0 +1,48 @@ +import React, { useState } from 'react'; +import { SignUpContext } from './sign-up.context'; +import { SignUpModel, createInitialSignUp } from './sign-up.model'; +import { Restaurant, Beer } from '#core/model'; + +interface Props { + children: React.ReactNode; +} + +export const SignUpProvider: React.FC = props => { + const { children } = props; + const [signUpData, setSignUpData] = useState(createInitialSignUp); + + const setUserName = (userName: string) => { + setSignUpData({ ...signUpData, userName }); + }; + + const setPassword = (password: string) => { + setSignUpData({ ...signUpData, password }); + }; + + const setEmail = (email: string) => { + setSignUpData({ ...signUpData, email }); + }; + + const setRestaurants = (restaurants: Restaurant[]) => { + setSignUpData({ ...signUpData, restaurants }); + }; + + const setBeers = (beers: Beer[]) => { + setSignUpData({ ...signUpData, beers }); + }; + + return ( + + {children} + + ); +}; + +export const useSignUpContext = () => { + const context = React.useContext(SignUpContext); + if (context === null) { + throw 'useSignUpContext: looks like you have forgotten to add the provider on top of the app :)'; + } + + return context; +}; diff --git a/intranet/src/pods/sign-up/sign-up.pod.tsx b/intranet/src/pods/sign-up/sign-up.pod.tsx index c9650fb..3646051 100644 --- a/intranet/src/pods/sign-up/sign-up.pod.tsx +++ b/intranet/src/pods/sign-up/sign-up.pod.tsx @@ -1,5 +1,8 @@ import { SignUpComponent } from './sign-up.component'; - +import { useSignUpContext } from '#core/providers/sign-up'; export const SignUpPod: React.FC = () => { + const { signUpData } = useSignUpContext(); + + console.log(signUpData); return ; }; From fe81df1767b4ca6c3226850da1d52ff296c668d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ruiz?= Date: Wed, 22 May 2024 09:55:20 +0200 Subject: [PATCH 2/2] Added suggested changes in PR --- intranet/src/core/model/index.ts | 11 -------- .../core/providers/sign-up/sign-up.model.ts | 26 ++++++++++++++++--- .../providers/sign-up/sign-up.provider.tsx | 7 +++-- 3 files changed, 25 insertions(+), 19 deletions(-) delete mode 100644 intranet/src/core/model/index.ts diff --git a/intranet/src/core/model/index.ts b/intranet/src/core/model/index.ts deleted file mode 100644 index c3ac11e..0000000 --- a/intranet/src/core/model/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -export interface Restaurant { - name: string; - address: string; - city: string; - phoneNumber: string; -} - -export interface Beer { - beerId: string; - beerName: string; -} diff --git a/intranet/src/core/providers/sign-up/sign-up.model.ts b/intranet/src/core/providers/sign-up/sign-up.model.ts index 6bb4bf9..9ff4935 100644 --- a/intranet/src/core/providers/sign-up/sign-up.model.ts +++ b/intranet/src/core/providers/sign-up/sign-up.model.ts @@ -1,10 +1,23 @@ -import { Restaurant, Beer } from '#core/model'; +export interface Restaurant { + name: string; + address: string; + city: string; + phoneNumber: string; +} + +export interface Beer { + id: string; + name: string; + alcohol: number; + volume: number; + photoUrl: string; +} export interface SignUpModel { userName: string; password: string; email: string; - restaurants: Restaurant[]; + restaurant: Restaurant; beers: Beer[]; } @@ -12,7 +25,12 @@ export const createInitialSignUp = (): SignUpModel => ({ userName: '', password: '', email: '', - restaurants: [], + restaurant: { + name: '', + address: '', + city: '', + phoneNumber: '', + }, beers: [], }); @@ -21,6 +39,6 @@ export interface SignUpContextModel { setUserName: (userName: string) => void; setPassword: (password: string) => void; setEmail: (email: string) => void; - setRestaurants: (restaurants: Restaurant[]) => void; + setRestaurants: (restaurant: Restaurant) => void; setBeers: (beers: Beer[]) => void; } diff --git a/intranet/src/core/providers/sign-up/sign-up.provider.tsx b/intranet/src/core/providers/sign-up/sign-up.provider.tsx index af3ab73..ed3a444 100644 --- a/intranet/src/core/providers/sign-up/sign-up.provider.tsx +++ b/intranet/src/core/providers/sign-up/sign-up.provider.tsx @@ -1,7 +1,6 @@ import React, { useState } from 'react'; import { SignUpContext } from './sign-up.context'; -import { SignUpModel, createInitialSignUp } from './sign-up.model'; -import { Restaurant, Beer } from '#core/model'; +import { SignUpModel, createInitialSignUp, Restaurant, Beer } from './sign-up.model'; interface Props { children: React.ReactNode; @@ -23,8 +22,8 @@ export const SignUpProvider: React.FC = props => { setSignUpData({ ...signUpData, email }); }; - const setRestaurants = (restaurants: Restaurant[]) => { - setSignUpData({ ...signUpData, restaurants }); + const setRestaurants = (restaurant: Restaurant) => { + setSignUpData({ ...signUpData, restaurant }); }; const setBeers = (beers: Beer[]) => {