Skip to content
Snippets Groups Projects
Commit 81a1f74b authored by jindra12's avatar jindra12 Committed by jan.bednarik
Browse files

make new api functional

parent 0f14b897
No related branches found
No related tags found
2 merge requests!804Release,!779Add basic newsletter form FE support
VueFormulate
Vue.use(VueFormulate, { Vue.use(VueFormulate, {
plugins: [VueFormulateI18n.cs.default], plugins: [VueFormulateI18n.cs.default],
locale: "cs", locale: "cs",
}); });
const errorDataKey = "mailtrainerror";
const errorMessage = ["Vyskytla se chyba při zpracování požadavku, omlouváme se"]; const errorMessage = ["Vyskytla se chyba při zpracování požadavku, omlouváme se"];
Vue.options.data ||= {}; Vue.options.data ||= {};
Vue.options.data.mailtrainerrors ||= []; Vue.options.data.mailtrainerrors ||= () => [];
Vue.options.methods ||= {}; Vue.options.methods ||= {};
Vue.options.methods.mailtrainsubmit = async function (data) { Vue.options.methods.mailtrainsubmit = async function (data) {
$(this.$el).data(errorDataKey, "");
Vue.options.data.mailtrainerrors = () => [];
$("[data-success]", this.$el).hide(); $("[data-success]", this.$el).hide();
const csrftoken = $("[name='csrfmiddlewaretoken']", this.$el).val(); const csrftoken = $("[name='csrfmiddlewaretoken']", this.$el).val();
const headers = new Headers(); const headers = new Headers();
headers.append('X-CSRFToken', csrftoken); headers.append('X-CSRFToken', csrftoken);
const createError = () => {
$(this.$el).data(errorDataKey, errorMessage);
Vue.options.data.mailtrainerrors = function() {
return $(this.$el).data(errorDataKey) || [];
};
};
try { try {
const response = await fetch("/newsletter/", { const response = await fetch("/newsletter/", {
method: "POST", method: "POST",
...@@ -24,11 +33,11 @@ Vue.options.methods.mailtrainsubmit = async function (data) { ...@@ -24,11 +33,11 @@ Vue.options.methods.mailtrainsubmit = async function (data) {
credentials: "include", credentials: "include",
}); });
if (response.status >= 400) { if (response.status >= 400) {
Vue.options.data.mailtrainerrors = errorMessage; createError();
} else { } else {
$("[data-success]", this.$el).show(); $("[data-success]", this.$el).show();
} }
} catch { } catch {
Vue.options.data.mailtrainerrors = errorMessage; createError();
} }
}; };
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
</ul> </ul>
</div> </div>
<div class="flex flex-col items-start"> <div class="flex flex-col items-start">
<formulate-form @submit="mailtrainsubmit" #default="{ isLoading }" :form-errors="mailtrainerrors"> <formulate-form name="newsletter" @submit="mailtrainsubmit" #default="{ isLoading }" :form-errors="mailtrainerrors()">
<formulate-errors></formulate-errors> <formulate-errors></formulate-errors>
{% csrf_token %} {% csrf_token %}
<formulate-input <formulate-input
......
...@@ -72,7 +72,7 @@ def subscribe_to_newsletter_ajax(request): ...@@ -72,7 +72,7 @@ def subscribe_to_newsletter_ajax(request):
client_response = HttpResponse() client_response = HttpResponse()
if request.method == "POST": if request.method == "POST":
body = json.loads(request.body) body = json.loads(request.body)
response = subscribe_to_newsletter(body["EMAIL"], body["LIST_ID"]) response = subscribe_to_newsletter(body["email"], body["list_id"])
if "error" in response.json(): if "error" in response.json():
client_response.status_code = 500 client_response.status_code = 500
else: else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment