forked from hexlet-rus/runit
/
NewRepl.jsx
140 lines (130 loc) · 4.29 KB
/
NewRepl.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { useEffect, useRef } from 'react';
import { Modal, Form, FloatingLabel, Button } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { useFormik } from 'formik';
import { useNavigate } from 'react-router-dom';
import * as yup from 'yup';
import { faker } from '@faker-js/faker';
import { useSnippets } from '../../hooks';
import { actions as modalActions } from '../../slices/modalSlice.js';
const generateInitialName = () => {
const adjectiveLength = 3 + Math.round(Math.random() * 6);
const adjective = faker.word.adjective(adjectiveLength);
const animal = faker.animal.type();
return `${adjective}-${animal}`;
};
function NewRepl() {
const dispatch = useDispatch();
const inputRef = useRef();
const { t } = useTranslation();
const snippetsApi = useSnippets();
const navigate = useNavigate();
const { code, currentLanguage } = useSelector(({ editor, languages }) => ({
code: editor.code,
currentLanguage: languages.currentLanguage,
}));
const languages = new Map()
.set('javascript', '.js')
.set('python', '.py')
.set('php', '.php');
useEffect(() => {
inputRef.current.focus();
});
const formik = useFormik({
initialValues: {
name: generateInitialName(),
},
validationSchema: yup.object({
name: yup
.string()
.required(t('modals.validation.required'))
.max(20, t('modals.validation.snippetNameMaxLength'))
.matches(/^[a-zA-Z0-9_-]*$/, t('modals.validation.singleWord')),
}),
onSubmit: async (values, actions) => {
actions.setSubmitting(true);
try {
const name = `${values.name}${languages.get(currentLanguage)}`;
const encodedId = await snippetsApi.saveSnippet(code, name);
const link = snippetsApi.genSnippetLink(encodedId);
const url = new URL(link);
navigate(`${url.pathname}${url.search}`);
dispatch(modalActions.closeModal());
actions.setSubmitting(false);
} catch (err) {
actions.setSubmitting(false);
if (!err.isAxiosError) {
console.log(t('errors.unknown'));
throw err;
} else {
console.log(t('errors.network'));
throw err;
}
}
},
});
return (
<Modal
animation
centered
onHide={() => dispatch(modalActions.closeModal())}
show
>
<Modal.Header
className="bg-dark text-white border-secondary"
closeButton
closeVariant="white"
>
<Modal.Title>{t('modals.newSnippetName')}</Modal.Title>
</Modal.Header>
<Modal.Body className="bg-dark">
<Form onSubmit={formik.handleSubmit}>
<Form.Group className="mb-3">
<FloatingLabel
controlId="name"
label={t('modals.share.snippetNameLabel')}
className="text-white"
>
<Form.Control
className="text-white bg-dark border-secondary"
name="name"
onChange={formik.handleChange}
placeholder={t('modals.share.snippetNameLabel')}
ref={inputRef}
value={formik.values.name}
isInvalid={formik.touched.name && formik.errors.name}
/>
<Form.Control.Feedback type="invalid">
{formik.touched.name && formik.errors.name}
</Form.Control.Feedback>
</FloatingLabel>
</Form.Group>
<div
className="d-flex mt-4 justify-content-end"
style={{ columnGap: '12px' }}
>
<Button
variant="danger"
onClick={() => dispatch(modalActions.closeModal())}
type="button"
style={{ width: 'calc(20% - 10px)' }}
>
{t('modals.cancelButton')}
</Button>
<div className="gap" style={{ marginLeft: 'auto' }} />
<Button
variant="success"
disabled={formik.isSubmitting}
type="submit"
style={{ width: 'calc(35% - 10px)' }}
>
{t('modals.share.saveSnippetButton')}
</Button>
</div>
</Form>
</Modal.Body>
</Modal>
);
}
export default NewRepl;