-
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 2, 2024
1 parent
5cdd0ac
commit b893ff3
Showing
1 changed file
with
82 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { createContext, useState } from 'react'; | ||
import { Card } from './mock_data'; | ||
|
||
//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); | ||
|
||
function UserProvider({ children }: any) { | ||
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, | ||
})); | ||
}; | ||
|
||
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], | ||
})); | ||
}; | ||
|
||
const value = user ? { ...user, setName, setEmail, setAddress, setPassword, setIsMember, addCard} : null; | ||
|
||
return <UserContext.Provider value={value}>{children}</UserContext.Provider>; | ||
} |