|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import styles from './RecommendationWizard.module.css'; |
| 3 | +import { questions } from '../data/questions.js'; |
| 4 | +import { technologies } from '../data/technologies.js'; |
| 5 | + |
| 6 | +const State = { |
| 7 | + Waiting: 'waiting', |
| 8 | + Questioning: 'questioning', |
| 9 | + Ended: 'ended', |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * Based on the user's answers returns a list of technologies |
| 14 | + * to look at in order of priority. |
| 15 | + */ |
| 16 | +const getRecommendations = (selectedTags) => { |
| 17 | + const scoredTechnologies = []; |
| 18 | + for (const technology of technologies) { |
| 19 | + let score = 0; |
| 20 | + let add = true; |
| 21 | + |
| 22 | + for (const tag of selectedTags) { |
| 23 | + const [feature, deal] = tag.split('-'); |
| 24 | + const weight = technology.categories[feature]; |
| 25 | + if ((deal === 'deal' && typeof weight === 'undefined') || weight === 0) { |
| 26 | + // A 0 score on a category is a deal breaker |
| 27 | + console.log( |
| 28 | + `${technology.name} removed because of ${feature} is missing and it's a deal breaker` |
| 29 | + ); |
| 30 | + add = false; |
| 31 | + break; |
| 32 | + } |
| 33 | + score += weight || 0; |
| 34 | + } |
| 35 | + |
| 36 | + if (add) { |
| 37 | + scoredTechnologies.push({ |
| 38 | + name: technology.name, |
| 39 | + normalizedName: technology.normalizedName, |
| 40 | + score, |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const sortedTechnologies = scoredTechnologies.sort( |
| 46 | + (technologyA, technologyB) => { |
| 47 | + if (technologyA.score < technologyB.score) { |
| 48 | + return 1; |
| 49 | + } |
| 50 | + if (technologyA.score == technologyB.score) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + if (technologyA.score > technologyB.score) { |
| 55 | + return -1; |
| 56 | + } |
| 57 | + } |
| 58 | + ); |
| 59 | + return sortedTechnologies; |
| 60 | +}; |
| 61 | + |
| 62 | +const FinalRecommendation = ({ restart, selections }) => { |
| 63 | + const technologies = getRecommendations(selections); |
| 64 | + if (technologies.length === 0) { |
| 65 | + return ( |
| 66 | + <div> |
| 67 | + <p> |
| 68 | + We could not find any technology that checks all your criteria. Please |
| 69 | + try again changing some of the values (like the targetted platforms). |
| 70 | + </p> |
| 71 | + <button onClick={restart} className="button button--secondary"> |
| 72 | + Start again! |
| 73 | + </button> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + return ( |
| 78 | + <div> |
| 79 | + <p> |
| 80 | + Based on your answers the technologies we think you should investigate |
| 81 | + are: |
| 82 | + </p> |
| 83 | + <ul> |
| 84 | + {technologies.map((technology) => { |
| 85 | + return ( |
| 86 | + <li> |
| 87 | + <a href={`/docs/${technology.normalizedName}`}> |
| 88 | + {technology.name} |
| 89 | + </a> |
| 90 | + </li> |
| 91 | + ); |
| 92 | + })} |
| 93 | + </ul> |
| 94 | + <button onClick={restart} className="button button--secondary"> |
| 95 | + Start again! |
| 96 | + </button> |
| 97 | + |
| 98 | + <p> |
| 99 | + Doesn't seem right? Open an{' '} |
| 100 | + <a href="https://github.com/crossplatform-dev/crossplatform.dev/issues/new"> |
| 101 | + issue |
| 102 | + </a>{' '} |
| 103 | + with more details! |
| 104 | + </p> |
| 105 | + </div> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * |
| 111 | + * @param {QuestioningProps} param0 |
| 112 | + * @returns |
| 113 | + */ |
| 114 | +const Questioning = ({ questions, done }) => { |
| 115 | + const [question, setQuestion] = useState(questions[0]); |
| 116 | + const [remainingQuestions, setRemainingQuestions] = useState( |
| 117 | + questions.slice(1) |
| 118 | + ); |
| 119 | + const [selectedTags, setTags] = useState([]); |
| 120 | + |
| 121 | + /** |
| 122 | + * Handles the selection changes of inputs in the form to make |
| 123 | + * sure their state is updated in the React side. |
| 124 | + */ |
| 125 | + const handleChange = (e) => { |
| 126 | + const { checked, value } = e.target; |
| 127 | + if (value === 'none') { |
| 128 | + return; |
| 129 | + } |
| 130 | + const indexOf = selectedTags.indexOf(value); |
| 131 | + |
| 132 | + if (checked) { |
| 133 | + if (indexOf === -1) { |
| 134 | + setTags([...selectedTags, value]); |
| 135 | + } |
| 136 | + } else if (indexOf !== -1) { |
| 137 | + selectedTags.splice(indexOf, 1); |
| 138 | + setTags([...selectedTags]); |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + /** |
| 143 | + * Updates the user's selection for the current question |
| 144 | + * and moves to the next one or the final step. |
| 145 | + */ |
| 146 | + const handleSubmit = (evt) => { |
| 147 | + evt.preventDefault(); |
| 148 | + |
| 149 | + if (remainingQuestions.length > 0) { |
| 150 | + setQuestion(remainingQuestions[0]); |
| 151 | + setRemainingQuestions(remainingQuestions.slice(1)); |
| 152 | + } else { |
| 153 | + done(selectedTags); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + return ( |
| 158 | + <form onSubmit={handleSubmit}> |
| 159 | + <fieldset id="quiz"> |
| 160 | + <legend>{question.message}</legend> |
| 161 | + {question.choices.map((choice) => { |
| 162 | + const value = `${choice.value}-${ |
| 163 | + question.dealBreaker ? 'deal' : 'noDeal' |
| 164 | + }`; |
| 165 | + return ( |
| 166 | + <div key={choice.value}> |
| 167 | + <input |
| 168 | + type={question.type || 'radio'} |
| 169 | + id={choice.value} |
| 170 | + name="question" |
| 171 | + value={value} |
| 172 | + onChange={handleChange} |
| 173 | + /> |
| 174 | + <label htmlFor={choice.value}>{choice.name}</label> |
| 175 | + <br /> |
| 176 | + </div> |
| 177 | + ); |
| 178 | + })} |
| 179 | + <button className="button button--secondary">Next</button> |
| 180 | + </fieldset> |
| 181 | + </form> |
| 182 | + ); |
| 183 | +}; |
| 184 | + |
| 185 | +export default function RecommendationWizard() { |
| 186 | + const [status, setState] = useState(State.Questioning); |
| 187 | + const [selections, setSelections] = useState([]); |
| 188 | + |
| 189 | + const done = (choices) => { |
| 190 | + setSelections(choices); |
| 191 | + setState(State.Ended); |
| 192 | + }; |
| 193 | + |
| 194 | + const restart = () => { |
| 195 | + setState(State.Questioning); |
| 196 | + }; |
| 197 | + |
| 198 | + let section; |
| 199 | + if (status === State.Waiting) { |
| 200 | + section = <Intro setState={setState} />; |
| 201 | + } else if (status === State.Questioning) { |
| 202 | + section = <Questioning questions={questions} done={done} />; |
| 203 | + } else if (status === State.Ended) { |
| 204 | + section = <FinalRecommendation restart={restart} selections={selections} />; |
| 205 | + } |
| 206 | + |
| 207 | + return <article>{section}</article>; |
| 208 | +} |
0 commit comments