Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Survey } from '../entities/index.js'
  2. import { fetchQuestions } from '../services/index.js'
  3. import { possible } from './lang.js'
  4. const promptToComponent = {
  5. email: 'FormInput',
  6. name: 'FormInput',
  7. seeking: 'FormDropdown',
  8. urgency: 'FormDropdown',
  9. presence: 'FormDropdown',
  10. duration: 'FormDropdown',
  11. experience: 'FormTags',
  12. pronouns: 'FormDropdown',
  13. language: 'FormDropdown',
  14. image: 'FormInput',
  15. distance: 'FormInput',
  16. zipcode: 'FormInput',
  17. blurb: 'FormInput',
  18. aspects: 'Aspects'
  19. }
  20. /**
  21. * Make a step from match or step information
  22. * @param {object} match
  23. * @param {object} step
  24. * @returns something like a response_key with possible responses
  25. */
  26. const formatStep = (match, step) => {
  27. const responsesByCategory = possible['usa']
  28. const responseKey = {
  29. response_key_id: match ? match.response_key_id : null,
  30. response_key_category: match ? match.response_key_category : 'profile',
  31. response_key_prompt: match ? match.response_key_prompt : step,
  32. response_key_description: match ? match.response_key_description : null,
  33. }
  34. return {
  35. ...responseKey,
  36. responses: responsesByCategory[step] ? responsesByCategory[step] : [],
  37. }
  38. }
  39. const associateWithComponent = responseKeyLike => {
  40. let component = promptToComponent[responseKeyLike.response_key_prompt]
  41. return { ...responseKeyLike, component }
  42. }
  43. const hasMatch = (step, inArray) => {
  44. return inArray.find(q => q.response_key_prompt == step)
  45. }
  46. class SurveyFactory {
  47. constructor() {
  48. this.questionsFromDb = []
  49. }
  50. _setSteps(langFile) {
  51. const stepsToProcess = [...Object.values(langFile)]
  52. const seenIds = []
  53. const stepsInCommon = stepsToProcess.map(step => {
  54. // Match question to step
  55. const match = hasMatch(step, this.questionsFromDb)
  56. if (match) {
  57. seenIds.push(match.response_key_id)
  58. }
  59. const responseKeyLike = formatStep(match, step)
  60. const withComponent = associateWithComponent(responseKeyLike)
  61. console.log('withComponent :>> ', withComponent)
  62. return withComponent
  63. })
  64. const unseen = this.questionsFromDb.filter(
  65. q => !seenIds.includes(q.response_key_id),
  66. )
  67. return [...stepsInCommon, ...unseen]
  68. }
  69. async getQuestions() {
  70. try {
  71. this.questionsFromDb = await fetchQuestions()
  72. return this.questionsFromDb
  73. } catch (err) {
  74. console.error(err)
  75. }
  76. }
  77. async createSurvey(langFile, roleTree) {
  78. if (!this.questionsFromDb.length) {
  79. const res = await this.getQuestions()
  80. console.warn(
  81. `Attempted to create a survey before getting questions: retrieved ${res.length} questions`,
  82. )
  83. }
  84. const steps = this._setSteps(langFile)
  85. return new Survey(steps, roleTree)
  86. }
  87. }
  88. export { SurveyFactory }