ExperimentBase.vue 4.2 KB

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