Guest User

Untitled

a guest
Mar 31st, 2019
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.85 KB | None | 0 0
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { bindActionCreators } from 'redux'
  4. import Dropzone from 'react-dropzone'
  5. import classNames from 'classnames'
  6. import Datetime from 'react-datetime'
  7. import moment from 'moment'
  8.  
  9. import {
  10. Modal,
  11. ModalBody,
  12. ModalFooter,
  13. ModalHeader,
  14. Button,
  15. Col,
  16. FormGroup,
  17. Card,
  18. CardHeader,
  19. CardBody,
  20. Row,
  21. Input,
  22. InputGroup,
  23. InputGroupAddon,
  24. InputGroupText
  25. } from 'reactstrap'
  26.  
  27. import { PanelHeader, FormInputs } from '../../components'
  28. import Gallery from '../../components/gallery'
  29. import Question from './components/question'
  30. import Header from './components/header'
  31.  
  32. import * as DataActions from '../../services/actions/data'
  33. import * as CommonActions from '../../services/actions/common'
  34.  
  35.  
  36. import './style.scss'
  37. import { Link } from "react-router-dom";
  38. import logo from "../../assets/img/black-logo.png";
  39.  
  40.  
  41. const mapStateToProps = (state) => {
  42. return ({
  43. selectedChildren: state.data.selectedChildren,
  44. galleries: state.data.galleries,
  45. questions: state.data.questions,
  46. facts: state.data.facts,
  47. user: state.auth.user
  48. })
  49. }
  50.  
  51.  
  52. const mapDispatchToProps = (dispatch) => {
  53. return ({
  54. commonActions: bindActionCreators(CommonActions, dispatch),
  55. dataActions: bindActionCreators(DataActions, dispatch)
  56. })
  57. }
  58.  
  59.  
  60. class Dashboard extends React.Component {
  61. componentDidMount() {
  62.  
  63. if (this.props.selectedChildren.length > 0) {
  64. this.loadGallery(this.props.selectedChildren)
  65. }
  66. this.setState({
  67. name: moment().format('Do MMMM YYYY'),
  68. real_name: moment().format('YYYY-MM-DD')
  69. })
  70. //
  71.  
  72. this.setState({
  73. userDates: this.props.user,
  74. allGalleries: this.props.galleries,
  75. selectedChildren: this.props.selectedChildren
  76. });
  77. }
  78.  
  79. componentDidUpdate() {
  80. //
  81.  
  82. if (this.props.user && this.props.user.children && this.props.user.children.data) {
  83. console.log('iiiii', this.props.user.children.data[0])
  84. this.props.dataActions.addChild(this.props.user.children.data[0])
  85. }
  86. }
  87.  
  88.  
  89. componentWillReceiveProps(newProps) {
  90. if (
  91. this.props.selectedChildren.length != newProps.selectedChildren.length &&
  92. newProps.selectedChildren.length > 0
  93. ) {
  94. this.loadGallery(newProps.selectedChildren)
  95. }
  96. if (newProps.selectedChildren.length == 0 && newProps.location.pathname.indexOf('/dashboard/relative') > -1) {
  97. newProps.history.push('/dashboard/browse')
  98. }
  99. this.setState({
  100. userDates: newProps.user,
  101. allGalleries: newProps.galleries,
  102. selectedChildren: newProps.selectedChildren
  103. });
  104.  
  105. }
  106.  
  107.  
  108. constructor(props) {
  109. super(props)
  110. this.state = {
  111. selectedFiles: [],
  112. name: '',
  113. real_name: '',
  114. year: '',
  115. description: '',
  116. selected_child: 'all',
  117. userDates: [],
  118. allGalleries: [],
  119. years: ['All', '2018', '2017', '2016', '2015'],
  120. selected_year: 'All',
  121. imageSpiner: false,
  122. selectedChildren: [],
  123.  
  124. months: [
  125. { label: 'All', value: 0 },
  126. { label: 'Dec', value: 12 },
  127. { label: 'Nov', value: 11 },
  128. { label: 'Oct', value: 10 },
  129. { label: 'Sep', value: 9 },
  130. { label: 'Aug', value: 8 },
  131. { label: 'Jul', value: 7 },
  132. { label: 'Jun', value: 6 },
  133. { label: 'May', value: 5 },
  134. { label: 'Apr', value: 4 },
  135. { label: 'Mar', value: 3 },
  136. { label: 'Feb', value: 2 },
  137. { label: 'Jan', value: 1 },
  138. ],
  139. selected_month: { label: 'All', value: 0 }
  140. }
  141.  
  142. this.loadGallery = this.loadGallery.bind(this)
  143. this.onDrop = this.onDrop.bind(this)
  144. this.handleInput = this.handleInput.bind(this)
  145. this.handleSubmit = this.handleSubmit.bind(this)
  146.  
  147. this.removeQuestion = this.removeQuestion.bind(this)
  148. this.renderQuestions = this.renderQuestions.bind(this)
  149. this.renderChildren = this.renderChildren.bind(this)
  150.  
  151. this.onClickChild = this.onClickChild.bind(this)
  152. this.onClickYear = this.onClickYear.bind(this)
  153. this.onClickMonth = this.onClickMonth.bind(this)
  154. }
  155.  
  156.  
  157. onClickChild(child_id) {
  158. this.setState({
  159. selected_child: child_id
  160. })
  161. }
  162.  
  163. onClickYear(year) {
  164. this.setState({
  165. selected_year: year
  166. })
  167. }
  168.  
  169. onClickMonth(month) {
  170. let value = 0
  171. this.state.months.map(m => {
  172. if (m.label == month) {
  173. value = m.value
  174. }
  175. })
  176. this.setState({
  177. selected_month: { label: month, value: value }
  178. })
  179. }
  180.  
  181. loadGallery(selectedChildren) {
  182. if (
  183. this.props.history.location.state &&
  184. !this.props.history.location.state.from
  185. ) {
  186. this.props.dataActions.getGaleries(this.props.history.location.state.id, selectedChildren).then(res => {
  187. }).catch(err => {
  188. this.props.commonActions.alert({ type: 'danger', message: err.data.message })
  189. throw err
  190. })
  191. } else {
  192. this.props.dataActions.getGaleries(null, selectedChildren).then(res => {
  193. }).catch(err => {
  194. this.props.commonActions.alert({ type: 'danger', message: err.data.message })
  195. throw err
  196. })
  197. this.props.dataActions.getQuestions(selectedChildren).then(res => {
  198. }).catch(err => {
  199. this.props.commonActions.alert({ type: 'danger', message: err.data.message })
  200. throw err
  201. })
  202. this.props.dataActions.getFacts(selectedChildren).then(res => {
  203. }).catch(err => {
  204. this.props.commonActions.alert({ type: 'danger', message: err.data.message })
  205. throw err
  206. })
  207. }
  208. }
  209.  
  210. onDrop(acceptedFiles, rejectedFiles) {
  211. this.setState(({ selectedFiles }) => ({
  212. selectedFiles: selectedFiles
  213. .concat(acceptedFiles)
  214. .filter(
  215. file => file.type.includes('image') || file.type.includes('video'),
  216. ),
  217. }))
  218. }
  219.  
  220. handleInput(key, value) {
  221. this.setState({ [key]: value })
  222. }
  223.  
  224.  
  225. handleSubmit() {
  226. if (!this.state.name) {
  227. this.props.commonActions.alert({ type: 'danger', message: 'Name is required!' })
  228. return
  229. }
  230. if (!this.state.selectedFiles.length) {
  231. this.props.commonActions.alert({ type: 'danger', message: 'You must provide some files!' })
  232. return
  233. }
  234. if (this.state.selected_child == '') {
  235. this.props.commonActions.alert({ type: 'danger', message: 'You must choose a children or all.' })
  236. return
  237. }
  238.  
  239. if (this.props.location.state && this.props.location.state.permission != 'see-edit-diary') {
  240. this.props.commonActions.alert({ type: 'danger', message: 'You do not have permission.' })
  241. return
  242. }
  243.  
  244. const formData = new FormData();
  245. this.state.selectedFiles.forEach(file => {
  246. formData.append('files[]', file)
  247. })
  248.  
  249. if (this.state.selected_child == 'all') {
  250. this.props.selectedChildren
  251. .map(x => x.id)
  252. .forEach(child => {
  253. formData.append('children[]', child)
  254. })
  255. } else {
  256. formData.append('children[]', this.state.selected_child)
  257. }
  258.  
  259. formData.set('name', this.state.real_name)
  260. formData.set('description', this.state.description)
  261. const URL = this.props.location.state
  262. ? `galleries/${this.props.location.state.id}`
  263. : 'galleries'
  264. this.setState({ imageSpiner: true })
  265. this.props.dataActions.saveGallery(URL, formData, {}).then(res => {
  266. this.loadGallery(this.props.selectedChildren)
  267. this.setState({
  268. selectedFiles: [],
  269. name: moment().format('Do MMMM YYYY'),
  270. real_name: moment().format('YYYY-MM-DD'),
  271. description: '',
  272. imageSpiner: false,
  273. })
  274. this.props.commonActions.alert({ type: 'success', message: 'Successfully added content' })
  275. }).catch(err => {
  276. this.props.commonActions.alert({ type: 'danger', message: err.data.message })
  277. this.setState({ imageSpiner: false })
  278. throw err
  279. })
  280. let d = new Date(this.state.real_name)
  281. let c = d.getFullYear()
  282. let YearStatus = false
  283. if (this.state.userDates.gallery_dates.years) {
  284. for (let i = 0; i < this.state.userDates.gallery_dates.years.length; i++) {
  285.  
  286. if (this.state.userDates.gallery_dates.years[i] === c) {
  287. YearStatus = true
  288.  
  289. }
  290. else {
  291. }
  292. }
  293. }
  294. if (YearStatus === true) {
  295. this.state.userDates = this.state.userDates
  296. }
  297. else {
  298. this.state.userDates = this.state.userDates.gallery_dates.years.push(c)
  299. }
  300. }
  301.  
  302. removeQuestion(id) {
  303. let tempList = []
  304. this.props.questions.map(item => {
  305. if (item.id != id) {
  306. tempList.push(Object.assign({}, item))
  307. }
  308. })
  309. this.props.dataActions.updateQuestions(tempList)
  310. }
  311.  
  312. renderQuestions() {
  313. if (this.props.selectedChildren.length) {
  314. const child_id = this.props.selectedChildren.slice(-1)[0].id
  315. const child = this.props.selectedChildren.slice(-1)[0]
  316. return (
  317. <Question
  318. removeQuestion={this.removeQuestion}
  319. child_id={child_id}
  320. child={child}
  321. {...this.props}
  322. />
  323. )
  324. } else {
  325. return null
  326. }
  327. }
  328.  
  329.  
  330. renderChildren() {
  331. if (this.props.user && this.props.user.children) {
  332. if (
  333. this.props.history.location.state &&
  334. !this.props.history.location.state.from
  335. ) {
  336. return this.props.history.location.state.children.data.map(child => (
  337. <span
  338. key={child.id}
  339. className={child.id == this.state.selected_child ? 'span-active span-child' : 'span-child'}
  340. onClick={() => this.onClickChild(child.id)}
  341. >
  342. {child.name}
  343. </span>
  344. ))
  345. }
  346. return this.props.user.children.data.map(child => (
  347. <span
  348. key={child.id}
  349. className={child.id == this.state.selected_child ? 'span-active span-child' : 'span-child'}
  350. onClick={() => this.onClickChild(child.id)}
  351. >
  352. {child.name}
  353. </span>
  354. ))
  355. }
  356.  
  357. return null
  358. }
  359.  
  360.  
  361.  
  362. render() {
  363. const { user } = this.props
  364. const { userDates } = this.state
  365. const { selected_year, selected_month } = this.state
  366. let newCheck = []
  367. let childrenNumber = 0
  368. if (userDates.children) {
  369. newCheck = userDates.children.data
  370. childrenNumber = newCheck.length > 0 ?
  371. <div></div> :
  372. <div className='addChildren'>
  373. Please add a new children
  374. </div>
  375. }
  376. let galleries = []
  377. this.state.allGalleries.map(item => {
  378. if (selected_year == 'All') {
  379. if (selected_month.label == 'All') {
  380. galleries.push(item)
  381. } else {
  382. let date = new Date(item.created_at.default.date)
  383. if (selected_month.value == date.getMonth() + 1) {
  384. galleries.push(item)
  385. }
  386. }
  387. } else {
  388. let date = new Date(item.created_at.default.date)
  389. if (selected_month.label == 'All') {
  390. if (selected_year == date.getFullYear().toString()) {
  391. galleries.push(item)
  392. }
  393. } else {
  394. if (selected_year == date.getFullYear().toString() && selected_month.value == date.getMonth() + 1) {
  395. galleries.push(item)
  396. }
  397. }
  398. }
  399. })
  400. let showQuestion = this.state.allGalleries.map((questions, i) => {
  401. return (
  402. <div>
  403. {questions.questions.data.map((quest, i) => (
  404. <div key={i}>{quest.name}</div>
  405. ))}
  406. </div>
  407. )
  408. })
  409. return (
  410. <div className="dashboard-page">
  411. <div className="mainLogo logo py-4">
  412. <div className="logo-img d-flex justify-content-center">
  413. <Link to="/">
  414. <img src={logo} alt="react-logo" />
  415. </Link>
  416. </div>
  417. </div>
  418. <PanelHeader
  419. size="lg"
  420. content={
  421. <Header {...this.props} />
  422. }
  423. />
  424. {this.state.imageSpiner ?
  425. <div className="spinner-bg">
  426. <div className="spinner">
  427. <div className="bounce1"></div>
  428. <div className="bounce2"></div>
  429. <div className="bounce3"></div>
  430. </div>
  431. </div> : <div></div>}
  432. {childrenNumber}
  433. <div className="content">
  434. <Row className="position-relative align-items-start">
  435. <Col className="dashboard-content">
  436. <Card>
  437. <CardBody>
  438. <div className="input-content">
  439. <Row>
  440. <Col md={12}>
  441. <FormGroup className="mb-0">
  442. <Input
  443. className="custom-text-field"
  444. type="text"
  445. placeholder="What cool happend today?"
  446. value={this.state.description}
  447. onChange={e => this.handleInput('description', e.target.value)}
  448. />
  449. </FormGroup>
  450. </Col>
  451. </Row>
  452. <Row>
  453. <Col className="d-flex align-items-center justify-content-start flex-wrap tags-bar">
  454. <i className="tag-icon now-ui-icons shopping_tag-content span-child" />
  455. {this.renderChildren()}
  456. <span
  457. className={'all' == this.state.selected_child ? 'span-active span-child' : 'span-child'}
  458. onClick={() => this.onClickChild('all')}
  459. >
  460. All
  461. </span>
  462. </Col>
  463. <Col className="d-flex align-items-center justify-content-center flex-wrap photos-bar">
  464. <Dropzone onDrop={this.onDrop}>
  465. {({ getRootProps, getInputProps, isDragActive }) => {
  466. return (
  467. <div
  468. {...getRootProps()}
  469. className={classNames('dropzone', {
  470. 'dropzone--isActive': isDragActive,
  471. })}
  472. >
  473. <input {...getInputProps()} />
  474. {isDragActive ? (
  475. <div className="d-flex align-items-center justify-content-center flex-wrap">
  476. <span className="span-child p-0 ml-1">Drop file here...</span>
  477. </div>
  478. ) : (
  479. <div className="d-flex align-items-center justify-content-center flex-wrap">
  480. <i className="tag-icon now-ui-icons media-1_camera-compact span-child p-0" />
  481. <span className="span-child p-0 ml-1">Add Photos / Videos</span>
  482. </div>
  483. )}
  484. </div>
  485. );
  486. }}
  487. </Dropzone>
  488. {this.props.selectedChildren.length !== 0 ?
  489. <Button
  490. size="sm"
  491. className="ml-2"
  492. color="primary"
  493. onClick={this.handleSubmit}>
  494. Submit
  495. </Button> :
  496. <Button
  497. size="sm"
  498. className="ml-2"
  499. color="primary"
  500. disabled>
  501. Submit
  502. </Button>}
  503.  
  504. </Col>
  505. <Col className="d-flex align-items-center justify-content-end flex-wrap date-bar">
  506. <InputGroup
  507. className="custom-date no-border form-control-lg"
  508. >
  509. <Datetime
  510. value={this.state.name}
  511. dateFormat={'Do MMMM YYYY'}
  512. timeFormat={false}
  513. inputProps={{ placeholder: '' }}
  514. onChange={x => {
  515. if (x.format) {
  516. this.setState({ name: x.format('Do MMMM YYYY'), real_name: x.format('YYYY-MM-DD') })
  517. } else {
  518. this.setState({ name: x, real_name: x })
  519. }
  520. }}
  521. />
  522. <InputGroupAddon addonType="append">
  523. <InputGroupText>
  524. <i className="fa fa-icon fa-pencil" />
  525. </InputGroupText>
  526. </InputGroupAddon>
  527. </InputGroup>
  528. </Col>
  529. </Row>
  530. <Row>
  531. <Col md={4} className="m-auto d-flex align-items-center justify-content-center flex-wrap">
  532. {this.state.selectedFiles.map((x, i) => (
  533. <p key={i}>{x.name}</p>
  534. ))}
  535. </Col>
  536. </Row>
  537. </div>
  538.  
  539. {
  540. userDates && userDates.gallery_dates && userDates.gallery_dates.years && userDates.gallery_dates.months ?
  541. <div className="date-select-content">
  542. <div className="d-flex align-items-center justify-content-end flex-wrap year-date">
  543. <span
  544. className={'All' == this.state.selected_year ? 'span-active span-year' : 'span-year'}
  545. onClick={() => this.onClickYear('All')}
  546. >
  547. All
  548. </span>
  549. {
  550. userDates.gallery_dates.years.map(year => {
  551. return (
  552. <span
  553. key={year.toString()}
  554. className={year.toString() == this.state.selected_year ? 'span-active span-year' : 'span-year'}
  555. onClick={() => this.onClickYear(year.toString())}
  556. >
  557. {year.toString()}
  558. </span>
  559. )
  560. })
  561. }
  562. </div>
  563. <div className="d-flex align-items-center justify-content-end flex-wrap">
  564. <span
  565. className={'All' == this.state.selected_month.label ? 'span-active span-month' : 'span-month'}
  566. onClick={() => this.onClickMonth('All')}
  567. >
  568. All
  569. </span>
  570. {
  571. userDates.gallery_dates.months.map(month => {
  572. return (
  573. <span
  574. key={month}
  575. className={month == this.state.selected_month.label ? 'span-active span-month' : 'span-month'}
  576. onClick={() => this.onClickMonth(month)}
  577. >
  578. {month}
  579. </span>
  580. )
  581. })
  582. }
  583. </div>
  584. </div>
  585. :
  586. null
  587. }
  588.  
  589. <div className="main-content">
  590. {
  591. galleries.length > 0 ?
  592. galleries.map(gallery => (
  593. <Gallery
  594. own={!this.props.location.state}
  595. gallery={gallery}
  596. key={gallery.id}
  597. {...this.props}
  598. />
  599. ))
  600. :
  601. null
  602. }
  603. </div>
  604. </CardBody>
  605. </Card>
  606. </Col>
  607. <Col xs={12} className="question-row">
  608. {this.renderQuestions()}
  609. </Col>
  610. </Row>
  611. </div>
  612.  
  613. </div>
  614. )
  615. }
  616. }
  617.  
  618. export default connect(mapStateToProps, mapDispatchToProps)(Dashboard)
Advertisement
Add Comment
Please, Sign In to add comment