ExperimentBase.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import { mapGetters, mapActions } from 'vuex'
  8. import { API_ROUTES } from '@/functions'
  9. import { getExperimentConfig } from '@/config.utils'
  10. import { EXPERIMENT as experimentMsgId } from '@/../config.messagesId'
  11. export default {
  12. name: 'ExperimentBase',
  13. props: {
  14. sceneName: {
  15. type: String,
  16. required: true
  17. }
  18. },
  19. data() {
  20. return {
  21. experimentName: this.$route.name,
  22. loadingMessage: null,
  23. loadingErrorMessage: null,
  24. qualities: null
  25. }
  26. },
  27. computed: {
  28. ...mapGetters(['getHostURI', 'getExperimentProgress', 'isExperimentDone'])
  29. },
  30. mounted() {
  31. if (!this.getExperimentProgress({ experimentName: this.experimentName, sceneName: this.sceneName }).experimentName)
  32. this.sendMessage({
  33. msgId: experimentMsgId.STARTED,
  34. msg: {
  35. experimentName: this.experimentName,
  36. sceneName: this.sceneName
  37. }
  38. })
  39. // Check if the experiment is already finished
  40. if (this.experimentName && this.sceneName && this.isExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName })) {
  41. console.warn('Redirected from experiment. You can\'t go back in an experiment after finishing it.')
  42. this.$router.push(`/experiments/${this.experimentName}`)
  43. }
  44. },
  45. methods: {
  46. ...mapActions(['setExperimentProgress', 'setExperimentDone', 'sendMessage']),
  47. // Load progress from store into local state
  48. loadProgress() {
  49. if (!this.experimentName || !this.sceneName)
  50. return console.warn('Could not load progress : experimentName and sceneName must be defined')
  51. const progress = this.getExperimentProgress({ experimentName: this.experimentName, sceneName: this.sceneName })
  52. Object.assign(this.$data, progress)
  53. // console.log('Loaded data from store to local state.', progress)
  54. },
  55. // Save progress from local state into store
  56. saveProgress() {
  57. if (!this.experimentName || !this.sceneName)
  58. return console.warn('Could not save progress : experimentName and sceneName must be defined')
  59. this.setExperimentProgress({ experimentName: this.experimentName, sceneName: this.sceneName, data: this.$data })
  60. // console.log('Saved data from local state to store.', this.$data)
  61. },
  62. // Load a config object into the local state
  63. loadConfig() {
  64. const config = getExperimentConfig(this.experimentName, this.sceneName)
  65. // console.log('Loaded configuration', config)
  66. Object.assign(this.$data, config)
  67. },
  68. setExperimentFinished(done = true) {
  69. this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done })
  70. },
  71. // Finish an experiment, sending full data to the server
  72. // Don't forget to surcharge this function when using this mixin to add more data
  73. finishExperiment() {
  74. const obj = Object.assign(this.$data, { sceneName: this.sceneName })
  75. obj.loadingMessage = undefined
  76. obj.loadingErrorMessage = undefined
  77. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  78. this.setExperimentFinished()
  79. this.$router.push(`/experiments/${this.experimentName}/${this.sceneName}/validated`)
  80. },
  81. // Load qualities list from the API
  82. async getQualitiesList() {
  83. if (this.qualities) return
  84. const URI = `${this.getHostURI}${API_ROUTES.listSceneQualities(this.sceneName)}`
  85. const { data } = await fetch(URI).then(res => res.json())
  86. this.qualities = data
  87. },
  88. // Load an image from the API
  89. async getImage(quality) {
  90. const URI = `${this.getHostURI}${API_ROUTES.getImage(this.sceneName, quality)}`
  91. const { data } = await fetch(URI)
  92. .then(async res => {
  93. res.json = await res.json()
  94. return res
  95. })
  96. .then(res => {
  97. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  98. return res.json
  99. })
  100. data.link = this.getHostURI + data.link
  101. return data
  102. }
  103. }
  104. }
  105. </script>