fix #21
This commit is contained in:
parent
b1418ef194
commit
5c467757fe
4 changed files with 58 additions and 6 deletions
|
|
@ -109,17 +109,22 @@ function slugify(str) {
|
||||||
|
|
||||||
function subscribe(event) {
|
function subscribe(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const email = document.querySelector("#subscribe-form input");
|
const emailInput = document.querySelector("#subscribe-form input");
|
||||||
|
|
||||||
if (email.value.toLowerCase().match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) {
|
if (emailInput.value.toLowerCase().match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) {
|
||||||
const header = {
|
const header = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: email.value,
|
body: JSON.stringify(emailInput.value),
|
||||||
};
|
};
|
||||||
|
|
||||||
fetch("/subscribe.json");
|
fetch("/subscribe.json", header)
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
const formNode = emailInput.parentNode.parentNode;
|
||||||
|
formNode.outerHTML = "<p>" + data.message + "</p>";
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
email.value = "E-mail invalide. Recommencez.";
|
emailInput.value = "E-mail invalide. Recommencez.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,6 @@ return array(
|
||||||
'routes' => array(
|
'routes' => array(
|
||||||
require __DIR__ . '/routes/virtual-category.php',
|
require __DIR__ . '/routes/virtual-category.php',
|
||||||
require __DIR__ . '/routes/send-newsletter.php',
|
require __DIR__ . '/routes/send-newsletter.php',
|
||||||
|
require __DIR__ . '/routes/subscribe.php',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ return [
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$kirby->email([
|
$kirby->email([
|
||||||
'from' => "adrien.payet@outlook.com",
|
'from' => "info@actuel-inactuel.fr",
|
||||||
'to' => 'payet.adrien@protonmail.com',
|
'to' => 'payet.adrien@protonmail.com',
|
||||||
'subject' => 'actualités',
|
'subject' => 'actualités',
|
||||||
'body' => 'Ceci est un test simple.',
|
'body' => 'Ceci est un test simple.',
|
||||||
|
|
|
||||||
46
site/config/routes/subscribe.php
Normal file
46
site/config/routes/subscribe.php
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'pattern' => '/subscribe.json',
|
||||||
|
'method' => 'POST',
|
||||||
|
'action' => function () {
|
||||||
|
$jsonRequest = file_get_contents("php://input");
|
||||||
|
$email = Str::lower(json_decode($jsonRequest));
|
||||||
|
|
||||||
|
if (V::email($email)) {
|
||||||
|
kirby()->impersonate('kirby');
|
||||||
|
|
||||||
|
$page = page("inscription");
|
||||||
|
$subscribers = $page->subscribers()->yaml();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$emailExists = in_array(["email" => $email], $subscribers);
|
||||||
|
|
||||||
|
if ($emailExists) {
|
||||||
|
return [
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Cet email est déjà inscris."
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$newSubscriber = ["email" => $email];
|
||||||
|
$subscribers[] = $newSubscriber;
|
||||||
|
|
||||||
|
$page->update([
|
||||||
|
'subscribers' => $subscribers
|
||||||
|
]);
|
||||||
|
|
||||||
|
return [
|
||||||
|
"status" => "success",
|
||||||
|
"message" => "Inscription réussie."
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
return [
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Email invalide."
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue