|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { connect } from 'react-redux' |
| 4 | +import { PopoverColorPicker } from 'components/PopoverColorPicker' |
| 5 | +import Button from 'components/Button' |
| 6 | +import BaseModal from 'containers/Modals/BaseModal' |
| 7 | +import { observer } from 'mobx-react' |
| 8 | +import { makeObservable, observable } from 'mobx' |
| 9 | + |
| 10 | +import { createNotice } from 'actions/notices' |
| 11 | + |
| 12 | +import helpers from 'lib/helpers' |
| 13 | +import $ from 'jquery' |
| 14 | + |
| 15 | +@observer |
| 16 | +class CreateNoticeModal extends React.Component { |
| 17 | + constructor (props) { |
| 18 | + super(props) |
| 19 | + |
| 20 | + makeObservable(this) |
| 21 | + } |
| 22 | + |
| 23 | + @observable name = '' |
| 24 | + @observable message = '' |
| 25 | + @observable color = '' |
| 26 | + @observable fontColor = '' |
| 27 | + |
| 28 | + componentDidMount () { |
| 29 | + this.color = '#4CAF50' |
| 30 | + this.fontColor = '#ffffff' |
| 31 | + |
| 32 | + helpers.UI.inputs() |
| 33 | + helpers.UI.reRenderInputs() |
| 34 | + helpers.formvalidator() |
| 35 | + } |
| 36 | + |
| 37 | + componentDidUpdate (prevProps, prevState, snapshot) { |
| 38 | + helpers.UI.reRenderInputs() |
| 39 | + } |
| 40 | + |
| 41 | + onInputChange (target, e) { |
| 42 | + this[target] = e.target.value |
| 43 | + } |
| 44 | + |
| 45 | + onFormSubmit (e) { |
| 46 | + e.preventDefault() |
| 47 | + const $form = $(e.target) |
| 48 | + if (!$form.isValid(null, null, false)) return false |
| 49 | + |
| 50 | + const payload = { |
| 51 | + name: this.name, |
| 52 | + message: this.message, |
| 53 | + color: this.color, |
| 54 | + fontColor: this.fontColor |
| 55 | + } |
| 56 | + |
| 57 | + this.props.createNotice(payload).then(() => { |
| 58 | + helpers.resizeAll() |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + render () { |
| 63 | + return ( |
| 64 | + <BaseModal {...this.props} options={{ bgclose: false }}> |
| 65 | + <div className={'mb-25'}> |
| 66 | + <h2>Create Notice</h2> |
| 67 | + </div> |
| 68 | + <form className={'uk-form-stacked'} onSubmit={e => this.onFormSubmit(e)}> |
| 69 | + <div className={'uk-margin-medium-bottom'}> |
| 70 | + <label>Name</label> |
| 71 | + <input |
| 72 | + type='text' |
| 73 | + className={'md-input'} |
| 74 | + value={this.name} |
| 75 | + onChange={e => this.onInputChange('name', e)} |
| 76 | + data-validation='length' |
| 77 | + data-validation-length={'min2'} |
| 78 | + data-validation-error-msg={'Please enter a notice name. (Must contain 2 characters)'} |
| 79 | + /> |
| 80 | + </div> |
| 81 | + <div className={'uk-margin-medium-bottom'}> |
| 82 | + <label>Message</label> |
| 83 | + <textarea |
| 84 | + className={'md-input'} |
| 85 | + value={this.message} |
| 86 | + onChange={e => this.onInputChange('message', e)} |
| 87 | + data-validation='length' |
| 88 | + data-validation-length={'min10'} |
| 89 | + data-validation-error-msg={'Please enter a notice message. (Must contain 10 characters)'} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + <div> |
| 93 | + <span style={{ display: 'inline-block', float: 'left', paddingTop: 5 }}>Background Color</span> |
| 94 | + <PopoverColorPicker |
| 95 | + color={this.color} |
| 96 | + onChange={c => { |
| 97 | + this.color = c |
| 98 | + }} |
| 99 | + style={{ float: 'left', marginLeft: 5, marginRight: 15 }} |
| 100 | + /> |
| 101 | + <span style={{ display: 'inline-block', float: 'left', paddingTop: 5 }}>Font Color</span> |
| 102 | + <PopoverColorPicker |
| 103 | + color={this.fontColor} |
| 104 | + onChange={c => { |
| 105 | + this.fontColor = c |
| 106 | + }} |
| 107 | + style={{ float: 'left', marginLeft: 5 }} |
| 108 | + /> |
| 109 | + </div> |
| 110 | + |
| 111 | + <div className='uk-modal-footer uk-text-right'> |
| 112 | + <Button text={'Close'} flat={true} waves={true} extraClass={'uk-modal-close'} /> |
| 113 | + <Button text={'Create Notice'} flat={true} waves={true} style={'primary'} type={'submit'} /> |
| 114 | + </div> |
| 115 | + </form> |
| 116 | + </BaseModal> |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +CreateNoticeModal.propTypes = { |
| 122 | + createNotice: PropTypes.func.isRequired |
| 123 | +} |
| 124 | + |
| 125 | +const mapStateToProps = state => ({}) |
| 126 | + |
| 127 | +export default connect(mapStateToProps, { createNotice })(CreateNoticeModal) |
0 commit comments