Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_line_note_form.js « mixins « notes « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34090d22cec0ed5a50ad760363ab5fbbe5c801ed (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapGetters, mapState } from 'vuex';
import { getDraftReplyFormData, getDraftFormData } from '~/batch_comments/utils';
import {
  TEXT_DIFF_POSITION_TYPE,
  IMAGE_DIFF_POSITION_TYPE,
  FILE_DIFF_POSITION_TYPE,
} from '~/diffs/constants';
import { createAlert } from '~/alert';
import { clearDraft } from '~/lib/utils/autosave';
import { sprintf } from '~/locale';
import { formatLineRange } from '~/notes/components/multiline_comment_utils';
import { SAVING_THE_COMMENT_FAILED, SOMETHING_WENT_WRONG } from '~/diffs/i18n';

export default {
  computed: {
    ...mapState({
      noteableData: (state) => state.notes.noteableData,
      notesData: (state) => state.notes.notesData,
      withBatchComments: (state) => state.batchComments?.withBatchComments,
    }),
    ...mapGetters('diffs', ['getDiffFileByHash']),
    ...mapGetters('batchComments', ['shouldRenderDraftRowInDiscussion', 'draftForDiscussion']),
    ...mapState('diffs', ['commit', 'showWhitespace']),
  },
  methods: {
    ...mapActions('diffs', ['cancelCommentForm', 'toggleFileCommentForm']),
    ...mapActions('batchComments', ['addDraftToReview', 'saveDraft', 'insertDraftIntoDrafts']),
    addReplyToReview(noteText, isResolving, parentElement, errorCallback) {
      const postData = getDraftReplyFormData({
        in_reply_to_discussion_id: this.discussion.reply_id,
        target_type: this.getNoteableData.targetType,
        notesData: this.notesData,
        draft_note: {
          note: noteText,
          resolve_discussion: isResolving,
        },
      });

      if (this.discussion.for_commit) {
        postData.note_project_id = this.discussion.project_id;
      }

      this.saveDraft(postData)
        .then(() => {
          this.isReplying = false;
          this.handleClearForm(this.discussion.line_code);
        })
        .catch((response) => {
          const reason = response?.data?.errors;
          const errorMessage = reason
            ? sprintf(SAVING_THE_COMMENT_FAILED, { reason })
            : SOMETHING_WENT_WRONG;

          createAlert({
            message: errorMessage,
            parent: parentElement,
          });

          errorCallback();
        });
    },
    addToReview(note, positionType = null, parentElement, errorCallback) {
      const lineRange =
        (this.line && this.commentLineStart && formatLineRange(this.commentLineStart, this.line)) ||
        {};
      const position =
        positionType ||
        (this.diffFileCommentForm ? IMAGE_DIFF_POSITION_TYPE : TEXT_DIFF_POSITION_TYPE);
      const diffFile = this.diffFile || this.file;
      const postData = getDraftFormData({
        note,
        notesData: this.notesData,
        noteableData: this.noteableData,
        noteableType: this.noteableType,
        noteTargetLine: this.noteTargetLine,
        diffViewType: this.diffViewType,
        diffFile,
        linePosition: this.position,
        positionType: position,
        ...this.diffFileCommentForm,
        lineRange,
        showWhitespace: this.showWhitespace,
      });

      const diffFileHeadSha = this.commit && diffFile?.diff_refs?.head_sha;

      postData.data.note.commit_id = diffFileHeadSha || null;

      return this.saveDraft(postData)
        .then(() => {
          if (position === IMAGE_DIFF_POSITION_TYPE) {
            this.closeDiffFileCommentForm(this.diffFileHash);
          } else if (this.line?.line_code) {
            this.handleClearForm(this.line.line_code);
          } else if (position === FILE_DIFF_POSITION_TYPE) {
            this.toggleFileCommentForm(diffFile.file_path);
          }
        })
        .catch((response) => {
          const reason = response?.data?.errors;
          const errorMessage = reason
            ? sprintf(SAVING_THE_COMMENT_FAILED, { reason })
            : SOMETHING_WENT_WRONG;

          createAlert({
            message: errorMessage,
            parent: parentElement,
          });

          errorCallback();
        });
    },
    handleClearForm(lineCode) {
      this.cancelCommentForm({
        lineCode,
        fileHash: this.diffFileHash,
      });
      this.$nextTick(() => {
        if (this.autosaveKey) {
          clearDraft(this.autosaveKey);
        } else {
          // TODO: remove the following after replacing the autosave mixin
          // https://gitlab.com/gitlab-org/gitlab-foss/issues/60587
          this.resetAutoSave();
        }
      });
    },
    showDraft(replyId) {
      return this.withBatchComments && this.shouldRenderDraftRowInDiscussion(replyId);
    },
  },
};