Skip to content

Commit

Permalink
hooksAndUpdateProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo committed Mar 14, 2024
1 parent cf65efb commit 0f47db6
Show file tree
Hide file tree
Showing 15 changed files with 680 additions and 367 deletions.
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"axios": "^1.6.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.0.1",
Expand Down
4 changes: 4 additions & 0 deletions src/assets/avatar_placeholder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 25 additions & 20 deletions src/components/header/index.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { Container,Profile,Logout} from "./style";
import {RiShutDownLine} from'react-icons/ri'

export function Header(){
return(

<Container>
<Profile to="/profile">
<img src="https://github.com/rickccastro.png" alt="foto do usuario" />
<div>
<span>bem vindo</span>
<strong>Ricardo Castro</strong>
</div>
</Profile>
<Logout>
<RiShutDownLine />
</Logout>
</Container>
)
}
import { Container,Profile,Logout} from "./style";
import {RiShutDownLine} from'react-icons/ri'
import { api } from "../../services/api";
import { useAuth } from "../../hooks/auth";

export function Header(){
const {signOut,user}=useAuth()

const avatarUrl=user.avatar ? `${api.defaults.baseURL}/files/${user.avatar}` : avatarPlacehoalder

return(
<Container>
<Profile to="/profile">
<img src={avatarUrl} alt={user.name} />
<div>
<span>bem vindo</span>
<strong>{user.name}</strong>
</div>
</Profile>
<Logout onClick={signOut}>
<RiShutDownLine />
</Logout>
</Container>
)
}
62 changes: 31 additions & 31 deletions src/components/input/style.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import styled from "styled-components";


export const Container = styled.div`
width: 100%;
display: flex;
align-items: center;
background-color: ${({ theme }) => theme.COLORS.BACKGROUND_900} ;
color: ${({ theme }) => theme.COLORS.GRAY_300};
margin-bottom: 8px;
border-radius: 10px;
> input {
height: 56px;
width: 100%;
padding: 12px;
color: ${({ theme }) => theme.COLORS.WHITE};
background: transparent;
border: 0;
&:placeholder {
color: ${({ theme }) => theme.COLORS.GRAY_300}
}
}
> svg {
margin-left: 16px;
}
import styled from "styled-components";


export const Container = styled.div`
width: 100%;
display: flex;
align-items: center;
background-color: ${({ theme }) => theme.COLORS.BACKGROUND_900} ;
color: ${({ theme }) => theme.COLORS.GRAY_300};
margin-bottom: 8px;
border-radius: 10px;
> input {
height: 56px;
width: 100%;
padding: 12px;
color: ${({ theme }) => theme.COLORS.WHITE};
background: transparent;
border: 0;
&::placeholder {
color: ${({ theme }) => theme.COLORS.GRAY_300}
}
}
> svg {
margin-left: 16px;
}
`
101 changes: 101 additions & 0 deletions src/hooks/auth.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { createContext,useContext,useState,useEffect } from "react";

import { api } from "../services/api";

export const AuthContext=createContext({})

function AuthProvider({children}){
const[data,setData]=useState({})

async function signIn({email,password}){
try{
const response=await api.post("/sessions",{email,password})
const {user,token}=response.data

localStorage.setItem("@rocketnotes:user",JSON.stringify(user));
localStorage.setItem("@rocketnotes:token",token);

api.defaults.headers.common['Authorization']=`Bearer ${token}`;

setData({user,token})
}
catch(error){
if(error.response){
alert(error.response.data.message)
}else{
alert("não foi possivel entrar")
}
}
}

function signOut(){
const token =localStorage.removeItem("@rocketnotes:token");
const user = localStorage.removeItem("@rocketnotes:user");

setData({});
}

async function updateProfile({user,avatarFile}){
try{
if(avatarFile){
const fileUpdloadForm=new FormData();

fileUpdloadForm.append("avatar",avatarFile)

const response =await api.patch("/users/avatar",fileUpdloadForm)

user.avatar =response.data.avatar
}


await api.put("/users",user);
localStorage.setItem("@rocketnotes:user",JSON.stringify(user))

setData({user, token: data.token})
alert("perfil atualizado")

}
catch(error){
if(error.response){
console.log(data.token)
console.error(error);
alert(error.response.data.message)
}else{
alert("não foi possivel entrar")
}
}

}

useEffect(()=>{
const token =localStorage.getItem("@rocketnotes:token");
const user = localStorage.getItem("@rocketnotes:user");

if(token && user){
api.defaults.headers.common['Authorization']=`Bearer ${token}`;
setData({
token,
user:JSON.parse(user)
})
}

},[])
return(
<AuthContext.Provider value={
{signIn,
signOut,
updateProfile,
user:data.user,
}}>
{children}

</AuthContext.Provider>
)
}

function useAuth(){
const context= useContext(AuthContext)
return context
}

export {AuthProvider,useAuth}
Loading

0 comments on commit 0f47db6

Please sign in to comment.