ExperimentBase.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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: null, // Must be redefined in parent component
  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. // Finish an experiment, sending full data to the server
  69. // Don't forget to surcharge this function when using this mixin to add more data
  70. finishExperiment() {
  71. const obj = Object.assign(this.$data, { sceneName: this.sceneName })
  72. obj.loadingMessage = undefined
  73. obj.loadingErrorMessage = undefined
  74. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  75. this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done: true })
  76. this.$router.push(`/experiments/${this.experimentName}/${this.sceneName}/validated`)
  77. },
  78. // Load qualities list from the API
  79. async getQualitiesList() {
  80. if (this.qualities) return
  81. const URI = `${this.getHostURI}${API_ROUTES.listSceneQualities(this.sceneName)}`
  82. const { data } = await fetch(URI).then(res => res.json())
  83. this.qualities = data
  84. this.saveProgress()
  85. },
  86. // Load an image from the API
  87. async getImage(quality) {
  88. const URI = `${this.getHostURI}${API_ROUTES.getImage(this.sceneName, quality)}`
  89. const { data } = await fetch(URI)
  90. .then(async res => {
  91. res.json = await res.json()
  92. return res
  93. })
  94. .then(res => {
  95. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  96. return res.json
  97. })
  98. return data
  99. }
  100. }
  101. }
  102. </script>