summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/repo_commit_section.vue
blob: 96b1bb78c1dea506e59576a343a7b40a39c4f269 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<script>
import { mapGetters, mapState, mapActions } from 'vuex';
import tooltip from '../../vue_shared/directives/tooltip';
import icon from '../../vue_shared/components/icon.vue';
import modal from '../../vue_shared/components/modal.vue';
import commitFilesList from './commit_sidebar/list.vue';

export default {
  components: {
    modal,
    icon,
    commitFilesList,
  },
  directives: {
    tooltip,
  },
  data() {
    return {
      showNewBranchModal: false,
      submitCommitsLoading: false,
      startNewMR: false,
      commitMessage: '',
    };
  },
  computed: {
    ...mapState([
      'currentProjectId',
      'currentBranchId',
      'rightPanelCollapsed',
    ]),
    ...mapGetters([
      'changedFiles',
    ]),
    commitButtonDisabled() {
      return this.commitMessage === '' || this.submitCommitsLoading || !this.changedFiles.length;
    },
    commitMessageCount() {
      return this.commitMessage.length;
    },
  },
  methods: {
    ...mapActions([
      'checkCommitStatus',
      'commitChanges',
      'getTreeData',
      'setPanelCollapsedStatus',
    ]),
    makeCommit(newBranch = false) {
      const createNewBranch = newBranch || this.startNewMR;

      const payload = {
        branch: createNewBranch ?
          `${this.currentBranchId}-${new Date().getTime().toString()}` :
          this.currentBranchId,
        commit_message: this.commitMessage,
        actions: this.changedFiles.map(f => ({
          action: f.tempFile ? 'create' : 'update',
          file_path: f.path,
          content: f.content,
          encoding: f.base64 ? 'base64' : 'text',
        })),
        start_branch: createNewBranch ? this.currentBranchId : undefined,
      };

      this.showNewBranchModal = false;
      this.submitCommitsLoading = true;

      this.commitChanges({ payload, newMr: this.startNewMR })
        .then(() => {
          this.submitCommitsLoading = false;
          this.commitMessage = '';
          this.startNewMR = false;
        })
        .catch(() => {
          this.submitCommitsLoading = false;
        });
    },
    tryCommit() {
      this.submitCommitsLoading = true;

      this.checkCommitStatus()
        .then((branchChanged) => {
          if (branchChanged) {
            this.showNewBranchModal = true;
          } else {
            this.makeCommit();
          }
        })
        .catch(() => {
          this.submitCommitsLoading = false;
        });
    },
    toggleCollapsed() {
      this.setPanelCollapsedStatus({
        side: 'right',
        collapsed: !this.rightPanelCollapsed,
      });
    },
  },
};
</script>

<template>
  <div class="multi-file-commit-panel-section">
    <modal
      v-if="showNewBranchModal"
      :primary-button-label="__('Create new branch')"
      kind="primary"
      :title="__('Branch has changed')"
      :text="__(`This branch has changed since
you started editing. Would you like to create a new branch?`)"
      @cancel="showNewBranchModal = false"
      @submit="makeCommit(true)"
    />
    <commit-files-list
      title="Staged"
      :file-list="changedFiles"
      :collapsed="rightPanelCollapsed"
      @toggleCollapsed="toggleCollapsed"
    />
    <form
      class="form-horizontal multi-file-commit-form"
      @submit.prevent="tryCommit"
      v-if="!rightPanelCollapsed"
    >
      <div class="multi-file-commit-fieldset">
        <textarea
          class="form-control multi-file-commit-message"
          name="commit-message"
          v-model="commitMessage"
          placeholder="Commit message"
        >
        </textarea>
      </div>
      <div class="multi-file-commit-fieldset">
        <label
          v-tooltip
          title="Create a new merge request with these changes"
          data-container="body"
          data-placement="top"
        >
          <input
            type="checkbox"
            v-model="startNewMR"
          />
          Merge Request
        </label>
        <button
          type="submit"
          :disabled="commitButtonDisabled"
          class="btn btn-default btn-sm append-right-10 prepend-left-10"
          :class="{ disabled: submitCommitsLoading }"
        >
          <i
            v-if="submitCommitsLoading"
            class="js-commit-loading-icon fa fa-spinner fa-spin"
            aria-hidden="true"
            aria-label="loading"
          >
          </i>
          Commit
        </button>
        <div
          class="multi-file-commit-message-count"
        >
          {{ commitMessageCount }}
        </div>
      </div>
    </form>
  </div>
</template>