Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. const EditKeycodeParametersFormComponent = ({
  2. className,
  3. closeModal,
  4. handleSubmit,
  5. submitting,
  6. invalid,
  7. pristine,
  8. submitFailed,
  9. selectedRowsData,
  10. }) => (
  11. <form className={className} onSubmit={handleSubmit}>
  12. <h2>LSPL (Low Stock Presentation Level)</h2>
  13. <Line />
  14. <InputGroup>
  15. <TextFieldWithValidation
  16. name="lsplMan"
  17. label="LSPL Manual"
  18. />
  19. </InputGroup>
  20. <h2>On / Off Range</h2>
  21. <Line />
  22. <InputGroup>
  23. <DatePickerWithValidation
  24. name="onRange"
  25. label="On Range Date"
  26. dayIsOutsideRange={() => (false)}
  27. />
  28. <DatePickerWithValidation
  29. name="offRange"
  30. label="Off Range Date"
  31. dayIsOutsideRange={() => (false)}
  32. />
  33. </InputGroup>
  34. <h2>Ranged</h2>
  35. <Line />
  36. <InputGroup>
  37. <CheckboxWithValidation
  38. label="Ranged"
  39. name="ranged"
  40. />
  41. </InputGroup>
  42. <h2>Active</h2>
  43. <Line />
  44. <InputGroup>
  45. <CheckboxWithValidation
  46. label="Active"
  47. name="active"
  48. />
  49. </InputGroup>
  50. <MarginBottomDiv>
  51. {submitFailed
  52. && <InfoPanel
  53. error
  54. text="There was an error updating these parameters.
  55. Please make sure you have entered values correctly and that the store(s) will be open during the supplied dates."
  56. />}
  57. </MarginBottomDiv>
  58. <MarginBottomLarge>
  59. <ButtonsGroupWrapper>
  60. <Button
  61. label="Apply Changes"
  62. primary
  63. loading={submitting}
  64. type="submit"
  65. disabled={invalid || pristine}
  66. />
  67. <Button type="button" label="Cancel" onClick={closeModal} />
  68. <RecordUpdateInformation>
  69. You are updating&nbsp;<span>{selectedRowsData.length}</span>&nbsp;{ selectedRowsData.length > 1 ? 'records' : 'record' }
  70. </RecordUpdateInformation>
  71. </ButtonsGroupWrapper>
  72. </MarginBottomLarge>
  73. </form>
  74. );
  75.  
  76. EditKeycodeParametersFormComponent.propTypes = {
  77. closeModal: PropTypes.func.isRequired,
  78. handleSubmit: PropTypes.func.isRequired,
  79. submitting: PropTypes.bool,
  80. submitFailed: PropTypes.bool,
  81. invalid: PropTypes.bool,
  82. pristine: PropTypes.bool,
  83. className: PropTypes.string,
  84. selectedRowsData: PropTypes.arrayOf(PropTypes.object).isRequired,
  85. };
  86.  
  87. EditKeycodeParametersFormComponent.defaultProps = {
  88. submitting: false,
  89. invalid: false,
  90. submitFailed: false,
  91. pristine: true,
  92. className: '',
  93. };
  94.  
  95. const validate = (values, props) => ({
  96. ...validateAllOnOffRangeDates(values, props.selectedRowsData),
  97. ...validateAllSingleLsplMans(values),
  98. });
  99.  
  100. const EditParametersFormReduxForm = reduxForm({
  101. form: 'editParameters',
  102. validate,
  103. enableReinitialize: true,
  104. touchOnChange: true,
  105. })(StyledEditParametersForm);
  106.  
  107. const mapStateToProps = (state) => {
  108. const selectedRowsData = state.parameters.selectedIndexes.map((selectedIndex) =>
  109. state.parameters.parametersSearchResults[selectedIndex]
  110. );
  111. return {
  112. initialValues: getFormValuesForDandFKeycode(selectedRowsData),
  113. selectedRowsData,
  114. };
  115. };
  116.  
  117. const mapDispatchToProps = {
  118. closeModal: closeEditParametersModal,
  119. };
  120.  
  121.  
  122. export default connect(mapStateToProps, mapDispatchToProps)(EditParametersFormReduxForm);
  123.  
  124. const getState = (meta) => {
  125. if (meta.touched) {
  126. if (meta.valid) {
  127. return textFieldStates.VALID;
  128. } else if (meta.invalid) {
  129. return textFieldStates.INVALID;
  130. }
  131. }
  132.  
  133. return textFieldStates.DEFAULT;
  134. };
  135.  
  136. const isBeforeTomorrow = (day) => day.isBefore(moment().add(1, 'days'), 'day');
  137.  
  138. export class DatePickerWithValidationComponent extends React.Component {
  139. constructor() {
  140. super();
  141.  
  142. this.state = {
  143. pickerOpen: false,
  144. selectedDate: null,
  145. };
  146. this.openDatePicker = this.openDatePicker.bind(this);
  147. this.closeDatePicker = this.closeDatePicker.bind(this);
  148. this.setDate = this.setDate.bind(this);
  149. this.setDateViaTyping = this.setDateViaTyping.bind(this);
  150. }
  151.  
  152. setDate(value) {
  153. this.props.input.onChange(value.format('DD/MM/YYYY'));
  154. this.setState({ selectedDate: value });
  155. this.closeDatePicker();
  156. }
  157.  
  158. setDateViaTyping(event) {
  159. let value = moment(event.target.value, 'DD/MM/YYYY');
  160. if (!value.isValid()) {
  161. value = null;
  162. }
  163. this.setState({ selectedDate: value });
  164. }
  165.  
  166. openDatePicker() {
  167. this.setState({ pickerOpen: true });
  168. }
  169.  
  170. closeDatePicker() {
  171. this.props.input.onBlur();
  172. this.setState({ pickerOpen: false });
  173. }
  174.  
  175. render() {
  176. const { label, meta, input } = this.props;
  177.  
  178. return (
  179. <DayPickerContainer>
  180. <TextField
  181. label={label}
  182. {...input}
  183. onClick={this.openDatePicker}
  184. onBlur={this.setDateViaTyping}
  185. state={getState(meta)}
  186. errorMessage={meta.touched ? meta.error : ''}
  187. />
  188. {this.state.pickerOpen &&
  189. <DayPickerWrapper>
  190. <DayPickerSingleDateController
  191. date={this.state.selectedDate}
  192. onDateChange={this.setDate}
  193. onOutsideClick={this.closeDatePicker}
  194. hideKeyboardShortcutsPanel
  195. enableOutsideDays
  196. isOutsideRange={this.props.dayIsOutsideRange}
  197. />
  198. </DayPickerWrapper>
  199. }
  200. </DayPickerContainer>
  201. );
  202. }
  203. }
  204.  
  205. const DayPickerWrapper = styled.div`
  206. position: absolute;
  207. z-index: 99;
  208.  
  209. /* react-dates css overrides */
  210. .DayPicker__withBorder {
  211. border-radius: 2px;
  212. box-shadow: 0 2px 4px 0 rgba(0,0,0,0.1), 0 0 0 1px #ddd;
  213. }
  214.  
  215. .CalendarDay__selected,
  216. .CalendarDay__selected:active,
  217. .CalendarDay__selected:hover {
  218. background: ${({ theme }) => theme.palette.ACTIVE};
  219. border: ${({ theme }) => theme.border.normal(theme.palette.ACTIVE)};
  220. }
  221. `;
  222.  
  223. const DayPickerContainer = styled.div`
  224. position: relative;
  225. `;
  226.  
  227. const DatePickerWithValidation = (props) => (
  228. <Field component={DatePickerWithValidationComponent} {...props} />
  229. );
  230.  
  231. DatePickerWithValidationComponent.propTypes = {
  232. label: PropTypes.string,
  233. meta: PropTypes.objectOf(PropTypes.any).isRequired,
  234. input: PropTypes.objectOf(PropTypes.any).isRequired,
  235. dayIsOutsideRange: PropTypes.func,
  236. };
  237.  
  238. DatePickerWithValidationComponent.defaultProps = {
  239. label: '',
  240. dayIsOutsideRange: isBeforeTomorrow,
  241. };
  242.  
  243. export default DatePickerWithValidation;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement