11import $ from 'jquery' ;
22import { svg } from '../svg.js' ;
33import { showErrorToast } from '../modules/toast.js' ;
4+ import { GET , POST } from '../modules/fetch.js' ;
45
5- const { appSubUrl, csrfToken } = window . config ;
6+ const { appSubUrl} = window . config ;
67let i18nTextEdited ;
78let i18nTextOptions ;
89let i18nTextDeleteFromHistory ;
@@ -31,19 +32,27 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
3132 $dialog . find ( '.dialog-header-options' ) . dropdown ( {
3233 showOnFocus : false ,
3334 allowReselection : true ,
34- onChange ( _value , _text , $item ) {
35+ async onChange ( _value , _text , $item ) {
3536 const optionItem = $item . data ( 'option-item' ) ;
3637 if ( optionItem === 'delete' ) {
3738 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+
4147 if ( resp . ok ) {
4248 $dialog . modal ( 'hide' ) ;
4349 } else {
4450 showErrorToast ( resp . message ) ;
4551 }
46- } ) ;
52+ } catch ( error ) {
53+ console . error ( 'Error:' , error ) ;
54+ showErrorToast ( 'An error occurred while deleting the history.' ) ;
55+ }
4756 }
4857 } else { // required by eslint
4958 showErrorToast ( `unknown option item: ${ optionItem } ` ) ;
@@ -54,19 +63,24 @@ function showContentHistoryDetail(issueBaseUrl, commentId, historyId, itemTitleH
5463 }
5564 } ) ;
5665 $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+
6476 $dialog . find ( '.comment-diff-data' ) . removeClass ( 'is-loading' ) . html ( resp . diffHtml ) ;
6577 // there is only one option "item[data-option-item=delete]", so the dropdown can be entirely shown/hidden.
6678 if ( resp . canSoftDelete ) {
6779 $dialog . find ( '.dialog-header-options' ) . removeClass ( 'gt-hidden' ) ;
6880 }
69- } ) ;
81+ } catch ( error ) {
82+ console . error ( 'Error:' , error ) ;
83+ }
7084 } ,
7185 onHidden ( ) {
7286 $dialog . remove ( ) ;
@@ -103,7 +117,7 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) {
103117 } ) ;
104118}
105119
106- export function initRepoIssueContentHistory ( ) {
120+ export async function initRepoIssueContentHistory ( ) {
107121 const issueIndex = $ ( '#issueIndex' ) . val ( ) ;
108122 if ( ! issueIndex ) return ;
109123
@@ -114,12 +128,10 @@ export function initRepoIssueContentHistory() {
114128 const repoLink = $ ( '#repolink' ) . val ( ) ;
115129 const issueBaseUrl = `${ appSubUrl } /${ repoLink } /issues/${ issueIndex } ` ;
116130
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+
123135 i18nTextEdited = resp . i18n . textEdited ;
124136 i18nTextDeleteFromHistory = resp . i18n . textDeleteFromHistory ;
125137 i18nTextDeleteFromHistoryConfirm = resp . i18n . textDeleteFromHistoryConfirm ;
@@ -133,5 +145,7 @@ export function initRepoIssueContentHistory() {
133145 const $itemComment = $ ( `#issuecomment-${ commentId } ` ) ;
134146 showContentHistoryMenu ( issueBaseUrl , $itemComment , commentId ) ;
135147 }
136- } ) ;
148+ } catch ( error ) {
149+ console . error ( 'Error:' , error ) ;
150+ }
137151}
0 commit comments