-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhowmuchyarn.js
56 lines (44 loc) · 1.97 KB
/
howmuchyarn.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
import { pulloverData, pulloverCrochetData, scarfData } from './data.js';
const howMuchYarnFrom = document.querySelector('.how-much-yarn');
/**
* Update the HTML values for a given project type.
*
* This will find all the elements with a class matching the project type and add the calculated value to the element's `textContent`.
*
* @param {import('./data.js').ProjectType} projectType
* @param {import('./data.js').ProjectTypeCrochet} projectTypeCrochet
* @param {Record<string, FormDataEntryValue>} formValues
*/
function renderMessageHtml(projectType, formValues) {
/** @type {import('./data.js').YarnWeight} */
const { yardageNeeded } = projectType[formValues.weight];
for (const [project, yardage] of Object.entries(yardageNeeded)) {
const ballsNeeded = Math.ceil(yardage * 1.05 / formValues.yardsPerBall);
const messageElement = document.querySelector(`.${project}`);
if (messageElement) {
messageElement.textContent = ballsNeeded;
}
}
}
function renderMessageHtmlCrochet(projectTypeCrochet, formValues) {
/** @type {import('./data.js').YarnWeight} */
const { yardageNeededCrochet } = projectTypeCrochet[formValues.weight];
for (const [projectCrochet, yardageCrochet] of Object.entries(yardageNeededCrochet)) {
const ballsNeededCrochet = Math.ceil(yardageCrochet * 1.05/ formValues.yardsPerBall);
const messageElementCrochet = document.querySelector(`.${projectCrochet}`);
if (messageElementCrochet) {
messageElementCrochet.textContent = ballsNeededCrochet;
}
}
}
howMuchYarnFrom.addEventListener('submit', function(evt) {
evt.preventDefault();
const formData = new FormData(evt.target);
const formValues = Object.fromEntries(formData.entries());
renderMessageHtml(pulloverData, formValues);
renderMessageHtmlCrochet(pulloverCrochetData, formValues);
renderMessageHtml(scarfData, formValues);
// renderMessageHtml(scarfDataCrochet, formValues);
// renderMessageHtml(blanketData, formValues);
// renderMessageHtml(blanketDataCrochet, formValues);
});