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

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