generated from vrk/eleventy-example
/
journal.js
174 lines (153 loc) · 5.22 KB
/
journal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const PROD_ENDPOINT = 'https://lively-phoenix-d82365.netlify.app/.netlify/functions/journalsubmit';
// const DEV_ENDPOINT = 'http://localhost:8888/.netlify/functions/journalsubmit'
const ENDPOINT = PROD_ENDPOINT;
const MB_IN_BYTES = 1000000;
const MAX_FILE_SIZE_IN_BYTES = 5 * MB_IN_BYTES; // 5 mb
const form = document.getElementById('journalsubmit');
const spinner = document.getElementById('submitting');
const formError = document.getElementById('submitting-error');
form.addEventListener('submit', async (event) => {
event.preventDefault();
spinner.hidden = false;
formError.hidden = true;
const journallayout = document.getElementById("journal-img-input");
const formData = new FormData();
formData.append('name', document.getElementById('name').value);
formData.append('email', document.getElementById('email').value);
formData.append("journallayout", journallayout.files[0]);
const desc = document.getElementById('layout-description').value;
if (desc && desc.trim().length > 0) {
formData.append('layoutdescription', desc);
}
const social = document.getElementById('social-media').value;
if (social && social.trim().length > 0) {
formData.append('socialmedia', social);
}
try {
const response = await fetch(ENDPOINT, {
method: "POST",
body: formData
});
if (response.status === 200) {
spinner.hidden = true;
localStorage.clear();
window.location.href = '/success';
return;
} else {
const data = await response.json();
formError.innerHTML = `
There was an error submitting your form :( <br>
Error message: "${data.error}" <br>
Please email your submission to hello@pouch.cafe!
`;
formError.hidden = false;
}
} catch (error) {
formError.innerHTML = `
There was an error submitting your form :( <br>
Error message: "${error}" <br>
Please email your submission to hello@pouch.cafe!
`;
formError.hidden = false;
}
spinner.hidden = true;
})
const chooseJournal = document.getElementById('upload-photo');
const resetJournal = document.getElementById('reset-photo');
const fileInput = document.getElementById('journal-img-input');
const fileInputError = document.querySelector(`#upload-photo + span.error`);
const canvas = document.getElementById('preview-journal');
const context = canvas.getContext("2d");
fileInput.onchange = e => {
fileInputError.innerHTML = '';
const file = e.target.files[0];
if (file.type.match('image.*')) {
const reader = new FileReader();
const img = new Image()
// Read in the image file as a data URL.
reader.readAsDataURL(file);
reader.onload = function(evt){
if (evt.target.readyState == FileReader.DONE) {
if (file.size > MAX_FILE_SIZE_IN_BYTES) {
fileInput.value = '';
fileInputError.innerHTML = `File too large (${(file.size / MB_IN_BYTES).toFixed(1)} MB, max 5 MB)`;
return;
}
img.src = evt.target.result;
img.onload = () => {
canvas.height = img.height;
canvas.width = img.width;
context.drawImage(img, 0, 0, img.width, img.height);
}
chooseJournal.hidden = true;
resetJournal.hidden = false;
}
}
} else {
fileInput.value = '';
fileInputError.innerHTML = 'Must select an image.';
}
}
canvas.addEventListener('click', () => {
fileInput.click();
});
resetJournal.addEventListener('click', () => {
canvas.height = 0;
canvas.width = 0;
context.clearRect(0, 0, canvas.width, canvas.height);
chooseJournal.hidden = false;
resetJournal.hidden = true;
fileInput.value = "";
});
/** validation */
const addValidationListenersFor = (id, validityCheck) => {
const input = document.getElementById(id);
let noBlur = true;
const errorHandler = (event) => {
noBlur = false;
validityCheck();
};
input.addEventListener('blur', errorHandler);
input.addEventListener('input', (event) => {
if (noBlur) { return };
return errorHandler(event);
});
}
function createValidityCheckFor(id, errorMsg) {
const input = document.getElementById(id);
const inputError = document.querySelector(`#${id} + span.error`);
return () => {
if (input.validity.valid) {
input.classList.remove('error');
inputError.innerHTML = '';
return true;
} else {
input.classList.add('error');
inputError.innerHTML = errorMsg;
return false;
}
}
}
const emailValidityCheck = createValidityCheckFor('email', 'A valid email is required');
const nameValidityCheck = createValidityCheckFor('name', 'Name is required');
addValidationListenersFor("email", emailValidityCheck);
addValidationListenersFor("name", nameValidityCheck);
setupLocalStorageSavingFor('layout-description');
setupLocalStorageSavingFor('social-media');
setupLocalStorageSavingFor('name');
setupLocalStorageSavingFor('email');
function setupLocalStorageSavingFor(id) {
const inputEl = document.getElementById(id);
const savedText = localStorage.getItem(id);
if (savedText) {
inputEl.value = savedText;
}
const save = () => {
localStorage.setItem(id, inputEl.value);
}
let timeout;
inputEl.addEventListener('input', () => {
clearTimeout(timeout);
timeout = setTimeout(save, 1000);
});
}