summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/commit_sidebar/actions.vue
blob: 1824a0f6147b71381a6a7cfc6e68992a7e54eccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<script>
import _ from 'underscore';
import { mapActions, mapState, mapGetters, createNamespacedHelpers } from 'vuex';
import { sprintf, __ } from '~/locale';
import consts from '../../stores/modules/commit/constants';
import RadioGroup from './radio_group.vue';

const { mapState: mapCommitState, mapGetters: mapCommitGetters } = createNamespacedHelpers(
  'commit',
);

export default {
  components: {
    RadioGroup,
  },
  computed: {
    ...mapState(['currentBranchId', 'changedFiles', 'stagedFiles']),
    ...mapCommitState(['commitAction', 'shouldCreateMR', 'shouldDisableNewMrOption']),
    ...mapGetters(['currentProject', 'currentBranch', 'currentMergeRequest']),
    ...mapCommitGetters(['shouldDisableNewMrOption']),
    commitToCurrentBranchText() {
      return sprintf(
        __('Commit to %{branchName} branch'),
        { branchName: `<strong class="monospace">${_.escape(this.currentBranchId)}</strong>` },
        false,
      );
    },
    disableMergeRequestRadio() {
      return this.changedFiles.length > 0 && this.stagedFiles.length > 0;
    },
  },
  watch: {
    disableMergeRequestRadio() {
      this.updateSelectedCommitAction();
    },
  },
  mounted() {
    this.updateSelectedCommitAction();
  },
  methods: {
    ...mapActions('commit', ['updateCommitAction', 'toggleShouldCreateMR']),
    updateSelectedCommitAction() {
      if (this.currentBranch && !this.currentBranch.can_push) {
        this.updateCommitAction(consts.COMMIT_TO_NEW_BRANCH);
      } else if (this.disableMergeRequestRadio) {
        this.updateCommitAction(consts.COMMIT_TO_CURRENT_BRANCH);
      }
    },
  },
  commitToCurrentBranch: consts.COMMIT_TO_CURRENT_BRANCH,
  commitToNewBranch: consts.COMMIT_TO_NEW_BRANCH,
  currentBranchPermissionsTooltip: __(
    "This option is disabled as you don't have write permissions for the current branch",
  ),
};
</script>

<template>
  <div class="append-bottom-15 ide-commit-radios">
    <radio-group
      :value="$options.commitToCurrentBranch"
      :disabled="currentBranch && !currentBranch.can_push"
      :title="$options.currentBranchPermissionsTooltip"
    >
      <span class="ide-radio-label" v-html="commitToCurrentBranchText"> </span>
    </radio-group>
    <radio-group
      :value="$options.commitToNewBranch"
      :label="__('Create a new branch')"
      :show-input="true"
    />
    <hr class="my-2" />
    <label class="mb-0">
      <input
        :checked="shouldCreateMR"
        :disabled="shouldDisableNewMrOption"
        type="checkbox"
        @change="toggleShouldCreateMR"
      />
      <span class="prepend-left-10" :class="{ 'text-secondary': shouldDisableNewMrOption }">
        {{ __('Start a new merge request') }}
      </span>
    </label>
  </div>
</template>