From c58507ac4ee133301e3bea799c11094268077813 Mon Sep 17 00:00:00 2001 From: ifraimka Date: Tue, 19 Nov 2024 14:43:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=BF=D1=87=D1=83,=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=BA=D1=83=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?,=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D1=83=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D1=8B=20=D0=BD=D0=B0=20=D1=81=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=B5=D1=80,=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=BE=D1=82=D0=BA=D1=80=D0=B5?= =?UTF-8?q?=D0=BF=D0=B8=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB,=20=D0=BE?= =?UTF-8?q?=D1=82=D1=80=D0=B5=D0=B4=D0=B0=D1=87=D0=B8=D0=BB=20=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 47 +++++ package.json | 2 + src/api.js | 27 ++- src/assets/images/cross.svg | 1 + src/assets/images/index.js | 1 + src/components/ContactForm/ContactForm.jsx | 184 ++++++++++++++---- .../ContactForm/ContactForm.styles.scss | 100 ++++++++-- src/components/Field/Field.jsx | 43 +++- src/components/Field/Field.styles.scss | 78 ++++---- 9 files changed, 381 insertions(+), 102 deletions(-) create mode 100644 src/assets/images/cross.svg diff --git a/package-lock.json b/package-lock.json index 592bbc5..4feb03b 100755 --- a/package-lock.json +++ b/package-lock.json @@ -12,10 +12,12 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "@yandex/smart-captcha": "2.6.1", "html-react-parser": "^5.1.10", "lodash": "^4.17.21", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-google-recaptcha": "^3.1.0", "react-router-dom": "^6.22.0", "react-router-hash-link": "^2.4.3", "react-scripts": "5.0.1", @@ -4969,6 +4971,14 @@ "resolved": "https://registry.npmjs.org/@xtuc/long/-/long-4.2.2.tgz", "integrity": "sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==" }, + "node_modules/@yandex/smart-captcha": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/@yandex/smart-captcha/-/smart-captcha-2.6.1.tgz", + "integrity": "sha512-FlX/P54x1FOqZvng41N/qnzHi4e+s0fPeA1MS9AUO2Gu2/0Q5BZX62DO7u06lkhoqZ0nNqjelDNjMjMXcrsNWQ==", + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0" + } + }, "node_modules/abab": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.6.tgz", @@ -9176,6 +9186,19 @@ "he": "bin/he" } }, + "node_modules/hoist-non-react-statics": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz", + "integrity": "sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==", + "dependencies": { + "react-is": "^16.7.0" + } + }, + "node_modules/hoist-non-react-statics/node_modules/react-is": { + "version": "16.13.1", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", + "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" + }, "node_modules/hoopy": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/hoopy/-/hoopy-0.1.4.tgz", @@ -15024,6 +15047,18 @@ "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz", "integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==" }, + "node_modules/react-async-script": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/react-async-script/-/react-async-script-1.2.0.tgz", + "integrity": "sha512-bCpkbm9JiAuMGhkqoAiC0lLkb40DJ0HOEJIku+9JDjxX3Rcs+ztEOG13wbrOskt3n2DTrjshhaQ/iay+SnGg5Q==", + "dependencies": { + "hoist-non-react-statics": "^3.3.0", + "prop-types": "^15.5.0" + }, + "peerDependencies": { + "react": ">=16.4.1" + } + }, "node_modules/react-dev-utils": { "version": "12.0.1", "resolved": "https://registry.npmjs.org/react-dev-utils/-/react-dev-utils-12.0.1.tgz", @@ -15158,6 +15193,18 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.11.tgz", "integrity": "sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==" }, + "node_modules/react-google-recaptcha": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/react-google-recaptcha/-/react-google-recaptcha-3.1.0.tgz", + "integrity": "sha512-cYW2/DWas8nEKZGD7SCu9BSuVz8iOcOLHChHyi7upUuVhkpkhYG/6N3KDiTQ3XAiZ2UAZkfvYKMfAHOzBOcGEg==", + "dependencies": { + "prop-types": "^15.5.0", + "react-async-script": "^1.2.0" + }, + "peerDependencies": { + "react": ">=16.4.1" + } + }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", diff --git a/package.json b/package.json index cc03334..785dc35 100755 --- a/package.json +++ b/package.json @@ -7,10 +7,12 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "@yandex/smart-captcha": "2.6.1", "html-react-parser": "^5.1.10", "lodash": "^4.17.21", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-google-recaptcha": "^3.1.0", "react-router-dom": "^6.22.0", "react-router-hash-link": "^2.4.3", "react-scripts": "5.0.1", diff --git a/src/api.js b/src/api.js index 07ece25..e633573 100755 --- a/src/api.js +++ b/src/api.js @@ -15,6 +15,31 @@ const wp = new WPAPI( : { endpoint: reactPress.api.rest_url, nonce: reactPress.api.nonce } ); +console.log(wp); + +export async function sendContactForm(formData) { + console.log('Форма отправляется'); + for (let el of formData.entries()) { + console.log(el[0] + ': ' + el[1]); + } + try { + const response = await fetch(`${wp._options.endpoint}skbkit/v1/sendcontactform`, { + method: 'POST', + body: formData, + }); + + if (!response.ok) { + console.log(response); + console.log(response.body); + throw new Error('Ошибка сети', response); + } + + return response; + } catch (error) { + console.error('Ошибка отправки формы (api):', error); + } +} + function parseContent(str) { if (typeof str !== 'string') return str; const content = Parse(str); @@ -138,7 +163,7 @@ console.log('posts', posts); export async function getPosts(category) { try { - if (category) { + if (category) { return posts.other.filter(post => post.categories.includes(category)); } diff --git a/src/assets/images/cross.svg b/src/assets/images/cross.svg new file mode 100644 index 0000000..0cdb914 --- /dev/null +++ b/src/assets/images/cross.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/assets/images/index.js b/src/assets/images/index.js index 2a6fe29..4258125 100644 --- a/src/assets/images/index.js +++ b/src/assets/images/index.js @@ -2,5 +2,6 @@ export { default as car } from './car.jpeg'; export { default as newsMock } from './newsMock.png'; export { default as scb } from './scb.svg'; export { default as fileUpload } from './fileUpload.svg'; +export { default as cross } from './cross.svg'; export { default as arrow } from './arrow.svg'; export { default as labMock } from './labMock.jpeg'; \ No newline at end of file diff --git a/src/components/ContactForm/ContactForm.jsx b/src/components/ContactForm/ContactForm.jsx index db46509..7e5a9e1 100644 --- a/src/components/ContactForm/ContactForm.jsx +++ b/src/components/ContactForm/ContactForm.jsx @@ -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 ( -
-

