|
1 | | -import { inject, injectable } from "tsyringe"; |
2 | | -import Ajax from "../../../lib/ajax/ajax"; |
3 | | -import AjaxInterface from "../../../lib/ajax/ajax-interface"; |
| 1 | +import { injectable } from "tsyringe"; |
4 | 2 | import { AxiosError, AxiosResponse } from "axios"; |
| 3 | +import axios from "axios"; |
| 4 | +import { flushSync } from "react-dom"; |
5 | 5 | import UnitRecruitment from "../capital-city/partials/unit-management/unit-recruitment"; |
| 6 | +import { runCapitalCityRequestBatches } from "./run-capital-city-request-batches"; |
6 | 7 |
|
7 | 8 | @injectable() |
8 | 9 | export default class ProcessUnitRequestAjax { |
9 | | - constructor(@inject(Ajax) private ajax: AjaxInterface) {} |
10 | | - |
11 | 10 | public processRequest( |
12 | 11 | component: UnitRecruitment, |
13 | 12 | characterId: number, |
14 | 13 | kingdomId: number, |
15 | | - params: any, |
| 14 | + params: any[], |
16 | 15 | ): void { |
17 | | - this.ajax |
18 | | - .setRoute( |
19 | | - "kingdom/capital-city/recruit-unit-requests/" + |
20 | | - characterId + |
21 | | - "/" + |
22 | | - kingdomId, |
23 | | - ) |
24 | | - .setParameters({ |
25 | | - request_data: params, |
| 16 | + const batches: any[][] = []; |
| 17 | + for (let i = 0; i < params.length; i += 5) { |
| 18 | + batches.push(params.slice(i, i + 5)); |
| 19 | + } |
| 20 | + |
| 21 | + runCapitalCityRequestBatches( |
| 22 | + batches, |
| 23 | + 2, |
| 24 | + (batch) => this.sendBatch(characterId, kingdomId, batch), |
| 25 | + (batch) => { |
| 26 | + flushSync(() => { |
| 27 | + this.applyAcceptedRequestState(component, batch); |
| 28 | + }); |
| 29 | + }, |
| 30 | + ) |
| 31 | + .then(() => { |
| 32 | + component.setState({ |
| 33 | + processing_request: false, |
| 34 | + }); |
26 | 35 | }) |
27 | | - .doAjaxCall( |
28 | | - "post", |
29 | | - (result: AxiosResponse) => { |
30 | | - this.applyAcceptedRequestState(component, params); |
31 | | - }, |
32 | | - (error: AxiosError) => { |
| 36 | + .catch((error: AxiosError) => { |
| 37 | + component.setState({ |
| 38 | + processing_request: false, |
| 39 | + }); |
| 40 | + |
| 41 | + if (typeof error.response !== "undefined") { |
| 42 | + const response: AxiosResponse = error.response; |
| 43 | + |
33 | 44 | component.setState({ |
34 | | - processing_request: false, |
| 45 | + error_message: response.data.message, |
35 | 46 | }); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
36 | 50 |
|
37 | | - if (typeof error.response !== "undefined") { |
38 | | - const response: AxiosResponse = error.response; |
39 | | - |
40 | | - component.setState({ |
41 | | - error_message: response.data.message, |
42 | | - }); |
43 | | - } |
44 | | - }, |
45 | | - ); |
| 51 | + private sendBatch( |
| 52 | + characterId: number, |
| 53 | + kingdomId: number, |
| 54 | + batch: any[], |
| 55 | + ): Promise<void> { |
| 56 | + return axios |
| 57 | + .post( |
| 58 | + "/api/kingdom/capital-city/recruit-unit-requests/" + |
| 59 | + characterId + |
| 60 | + "/" + |
| 61 | + kingdomId, |
| 62 | + { request_data: batch }, |
| 63 | + { headers: { "Content-Type": "application/json" } }, |
| 64 | + ) |
| 65 | + .then(() => undefined) |
| 66 | + .catch((error: AxiosError) => { |
| 67 | + if (error.response?.status === 401) { |
| 68 | + window.location.reload(); |
| 69 | + } |
| 70 | + throw error; |
| 71 | + }); |
46 | 72 | } |
47 | 73 |
|
48 | 74 | private applyAcceptedRequestState( |
@@ -73,7 +99,6 @@ export default class ProcessUnitRequestAjax { |
73 | 99 |
|
74 | 100 | if (fadingKingdomIds.size <= 0) { |
75 | 101 | component.setState({ |
76 | | - processing_request: false, |
77 | 102 | unit_recruitment_data: updatedUnitRecruitmentData, |
78 | 103 | filtered_unit_recruitment_data: |
79 | 104 | updatedFilteredUnitRecruitmentData, |
@@ -106,7 +131,6 @@ export default class ProcessUnitRequestAjax { |
106 | 131 | }); |
107 | 132 |
|
108 | 133 | component.setState({ |
109 | | - processing_request: false, |
110 | 134 | unit_recruitment_data: this.keepFadingKingdomsRendered( |
111 | 135 | component.state.unit_recruitment_data, |
112 | 136 | updatedUnitRecruitmentData, |
|
0 commit comments