diff --git a/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js b/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js index 6c1840361af29f8fae47457dd4998aea2f1462ac..97769e0d067040b378f78e0a05a8159a61658bba 100644 --- a/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js +++ b/app/assets/javascripts/vue_shared/components/diff_viewer/constants.js @@ -10,3 +10,14 @@ export const imageViewMode = { swipe: 'swipe', onion: 'onion', }; + +// State machine states +export const RENAMED_STATE_IDLING = 'idle'; +export const RENAMED_STATE_LOADING = 'loading'; +export const RENAMED_STATE_ERRORED = 'errored'; + +// State machine transitions +export const RENAMED_TRANSITION_LOAD_START = 'LOAD_START'; +export const RENAMED_TRANSITION_LOAD_ERROR = 'LOAD_ERROR'; +export const RENAMED_TRANSITION_LOAD_SUCCEED = 'LOAD_SUCCEED'; +export const RENAMED_TRANSITION_ACKNOWLEDGE_ERROR = 'ACKNOWLEDGE_ERROR'; diff --git a/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue b/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue index b786f7752dfa7bc19daee7efd49e7cd034238ab1..f9d86f4740530b05f22acd68d9968718f2bd7f34 100644 --- a/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue +++ b/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/renamed.vue @@ -2,23 +2,23 @@ import { GlAlert, GlLink, GlLoadingIcon, GlSprintf } from '@gitlab/ui'; import { mapActions } from 'vuex'; +import { truncateSha } from '~/lib/utils/text_utility'; +import { __ } from '~/locale'; + import { TRANSITION_LOAD_START, TRANSITION_LOAD_ERROR, TRANSITION_LOAD_SUCCEED, TRANSITION_ACKNOWLEDGE_ERROR, - STATE_IDLING, STATE_LOADING, STATE_ERRORED, - RENAMED_DIFF_TRANSITIONS, -} from '~/diffs/constants'; -import { truncateSha } from '~/lib/utils/text_utility'; -import { __ } from '~/locale'; + getStateMachine, +} from './renamed/state_machine'; export default { STATE_LOADING, STATE_ERRORED, - TRANSITIONS: RENAMED_DIFF_TRANSITIONS, + stateMachine: getStateMachine(), uiText: { showLink: __('Show file contents'), commitLink: __('View file @ %{commitSha}'), @@ -37,11 +37,6 @@ export default { required: true, }, }, - data() { - return { - state: STATE_IDLING, - }; - }, computed: { shortSha() { return truncateSha(this.diffFile.content_sha); @@ -52,25 +47,15 @@ export default { }, methods: { ...mapActions('diffs', ['switchToFullDiffFromRenamedFile']), - transition(transitionEvent) { - const key = `${this.state}:${transitionEvent}`; - - if (this.$options.TRANSITIONS[key]) { - this.state = this.$options.TRANSITIONS[key]; - } - }, - is(state) { - return this.state === state; - }, switchToFull() { - this.transition(TRANSITION_LOAD_START); + this.$options.stateMachine.send(TRANSITION_LOAD_START); this.switchToFullDiffFromRenamedFile({ diffFile: this.diffFile }) .then(() => { - this.transition(TRANSITION_LOAD_SUCCEED); + this.$options.stateMachine.send(TRANSITION_LOAD_SUCCEED); }) .catch(() => { - this.transition(TRANSITION_LOAD_ERROR); + this.$options.stateMachine.send(TRANSITION_LOAD_ERROR); }); }, clickLink(event) { @@ -81,7 +66,7 @@ export default { } }, dismissError() { - this.transition(TRANSITION_ACKNOWLEDGE_ERROR); + this.$options.stateMachine.send(TRANSITION_ACKNOWLEDGE_ERROR); }, }, }; @@ -89,10 +74,10 @@ export default {