+import axios from 'axios';
+import { withFormik } from 'formik';
+import PropTypes from 'prop-types';
+import React from 'react';
+import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
+import { withTranslation } from 'react-i18next';
+import toastr from 'toastr';
+
+import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
+import i18n from '../../i18n';
+import yup from '../../schema/yup';
+
+const EditStreamLinkForm = ({
+ errors,
+ handleBlur,
+ handleChange,
+ handleSubmit,
+ onCancel,
+ touched,
+ user,
+ values,
+}) =>
+<Form noValidate onSubmit={handleSubmit}>
+ <Modal.Body>
+ <Row>
+ <Form.Group as={Col} controlId="user.nickname">
+ <Form.Label>{i18n.t('users.nickname')}</Form.Label>
+ <Form.Control
+ isInvalid={!!(touched.nickname && errors.nickname)}
+ name="nickname"
+ onBlur={handleBlur}
+ onChange={handleChange}
+ placeholder={user.username}
+ type="text"
+ value={values.nickname || ''}
+ />
+ {touched.nickname && errors.nickname ?
+ <Form.Control.Feedback type="invalid">
+ {i18n.t(errors.nickname)}
+ </Form.Control.Feedback>
+ : null}
+ </Form.Group>
+ </Row>
+ </Modal.Body>
+ <Modal.Footer>
+ {onCancel ?
+ <Button onClick={onCancel} variant="secondary">
+ {i18n.t('button.cancel')}
+ </Button>
+ : null}
+ <Button type="submit" variant="primary">
+ {i18n.t('button.save')}
+ </Button>
+ </Modal.Footer>
+</Form>;
+
+EditStreamLinkForm.propTypes = {
+ errors: PropTypes.shape({
+ nickname: PropTypes.string,
+ }),
+ handleBlur: PropTypes.func,
+ handleChange: PropTypes.func,
+ handleSubmit: PropTypes.func,
+ onCancel: PropTypes.func,
+ touched: PropTypes.shape({
+ nickname: PropTypes.bool,
+ }),
+ user: PropTypes.shape({
+ username: PropTypes.bool,
+ }),
+ values: PropTypes.shape({
+ nickname: PropTypes.string,
+ }),
+};
+
+export default withFormik({
+ displayName: 'SeedForm',
+ enableReinitialize: true,
+ handleSubmit: async (values, actions) => {
+ const { user_id, nickname } = values;
+ const { setErrors } = actions;
+ const { onCancel } = actions.props;
+ try {
+ await axios.post(`/api/users/${user_id}/setNickname`, {
+ nickname,
+ });
+ toastr.success(i18n.t('users.setNicknameSuccess'));
+ if (onCancel) {
+ onCancel();
+ }
+ } catch (e) {
+ toastr.error(i18n.t('users.setNicknameError'));
+ if (e.response && e.response.data && e.response.data.errors) {
+ setErrors(laravelErrorsToFormik(e.response.data.errors));
+ }
+ }
+ },
+ mapPropsToValues: ({ user }) => ({
+ user_id: user.id,
+ nickname: user.nickname || '',
+ }),
+ validationSchema: yup.object().shape({
+ nickname: yup.string(),
+ }),
+})(withTranslation()(EditStreamLinkForm));