Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import styles from '../ClientView/ClientView.css'
  3. import Select from 'react-select'
  4.  
  5. var localStorage = require('localStorage')
  6. var userRoles = []
  7.  
  8. // check for localStorage before creating a new item
  9. // failsafes need to be added here, some data isn't passed the same and it breaks
  10. function checkLocalStorageAndConvert () {
  11.   let storedRoles = localStorage.getItem('User_Roles')
  12.   if (JSON.parse(localStorage.getItem('User_Roles')) === null) {
  13.     console.log('local storage is empty ')
  14.   } else {
  15.     storedRoles = JSON.parse(localStorage.getItem('User_Roles'))
  16.     let i = 0
  17.     for (i = 0; i < storedRoles.length; i++) {
  18.       userRoles.push(storedRoles[i])
  19.     }
  20.   }
  21. }
  22.  
  23. checkLocalStorageAndConvert()
  24.  
  25. const formOptions = [
  26.   { value: `${userRoles[0]}`, label: `${userRoles[0]}` },
  27.   { value: `${userRoles[1]}`, label: 'Test Firm 1' },
  28.   { value: `${userRoles[2]}`, label: 'Test Firm 2' }
  29. ]
  30.  
  31. // empty until function below puts roles in it
  32. var testOptions = [
  33. ]
  34. // for each item in the array, create a key + value pair in the options object
  35. userRoles.forEach(function (element, index) {
  36.   testOptions[`Role_${index + 1}`] = element
  37. })
  38. console.log(testOptions)
  39.  
  40. export default class Dashboard extends React.Component {
  41.   constructor (props) {
  42.     super(props)
  43.     this.state = {
  44.       selectionMade: null,
  45.       selectedOption: [],
  46.       isHidden: true
  47.     }
  48.   }
  49.  
  50.   componentDidMount () {
  51.   }
  52.  
  53.   render () {
  54.     return (
  55.       <div id='dashboardContainer'>
  56.         <h2>Clients:</h2>
  57.         <ClientView selectionMade={this.state.selectionMade} />
  58.       </div>
  59.     )
  60.   }
  61. }
  62.  
  63. // first dropdown menu -- ClientView
  64. const clientOptions = [
  65.   { value: `${testOptions.Role_1}`, label: `${testOptions.Role_1}` },
  66.   { value: `${testOptions.Role_2}`, label: `${testOptions.Role_2}` },
  67.   { value: `${testOptions.Role_3}`, label: `${testOptions.Role_3}` }
  68. ]
  69.  
  70. export class ClientView extends React.Component {
  71.   constructor (props) {
  72.     super(props)
  73.     this.state = {
  74.       selectionMade: null,
  75.       selectedOption: [],
  76.       isHidden: true
  77.     }
  78.     this.handleClientSelection = this.handleClientSelection.bind(this)
  79.   }
  80.  
  81.   handleChange (event) {
  82.     event.preventDefault()
  83.   }
  84.  
  85.   handleClientSelection = (selectedOption) => {
  86.     this.setState({ selectedOption })
  87.     this.setState({ selectionMade: true })
  88.     this.setState({ isHidden: false })
  89.   }
  90.  
  91.   // if a selection has not been made, this method keeps formView from rendering
  92.   selectionMade () {
  93.     if (this.state.selectionMade === null) {
  94.       return false
  95.     } else {
  96.       return true
  97.     }
  98.   }
  99.  
  100.   render () {
  101.     return (
  102.       <div id='clientContainer' className='App'>
  103.         <Select options={clientOptions} styles={styles} onChange={this.handleClientSelection} onInputChange={this.handleFormSelection} />
  104.         <br />
  105.         <hr />
  106.         <>
  107.           {this.selectionMade() && <FormView />}
  108.         </>
  109.       </div>
  110.     );
  111.   }
  112. }
  113.  
  114. export class FormView extends React.Component {
  115.   constructor (props) {
  116.     super(props)
  117.     this.state = {
  118.       visibility: false,
  119.       isHidden: true
  120.     }
  121.   }
  122.  
  123.   handleFormSelection (event) {
  124.     event.preventDefault()
  125.   }
  126.  
  127.   shouldComponentUpdate () {
  128.     return true
  129.   }
  130.  
  131.   render () {
  132.     return (
  133.       <div id='formContainer'>
  134.         <h2>Forms:</h2>
  135.         <Select options={formOptions} onChange={this.handleFormSelection} isHidden={this.state.isHidden} key />
  136.       </div>
  137.     )
  138.   }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement