-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from KozielGPC/97.2-adicionar-botao-de-notifi…
…cacoes-no-layout-do-site-1 97.2 adicionar botao de notificacoes no layout do site 1
- Loading branch information
Showing
10 changed files
with
384 additions
and
181 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
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
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
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,117 +1,129 @@ | ||
import { createContext, ReactNode,useEffect,useState } from "react"; | ||
import jwt_decode from "jwt-decode" | ||
import {User} from "../interfaces"; | ||
import {destroyCookie, parseCookies,setCookie} from 'nookies' | ||
import { createContext, ReactNode, useEffect, useState } from "react"; | ||
import jwt_decode from "jwt-decode"; | ||
import { User } from "../interfaces"; | ||
import { destroyCookie, parseCookies } from "nookies"; | ||
import { useRouter } from "next/router"; | ||
import { getUserById } from "@/services/users/retrieve"; | ||
import { Token } from "../interfaces"; | ||
import { getUserById, getMyNotifications } from "@/services/users/retrieve"; | ||
import { Token, Notification } from "../interfaces"; | ||
|
||
type UserContextProps = { | ||
children: ReactNode; | ||
} | ||
children: ReactNode; | ||
}; | ||
|
||
type UserContextType = { | ||
id: Number; | ||
setId: (id: Number) => void; | ||
username: string; | ||
setUsername: (username: string) => void; | ||
email: string; | ||
setEmail: (email: string) => void; | ||
clearUser: () => void; | ||
signin: (token:string) => void | ||
signout: () => void | ||
} | ||
|
||
|
||
id: Number; | ||
setId: (id: Number) => void; | ||
username: string; | ||
setUsername: (username: string) => void; | ||
email: string; | ||
setEmail: (email: string) => void; | ||
notifications: Notification[]; | ||
setNotifications: (notifications: Notification[]) => void; | ||
clearUser: () => void; | ||
signin: (token: string) => void; | ||
signout: () => void; | ||
getNotifications: () => void; | ||
}; | ||
|
||
const initialValue = { | ||
id: -1, | ||
setId: () => {}, | ||
username: "", | ||
setUsername: () => {}, | ||
email: "", | ||
setEmail: () => {}, | ||
clearUser: () => {}, | ||
signin: () => {}, | ||
signout: () => {} | ||
} | ||
|
||
id: -1, | ||
setId: () => {}, | ||
username: "", | ||
setUsername: () => {}, | ||
email: "", | ||
setEmail: () => {}, | ||
notifications: [], | ||
setNotifications: () => {}, | ||
clearUser: () => {}, | ||
signin: () => {}, | ||
signout: () => {}, | ||
getNotifications: () => {}, | ||
}; | ||
|
||
export const UserContext = createContext<UserContextType>(initialValue); | ||
|
||
export const UserProvider = ({ children }: UserContextProps) => { | ||
const router = useRouter(); | ||
|
||
export const UserProvider = ({ children }: UserContextProps)=>{ | ||
const router = useRouter(); | ||
|
||
const [id, setId] = useState<Number>(initialValue.id); | ||
const [username, setUsername] = useState<string>(initialValue.username); | ||
const [email, setEmail] = useState<string>(initialValue.email); | ||
const [notifications, setNotifications] = useState<Notification[]>([]); | ||
|
||
const [id, setId] = useState<Number>(initialValue.id); | ||
const [username, setUsername] = useState<string>(initialValue.username); | ||
const [email, setEmail] = useState<string>(initialValue.email); | ||
const token = parseCookies()["championship-token"]; | ||
|
||
const token = parseCookies()["championship-token"]; | ||
|
||
useEffect( | ||
() => { | ||
if(token){ | ||
signin(token) | ||
}; | ||
},[token] | ||
) | ||
|
||
function clearUser(){ | ||
setId(-1); | ||
setUsername(""); | ||
useEffect(() => { | ||
if (token) { | ||
signin(token); | ||
getNotifications(); | ||
} | ||
|
||
|
||
async function signin(token:string){ | ||
try{ | ||
setCookie(null, "championship-token", token, { | ||
maxAge: 60 * 60 * 24, // 24 hours | ||
}); | ||
const tokenData:Token = jwt_decode(token); | ||
if(!tokenData.id){ | ||
throw new Error("Invalid token"); | ||
} | ||
const response = await getUserById(tokenData.id.toString()); | ||
if(response.status == "error" || !response.data )throw new Error(response.message); | ||
|
||
const userData:User = response.data; | ||
|
||
setId(userData.id); | ||
setUsername(userData.username); | ||
setEmail(userData.email) | ||
} | ||
catch{ | ||
signout() | ||
} | ||
}, [token]); | ||
|
||
function clearUser() { | ||
setId(-1); | ||
setUsername(""); | ||
} | ||
|
||
async function getNotifications() { | ||
const response = await getMyNotifications(); | ||
if (response && response.data) { | ||
const notificationsArray: Notification[] = response.data; | ||
if (notificationsArray) { | ||
setNotifications(notificationsArray.reverse()); | ||
} | ||
} | ||
|
||
function signout(){ | ||
destroyCookie(null,'championship-token',{path: '/'}); | ||
clearUser(); | ||
router.push('/signin'); | ||
} | ||
|
||
useEffect(() => { | ||
console.log(notifications); | ||
}, [notifications]); | ||
|
||
async function signin(token: string) { | ||
try { | ||
const tokenData: Token = jwt_decode(token); | ||
if (!tokenData.id) { | ||
throw new Error("Invalid token"); | ||
} | ||
const response = await getUserById(tokenData.id.toString()); | ||
if (response.status == "error" || !response.data) | ||
throw new Error(response.message); | ||
|
||
const userData: User = response.data; | ||
|
||
setId(userData.id); | ||
setUsername(userData.username); | ||
setEmail(userData.email); | ||
} catch { | ||
signout(); | ||
} | ||
|
||
return ( | ||
<UserContext.Provider | ||
|
||
value={{ | ||
id, setId, | ||
username,setUsername, | ||
email, setEmail, | ||
clearUser, | ||
signin, signout | ||
}} | ||
|
||
> | ||
{children} | ||
</UserContext.Provider> | ||
) | ||
} | ||
|
||
|
||
} | ||
|
||
function signout() { | ||
destroyCookie(null, "championship-token", { path: "/" }); | ||
clearUser(); | ||
router.push("/signin"); | ||
} | ||
|
||
return ( | ||
<UserContext.Provider | ||
value={{ | ||
id, | ||
setId, | ||
username, | ||
setUsername, | ||
email, | ||
setEmail, | ||
notifications, | ||
setNotifications, | ||
clearUser, | ||
signin, | ||
signout, | ||
getNotifications, | ||
}} | ||
> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
export default UserContext; | ||
|
||
|
||
|
Oops, something went wrong.