Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cd1a9c1e4 | ||
|
|
7c14baca77 | ||
|
|
267ab815b4 | ||
|
|
28e1c91961 |
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,162 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { Modal } from "antd";
|
||||||
|
import { motion } from "framer-motion";
|
||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import * as yup from "yup";
|
||||||
|
import { yupResolver } from "@hookform/resolvers/yup";
|
||||||
|
import styles from "./ModalRequestForm.module.css";
|
||||||
|
import Button from "../Button/Button";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
// ✅ Схема валидации
|
||||||
|
const schema = yup.object().shape({
|
||||||
|
name: yup.string().required("Введите имя"),
|
||||||
|
email: yup.string().email("Некорректный Email").required("Введите Email"),
|
||||||
|
phone: yup.string().min(18, "Введите корректный номер").required("Введите номер"),
|
||||||
|
});
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
open: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FormData {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
phone: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const PriceLisForm: React.FC<Props> = ({ open, onClose }) => {
|
||||||
|
console.log('ModalRequestForm render, open:', open);
|
||||||
|
const {
|
||||||
|
register,
|
||||||
|
handleSubmit,
|
||||||
|
formState: { errors, isSubmitting },
|
||||||
|
reset,
|
||||||
|
} = useForm({ resolver: yupResolver(schema) });
|
||||||
|
|
||||||
|
const handlePhoneInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
let value = e.target.value.replace(/\D/g, '');
|
||||||
|
|
||||||
|
if (value.startsWith('7') || value.startsWith('8')) {
|
||||||
|
value = value.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let formattedValue = '+7 ';
|
||||||
|
|
||||||
|
if (value.length > 0) {
|
||||||
|
formattedValue += '(' + value.substring(0, 3);
|
||||||
|
}
|
||||||
|
if (value.length > 3) {
|
||||||
|
formattedValue += ') ' + value.substring(3, 6);
|
||||||
|
}
|
||||||
|
if (value.length > 6) {
|
||||||
|
formattedValue += '-' + value.substring(6, 8);
|
||||||
|
}
|
||||||
|
if (value.length > 8) {
|
||||||
|
formattedValue += '-' + value.substring(8, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
e.target.value = formattedValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
const sendToBackend = async (data: FormData): Promise<boolean> => {
|
||||||
|
try{
|
||||||
|
const response = await axios.post("/api/requests", {
|
||||||
|
...data,
|
||||||
|
source: "website-form",
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
})
|
||||||
|
|
||||||
|
return response.data.success;
|
||||||
|
}catch (error: any){
|
||||||
|
console.error("Error sending request:", error);
|
||||||
|
|
||||||
|
throw new Error("Не удалось отправить заявку. Попробуйте позже.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onSubmit = async (data: FormData) => {
|
||||||
|
try {
|
||||||
|
await sendToBackend(data);
|
||||||
|
|
||||||
|
Modal.success({
|
||||||
|
title: "Прайс-лист отправлен!",
|
||||||
|
okText: "Хорошо",
|
||||||
|
});
|
||||||
|
|
||||||
|
reset();
|
||||||
|
onClose();
|
||||||
|
} catch (error) {
|
||||||
|
Modal.error({
|
||||||
|
title: "Ошибка",
|
||||||
|
content: error instanceof Error ? error.message : "Произошла неизвестная ошибка",
|
||||||
|
okText: "Понятно",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
open={open}
|
||||||
|
footer={null}
|
||||||
|
onCancel={onClose}
|
||||||
|
centered
|
||||||
|
width={620}
|
||||||
|
closable={!isSubmitting}
|
||||||
|
maskClosable={!isSubmitting}
|
||||||
|
>
|
||||||
|
<motion.div
|
||||||
|
initial={{ opacity: 0, y: 40 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.35 }}
|
||||||
|
className={styles.modalBox}
|
||||||
|
>
|
||||||
|
<h2 className={styles.title}>СКАЧАЙТЕ ПРАЙС_ЛИСТ</h2>
|
||||||
|
<p className={styles.subtitle}>
|
||||||
|
Заполните форму ниже, и прайс-лист будт выслан вам на почту
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<form onSubmit={handleSubmit(onSubmit)} className={styles.form}>
|
||||||
|
<div className={styles.field}>
|
||||||
|
<input
|
||||||
|
className={styles.input}
|
||||||
|
placeholder="Введите имя *"
|
||||||
|
{...register("name")}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
/>
|
||||||
|
{errors.name && <span className={styles.error}>{errors.name.message}</span>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={styles.field}>
|
||||||
|
<input
|
||||||
|
className={styles.input}
|
||||||
|
placeholder="Введите email *"
|
||||||
|
{...register("email")}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
/>
|
||||||
|
{errors.email && <span className={styles.error}>{errors.email.message}</span>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={styles.field}>
|
||||||
|
<input
|
||||||
|
className={styles.input}
|
||||||
|
placeholder="+7 (___) ___-__-__ *"
|
||||||
|
{...register("phone")}
|
||||||
|
onInput={handlePhoneInput}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
/>
|
||||||
|
{errors.phone && <span className={styles.error}>{errors.phone.message}</span>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
label={isSubmitting ? "ОТПРАВКА..." : "СКАЧАТЬ ПРАЙС-ЛИСТ"}
|
||||||
|
className={styles.button}
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
|
</motion.div>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default PriceLisForm;
|
||||||
@@ -2,6 +2,7 @@ import React from "react";
|
|||||||
import styles from "./QuestionsBlock.module.css";
|
import styles from "./QuestionsBlock.module.css";
|
||||||
import Button from "../Button/Button";
|
import Button from "../Button/Button";
|
||||||
import { PhoneOutlined } from "@ant-design/icons";
|
import { PhoneOutlined } from "@ant-design/icons";
|
||||||
|
import PhoneRequest from "../../features/MainPage/PhoneRequest";
|
||||||
|
|
||||||
const QuestionsBlock: React.FC = () => {
|
const QuestionsBlock: React.FC = () => {
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ const QuestionsBlock: React.FC = () => {
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
{/* FORM */}
|
{/* FORM */}
|
||||||
<div className={styles.form}>
|
{/*<div className={styles.form}>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="+7 (___) ___-__-__"
|
placeholder="+7 (___) ___-__-__"
|
||||||
@@ -49,7 +50,8 @@ const QuestionsBlock: React.FC = () => {
|
|||||||
label="ОТПРАВИТЬ"
|
label="ОТПРАВИТЬ"
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>*/}
|
||||||
|
<PhoneRequest/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
.form {
|
||||||
|
margin-top: 20px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input {
|
||||||
|
flex: 1;
|
||||||
|
padding: 18px 24px;
|
||||||
|
border-radius: 50px;
|
||||||
|
border: none;
|
||||||
|
font-size: 18px;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
color: red;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.success {
|
||||||
|
color: green;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import React, { useState } from "react";
|
||||||
|
import axios from "axios";
|
||||||
|
import styles from "./MainPage.module.css";
|
||||||
|
import Button from "../../components/Button/Button";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
onSend?: (phone: string) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const PhoneRequest: React.FC<Props> = ({ onSend }) => {
|
||||||
|
const [phone, setPhone] = useState("");
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [success, setSuccess] = useState(false);
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
|
||||||
|
// Автоформатирование "+7 (999) 999-99-99"
|
||||||
|
const formatPhone = (value: string) => {
|
||||||
|
const digits = value.replace(/\D/g, "");
|
||||||
|
|
||||||
|
let result = "+7 ";
|
||||||
|
if (digits.length > 1) result += "(" + digits.substring(1, 4);
|
||||||
|
if (digits.length >= 4) result += ") " + digits.substring(4, 7);
|
||||||
|
if (digits.length >= 7) result += "-" + digits.substring(7, 9);
|
||||||
|
if (digits.length >= 9) result += "-" + digits.substring(9, 11);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePhoneChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
let v = e.target.value;
|
||||||
|
|
||||||
|
if (v.replace(/\D/g, "").length === 0) {
|
||||||
|
setPhone("");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v[0] !== "+") {
|
||||||
|
v = "+7" + v;
|
||||||
|
}
|
||||||
|
|
||||||
|
setPhone(formatPhone(v));
|
||||||
|
};
|
||||||
|
|
||||||
|
const validatePhone = (p: string) => {
|
||||||
|
return p.replace(/\D/g, "").length === 11;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async () => {
|
||||||
|
setError("");
|
||||||
|
|
||||||
|
if (!validatePhone(phone)) {
|
||||||
|
setError("Введите корректный номер телефона");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (onSend) {
|
||||||
|
await onSend(phone);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
await axios.post("/api/contact/send-phone", { phone });
|
||||||
|
}
|
||||||
|
|
||||||
|
setSuccess(true);
|
||||||
|
setPhone("");
|
||||||
|
|
||||||
|
setTimeout(() => setSuccess(false), 3000);
|
||||||
|
} catch (e) {
|
||||||
|
setError("Ошибка отправки. Попробуйте позже.");
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={styles.form}>
|
||||||
|
<input
|
||||||
|
className={styles.input}
|
||||||
|
placeholder="+7 (___) ___-__-__"
|
||||||
|
type="tel"
|
||||||
|
value={phone}
|
||||||
|
onChange={handlePhoneChange}
|
||||||
|
maxLength={18}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
label="ОТПРАВИТЬ"
|
||||||
|
onClick={handleSubmit}
|
||||||
|
/>
|
||||||
|
{/*<button
|
||||||
|
className={styles.button}
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{loading ? "..." : "ОТПРАВИТЬ"}
|
||||||
|
</button>*/}
|
||||||
|
|
||||||
|
{error && <p className={styles.error}>{error}</p>}
|
||||||
|
{success && <p className={styles.success}>Заявка отправлена!</p>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default PhoneRequest;
|
||||||
Reference in new issue
Block a user