1
1
import $ from 'jquery' ;
2
2
import { svg } from '../svg.js' ;
3
3
import { showErrorToast } from '../modules/toast.js' ;
4
+ import { GET , POST } from '../modules/fetch.js' ;
4
5
5
- const { appSubUrl, csrfToken } = window . config ;
6
+ const { appSubUrl} = window . config ;
6
7
let i18nTextEdited ;
7
8
let i18nTextOptions ;
8
9
let i18nTextDeleteFromHistory ;
@@ -31,19 +32,27 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
31
32
$dialog . find ( '.dialog-header-options' ) . dropdown ( {
32
33
showOnFocus : false ,
33
34
allowReselection : true ,
34
- onChange ( _value , _text , $item ) {
35
+ async onChange ( _value , _text , $item ) {
35
36
const optionItem = $item . data ( 'option-item' ) ;
36
37
if ( optionItem === 'delete' ) {
37
38
if ( window . confirm ( i18nTextDeleteFromHistoryConfirm ) ) {
38
- $ . post ( `${ issueBaseUrl } /content-history/soft-delete?comment_id=${ commentId } &history_id=${ historyId } ` , {
39
- _csrf : csrfToken ,
40
- } ) . done ( ( resp ) => {
39
+ try {
40
+ const params = new URLSearchParams ( ) ;
41
+ params . append ( 'comment_id' , commentId ) ;
42
+ params . append ( 'history_id' , historyId ) ;
43
+
44
+ const response = await POST ( `${ issueBaseUrl } /content-history/soft-delete?${ params . toString ( ) } ` ) ;
45
+ const resp = await response . json ( ) ;
46
+
41
47
if ( resp . ok ) {
42
48
$dialog . modal ( 'hide' ) ;
43
49
} else {
44
50
showErrorToast ( resp . message ) ;
45
51
}
46
- } ) ;
52
+ } catch ( error ) {
53
+ console . error ( 'Error:' , error ) ;
54
+ showErrorToast ( 'An error occurred while deleting the history.' ) ;
55
+ }
47
56
}
48
57
} else { // required by eslint
49
58
showErrorToast ( `unknown option item: ${ optionItem } ` ) ;
@@ -54,19 +63,24 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
54
63
}
55
64
} ) ;
56
65
$dialog . modal ( {
57
- onShow ( ) {
58
- $ . ajax ( {
59
- url : `${ issueBaseUrl } /content-history/detail?comment_id=${ commentId } &history_id=${ historyId } ` ,
60
- data : {
61
- _csrf : csrfToken ,
62
- } ,
63
- } ) . done ( ( resp ) => {
66
+ async onShow ( ) {
67
+ try {
68
+ const params = new URLSearchParams ( ) ;
69
+ params . append ( 'comment_id' , commentId ) ;
70
+ params . append ( 'history_id' , historyId ) ;
71
+
72
+ const url = `${ issueBaseUrl } /content-history/detail?${ params . toString ( ) } ` ;
73
+ const response = await GET ( url ) ;
74
+ const resp = await response . json ( ) ;
75
+
64
76
$dialog . find ( '.comment-diff-data' ) . removeClass ( 'is-loading' ) . html ( resp . diffHtml ) ;
65
77
// there is only one option "item[data-option-item=delete]", so the dropdown can be entirely shown/hidden.
66
78
if ( resp . canSoftDelete ) {
67
79
$dialog . find ( '.dialog-header-options' ) . removeClass ( 'gt-hidden' ) ;
68
80
}
69
- } ) ;
81
+ } catch ( error ) {
82
+ console . error ( 'Error:' , error ) ;
83
+ }
70
84
} ,
71
85
onHidden ( ) {
72
86
$dialog . remove ( ) ;
@@ -103,7 +117,7 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) {
103
117
} ) ;
104
118
}
105
119
106
- export function initRepoIssueContentHistory ( ) {
120
+ export async function initRepoIssueContentHistory ( ) {
107
121
const issueIndex = $ ( '#issueIndex' ) . val ( ) ;
108
122
if ( ! issueIndex ) return ;
109
123
@@ -114,12 +128,10 @@ export function initRepoIssueContentHistory() {
114
128
const repoLink = $ ( '#repolink' ) . val ( ) ;
115
129
const issueBaseUrl = `${ appSubUrl } /${ repoLink } /issues/${ issueIndex } ` ;
116
130
117
- $ . ajax ( {
118
- url : `${ issueBaseUrl } /content-history/overview` ,
119
- data : {
120
- _csrf : csrfToken ,
121
- } ,
122
- } ) . done ( ( resp ) => {
131
+ try {
132
+ const response = await GET ( `${ issueBaseUrl } /content-history/overview` ) ;
133
+ const resp = await response . json ( ) ;
134
+
123
135
i18nTextEdited = resp . i18n . textEdited ;
124
136
i18nTextDeleteFromHistory = resp . i18n . textDeleteFromHistory ;
125
137
i18nTextDeleteFromHistoryConfirm = resp . i18n . textDeleteFromHistoryConfirm ;
@@ -133,5 +145,7 @@ export function initRepoIssueContentHistory() {
133
145
const $itemComment = $ ( `#issuecomment-${ commentId } ` ) ;
134
146
showContentHistoryMenu ( issueBaseUrl , $itemComment , commentId ) ;
135
147
}
136
- } ) ;
148
+ } catch ( error ) {
149
+ console . error ( 'Error:' , error ) ;
150
+ }
137
151
}
0 commit comments