gitlab-org--gitlab-foss/app/assets/javascripts/repo/components/repo_commit_section.vue

133 lines
3.3 KiB
Vue
Raw Normal View History

2017-07-25 09:18:45 -04:00
<script>
import Flash from '../../flash';
import Store from '../stores/repo_store';
import RepoMixin from '../mixins/repo_mixin';
2017-08-06 14:29:38 -04:00
import Service from '../services/repo_service';
2017-07-25 09:18:45 -04:00
2017-08-15 14:16:42 -04:00
export default {
2017-07-25 09:18:45 -04:00
data: () => Store,
mixins: [RepoMixin],
2017-08-01 09:41:24 -04:00
computed: {
2017-08-15 15:53:41 -04:00
showCommitable() {
return this.isCommitable && this.changedFiles.length;
},
branchPaths() {
2017-08-15 15:53:41 -04:00
return this.changedFiles.map(f => f.path);
2017-07-28 23:08:05 -04:00
},
2017-08-06 11:12:29 -04:00
cantCommitYet() {
2017-08-06 14:29:38 -04:00
return !this.commitMessage || this.submitCommitsLoading;
2017-08-06 11:12:29 -04:00
},
2017-07-28 23:08:05 -04:00
filePluralize() {
2017-08-01 09:41:24 -04:00
return this.changedFiles.length > 1 ? 'files' : 'file';
2017-07-28 23:08:05 -04:00
},
},
2017-07-25 09:18:45 -04:00
methods: {
makeCommit() {
// see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
const commitMessage = this.commitMessage;
2017-08-01 11:38:00 -04:00
const actions = this.changedFiles.map(f => ({
action: 'update',
2017-08-15 15:53:41 -04:00
file_path: f.path,
2017-08-01 11:38:00 -04:00
content: f.newContent,
}));
const payload = {
branch: Store.currentBranch,
commit_message: commitMessage,
2017-07-26 08:56:31 -04:00
actions,
};
2017-07-25 22:06:12 -04:00
Store.submitCommitsLoading = true;
2017-08-09 09:24:48 -04:00
Service.commitFiles(payload)
.then(this.resetCommitState)
2017-09-25 16:02:08 -04:00
.catch(() => Flash('An error occurred while committing your changes'));
},
resetCommitState() {
this.submitCommitsLoading = false;
this.changedFiles = [];
this.commitMessage = '';
this.editMode = false;
2017-08-15 15:53:41 -04:00
window.scrollTo(0, 0);
2017-07-26 08:56:31 -04:00
},
2017-07-25 09:18:45 -04:00
},
2017-07-26 08:56:31 -04:00
};
2017-07-25 09:18:45 -04:00
</script>
<template>
2017-08-15 15:53:41 -04:00
<div
v-if="showCommitable"
id="commit-area">
<form
class="form-horizontal"
@submit.prevent="makeCommit">
2017-07-25 09:18:45 -04:00
<fieldset>
<div class="form-group">
2017-08-15 15:53:41 -04:00
<label class="col-md-4 control-label staged-files">
Staged files ({{changedFiles.length}})
</label>
<div class="col-md-6">
2017-07-27 10:49:25 -04:00
<ul class="list-unstyled changed-files">
2017-08-15 15:53:41 -04:00
<li
v-for="branchPath in branchPaths"
:key="branchPath">
<span class="help-block">
{{branchPath}}
</span>
2017-07-25 09:18:45 -04:00
</li>
</ul>
</div>
</div>
<div class="form-group">
2017-08-15 15:53:41 -04:00
<label
class="col-md-4 control-label"
for="commit-message">
Commit message
</label>
<div class="col-md-6">
<textarea
id="commit-message"
class="form-control"
name="commit-message"
v-model="commitMessage">
</textarea>
2017-07-25 09:18:45 -04:00
</div>
</div>
<div class="form-group target-branch">
2017-08-15 15:53:41 -04:00
<label
class="col-md-4 control-label"
for="target-branch">
Target branch
</label>
<div class="col-md-6">
<span class="help-block">
{{currentBranch}}
2017-08-15 15:53:41 -04:00
</span>
2017-07-25 09:18:45 -04:00
</div>
</div>
2017-08-15 15:53:41 -04:00
<div class="col-md-offset-4 col-md-6">
<button
ref="submitCommit"
type="submit"
:disabled="cantCommitYet"
class="btn btn-success">
<i
v-if="submitCommitsLoading"
class="fa fa-spinner fa-spin"
aria-hidden="true"
aria-label="loading">
</i>
<span class="commit-summary">
Commit {{changedFiles.length}} {{filePluralize}}
</span>
2017-07-25 22:06:12 -04:00
</button>
2017-07-25 09:18:45 -04:00
</div>
</fieldset>
</form>
</div>
</template>