Skip to content

Commit

Permalink
Fixed formmating for Toms sake
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake McCoy - New committed May 9, 2024
1 parent b893ff3 commit 46bca45
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 72 deletions.
148 changes: 76 additions & 72 deletions contextManager.tsx
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,
email
}));
};
// 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,
// email
// }));
// };

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>;
}
79 changes: 79 additions & 0 deletions hooks/useUser.ts
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,
};
};

0 comments on commit 46bca45

Please sign in to comment.