-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRecommendationWizard.js
208 lines (189 loc) · 5.34 KB
/
RecommendationWizard.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, { useState } from 'react';
import styles from './RecommendationWizard.module.css';
import { questions } from '../data/questions.js';
import { technologies } from '../data/technologies.js';
const State = {
Waiting: 'waiting',
Questioning: 'questioning',
Ended: 'ended',
};
/**
* Based on the user's answers returns a list of technologies
* to look at in order of priority.
*/
const getRecommendations = (selectedTags) => {
const scoredTechnologies = [];
for (const technology of technologies) {
let score = 0;
let add = true;
for (const tag of selectedTags) {
const [feature, deal] = tag.split('-');
const weight = technology.categories[feature];
if ((deal === 'deal' && typeof weight === 'undefined') || weight === 0) {
// A 0 score on a category is a deal breaker
console.log(
`${technology.name} removed because of ${feature} is missing and it's a deal breaker`
);
add = false;
break;
}
score += weight || 0;
}
if (add) {
scoredTechnologies.push({
name: technology.name,
normalizedName: technology.normalizedName,
score,
});
}
}
const sortedTechnologies = scoredTechnologies.sort(
(technologyA, technologyB) => {
if (technologyA.score < technologyB.score) {
return 1;
}
if (technologyA.score == technologyB.score) {
return 0;
}
if (technologyA.score > technologyB.score) {
return -1;
}
}
);
return sortedTechnologies;
};
const FinalRecommendation = ({ restart, selections }) => {
const technologies = getRecommendations(selections);
if (technologies.length === 0) {
return (
<div>
<p>
We could not find any technology that checks all your criteria. Please
try again changing some of the values (like the targetted platforms).
</p>
<button onClick={restart} className="button button--secondary">
Start again!
</button>
</div>
);
}
return (
<div>
<p>
Based on your answers the technologies we think you should investigate
are:
</p>
<ul>
{technologies.map((technology) => {
return (
<li>
<a href={`/docs/${technology.normalizedName}`}>
{technology.name}
</a>
</li>
);
})}
</ul>
<button onClick={restart} className="button button--secondary">
Start again!
</button>
<p>
Doesn't seem right? Open an{' '}
<a href="https://github.com/crossplatform-dev/crossplatform.dev/issues/new">
issue
</a>{' '}
with more details!
</p>
</div>
);
};
/**
*
* @param {QuestioningProps} param0
* @returns
*/
const Questioning = ({ questions, done }) => {
const [question, setQuestion] = useState(questions[0]);
const [remainingQuestions, setRemainingQuestions] = useState(
questions.slice(1)
);
const [selectedTags, setTags] = useState([]);
/**
* Handles the selection changes of inputs in the form to make
* sure their state is updated in the React side.
*/
const handleChange = (e) => {
const { checked, value } = e.target;
if (value === 'none') {
return;
}
const indexOf = selectedTags.indexOf(value);
if (checked) {
if (indexOf === -1) {
setTags([...selectedTags, value]);
}
} else if (indexOf !== -1) {
selectedTags.splice(indexOf, 1);
setTags([...selectedTags]);
}
};
/**
* Updates the user's selection for the current question
* and moves to the next one or the final step.
*/
const handleSubmit = (evt) => {
evt.preventDefault();
if (remainingQuestions.length > 0) {
setQuestion(remainingQuestions[0]);
setRemainingQuestions(remainingQuestions.slice(1));
} else {
done(selectedTags);
}
};
return (
<form onSubmit={handleSubmit}>
<fieldset id="quiz">
<legend>{question.message}</legend>
{question.choices.map((choice) => {
const value = `${choice.value}-${
question.dealBreaker ? 'deal' : 'noDeal'
}`;
return (
<div key={choice.value}>
<input
type={question.type || 'radio'}
id={choice.value}
name="question"
value={value}
onChange={handleChange}
/>
<label htmlFor={choice.value}>{choice.name}</label>
<br />
</div>
);
})}
<button className="button button--secondary">Next</button>
</fieldset>
</form>
);
};
export default function RecommendationWizard() {
const [status, setState] = useState(State.Questioning);
const [selections, setSelections] = useState([]);
const done = (choices) => {
setSelections(choices);
setState(State.Ended);
};
const restart = () => {
setState(State.Questioning);
};
let section;
if (status === State.Waiting) {
section = <Intro setState={setState} />;
} else if (status === State.Questioning) {
section = <Questioning questions={questions} done={done} />;
} else if (status === State.Ended) {
section = <FinalRecommendation restart={restart} selections={selections} />;
}
return <article>{section}</article>;
}