-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jake McCoy - New
committed
May 9, 2024
1 parent
b893ff3
commit 46bca45
Showing
2 changed files
with
155 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,86 @@ | ||
import { createContext, useState } from 'react'; | ||
import { Card } from './mock_data'; | ||
import { createContext, useState } from "react"; | ||
import { Card } from "./mock_data"; | ||
import { useUser, useUserReturn } from "./hooks/useUser"; | ||
|
||
//Interface Type for my | ||
export interface user { | ||
id: string | ||
firstName: string | ||
lastName: string | ||
email: string | ||
phone: number | ||
address: string | ||
password: string | ||
isMember: boolean | ||
cards: Card[] | ||
setName: (firstName: string, lastName: string) => void | ||
setEmail: (email: string) => void | ||
setPhone: (phone: number) => void | ||
setAddress: (address: string) => void | ||
setPassword: (password: string) => void | ||
setIsMember: (isMember: boolean) => void | ||
addCard: (newCard: Card) => void | ||
//Interface Type for my | ||
export interface User { | ||
id: string; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phone: number; | ||
address: string; | ||
password: string; | ||
isMember: boolean; | ||
cards: Card[]; | ||
setName: (firstName: string, lastName: string) => void; | ||
setEmail: (email: string) => void; | ||
setPhone: (phone: number) => void; | ||
setAddress: (address: string) => void; | ||
setPassword: (password: string) => void; | ||
setIsMember: (isMember: boolean) => void; | ||
addCard: (newCard: Card) => void; | ||
} | ||
|
||
const UserContext = createContext<user | null>(null); | ||
//const UserContext = createContext<User | null>(null); | ||
const UserContext = createContext<useUserReturn>(null as any); | ||
|
||
function UserProvider({ children }: any) { | ||
const [user, setUser] = useState<user>(null as any); | ||
|
||
const setName = (firstName: string, lastName: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
firstName, | ||
lastName, | ||
})); | ||
}; | ||
// const [user, setUser] = useState<User>(null as any); | ||
|
||
const setEmail = (email: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
})); | ||
}; | ||
// const setName = (firstName: string, lastName: string) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// firstName, | ||
// lastName, | ||
// })); | ||
// }; | ||
|
||
const setPhone = (phone: number) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
phone | ||
})); | ||
}; | ||
|
||
const setAddress = (address: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
address | ||
})); | ||
}; | ||
// const setEmail = (email: string) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// })); | ||
// }; | ||
|
||
const setPassword = (password: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
password | ||
})); | ||
}; | ||
// const setPhone = (phone: number) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// phone | ||
// })); | ||
// }; | ||
|
||
const setIsMember = (isMember: boolean) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
isMember | ||
})); | ||
}; | ||
// const setAddress = (address: string) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// address | ||
// })); | ||
// }; | ||
|
||
const addCard = (newCard: Card) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
cards: [...(prevUser?.cards || []), newCard], | ||
})); | ||
}; | ||
|
||
const value = user ? { ...user, setName, setEmail, setAddress, setPassword, setIsMember, addCard} : null; | ||
|
||
return <UserContext.Provider value={value}>{children}</UserContext.Provider>; | ||
} | ||
// const setPassword = (password: string) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// password | ||
// })); | ||
// }; | ||
|
||
// const setIsMember = (isMember: boolean) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// isMember | ||
// })); | ||
// }; | ||
|
||
// const addCard = (newCard: Card) => { | ||
// setUser((prevUser) => ({ | ||
// ...prevUser, | ||
// cards: [...(prevUser?.cards || []), newCard], | ||
// })); | ||
// }; | ||
|
||
// const value = user ? { ...user, setName, setEmail, setAddress, setPassword, setIsMember, addCard} : null; | ||
|
||
const value = useUser(); | ||
|
||
return <UserContext.Provider value={value}>{children}</UserContext.Provider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { User } from "@/contextManager"; | ||
import { Card } from "@/mock_data"; | ||
import { useState } from "react"; | ||
|
||
export interface useUserReturn { | ||
user: User | null; | ||
setName: (firstName: string, lastName: string) => void; | ||
setEmail: (email: string) => void; | ||
setPhone: (phone: number) => void; | ||
setAddress: (address: string) => void; | ||
setPassword: (password: string) => void; | ||
setIsMember: (isMember: boolean) => void; | ||
addCard: (newCard: Card) => void; | ||
} | ||
|
||
export const useUser = (): useUserReturn => { | ||
const [user, setUser] = useState<User>(null as any); | ||
|
||
const setName = (firstName: string, lastName: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
firstName, | ||
lastName, | ||
})); | ||
}; | ||
|
||
const setEmail = (email: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
email, | ||
})); | ||
}; | ||
|
||
const setPhone = (phone: number) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
phone, | ||
})); | ||
}; | ||
|
||
const setAddress = (address: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
address, | ||
})); | ||
}; | ||
|
||
const setPassword = (password: string) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
password, | ||
})); | ||
}; | ||
|
||
const setIsMember = (isMember: boolean) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
isMember, | ||
})); | ||
}; | ||
|
||
const addCard = (newCard: Card) => { | ||
setUser((prevUser) => ({ | ||
...prevUser, | ||
cards: [...(prevUser?.cards || []), newCard], | ||
})); | ||
}; | ||
|
||
return { | ||
user, | ||
setName, | ||
setEmail, | ||
setPhone, | ||
setAddress, | ||
setPassword, | ||
setIsMember, | ||
addCard, | ||
}; | ||
}; |