1 import axios from 'axios';
2 import { withFormik } from 'formik';
3 import PropTypes from 'prop-types';
4 import React from 'react';
5 import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
6 import { withTranslation } from 'react-i18next';
7 import toastr from 'toastr';
9 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
10 import i18n from '../../i18n';
11 import yup from '../../schema/yup';
13 const EditStreamLinkForm = ({
23 <Form noValidate onSubmit={handleSubmit}>
26 <Form.Group as={Col} controlId="user.nickname">
27 <Form.Label>{i18n.t('users.nickname')}</Form.Label>
29 isInvalid={!!(touched.nickname && errors.nickname)}
32 onChange={handleChange}
33 placeholder={user.username}
35 value={values.nickname || ''}
37 {touched.nickname && errors.nickname ?
38 <Form.Control.Feedback type="invalid">
39 {i18n.t(errors.nickname)}
40 </Form.Control.Feedback>
47 <Button onClick={onCancel} variant="secondary">
48 {i18n.t('button.cancel')}
51 <Button type="submit" variant="primary">
52 {i18n.t('button.save')}
57 EditStreamLinkForm.propTypes = {
58 errors: PropTypes.shape({
59 nickname: PropTypes.string,
61 handleBlur: PropTypes.func,
62 handleChange: PropTypes.func,
63 handleSubmit: PropTypes.func,
64 onCancel: PropTypes.func,
65 touched: PropTypes.shape({
66 nickname: PropTypes.bool,
68 user: PropTypes.shape({
69 username: PropTypes.string,
71 values: PropTypes.shape({
72 nickname: PropTypes.string,
76 export default withFormik({
77 displayName: 'NicknameForm',
78 enableReinitialize: true,
79 handleSubmit: async (values, actions) => {
80 const { user_id, nickname } = values;
81 const { setErrors } = actions;
82 const { onCancel } = actions.props;
84 await axios.post(`/api/users/${user_id}/setNickname`, {
87 toastr.success(i18n.t('users.setNicknameSuccess'));
92 toastr.error(i18n.t('users.setNicknameError'));
93 if (e.response && e.response.data && e.response.data.errors) {
94 setErrors(laravelErrorsToFormik(e.response.data.errors));
98 mapPropsToValues: ({ user }) => ({
100 nickname: user.nickname || '',
102 validationSchema: yup.object().shape({
103 nickname: yup.string(),
105 })(withTranslation()(EditStreamLinkForm));