source: phonelux-frontend/src/context/UserContext.js@ d66b8eb

Last change on this file since d66b8eb was d66b8eb, checked in by Marko <Marko@…>, 2 years ago

Component for editing offers added

  • Property mode set to 100644
File size: 1.0 KB
Line 
1import axios from 'axios'
2import React, { Component } from 'react'
3
4const UserContext = React.createContext()
5
6export class UserProvider extends Component {
7
8 constructor(props) {
9 super(props)
10
11 this.state = {
12 userId: '',
13 name: '',
14 role: ''
15 }
16 }
17
18 componentDidMount(){
19 if(localStorage.getItem('token'))
20 {
21 let token = localStorage.getItem('token')
22 axios.get('/token/'+token)
23 .then(response => {
24 const {id, firstName,userRole} = response.data
25 this.setState({
26 userId: id,
27 name: firstName,
28 role: userRole
29 })
30 }).catch(error => console.log(error))
31 }
32 }
33
34
35 render() {
36 const {userId,name,role} = this.state
37 return (
38 <UserContext.Provider value={{
39 userId,
40 name,
41 role
42 }}>
43 {this.props.children}
44 </UserContext.Provider>
45 )
46 }
47}
48
49export default UserContext
Note: See TracBrowser for help on using the repository browser.