Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import { connect } from 'react-redux'
  3. import { graphql, Query } from 'react-apollo'
  4. import { Button, Modal, Form, Checkbox } from 'semantic-ui-react'
  5.  
  6. import { createArea } from '../../actions/area'
  7. import { AddArea, GetArea } from '../../graphql/mutations/area_mutation'
  8.  
  9. import { AddFacility } from '../../graphql/mutations/feature_mutation'
  10.  
  11. import ThankYouModal from './ThankYouModal'
  12. import { getUser } from '../../graphql/queries/users'
  13. import { listofAreas } from '../../graphql/queries/areas'
  14. //listofArea
  15.  
  16. class ModalUserAddDSA extends Component {
  17.  
  18.  
  19.     state = {
  20.         dates: [
  21.             'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'
  22.         ],
  23.         form: {
  24.             id_user: '',
  25.             name: '',
  26.             address: '',
  27.             days: [],
  28.             in_valid: true,
  29.             poster_ads: null,
  30.             start_time: '01:00 AM',
  31.             end_time: '01:00 AM',
  32.             facilities: [],
  33.             displayMessage: false,
  34.             user_id: '0',
  35.             // well_ventilated: '',
  36.             openarea: '0',
  37.             enclosedarea: '0',
  38.             wifi: '0',
  39.             seats: '0',
  40.             ashtrays: '0',
  41.             trashbin: '0',
  42.             spacious: '0',
  43.             roof: '0',
  44.             area_name: '0',
  45.         },
  46.         hours: Array(24).fill().map(
  47.             (item, index) => {
  48.                 const hour = index + 1
  49.                 const isAMorPM = hour < 13 ? 'AM' : 'PM'
  50.                 const pmHour = hour - 12
  51.                 const time =
  52.                     hour < 10
  53.                         ? `0${hour}`
  54.                         : hour > 12
  55.                             ? pmHour < 10
  56.                                 ? `0${pmHour}`
  57.                                 : pmHour
  58.                             : hour
  59.  
  60.                 return `${time}:00 ${isAMorPM}`
  61.             }
  62.         ),
  63.         // facility: [
  64.         //   'Well-ventilated',
  65.         //   'Open Area',
  66.         //   'Enclosed Area',
  67.         //   'Wifi',
  68.         //   'Seats',
  69.         //   'Ashtrays',
  70.         //   'Trash Bins',
  71.         //   'Spacious',
  72.         //   'Roof',
  73.         //   'area_name'
  74.         // ],
  75.         area_name: 'default'
  76.     }
  77.  
  78.  
  79.     render() {
  80.  
  81.         const { open } = this.props
  82.         const { dates, hours, displayMessage, form: { name, address, in_valid, start_time, end_time, well_ventilated }, facility } = this.state
  83.  
  84.         return (
  85.             <Query query={getUser}>
  86.                
  87.                 {({ data }) => {
  88.                     console.log(data.user.profile.first_name, "USERR")
  89.                     console.log(data, "USER DATA")
  90.                    
  91.                     // this.setState({
  92.                     //     id_user: data.user.id
  93.                     // })
  94.                    
  95.                     return (
  96.                         //
  97.                         <Query query={listofAreas}>
  98.                         {({ data }) => {
  99.                            // console.log(data)
  100.                             return (
  101.                             <div>
  102.                             <ThankYouModal open={displayMessage} onDone={this.handleModalClose} />
  103.                             <Modal size="small" className="edit-profile default" open={open} onClose={this.handleClose} closeIcon trigger={<Button>Scrolling Content Modal</Button>}>
  104.                                 <Modal.Content>
  105.                                     <Modal.Description>
  106.                                         <div className="redaeh">
  107.                                             <h4>Add a Location</h4>
  108.                                         </div>
  109.                                         <div className="body">
  110.                                             <div className="yo_form is-edit">
  111.                                                 <Form onSubmit={this.handleSubmit}>
  112.                                                     <Form.Field>
  113.                                                         <label>Location Name <span>*</span></label>
  114.                                                         <input required name="name" value={name} onChange={this.handleChange} type="text" />
  115.                                                     </Form.Field>
  116.                                                     {/*
  117.                                         <Form.Field>
  118.                                             <label>Address <span>*</span></label>
  119.                                             <input required name="address" value={address} onChange={this.handleChange} type="text" />
  120.                                         </Form.Field>
  121.                                         */}
  122.                                                     <Form.Group className="in-line">
  123.                                                         <p>Days Available</p>
  124.                                                         {dates && dates.map(
  125.                                                             (label, i) => (
  126.                                                                 <Form.Field key={`label-for-${label}`}>
  127.                                                                     <Checkbox onChange={() => this.handleCheckboxChange(i, label)} label={label} />
  128.                                                                 </Form.Field>
  129.                                                             )
  130.                                                         )}
  131.                                                     </Form.Group>
  132.                                                     <Form.Group className="col2">
  133.                                                         <p>Time Available</p>
  134.                                                         <Form.Field>
  135.                                                             <div className="yo_select">
  136.                                                                 <select required value={start_time} onChange={this.handleChange} name='start_time'>
  137.                                                                     <option defaultValue disabled>From</option>
  138.                                                                     {hours && hours.map(
  139.                                                                         (timeString, index) => (
  140.                                                                             <option key={index} value={timeString} defaultValue children={timeString} />
  141.                                                                         )
  142.                                                                     )}
  143.                                                                 </select>
  144.                                                             </div>
  145.                                                         </Form.Field>
  146.                                                         <Form.Field>
  147.                                                             <div className="yo_select">
  148.                                                                 <select required value={end_time} onChange={this.handleChange} name='end_time'>
  149.                                                                     <option defaultValue disabled>To</option>
  150.                                                                     {hours && hours.map(
  151.                                                                         (timeString, index) => (
  152.                                                                             <option key={index} value={timeString} defaultValue children={timeString} />
  153.                                                                         )
  154.                                                                     )}
  155.                                                                 </select>
  156.                                                             </div>
  157.                                                         </Form.Field>
  158.                                                     </Form.Group>
  159.                                                     <Form.Field>
  160.                                                         <label>Photo</label>
  161.                                                         <input type="file" onChange={this.handleFileChange} name='poster_ads' />
  162.                                                     </Form.Field>
  163.                                                     <Form.Group className="in-line">
  164.                                                         <p>Facilities</p>
  165.                                                         {/* <input required name="name" value={name} onChange={this.handleChange} type="text" /> */}
  166.                                                         <Form.Field>
  167.                                                             <div className="">
  168.                                                                 <input className="" checked={this.state.checked} type="checkbox" label="Well-ventilated" name="well-ventilated"
  169.                                                                 value={well_ventilated}
  170.                                                                 onChange={this.handleFacilityObject.bind(this)} />
  171.                                                                
  172.                                                             </div>
  173.                                                             <label>Well-ventilated </label>
  174.                                                         </Form.Field>
  175.                                                        
  176.                                                         <Form.Field>
  177.                                                             <Checkbox label="Open Area" name="openarea" value={this.openarea} onChange={this.handleFacilityObject.bind(this)} />
  178.                                                         </Form.Field>
  179.                                                         <Form.Field>
  180.                                                             <Checkbox label="Enclosed Area" name="enclosedarea" value={this.enclosedarea} onChange={this.handleFacilityObject} />
  181.                                                         </Form.Field>
  182.                                                         <Form.Field>
  183.                                                             <Checkbox label="Wifi" name="wifi" value={this.wifi} onChange={this.handleFacilityObject} />
  184.                                                         </Form.Field>
  185.                                                         <Form.Field>
  186.                                                             <Checkbox label="Seats" name="seats" value={this.seats} onChange={this.handleFacilityObject} />
  187.                                                         </Form.Field>
  188.                                                         <Form.Field>
  189.                                                             <Checkbox label="Ashtrays" name="ashtrays" value={this.ashtrays} onChange={this.handleFacilityObject} />
  190.                                                         </Form.Field>
  191.                                                         <Form.Field>
  192.                                                             <Checkbox label="Trash Bins" name="trashbin" value={this.trashbin} onChange={this.handleFacilityObject} />
  193.                                                         </Form.Field>
  194.                                                         <Form.Field>
  195.                                                             <Checkbox label="Spacious" name="spacious" value={this.spacious} onChange={this.handleFacilityObject} />
  196.                                                         </Form.Field>
  197.                                                         <Form.Field>
  198.                                                             <Checkbox label="Roof" name="roof" value={this.roof} onChange={this.handleFacilityObject} />
  199.                                                        </Form.Field>
  200.                                                         {/* { facility && facility.map(
  201.                                                           (label, i ) => (
  202.                                                             <Form.Field key={`label-for-${label}`}>
  203.                                                               <Checkbox onChange={() => this.handleCheckboxFacilityChange(i, label)} label={label} />
  204.                                                             </Form.Field>
  205.                                                           )
  206.                                                         )} */}
  207.                                                     </Form.Group>
  208.                                                     <div className="text-right">
  209.                                                         <Button disabled={in_valid} className='default' type='submit'>Submit</Button>
  210.  
  211.                                                     </div>
  212.                                                 </Form>
  213.                                             </div>
  214.                                         </div>
  215.                                     </Modal.Description>
  216.                                 </Modal.Content>
  217.                             </Modal>
  218.                         </div>)
  219.                        
  220.                     }}
  221.                     </Query>)
  222.                 }}
  223.                
  224.             </Query>
  225.         )
  226.     }
  227.  
  228.  
  229.  
  230.     handleSubmit = () => {
  231.         const reader = new FileReader()
  232.         const { mutate, createArea, coordinate: { lat, lng } } = this.props
  233.       //console.log(this.props)
  234.         const { form: {
  235.             days,  
  236.             openarea,
  237.             enclosedarea,
  238.             wifi,
  239.             seats,
  240.             ashtrays,
  241.             trashbin,
  242.             spacious,
  243.             roof,
  244.             area_name,
  245.             poster_ads, in_valid, ...rest },
  246.         } = this.state
  247.         const data = {
  248.             ...rest,
  249.             days: `[${days.join(', ')}]`,
  250.             days_of_availability: '',
  251.             time_schedule: '',
  252.             notes: '',
  253.             partnership: '',
  254.             promotional_ads: '',
  255.             date_period: '',
  256.             lat: `${lat}`,
  257.             long: `${lng}`,
  258.             typeOfDSA: 'userdsa',
  259.             verify: '0',
  260.             // facilities: `[${facilities.join(', ')}]`
  261.             user_id: '69',
  262.             well_ventilated: '',
  263.             // facilities: `[${facilities}]`
  264.             openarea: '0',
  265.             enclosedarea: '0',
  266.             wifi: '0',
  267.             seats: '0',
  268.             ashtrays: '0',
  269.             trashbin: '0',
  270.             spacious: '0',
  271.             roof: '0',
  272.             area_name: ''
  273.  
  274.         }
  275.  
  276.         console.log(data.well_ventilated, "FACILITIES")
  277.         console.log(data.name, "Name")
  278.         console.log(data)
  279.  
  280.  
  281.  
  282.         this.setState({ displayMessage: true })
  283.  
  284.         if (poster_ads) {
  285.             reader.readAsBinaryString(poster_ads)
  286.             reader.onload = () => {
  287.                 const dataWithFile = { ...data, poster_ads: `data:application/octet-stream;base64,${btoa(reader.result)}` }
  288.  
  289.                createArea(mutate, dataWithFile, true)
  290.             }
  291.  
  292.             return
  293.         }
  294.       createArea(mutate, { ...data, poster_ads: '' }, true)
  295.     }
  296.  
  297.     // handleFacilityObject = ({ target: { name, value } }) => {
  298.        
  299.     //     console.log([name], "NAME")
  300.  
  301.     // }
  302.  
  303.     handleFacilityObject (e) {
  304.         let value = e.target.value
  305.             if (e.target.checked && e.target.name === "well-ventilated") {
  306.                 this.setState(({ data }) => ({
  307.                     data: {
  308.                         ...data,
  309.                         well_ventilated: '1'
  310.                     }
  311.                 }))
  312.             } else {
  313.                 this.setState(({ data }) => ({
  314.                     data: {
  315.                         ...data,
  316.                         well_ventilated: '0'
  317.                     }
  318.                 }))
  319.             }
  320.         // console.log(well_ventilated, "state")
  321.         console.log(value)
  322.         console.log(e.target.checked)  
  323.     }
  324.  
  325.     handleChange = ({ target: { name, value } }) => {
  326.         this.setState(({ form }) => ({
  327.             form: {
  328.                 ...form,
  329.                 [name]: value
  330.             }
  331.         }))
  332.     }
  333.  
  334.     handleFileChange = ({ target: { files } }) => {
  335.         this.setState(({ form }) => ({
  336.             form: {
  337.                 ...form,
  338.                 poster_ads: files ? files[0] : null
  339.             }
  340.         }))
  341.     }
  342.  
  343.     handleCheckboxFacilityChange = (index, label) => {
  344.       this.setState(({ form }) => {
  345.         const facilities =
  346.                  form.facilities.includes(label)
  347.                     ? form.facilities.filter((fa) => fa !== label)
  348.                    : form.facilities.length ? [...form.facilities, label] : [label]
  349.  
  350.           //const facilities = [...form.facilities, label] : ]label]
  351.  
  352.             return {
  353.                 form: {
  354.                   ...form,
  355.                   facilities
  356.                 }
  357.             }
  358.         })
  359.     }
  360.  
  361.  
  362.     handleCheckboxChange = (index, label) => {
  363.         this.setState(({ form }) => {
  364.             const days =
  365.                 form.days.includes(label)
  366.                     ? form.days.filter((day) => day !== label)
  367.                     : form.days.length ? [...form.days, label] : [label]
  368.             return {
  369.                 form: {
  370.                     ...form,
  371.                     days,
  372.                     in_valid: days && days.length < 1
  373.                 }
  374.             }
  375.         })
  376.     }
  377.  
  378.     handleClose = () => {
  379.         this.props.onClose()
  380.     }
  381.  
  382.     handleModalClose = () => {
  383.         this.setState({ displayMessage: false })
  384.         window.location.href = '/map'
  385.     }
  386. }
  387.  
  388. const ModalUserAddDSAComponent = graphql(AddArea)(ModalUserAddDSA)
  389.  
  390. export default connect(null, { createArea })(ModalUserAddDSAComponent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement