repo_commit_section.vue 4.5 KB
Newer Older
J
Jacob Schatz 已提交
1
<script>
2
import { mapGetters, mapState, mapActions } from 'vuex';
3 4 5
import tooltip from '~/vue_shared/directives/tooltip';
import icon from '~/vue_shared/components/icon.vue';
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;
T
Tim Zallmann 已提交
71 72
          this.commitMessage = '';
          this.startNewMR = false;
J
Jacob Schatz 已提交
73 74
        })
        .catch(() => {
75
          this.submitCommitsLoading = false;
J
Jacob Schatz 已提交
76 77
        });
    },
78
    tryCommit() {
79 80
      this.submitCommitsLoading = true;

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

<template>
F
Filipa Lacerda 已提交
104 105 106 107 108 109
  <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 已提交
110 111
      :text="__(`This branch has changed since
you started editing. Would you like to create a new branch?`)"
F
Filipa Lacerda 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
      @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 已提交
132
        >
F
Filipa Lacerda 已提交
133
        </textarea>
J
Jacob Schatz 已提交
134
      </div>
F
Filipa Lacerda 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      <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"
T
Tim Zallmann 已提交
152
          :class="{ disabled: submitCommitsLoading }"
F
Filipa Lacerda 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        >
          <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>
171
</template>