mirror of
https://git.rdcenter.ru/RDCenter/skb-kit.git
synced 2026-12-05 04:29:45 +00:00
Добавил капчу, проверку файлов, отправку формы на сервер, возможность открепить файл, отредачил стили
This commit is contained in:
1 parent
963ad3b544
commit
c58507ac4e
9 files changed
+381
-102
No files matched your search
@@ -1,51 +1,153 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import { Form } from "react-router-dom";
|
||||
import { SmartCaptcha } from "@yandex/smart-captcha";
|
||||
|
||||
import { sendContactForm } from "../../api";
|
||||
import Field from "../Field";
|
||||
|
||||
import "./ContactForm.styles.scss";
|
||||
|
||||
import { fileUpload } from "../../assets/images";
|
||||
|
||||
function ContactForm() {
|
||||
return (
|
||||
<Form method="post" className="half contact-form">
|
||||
<h2>Свяжитесь с нами!</h2>
|
||||
<div className="inputs">
|
||||
<div className="row">
|
||||
<Field
|
||||
key={1}
|
||||
type="text"
|
||||
name="name"
|
||||
placeholder="Имя"
|
||||
/>
|
||||
<Field
|
||||
key={2}
|
||||
type="email"
|
||||
name="email"
|
||||
placeholder="E-mail"
|
||||
/>
|
||||
</div>
|
||||
<Field
|
||||
key={3}
|
||||
type="textarea"
|
||||
name="text"
|
||||
placeholder="Опишите в этом поле свой проект и, по возможности, прикрепите файл с ТЗ"
|
||||
/>
|
||||
<div className="row">
|
||||
<Field
|
||||
key={4}
|
||||
type="file"
|
||||
name="file"
|
||||
icon={fileUpload}
|
||||
/>
|
||||
<Field
|
||||
key={5}
|
||||
type="submit"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
);
|
||||
const [formData, setFormData] = useState({
|
||||
name: '',
|
||||
email: '',
|
||||
message: '',
|
||||
file: null,
|
||||
captcha: '',
|
||||
});
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const handleChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData({ ...formData, [name]: value });
|
||||
};
|
||||
|
||||
const handleFileChange = (e) => {
|
||||
const extension = e.target.files[0].name.match(/\.([^.]+)$/)[1];
|
||||
switch (extension) {
|
||||
case 'doc':
|
||||
case 'docx':
|
||||
case 'pdf':
|
||||
case 'txt':
|
||||
case 'pptx':
|
||||
case 'ppt':
|
||||
setFormData({ ...formData, file: e.target.files[0] });
|
||||
break;
|
||||
default:
|
||||
alert('Неверный формат файла.');
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemoveFile = () => {
|
||||
setFormData({ ...formData, file: null });
|
||||
};
|
||||
|
||||
const handleCaptchaChange = (token) => {
|
||||
setFormData({ ...formData, captcha: token });
|
||||
console.log('Капча пришла');
|
||||
};
|
||||
|
||||
const handleTokenExpired = useCallback(() => {
|
||||
setFormData({ ...formData, captcha: null });
|
||||
console.log('Капча просрочилась');
|
||||
}, []);
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
console.log('handleSubmit isSubmitting: ', isSubmitting);
|
||||
|
||||
const formDataObj = new FormData();
|
||||
formDataObj.append('name', formData.name);
|
||||
formDataObj.append('email', formData.email);
|
||||
formDataObj.append('message', formData.message);
|
||||
formDataObj.append('file', formData.file);
|
||||
formDataObj.append('captcha', formData.captcha);
|
||||
|
||||
try {
|
||||
const response = await sendContactForm(formDataObj);
|
||||
if (response) {
|
||||
alert('Сообщение отправлено!');
|
||||
console.log('response', response);
|
||||
} else {
|
||||
alert('Ошибка отправки.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Ошибка:', error);
|
||||
alert('Ошибка отправки.');
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form method="post" onSubmit={handleSubmit} className="half contact-form">
|
||||
<h2>Свяжитесь с нами!</h2>
|
||||
<div className="inputs">
|
||||
<div className="row">
|
||||
<Field
|
||||
key={1}
|
||||
type="text"
|
||||
name="name"
|
||||
placeholder="Имя"
|
||||
value={formData.name}
|
||||
onChange={handleChange}
|
||||
required
|
||||
isDisabled={isSubmitting}
|
||||
/>
|
||||
<Field
|
||||
key={2}
|
||||
type="email"
|
||||
name="email"
|
||||
placeholder="E-mail"
|
||||
value={formData.email}
|
||||
onChange={handleChange}
|
||||
required
|
||||
isDisabled={isSubmitting}
|
||||
/>
|
||||
</div>
|
||||
<Field
|
||||
key={3}
|
||||
type="textarea"
|
||||
name="message"
|
||||
placeholder="Опишите в этом поле свой проект и, по возможности, прикрепите файл с ТЗ"
|
||||
value={formData.message}
|
||||
onChange={handleChange}
|
||||
required
|
||||
isDisabled={isSubmitting}
|
||||
/>
|
||||
<div className="row">
|
||||
<Field
|
||||
key={4}
|
||||
type="file"
|
||||
name="file"
|
||||
onChange={handleFileChange}
|
||||
handleRemoveFile={handleRemoveFile}
|
||||
fileAttached={formData && formData.file ? true : false}
|
||||
isDisabled={isSubmitting}
|
||||
/>
|
||||
<div className="submit-container">
|
||||
{formData.captcha ? (
|
||||
<Field
|
||||
key={5}
|
||||
type="submit"
|
||||
value={isSubmitting ? 'Отправляется' : 'Отправить'}
|
||||
isDisabled={isSubmitting}
|
||||
/>
|
||||
) : (
|
||||
<SmartCaptcha
|
||||
key={6}
|
||||
sitekey="ysc1_8nFqhYasby2fJ9J7pCTxdBMe0Xc3Y6CrWk4aylB03d4f0045"
|
||||
onSuccess={handleCaptchaChange}
|
||||
onTokenExpired={handleTokenExpired}
|
||||
style={formData.captcha ? { display: 'none' } : { display: 'block' }}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
export default ContactForm;
|
||||
@@ -26,30 +26,108 @@ $color-file-button: rgb(7, 42, 200);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
*:disabled {
|
||||
background: dimgrey;
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
gap: $gap;
|
||||
}
|
||||
|
||||
.file-input * {
|
||||
width: fit-content;
|
||||
color: $color-file-button;
|
||||
font-weight: 500;
|
||||
.field {
|
||||
padding: 12px 16px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
|
||||
font-size: 16px;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.field.big {
|
||||
height: 200px;
|
||||
line-height: 30px;
|
||||
resize: none;
|
||||
}
|
||||
|
||||
.file-input-container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
|
||||
.file-input {
|
||||
position: relative;
|
||||
width: fit-content;
|
||||
color: $color-file-button;
|
||||
font-weight: 500;
|
||||
|
||||
input {
|
||||
position: absolute;
|
||||
z-index: -1;
|
||||
opacity: 0;
|
||||
display: block;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
|
||||
.upload-file-btn {
|
||||
display: flex;
|
||||
cursor: pointer;
|
||||
|
||||
.icon {
|
||||
filter: invert(15%) sepia(100%) saturate(2861%) hue-rotate(229deg) brightness(93%) contrast(120%);
|
||||
}
|
||||
|
||||
span {
|
||||
display: inline-block;
|
||||
font-size: 18px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.icon {
|
||||
margin-right: 6px;
|
||||
height: $icon-size;
|
||||
width: $icon-size;
|
||||
filter: invert(15%) sepia(100%) saturate(2861%) hue-rotate(229deg) brightness(93%) contrast(120%);;
|
||||
}
|
||||
|
||||
.remove-btn {
|
||||
display: flex;
|
||||
width: fit-content;
|
||||
height: fit-content;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
|
||||
.icon.remove {
|
||||
margin-left: 6px;
|
||||
filter: hue-rotate(0) !important;
|
||||
height: calc($icon-size * 0.75);
|
||||
width: calc($icon-size * 0.75);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.submit {
|
||||
width: 100%;
|
||||
cursor: pointer;
|
||||
.file-input input:focus+.upload-file-btn {
|
||||
outline: -webkit-focus-ring-color auto 1px;
|
||||
}
|
||||
|
||||
padding: 12px;
|
||||
font-size: 18px;
|
||||
line-height: normal;
|
||||
.submit-container {
|
||||
|
||||
.submit {
|
||||
width: 100%;
|
||||
cursor: pointer;
|
||||
|
||||
padding: 12px;
|
||||
font-size: 18px;
|
||||
line-height: normal;
|
||||
}
|
||||
|
||||
>div>div>div {
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import "./Field.styles.scss";
|
||||
import { fileUpload, cross } from "../../assets/images";
|
||||
|
||||
function Field({ type, name, placeholder, icon }) {
|
||||
function Field({ type, name, placeholder, value, onChange, isDisabled = false, fileAttached = false, handleRemoveFile = null }) {
|
||||
const className = type === "submit" ? "submit btn"
|
||||
: type === "textarea" ? "field big"
|
||||
: "field";
|
||||
: type === "textarea" ? "field big"
|
||||
: "field";
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -12,21 +13,43 @@ function Field({ type, name, placeholder, icon }) {
|
||||
className={className}
|
||||
name={name}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
required
|
||||
disabled={isDisabled}
|
||||
/>
|
||||
) : type === "file" ? (
|
||||
<label className="file-input">
|
||||
<input type="file" name="file" />
|
||||
<div className="upload-file-btn">
|
||||
<img src={icon} alt="" className="icon" />
|
||||
<span>Прикрепить файл</span>
|
||||
</div>
|
||||
</label>
|
||||
<div className="file-input-container">
|
||||
<label className="file-input">
|
||||
<input
|
||||
type="file"
|
||||
name="file"
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
disabled={isDisabled}
|
||||
accept=".doc,.docx,.txt,.pdf,.ppt,.pptx"
|
||||
/>
|
||||
<div className="upload-file-btn">
|
||||
<img src={fileUpload} alt="" className="icon" />
|
||||
<span>{fileAttached ? 'Файл прикреплен' : 'Прикрепить файл'}</span>
|
||||
</div>
|
||||
</label>
|
||||
{fileAttached &&
|
||||
<button className="remove-btn icon" onClick={handleRemoveFile}>
|
||||
<img src={cross} alt="Открепить файл" className="icon remove" />
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
) : (
|
||||
<input
|
||||
className={className}
|
||||
type={type}
|
||||
name={name}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
required
|
||||
disabled={isDisabled}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -1,47 +1,47 @@
|
||||
.field {
|
||||
padding: 12px 16px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
// .field {
|
||||
// padding: 12px 16px;
|
||||
// border: none;
|
||||
// border-radius: 8px;
|
||||
|
||||
font-size: 16px;
|
||||
font-weight: 400;
|
||||
}
|
||||
// font-size: 16px;
|
||||
// font-weight: 400;
|
||||
// }
|
||||
|
||||
.field.big {
|
||||
height: 200px;
|
||||
line-height: 30px;
|
||||
resize: none;
|
||||
}
|
||||
// .field.big {
|
||||
// height: 200px;
|
||||
// line-height: 30px;
|
||||
// resize: none;
|
||||
// }
|
||||
|
||||
.file-input {
|
||||
position: relative;
|
||||
width: fit-content;
|
||||
// .file-input {
|
||||
// position: relative;
|
||||
// width: fit-content;
|
||||
|
||||
input {
|
||||
position: absolute;
|
||||
z-index: -1;
|
||||
opacity: 0;
|
||||
display: block;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
// input {
|
||||
// position: absolute;
|
||||
// z-index: -1;
|
||||
// opacity: 0;
|
||||
// display: block;
|
||||
// width: 0;
|
||||
// height: 0;
|
||||
// }
|
||||
|
||||
.upload-file-btn {
|
||||
display: flex;
|
||||
cursor: pointer;
|
||||
// .upload-file-btn {
|
||||
// display: flex;
|
||||
// cursor: pointer;
|
||||
|
||||
.icon {
|
||||
margin-right: 6px;
|
||||
height: 22px;
|
||||
}
|
||||
// .icon {
|
||||
// margin-right: 6px;
|
||||
// height: 22px;
|
||||
// }
|
||||
|
||||
span {
|
||||
display: inline-block;
|
||||
font-size: 18px;
|
||||
}
|
||||
}
|
||||
}
|
||||
// span {
|
||||
// display: inline-block;
|
||||
// font-size: 18px;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
.file-input input:focus + .upload-file-btn {
|
||||
outline: -webkit-focus-ring-color auto 1px;
|
||||
}
|
||||
// .file-input input:focus + .upload-file-btn {
|
||||
// outline: -webkit-focus-ring-color auto 1px;
|
||||
// }
|
||||
Reference in new issue
Block a user