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