summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/new_branch_form.vue
blob: 56e31256132b09534e5b0ef9aa0ad8a0861bfae6 (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
<script>
  import { mapState, mapActions } from 'vuex';
  import flash, { hideFlash } from '../../flash';
  import loadingIcon from '../../vue_shared/components/loading_icon.vue';

  export default {
    components: {
      loadingIcon,
    },
    data() {
      return {
        branchName: '',
        loading: false,
      };
    },
    computed: {
      ...mapState([
        'currentBranch',
      ]),
      btnDisabled() {
        return this.loading || this.branchName === '';
      },
    },
    created() {
      // Dropdown is outside of Vue instance & is controlled by Bootstrap
      this.$dropdown = $('.git-revision-dropdown');

      // text element is outside Vue app
      this.dropdownText = document.querySelector('.project-refs-form .dropdown-toggle-text');
    },
    methods: {
      ...mapActions([
        'createNewBranch',
      ]),
      toggleDropdown() {
        this.$dropdown.dropdown('toggle');
      },
      submitNewBranch() {
        // need to query as the element is appended outside of Vue
        const flashEl = this.$refs.flashContainer.querySelector('.flash-alert');

        this.loading = true;

        if (flashEl) {
          hideFlash(flashEl, false);
        }

        this.createNewBranch(this.branchName)
          .then(() => {
            this.loading = false;
            this.branchName = '';

            if (this.dropdownText) {
              this.dropdownText.textContent = this.currentBranchId;
            }

            this.toggleDropdown();
          })
          .catch(res => res.json().then((data) => {
            this.loading = false;
            flash(data.message, 'alert', this.$el);
          }));
      },
    },
  };
</script>

<template>
  <div>
    <div
      class="flash-container"
      ref="flashContainer"
    >
    </div>
    <p>
      Create from:
      <code>{{ currentBranch }}</code>
    </p>
    <input
      class="form-control js-new-branch-name"
      type="text"
      placeholder="Name new branch"
      v-model="branchName"
      @keyup.enter.stop.prevent="submitNewBranch"
    />
    <div class="prepend-top-default clearfix">
      <button
        type="button"
        class="btn btn-primary pull-left"
        :disabled="btnDisabled"
        @click.stop.prevent="submitNewBranch"
      >
        <loading-icon
          v-if="loading"
          :inline="true"
        />
        <span>Create</span>
      </button>
      <button
        type="button"
        class="btn btn-default pull-right"
        @click.stop.prevent="toggleDropdown"
      >
        Cancel
      </button>
    </div>
  </div>
</template>