xonecas.io
3517
Mt

meal_tracker / files

meal_tracker / bmi_ui / src / components / MealModal.js

1.5 KB
1import React, { Component, Fragment } from 'react'
2import PropTypes from 'prop-types'
3import { Button, Modal, ModalHeader, ModalBody } from 'reactstrap'
4import MealForm from './MealForm'
5
6class MealModal extends Component {
7  constructor (props) {
8    super(props)
9
10    this.state = {
11      modal: false
12    }
13  }
14
15  static get propTypes () {
16    return {
17      create: PropTypes.boolean,
18      meal: PropTypes.any,
19      user: PropTypes.any,
20      update: PropTypes.func
21    }
22  }
23
24  toggle (e) {
25    e && e.preventDefault()
26
27    this.setState(previous => ({
28      modal: !previous.modal
29    }))
30  }
31
32  render () {
33    const create = this.props.create
34    const toggle = this.toggle.bind(this)
35
36    let title = 'Editing meal'
37    let button = (
38      <Button onClick={toggle} size='sm' outline className='mr-3 float-right'>Edit</Button>
39    )
40
41    if (create) {
42      title = 'Creating meal'
43
44      button = (
45        <Button
46          color='primary'
47          size='sm'
48          outline
49          onClick={toggle}
50          className='mr-3 float-right'
51        >Add new meal</Button>
52      )
53    }
54
55    return (
56      <Fragment>
57        {button}
58        <Modal isOpen={this.state.modal} toggle={toggle}>
59          <ModalHeader toggle={toggle}>{title}</ModalHeader>
60
61          <ModalBody>
62            <MealForm
63              toggle={toggle}
64              meal={this.props.meal}
65              user={this.props.user}
66              update={this.props.update}
67            />
68          </ModalBody>
69        </Modal>
70      </Fragment>
71    )
72  }
73}
74
75export default MealModal