Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | import { useRouter } from 'next/router'; import { useReducer, useEffect, useRef } from 'react'; interface Answer { question: string; answer: string; isCorrect: boolean; questionNum: number; } interface AnswerOption { answerText: string; isCorrect: boolean; } interface Question { questionText?: string; questionHtml?: string; answerOptions?: AnswerOption[]; } interface SurveyState { currentQuestion: number; questionsDone: number; success: boolean; answers: Answer[]; } type SurveyAction = | { type: 'NEXT_QUESTION' } | { type: 'PREVIOUS_QUESTION' } | { type: 'ADD_ANSWER'; payload: Answer }; const initialState: SurveyState = { currentQuestion: 0, questionsDone: 0, success: false, answers: [], }; const reducer = (state: SurveyState, action: SurveyAction): SurveyState => { const { questionNum } = 'payload' in action ? action.payload : ({} as Partial<Answer>); const { answers, questionsDone, currentQuestion } = state; const newAnswers: Answer[] = [...answers]; switch (action.type) { case 'NEXT_QUESTION': return { ...state, currentQuestion: currentQuestion + 1, }; case 'PREVIOUS_QUESTION': return { ...state, currentQuestion: currentQuestion - 1, }; case 'ADD_ANSWER': Iif (questionNum !== undefined) { newAnswers[questionNum] = { ...action.payload, }; return { ...state, answers: newAnswers, currentQuestion: currentQuestion + 1, questionsDone: questionNum > questionsDone ? questionNum : questionsDone, success: newAnswers.every((answer) => answer.isCorrect) && currentQuestion === 10, }; } return state; default: return state; } }; const useSurvey = () => { const { query } = useRouter(); const { name = '👋' } = query; const parsedName = Array.isArray(name) ? name[0] : name; const emailRef = useRef<boolean>(false); const [state, dispatch] = useReducer(reducer, initialState); const questions: Question[] = [ { questionText: '¿ Continuamos ?', questionHtml: ` <h1>Hola ${ parsedName.charAt(0).toUpperCase() + parsedName.slice(1) }, gracias por ponerte en contanto!</h1> <p> Si has llegado hasta aquí, seguro que es por que tienes una posición increíble y me lo quieres contar!! Pero antes de conocernos y que me hagas muchas preguntas, a mi también me gustaría verificar algunas cosas primero, para saber si la posición y yo, somos compatibles. </p> <p> Si lo somos... te mostraré mi <strong>número de teléfono</strong>, <strong>disponibilidad</strong>, <strong>currículum actualizado</strong> y muchas cosas más. </p> <h2>¿ Te apuntas ?</h2> `, answerOptions: [{ answerText: 'Si', isCorrect: true }], }, { questionText: 'Tipo de posición:', questionHtml: '<h1>La posición es para un perfil :</h1>', answerOptions: [ { answerText: 'Frontend', isCorrect: true }, { answerText: 'Backend', isCorrect: false }, { answerText: 'Fullstack', isCorrect: false }, ], }, { questionText: 'Tipo de contrato:', questionHtml: '<h1>El contrato será :</h1>', answerOptions: [ { answerText: 'Remoto 100% pero solo en España', isCorrect: true }, { answerText: 'Remoto 100% en todo el mundo', isCorrect: true }, { answerText: 'Ninguna de las dos', isCorrect: false }, ], }, { questionText: 'Rango salarial:', questionHtml: '<h1>El rango salarial es :</h1>', answerOptions: [ { answerText: 'Menor o igual a 59.000€', isCorrect: false }, { answerText: 'Entre 60.000€ y 69.000€', isCorrect: true }, { answerText: 'Mayor o igual a 70.000€', isCorrect: true }, ], }, { questionText: 'Tipo de equipo:', questionHtml: '<h1>El equipo de trabajo será :</h1>', answerOptions: [ { answerText: 'Nacional', isCorrect: true }, { answerText: 'Internacional', isCorrect: true }, { answerText: 'Lo desconozco', isCorrect: true }, ], }, { questionText: 'Salario variable:', questionHtml: '<h1>¿ El salario tendrá una parte variable ?</h1>', answerOptions: [ { answerText: 'Si', isCorrect: false }, { answerText: 'No', isCorrect: true }, { answerText: 'Lo desconozco', isCorrect: true }, ], }, { questionText: 'Días de vacaciones:', questionHtml: '<h1>Los días de vacaciones son :</h1>', answerOptions: [ { answerText: '22 - 23', isCorrect: true }, { answerText: '24 - 26', isCorrect: true }, { answerText: '27 o más', isCorrect: true }, ], }, { questionText: 'Horario de trabajo flexible:', questionHtml: '<h1>¿ El horario de trabajo es flexible ?</h1>', answerOptions: [ { answerText: 'Si, pero con peros.', isCorrect: true }, { answerText: 'Si, totalmente.', isCorrect: true }, { answerText: 'No', isCorrect: false }, ], }, { questionText: 'Tipo de hardware:', questionHtml: '<h1>El hardware de trabajo será :', answerOptions: [ { answerText: 'Windows', isCorrect: true }, { answerText: 'Mac/Linux', isCorrect: true }, { answerText: 'A escoger', isCorrect: true }, ], }, { questionText: 'Promedio de antigüedad:', questionHtml: '<h1>La media de antigüedad de los compañeros es de :</h1>', answerOptions: [ { answerText: 'Menos de 1 año', isCorrect: false }, { answerText: 'Menos de 2 años', isCorrect: true }, { answerText: 'Más de 2 años', isCorrect: true }, ], }, { questionText: 'Proceso de selección:', questionHtml: '<h1>El proceso de selección consta de :</h1>', answerOptions: [ { answerText: 'Entrevistas', isCorrect: true }, { answerText: 'Entrevistas y prueba técnica larga', isCorrect: true }, { answerText: 'Entrevistas y prueba técnica corta', isCorrect: true }, ], }, { questionHtml: ` <section> ${ state.success ? ` <h1>¡¡¡ OMG !!! Somos compatibles</h1> <img src="/celebration.gif" alt="celebration" width="100%" /> <h2> ¿ Quieres contarme más ? <a href="tel:+34603018268'" >603018268</a> </h2> <table> <tr> <td> Lunes </td> <td> 10:00 - 14:15 </td> <td> 17:45 - 21:00 </td> </tr> <tr> <td> Martes </td> <td> 10:00 - 14:15 </td> <td> 17:45 - 21:00 </td> </tr> <tr> <td> Miércoles </td> <td> 10:00 - 14:15 </td> <td> 17:45 - 21:00 </td> </tr> <tr> <td> Jueves </td> <td> 10:00 - 14:15 </td> <td> 17:45 - 21:00 </td> </tr> <tr> <td> Viernes </td> <td> 10:00 - 14:15 </td> </tr> </table> <ul> <li> Escríbeme a <a href="mailto:xabier.lameiro@gmail.cm" target="_blank" rel="noopener noreferrer">xabier.lameiro@gmail.com</a></li> <li> Enlace a mi <a href="https://github.com/xabierlameiro" target="_blank" rel="noopener noreferrer"> github </a></li> <li> Enlace a mi <a href="https://www.linkedin.com/in/xlameiro/" target="_blank" rel="noopener noreferrer"> linkedin </a></li> <li> Descárgate mi <a href="/xabierlameiro.com.pdf" download> currículum </a></li> </ul>` : ` <h1> Lo siento mucho ${name} </h1> <img src="/disappointed.gif" alt="celebration" width="100%" /> <p> Pero parece que la posición y yo no somos compatibles en estos momentos! </p> <p> Te agradezco mucho tu tiempo y espero que encuentres lo que buscas muy pronto. </p> <p> Un saludo. Xabier! 👋 </p> ` } </section>`, }, ]; useEffect(() => { Iif (state.currentQuestion === questions.length - 1 && !emailRef.current) { (async () => { try { await fetch('/api/email', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ subject: `Job survey from ${name} - ${new Date().toLocaleString()}`, message: ` <h1> ${name} </h1> <code> ${navigator.userAgent} </code> <ol> ${state.answers.map( (answer) => `<li> ${answer.question} : ${answer.answer} - ${ answer.isCorrect ? '✅' : '🚫' } </li>` )} </ol> `.replace(/,/g, ''), }), }); emailRef.current = true; } catch { // Error ignored } })(); } }, [state, name, questions.length]); const handlePreviousQuestion = () => { Iif (state.currentQuestion > 1) dispatch({ type: 'PREVIOUS_QUESTION' }); }; const handleNextQuestion = () => { Iif (state.currentQuestion !== 0 && state.questionsDone >= state.currentQuestion) dispatch({ type: 'NEXT_QUESTION' }); }; const handleAnswerOptionClick = (payload: { question: string; answer: string; isCorrect: boolean; questionNum: number; }) => dispatch({ type: 'ADD_ANSWER', payload }); return { questions, answers: state.answers, surveySuccess: state.success, currentQuestionNum: state.currentQuestion, questionsDoneNum: state.questionsDone, totalQuestions: questions.length, handleAnswerOptionClick, handleNextQuestion, handlePreviousQuestion, }; }; export default useSurvey; export type { Question }; |