@@ -4,55 +4,13 @@ import type { DataGridRef, DataGridTypes } from 'devextreme-react/data-grid';
44import { createStore } from 'devextreme-aspnet-data-nojquery' ;
55import 'whatwg-fetch' ;
66
7- const BASE_PATH = 'https://js.devexpress.com/Demos/NetCore' ;
8- const URL = `${ BASE_PATH } /api/DataGridBatchUpdateWebApi` ;
9-
10- async function fetchAntiForgeryToken ( ) : Promise < { headerName : string ; token : string } > {
11- try {
12- const response = await fetch ( `${ BASE_PATH } /api/Common/GetAntiForgeryToken` , {
13- method : 'GET' ,
14- credentials : 'include' ,
15- cache : 'no-cache' ,
16- } ) ;
17-
18- if ( ! response . ok ) {
19- const errorMessage = await response . text ( ) ;
20- throw new Error ( `Failed to retrieve anti-forgery token: ${ errorMessage || response . statusText } ` ) ;
21- }
22-
23- return await response . json ( ) ;
24- } catch ( error ) {
25- const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
26- throw new Error ( errorMessage ) ;
27- }
28- }
29-
30- async function getAntiForgeryTokenValue ( ) : Promise < { headerName : string ; token : string } > {
31- const tokenMeta = document . querySelector < HTMLMetaElement > ( 'meta[name="csrf-token"]' ) ;
32- if ( tokenMeta ) {
33- const headerName = tokenMeta . dataset . headerName || 'RequestVerificationToken' ;
34- const token = tokenMeta . getAttribute ( 'content' ) || '' ;
35- return Promise . resolve ( { headerName, token } ) ;
36- }
37-
38- const tokenData = await fetchAntiForgeryToken ( ) ;
39- const meta = document . createElement ( 'meta' ) ;
40- meta . name = 'csrf-token' ;
41- meta . content = tokenData . token ;
42- meta . dataset . headerName = tokenData . headerName ;
43- document . head . appendChild ( meta ) ;
44- return tokenData ;
45- }
7+ const URL = 'https://js.devexpress.com/Demos/NetCore/api/DataGridBatchUpdateWebApi' ;
468
479const ordersStore = createStore ( {
4810 key : 'OrderID' ,
4911 loadUrl : `${ URL } /Orders` ,
50- async onBeforeSend ( _method , ajaxOptions ) {
51- const tokenData = await getAntiForgeryTokenValue ( ) ;
52- ajaxOptions . xhrFields = {
53- withCredentials : true ,
54- headers : { [ tokenData . headerName ] : tokenData . token } ,
55- } ;
12+ onBeforeSend : ( method , ajaxOptions ) => {
13+ ajaxOptions . xhrFields = { withCredentials : true } ;
5614 } ,
5715} ) ;
5816
@@ -81,31 +39,25 @@ function normalizeChanges(changes: DataGridTypes.DataChange[]): DataGridTypes.Da
8139 } ) as DataGridTypes . DataChange [ ] ;
8240}
8341
84- async function sendBatchRequest ( url : string , changes : DataGridTypes . DataChange [ ] , headers : Record < string , string > ) {
85- try {
86- const response = await fetch ( url , {
87- method : 'POST' ,
88- body : JSON . stringify ( changes ) ,
89- headers : {
90- 'Content-Type' : 'application/json;charset=UTF-8' ,
91- ...headers ,
92- } ,
93- credentials : 'include' ,
94- } ) ;
42+ async function sendBatchRequest ( url : string , changes : DataGridTypes . DataChange [ ] ) {
43+ const result = await fetch ( url , {
44+ method : 'POST' ,
45+ body : JSON . stringify ( changes ) ,
46+ headers : {
47+ 'Content-Type' : 'application/json;charset=UTF-8' ,
48+ } ,
49+ credentials : 'include' ,
50+ } ) ;
9551
96- if ( ! response . ok ) {
97- const errorMessage = await response . text ( ) ;
98- throw new Error ( `Batch save failed: ${ errorMessage || response . statusText } ` ) ;
99- }
100- } catch ( error ) {
101- const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
102- throw new Error ( errorMessage ) ;
52+ if ( ! result . ok ) {
53+ const json = await result . json ( ) ;
54+
55+ throw json . Message ;
10356 }
10457}
10558
10659async function processBatchRequest ( url : string , changes : DataGridTypes . DataChange [ ] , component : ReturnType < DataGridRef [ 'instance' ] > ) {
107- const tokenData = await getAntiForgeryTokenValue ( ) ;
108- await sendBatchRequest ( url , changes , { [ tokenData . headerName ] : tokenData . token } ) ;
60+ await sendBatchRequest ( url , changes ) ;
10961 await component . refresh ( true ) ;
11062 component . cancelEditData ( ) ;
11163}
0 commit comments