summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/markdown/suggestion_diff.vue
blob: bcd8c02e9688ec0022eb70e9d803a0389ceeb96b (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
<script>
import { selectDiffLines } from '../lib/utils/diff_utils';
import SuggestionDiffHeader from './suggestion_diff_header.vue';
import SuggestionDiffRow from './suggestion_diff_row.vue';

export default {
  components: {
    SuggestionDiffHeader,
    SuggestionDiffRow,
  },
  props: {
    suggestion: {
      type: Object,
      required: true,
    },
    batchSuggestionsInfo: {
      type: Array,
      required: false,
      default: () => [],
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    helpPagePath: {
      type: String,
      required: true,
    },
    defaultCommitMessage: {
      type: String,
      required: true,
    },
    suggestionsCount: {
      type: Number,
      required: false,
      default: 0,
    },
  },
  computed: {
    batchSuggestionsCount() {
      return this.batchSuggestionsInfo.length;
    },
    isBatched() {
      return Boolean(
        this.batchSuggestionsInfo.find(({ suggestionId }) => suggestionId === this.suggestion.id),
      );
    },
    lines() {
      return selectDiffLines(this.suggestion.diff_lines);
    },
  },
  methods: {
    applySuggestion(callback, message) {
      this.$emit('apply', { suggestionId: this.suggestion.id, callback, message });
    },
    applySuggestionBatch() {
      this.$emit('applyBatch');
    },
    addSuggestionToBatch() {
      this.$emit('addToBatch', this.suggestion.id);
    },
    removeSuggestionFromBatch() {
      this.$emit('removeFromBatch', this.suggestion.id);
    },
  },
};
</script>

<template>
  <div class="md-suggestion">
    <suggestion-diff-header
      class="qa-suggestion-diff-header js-suggestion-diff-header"
      :suggestions-count="suggestionsCount"
      :can-apply="suggestion.appliable && suggestion.current_user.can_apply && !disabled"
      :is-applied="suggestion.applied"
      :is-batched="isBatched"
      :is-applying-batch="suggestion.is_applying_batch"
      :batch-suggestions-count="batchSuggestionsCount"
      :help-page-path="helpPagePath"
      :default-commit-message="defaultCommitMessage"
      :inapplicable-reason="suggestion.inapplicable_reason"
      @apply="applySuggestion"
      @applyBatch="applySuggestionBatch"
      @addToBatch="addSuggestionToBatch"
      @removeFromBatch="removeSuggestionFromBatch"
    />
    <table class="mb-3 md-suggestion-diff js-syntax-highlight code">
      <tbody>
        <suggestion-diff-row
          v-for="(line, index) of lines"
          :key="`${index}-${line.text}`"
          :line="line"
        />
      </tbody>
    </table>
  </div>
</template>