Свяжитесь с нами!

-
-
- - -
- -
- - -
-
-
- ); + 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 ( +
+

Свяжитесь с нами!

+
+
+ + +
+ +
+ +
+ {formData.captcha ? ( + + ) : ( + + )} +
+
+
+
+ ); } export default ContactForm; \ No newline at end of file diff --git a/src/components/ContactForm/ContactForm.styles.scss b/src/components/ContactForm/ContactForm.styles.scss index d47ed4c..61f2826 100644 --- a/src/components/ContactForm/ContactForm.styles.scss +++ b/src/components/ContactForm/ContactForm.styles.scss @@ -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; + } } } } \ No newline at end of file diff --git a/src/components/Field/Field.jsx b/src/components/Field/Field.jsx index ebfbb04..61d1d81 100644 --- a/src/components/Field/Field.jsx +++ b/src/components/Field/Field.jsx @@ -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" ? ( - +
+ + {fileAttached && + + } +
) : ( )} diff --git a/src/components/Field/Field.styles.scss b/src/components/Field/Field.styles.scss index 894a3de..c3265a2 100644 --- a/src/components/Field/Field.styles.scss +++ b/src/components/Field/Field.styles.scss @@ -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; -} \ No newline at end of file +// .file-input input:focus + .upload-file-btn { +// outline: -webkit-focus-ring-color auto 1px; +// } \ No newline at end of file