ExperimentBase.vue 4.0 KB

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