Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import API_URL from './config.js';
  3. import GetTokenCookie from './components/GetTokenCookie.js';
  4. import Chart from './components/Chart';
  5.  
  6. class Profile extends React.Component {
  7.     constructor(props) {
  8.         super(props);
  9.         this.state = {
  10.             chartData: {},
  11.             profile: {},
  12.             isLoading: true,
  13.             error: {
  14.                 visible: false,
  15.                 message: ""
  16.             }
  17.         }
  18.     }
  19.  
  20.     componentDidMount() {
  21.         const url = API_URL + "/api/customers";
  22.         fetch(url, {
  23.             headers: {
  24.                 'Authorization': 'Bearer ' + GetTokenCookie("uid")
  25.             }
  26.         })
  27.             .then(res => res.json())
  28.             .then(res => {
  29.                 this.setState({ profile: res });
  30.                 this.setState({ isLoading: false });
  31.                 this.setChartData();
  32.             })
  33.             .catch(error => this.setState({ error, isLoading: false }));
  34.     }
  35.  
  36.     setChartData() {
  37.         this.setState({
  38.             chartData: {
  39.                 labels: ['Boston', 'Worcester', 'Springfield', 'Lowell', 'Cambridge', 'New Bedford'],
  40.                 datasets: [
  41.                     {
  42.                         label: 'Population',
  43.                         data: [
  44.                             617594,
  45.                             181045,
  46.                             153060,
  47.                             106519,
  48.                             105162,
  49.                             95072
  50.                         ],
  51.                         backgroundColor: [
  52.                             'rgba(255, 99, 132, 0.6)',
  53.                             'rgba(54, 162, 235, 0.6)',
  54.                             'rgba(255, 206, 86, 0.6)',
  55.                             'rgba(75, 192, 192, 0.6)',
  56.                             'rgba(153, 102, 255, 0.6)',
  57.                             'rgba(255, 159, 64, 0.6)',
  58.                             'rgba(255, 99, 132, 0.6)'
  59.                         ]
  60.                     }
  61.                 ]
  62.             }
  63.         });
  64.     }
  65.  
  66.     render() {
  67.         if (this.state.isLoading) {
  68.             return (
  69.                 <div className="loader">
  70.                     <img src="/loading.gif" />
  71.                     <h2>Loading...</h2>
  72.                 </div>
  73.             );
  74.         }
  75.  
  76.         return (
  77.             <div>
  78.                 <h4>Cześć {this.state.profile.firstName}!</h4>
  79.                 <p class="lead">{this.state.profile.description}</p>
  80.                 <Chart chartData={this.state.chartData} location="Massachusetts" legendPosition="bottom" />
  81.             </div>
  82.         );
  83.     }
  84. }
  85.  
  86. export default Profile;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement