|
| 1 | +// |
| 2 | +// IMPORTANT: |
| 3 | +// You must update ResourceUrl.RESOURCES_VERSION each time whenever you're modified this file! |
| 4 | +// |
| 5 | +import React from 'react'; |
| 6 | + |
| 7 | +// @todo #1383 HideImageForm: add tests |
| 8 | +class HideImageForm extends React.PureComponent { |
| 9 | + constructor(props) { |
| 10 | + super(props); |
| 11 | + this.state = { |
| 12 | + imageId: null, |
| 13 | + validationErrors: [], |
| 14 | + hasServerError: false, |
| 15 | + isDisabled: false |
| 16 | + }; |
| 17 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 18 | + this.handleChange = this.handleChange.bind(this); |
| 19 | + } |
| 20 | + |
| 21 | + handleChange(event) { |
| 22 | + event.preventDefault(); |
| 23 | + this.setState({ |
| 24 | + comment: event.target.value |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + handleSubmit(event) { |
| 29 | + event.preventDefault(); |
| 30 | + |
| 31 | + this.setState({ |
| 32 | + isDisabled: true, |
| 33 | + hasServerError: false, |
| 34 | + validationErrors: [] |
| 35 | + }); |
| 36 | + |
| 37 | + axios.patch( |
| 38 | + this.props.url, [ |
| 39 | + { |
| 40 | + op: 'add', |
| 41 | + path: '/comment', |
| 42 | + value: this.state.imageId |
| 43 | + } |
| 44 | + ], |
| 45 | + { |
| 46 | + headers: { |
| 47 | + [this.props.csrfHeaderName]: this.props.csrfTokenValue, |
| 48 | + 'Cache-Control': 'no-store' |
| 49 | + }, |
| 50 | + validateStatus: status => { |
| 51 | + return status == 204 || status == 400; |
| 52 | + } |
| 53 | + }) |
| 54 | + .then(response => { |
| 55 | + const data = response.data; |
| 56 | + |
| 57 | + if (data.hasOwnProperty('fieldErrors')) { |
| 58 | + const fieldErrors = []; |
| 59 | + if (data.fieldErrors.value) { |
| 60 | + fieldErrors.push(...data.fieldErrors.value); |
| 61 | + } |
| 62 | + this.setState({ |
| 63 | + isDisabled: false, |
| 64 | + validationErrors: fieldErrors |
| 65 | + }); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // no need to reset the state as page will be reloaded |
| 70 | + window.location.reload(); |
| 71 | + }) |
| 72 | + .catch(error => { |
| 73 | + console.error(error); |
| 74 | + this.setState({ isDisabled: false, hasServerError: true }); |
| 75 | + }); |
| 76 | + } |
| 77 | + render() { |
| 78 | + return ( |
| 79 | + <HideImageFormView |
| 80 | + l10n={this.props.l10n} |
| 81 | + imageIds={this.props.imageIds} |
| 82 | + handleSubmit={this.handleSubmit} |
| 83 | + hasServerError={this.state.hasServerError} |
| 84 | + handleChange={this.handleChange} |
| 85 | + validationErrors={this.state.validationErrors} |
| 86 | + isDisabled={this.state.isDisabled} |
| 87 | + /> |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class HideImageFormView extends React.PureComponent { |
| 93 | + render() { |
| 94 | + const {handleSubmit, hasServerError, handleChange, validationErrors = [], isDisabled, l10n = {}, imageIds} = this.props; |
| 95 | + const hasValidationErrors = validationErrors.length > 0; |
| 96 | + return ( |
| 97 | + <div className="col-sm-10"> |
| 98 | + <form id="hide-image-form" className="form-horizontal" onSubmit={ handleSubmit }> |
| 99 | + |
| 100 | + { hasServerError && |
| 101 | + <div id="hide-image-failed-msg" |
| 102 | + role="alert" |
| 103 | + className="alert alert-danger text-center col-sm-8 col-sm-offset-2"> |
| 104 | + { l10n['t_server_error'] || 'Server error' } |
| 105 | + </div> |
| 106 | + } |
| 107 | + |
| 108 | + <div className={ `form-group form-group-sm ${hasValidationErrors ? 'has-error' : ''}` }> |
| 109 | + <label htmlFor="hide-image-id" className="control-label col-sm-3 required-field"> |
| 110 | + { l10n['t_image_id'] || 'Image ID' } |
| 111 | + </label> |
| 112 | + <div className="col-sm-4"> |
| 113 | + <select |
| 114 | + id="hide-image-id" |
| 115 | + className="form-control" |
| 116 | + required="required" |
| 117 | + onChange={ handleChange }> |
| 118 | + <option value="">{ l10n['t_not_chosen'] || 'Not chosen' }</option> |
| 119 | + { |
| 120 | + this.props.imageIds.map(imageId => |
| 121 | + <option key={ imageId } value="{ imageId }">{ imageId }</option> |
| 122 | + ) |
| 123 | + } |
| 124 | + </select> |
| 125 | + { hasValidationErrors && |
| 126 | + <span id="hide-image-id.errors" className="help-block"> |
| 127 | + { validationErrors.join(', ') } |
| 128 | + </span> |
| 129 | + } |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div className="form-group from-group-sm"> |
| 134 | + <div className="col-sm-offset-3 col-sm-8"> |
| 135 | + <button |
| 136 | + type="submit" |
| 137 | + className="btn btn-default btn-sm" |
| 138 | + disabled={ isDisabled }> |
| 139 | + { l10n['t_hide_image'] || 'Hide image' } |
| 140 | + </button> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </form> |
| 144 | + </div> |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +window.HideImageForm = HideImageForm; |
0 commit comments