summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs/components/diff_line_note_form.vue
blob: 86f5e98194d7cb3ce8d65280c3fc17cd55eda66e (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
<script>
import { mapState, mapGetters, mapActions } from 'vuex';
import createFlash from '~/flash';
import { s__ } from '~/locale';
import noteForm from '../../notes/components/note_form.vue';
import { getNoteFormData } from '../store/utils';

export default {
  components: {
    noteForm,
  },
  props: {
    diffFile: {
      type: Object,
      required: true,
    },
    diffLines: {
      type: Array,
      required: true,
    },
    line: {
      type: Object,
      required: true,
    },
    position: {
      type: String,
      required: false,
      default: '',
    },
    noteTargetLine: {
      type: Object,
      required: true,
    },
  },
  computed: {
    ...mapState({
      noteableData: state => state.notes.noteableData,
      diffViewType: state => state.diffs.diffViewType,
    }),
    ...mapGetters(['noteableType', 'getNotesDataByProp']),
  },
  methods: {
    ...mapActions(['cancelCommentForm', 'saveNote', 'fetchDiscussions']),
    handleCancelCommentForm() {
      this.cancelCommentForm({
        lineCode: this.line.lineCode,
      });
    },
    handleSaveNote(note) {
      const postData = getNoteFormData({
        note,
        noteableData: this.noteableData,
        noteableType: this.noteableType,
        noteTargetLine: this.noteTargetLine,
        diffViewType: this.diffViewType,
        diffFile: this.diffFile,
        linePosition: this.position,
      });

      this.saveNote(postData)
        .then(() => {
          const endpoint = this.getNotesDataByProp('discussionsPath');

          this.fetchDiscussions(endpoint)
            .then(() => {
              this.handleCancelCommentForm();
            })
            .catch(() => {
              createFlash(s__('MergeRequests|Updating discussions failed'));
            });
        })
        .catch(() => {
          createFlash(s__('MergeRequests|Saving the comment failed'));
        });
    },
  },
};
</script>

<template>
  <div
    class="content discussion-form discussion-form-container discussion-notes"
  >
    <note-form
      :is-editing="true"
      :line-code="line.lineCode"
      save-button-title="Comment"
      class="diff-comment-form"
      @cancelForm="handleCancelCommentForm"
      @handleFormUpdate="handleSaveNote"
    />
  </div>
</template>