repo_commit_section.vue 4.6 KB
Newer Older
J
Jacob Schatz 已提交
1
<script>
2
import { mapGetters, mapState, mapActions } from 'vuex';
P
Phil Hughes 已提交
3 4
import tooltip from '../../vue_shared/directives/tooltip';
import icon from '../../vue_shared/components/icon.vue';
W
Winnie Hellmann 已提交
5
import modal from '../../vue_shared/components/modal.vue';
P
Phil Hughes 已提交
6
import commitFilesList from './commit_sidebar/list.vue';
J
Jacob Schatz 已提交
7

B
Bryce Johnson 已提交
8
export default {
J
Jacob Schatz 已提交
9
  components: {
W
Winnie Hellmann 已提交
10
    modal,
P
Phil Hughes 已提交
11 12 13 14 15
    icon,
    commitFilesList,
  },
  directives: {
    tooltip,
J
Jacob Schatz 已提交
16
  },
17 18
  data() {
    return {
W
Winnie Hellmann 已提交
19
      showNewBranchModal: false,
20 21 22 23 24
      submitCommitsLoading: false,
      startNewMR: false,
      commitMessage: '',
    };
  },
25
  computed: {
26
    ...mapState([
27 28 29
      'currentProjectId',
      'currentBranchId',
      'rightPanelCollapsed',
30 31 32 33 34
    ]),
    ...mapGetters([
      'changedFiles',
    ]),
    commitButtonDisabled() {
P
Phil Hughes 已提交
35
      return this.commitMessage === '' || this.submitCommitsLoading || !this.changedFiles.length;
J
Jacob Schatz 已提交
36
    },
P
Phil Hughes 已提交
37 38
    commitMessageCount() {
      return this.commitMessage.length;
J
Jacob Schatz 已提交
39
    },
40
  },
J
Jacob Schatz 已提交
41
  methods: {
42 43 44
    ...mapActions([
      'checkCommitStatus',
      'commitChanges',
P
Phil Hughes 已提交
45
      'getTreeData',
46
      'setPanelCollapsedStatus',
47 48 49
    ]),
    makeCommit(newBranch = false) {
      const createNewBranch = newBranch || this.startNewMR;
J
Jacob Schatz 已提交
50

51
      const payload = {
52 53 54
        branch: createNewBranch ?
          `${this.currentBranchId}-${new Date().getTime().toString()}` :
          this.currentBranchId,
55 56 57 58 59
        commit_message: this.commitMessage,
        actions: this.changedFiles.map(f => ({
          action: f.tempFile ? 'create' : 'update',
          file_path: f.path,
          content: f.content,
60
          encoding: f.base64 ? 'base64' : 'text',
61
        })),
62
        start_branch: createNewBranch ? this.currentBranchId : undefined,
L
Luke "Jared" Bennett 已提交
63
      };
64

W
Winnie Hellmann 已提交
65
      this.showNewBranchModal = false;
66 67 68
      this.submitCommitsLoading = true;

      this.commitChanges({ payload, newMr: this.startNewMR })
J
Jacob Schatz 已提交
69
        .then(() => {
70
          this.submitCommitsLoading = false;
71 72 73 74 75 76
          this.$store.dispatch('getTreeData', {
            projectId: this.currentProjectId,
            branch: this.currentBranchId,
            endpoint: `/tree/${this.currentBranchId}`,
            force: true,
          });
J
Jacob Schatz 已提交
77 78
        })
        .catch(() => {
79
          this.submitCommitsLoading = false;
J
Jacob Schatz 已提交
80 81
        });
    },
82
    tryCommit() {
83 84
      this.submitCommitsLoading = true;

85 86 87
      this.checkCommitStatus()
        .then((branchChanged) => {
          if (branchChanged) {
W
Winnie Hellmann 已提交
88
            this.showNewBranchModal = true;
89 90 91 92 93 94 95
          } else {
            this.makeCommit();
          }
        })
        .catch(() => {
          this.submitCommitsLoading = false;
        });
L
Luke "Jared" Bennett 已提交
96
    },
P
Phil Hughes 已提交
97
    toggleCollapsed() {
98 99 100 101
      this.setPanelCollapsedStatus({
        side: 'right',
        collapsed: !this.rightPanelCollapsed,
      });
P
Phil Hughes 已提交
102
    },
J
Jacob Schatz 已提交
103
  },
L
Luke "Jared" Bennett 已提交
104
};
J
Jacob Schatz 已提交
105 106 107
</script>

<template>
F
Filipa Lacerda 已提交
108 109 110 111 112 113
  <div class="multi-file-commit-panel-section">
    <modal
      v-if="showNewBranchModal"
      :primary-button-label="__('Create new branch')"
      kind="primary"
      :title="__('Branch has changed')"
F
Filipa Lacerda 已提交
114 115
      :text="__(`This branch has changed since
you started editing. Would you like to create a new branch?`)"
F
Filipa Lacerda 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      @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"
P
Phil Hughes 已提交
136
        >
F
Filipa Lacerda 已提交
137
        </textarea>
J
Jacob Schatz 已提交
138
      </div>
F
Filipa Lacerda 已提交
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 172 173
      <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"
        >
          <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>
174
</template>