From 2362fd74d871849937b7b5aa46a74d5fb2070602 Mon Sep 17 00:00:00 2001 From: Sanjay B Date: Thu, 28 Oct 2021 16:40:26 +0530 Subject: [PATCH 1/2] POST contact form via javascript --- app/contact/views.py | 12 +++++----- app/static/js/about.js | 48 +++++++++++++++++++++++++++++++++++++++- app/templates/about.html | 8 ++----- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/contact/views.py b/app/contact/views.py index d142a6a..91a0cd2 100644 --- a/app/contact/views.py +++ b/app/contact/views.py @@ -1,13 +1,12 @@ from django.conf import settings from django.core.mail import EmailMessage from django.shortcuts import render - +from app.video.utils import render_to_json_response from . import forms from . import models def index(request): - context = { - } + success = True if request.method == 'POST': form = forms.ContactForm(request.POST) if form.is_valid(): @@ -17,7 +16,8 @@ def index(request): to = [settings.CONTACT_TO_EMAIL] msg = EmailMessage(subject, message, from_, to, reply_to=[form.cleaned_data['email']]) msg.send(fail_silently=True) - context['submitted'] = True else: - context['form'] = form - return render(request, 'contact.html', context) + success = False + else: + success = False + return render_to_json_response({'success': success}) diff --git a/app/static/js/about.js b/app/static/js/about.js index 7be21d2..550cb99 100755 --- a/app/static/js/about.js +++ b/app/static/js/about.js @@ -1,11 +1,57 @@ document.querySelectorAll('.accordion-checkbox').forEach(cbox => { cbox.addEventListener('change', function() { const label = this.previousElementSibling - const caret = label.querySelector('.text') + const caret = label.querySelector('.text-about') if (cbox.checked) { caret.innerHTML = '' } else { caret.innerHTML = '' } }) +}) + +function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie !== '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim() + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + if (!cookieValue && name === 'csrftoken') { + var input = document.querySelector('input[name="csrfmiddlewaretoken"]') + if (input) { + cookieValue = input.value + } + } + return cookieValue; +} + +document.getElementById('contact-form').addEventListener('submit', async function(event) { + // console.log('foobar') + event.preventDefault() + let data = new FormData() + data.append('email', document.getElementById('input-email').value) + data.append('message', document.getElementById('input-msg').value) + data.append('csrfmiddlewaretoken', getCookie('csrftoken')) + document.getElementById('contact-container').innerHTML = '...' + let success; + try { + const response = await fetch('/polis+contact/', { + method: 'POST', + body: data + }) + let resData = await response.json() + success = resData.success + } catch (e) { + console.log('error', e) + success = false + } + const thanksMsg = success ? 'Thanks for your message 謝謝您的訊息' : 'Failed to send message. Try again later.' + document.getElementById('contact-container').innerHTML = thanksMsg }) \ No newline at end of file diff --git a/app/templates/about.html b/app/templates/about.html index bc530c2..765f69e 100755 --- a/app/templates/about.html +++ b/app/templates/about.html @@ -130,11 +130,8 @@
-
- {% if submitted %} - Thanks for your message - {% else %} -
+
+ @@ -144,7 +141,6 @@ {% csrf_token %} - {% endif %}
From 0795cd3b956f268100b4bb3a7639b42d373b81a1 Mon Sep 17 00:00:00 2001 From: imohkay Date: Thu, 28 Oct 2021 16:58:52 +0530 Subject: [PATCH 2/2] styling for alert --- app/static/css/partials/_about.scss | 12 +++++++++++- app/static/css/partials/_contact.scss | 1 + app/static/js/about.js | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/static/css/partials/_about.scss b/app/static/css/partials/_about.scss index 8238c6b..d61e6cb 100755 --- a/app/static/css/partials/_about.scss +++ b/app/static/css/partials/_about.scss @@ -103,8 +103,18 @@ display: block; } } + + .alert { + @media screen and (min-width: 800px) { + width: 50%; + } + } } .accordion-checkbox:checked + .accordion-content { display: block; -} \ No newline at end of file +} + +.alert { + padding: var(--spacing) 0; +} \ No newline at end of file diff --git a/app/static/css/partials/_contact.scss b/app/static/css/partials/_contact.scss index d325156..4512d96 100755 --- a/app/static/css/partials/_contact.scss +++ b/app/static/css/partials/_contact.scss @@ -48,6 +48,7 @@ display: block; background-size: 400% 100%; animation: background_animation 60s ease-in-out infinite, text_animation 60s ease-in-out infinite; + cursor: pointer; } } diff --git a/app/static/js/about.js b/app/static/js/about.js index 550cb99..ca22c44 100755 --- a/app/static/js/about.js +++ b/app/static/js/about.js @@ -53,5 +53,5 @@ document.getElementById('contact-form').addEventListener('submit', async functio success = false } const thanksMsg = success ? 'Thanks for your message 謝謝您的訊息' : 'Failed to send message. Try again later.' - document.getElementById('contact-container').innerHTML = thanksMsg + document.getElementById('contact-container').innerHTML = `
${thanksMsg}
` }) \ No newline at end of file