summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/commit_sidebar/actions.vue
blob: c7f79b3ace41021a08d98473afdef7b1618bdb52 (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
86
87
88
89
90
91
92
93
94
95
96
97
<script>
import { escape } from 'lodash';
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
import { GlSprintf } from '@gitlab/ui';
import { s__ } from '~/locale';
import {
  COMMIT_TO_CURRENT_BRANCH,
  COMMIT_TO_NEW_BRANCH,
} from '../../stores/modules/commit/constants';
import RadioGroup from './radio_group.vue';
import NewMergeRequestOption from './new_merge_request_option.vue';

const { mapState: mapCommitState, mapActions: mapCommitActions } = createNamespacedHelpers(
  'commit',
);

export default {
  components: {
    GlSprintf,
    RadioGroup,
    NewMergeRequestOption,
  },
  computed: {
    ...mapState(['currentBranchId', 'changedFiles', 'stagedFiles']),
    ...mapCommitState(['commitAction']),
    ...mapGetters(['currentBranch', 'emptyRepo', 'canPushToBranch']),
    currentBranchText() {
      return escape(this.currentBranchId);
    },
    containsStagedChanges() {
      return this.changedFiles.length > 0 && this.stagedFiles.length > 0;
    },
    shouldDefaultToCurrentBranch() {
      if (this.emptyRepo) {
        return true;
      }

      return this.canPushToBranch && !this.currentBranch?.default;
    },
  },
  watch: {
    containsStagedChanges() {
      this.updateSelectedCommitAction();
    },
  },
  mounted() {
    if (!this.commitAction) {
      this.updateSelectedCommitAction();
    }
  },
  methods: {
    ...mapCommitActions(['updateCommitAction']),
    updateSelectedCommitAction() {
      if (!this.currentBranch && !this.emptyRepo) {
        return;
      }

      if (this.shouldDefaultToCurrentBranch) {
        this.updateCommitAction(COMMIT_TO_CURRENT_BRANCH);
      } else {
        this.updateCommitAction(COMMIT_TO_NEW_BRANCH);
      }
    },
  },
  commitToCurrentBranch: COMMIT_TO_CURRENT_BRANCH,
  commitToNewBranch: COMMIT_TO_NEW_BRANCH,
  currentBranchPermissionsTooltip: s__(
    "IDE|This option is disabled because you don't have write permissions for the current branch.",
  ),
};
</script>

<template>
  <div class="gl-mb-5 ide-commit-options">
    <radio-group
      :value="$options.commitToCurrentBranch"
      :disabled="!canPushToBranch"
      :title="$options.currentBranchPermissionsTooltip"
    >
      <span class="ide-option-label" data-qa-selector="commit_to_current_branch_radio">
        <gl-sprintf :message="s__('IDE|Commit to %{branchName} branch')">
          <template #branchName>
            <strong class="monospace">{{ currentBranchText }}</strong>
          </template>
        </gl-sprintf>
      </span>
    </radio-group>
    <template v-if="!emptyRepo">
      <radio-group
        :value="$options.commitToNewBranch"
        :label="__('Create a new branch')"
        :show-input="true"
      />
      <new-merge-request-option />
    </template>
  </div>
</template>