index.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. export default {
  10. name: 'ExperimentBase',
  11. props: {
  12. sceneName: {
  13. type: String,
  14. required: true
  15. }
  16. },
  17. data() {
  18. return {
  19. experimentName: null, // Must be redefined in parent component
  20. loadingMessage: null,
  21. loadingErrorMessage: null,
  22. qualities: null
  23. }
  24. },
  25. computed: {
  26. ...mapGetters(['getHostURI', 'getExperimentProgress', 'isExperimentDone'])
  27. },
  28. mounted() {
  29. // Check if the experiment is already finished
  30. if (this.experimentName && this.sceneName && this.isExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName })) {
  31. console.warn('Redirected from experiment. You can\'t go back in an experiment after finishing it.')
  32. this.$router.push(`/experiments/${this.experimentName}`)
  33. }
  34. },
  35. methods: {
  36. ...mapActions(['setExperimentProgress', 'setExperimentDone', 'sendMessage']),
  37. // Load progress from store into local state
  38. loadProgress() {
  39. if (!this.experimentName || !this.sceneName)
  40. return console.warn('Could not load progress : experimentName and sceneName must be defined')
  41. const progress = this.getExperimentProgress({ experimentName: this.experimentName, sceneName: this.sceneName })
  42. Object.assign(this.$data, progress)
  43. // console.log('Loaded data from store to local state.', progress)
  44. },
  45. // Save progress from local state into store
  46. saveProgress() {
  47. if (!this.experimentName || !this.sceneName)
  48. return console.warn('Could not save progress : experimentName and sceneName must be defined')
  49. this.setExperimentProgress({ experimentName: this.experimentName, sceneName: this.sceneName, data: this.$data })
  50. // console.log('Saved data from local state to store.', this.$data)
  51. },
  52. async finishExperiment() {
  53. this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done: true })
  54. this.$router.push(`/experiments/${this.experimentName}`)
  55. },
  56. // Load qualities list from the API
  57. async getQualitiesList() {
  58. if (this.qualities) return
  59. const URI = `${this.getHostURI}${API_ROUTES.listSceneQualities(this.sceneName)}`
  60. const { data } = await fetch(URI).then(res => res.json())
  61. this.qualities = data
  62. this.saveProgress()
  63. }
  64. }
  65. }
  66. </script>