Skip to content

Commit 8bc76e8

Browse files
add rough logic for direct checking of logical state path with possibility, and setting state.props.license to a matching license when found
1 parent 19a6704 commit 8bc76e8

File tree

1 file changed

+90
-5
lines changed

1 file changed

+90
-5
lines changed

src/scripts.js

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ let rawStatePathRoutes = [
2121
// empty state obj
2222
let state = {};
2323

24+
// all found fieldsets
25+
const fieldsets = document.querySelectorAll('fieldset');
26+
2427
// function to parse and build state.possibilities
2528
// from rawStatePathRoutes
2629
function setStatePossibilities(state) {
@@ -63,17 +66,98 @@ function setStateParts(state) {
6366
state.parts[1] = 'which-license-do-you-need/cc-by/';
6467
state.parts[8] = 'attribution-details/';
6568
}
69+
// function to update state.parts
70+
function updateStateParts(element, index, event, state) {
71+
72+
state.parts[index] = element.id + '/' + event.target.value + '/';
73+
74+
// check if checkbox, with siblings
75+
if (event.target.getAttribute('type') == 'checkbox') {
76+
let checkboxElements = element.querySelectorAll('input[type="checkbox"]');
77+
let checkboxes = [];
78+
checkboxElements.forEach((checkbox, index) => {
79+
if (checkbox.checked) {
80+
checkboxes[index] = checkbox.value;
81+
}
82+
});
83+
84+
85+
let joinedCheckboxes = checkboxes.filter(Boolean).join('+');
86+
87+
state.parts[index] = element.id + '+' + joinedCheckboxes + '/';;
88+
}
6689

67-
// function to update and track state.parts
90+
// check if text input
91+
if (event.target.getAttribute('type') == 'text') {
92+
93+
state.parts[index] = element.id + '/';
94+
95+
}
96+
97+
console.log("state.parts (after change)");
98+
console.log(state.parts);
99+
}
68100

69101
// function to combine current tracked
70102
// state.parts into state.current
103+
function setStateCurrent(element, index, state) {
104+
state.parts.forEach((element, i) => {
105+
if (i > index) {
106+
state.parts.splice(i);
107+
}
108+
});
109+
// [T]: also reset value to nothing each time
71110

72-
// function to compare state.possibilities to state.current,
73-
// determine if valid license, or error
111+
state.current = state.parts.join('') //.slice(0, -1);
112+
}
74113

75114
// function to set state.props
76115
// including setting state.props.license (if valid)
116+
function setStateProps(state) {
117+
118+
state.props = {};
119+
state.props.license = 'unknown';
120+
121+
// check and match possibilities
122+
Object.keys(state.possibilities).forEach((possibility) => {
123+
if(state.possibilities[possibility].includes(state.current)) {
124+
state.props.license = possibility;
125+
console.log('matched');
126+
}
127+
});
128+
129+
}
130+
131+
// function to watch for fieldset changes
132+
function watchFieldsets(fieldsets, state) {
133+
fieldsets.forEach((element, index) => {
134+
135+
// [T]: set defaults here first in state.parts dynamically
136+
137+
element.addEventListener("change", (event) => {
138+
139+
console.log("something changed!");
140+
updateStateParts(element, index, event, state);
141+
142+
setStateCurrent(element, index, state);
143+
console.log("state.current (after change)");
144+
console.log(state.current);
145+
146+
setStateProps(state);
147+
console.log("state.props (after change)");
148+
console.log(state.props);
149+
150+
});
151+
152+
});
153+
}
154+
155+
156+
157+
// function to compare state.possibilities to state.current,
158+
// determine if valid license, or error
159+
160+
77161

78162
// stepper logic here for what parts of form are
79163
// displayed/hidden, as state.parts and state.current
@@ -90,10 +174,11 @@ function setStateParts(state) {
90174

91175
// full flow logic
92176
setStateParts(state);
93-
console.log('state.parts (at default)');
177+
console.log("state.parts (at default)");
94178
console.log(state.parts);
95179

96180
setStatePossibilities(state);
97-
console.log('state.possibilities');
181+
console.log("state.possibilities");
98182
console.log(state.possibilities);
99183

184+
watchFieldsets(fieldsets, state);

0 commit comments

Comments
 (0)