diff --git a/.env b/.env index db23e6a..a473846 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -REACT_APP_API_URL=https://localhost:7115/api/v1 +REACT_APP_API_URL_LOCAL=https://localhost:7115/api/v1 +REACT_APP_API_URL=http://vsa-jino.my.to:49205/api/v1 diff --git a/src/components/GamePage/GamePage.tsx b/src/components/GamePage/GamePage.tsx index ecf2b71..8df4336 100644 --- a/src/components/GamePage/GamePage.tsx +++ b/src/components/GamePage/GamePage.tsx @@ -75,7 +75,11 @@ const GamePage: React.FC = (props) => {
Свой сервис:
diff --git a/src/store/apiStore/apiStore.service.ts b/src/store/apiStore/apiStore.service.ts index 997ce11..a2dcd5b 100644 --- a/src/store/apiStore/apiStore.service.ts +++ b/src/store/apiStore/apiStore.service.ts @@ -11,16 +11,16 @@ import { Mapper } from '../mapper'; const service = { // region Публичные функции - async getGame(): Promise { - const resultPromise = tool.get('/game', []); + async getGame(apiUrl: string): Promise { + const resultPromise = tool.get(apiUrl, '/game', []); return resultPromise.then((response: IGameDto | undefined): (Game | undefined) => { if (!response) return; return Mapper.fromGame(response); }); }, - async gameTurn(gameGuid: string, witchGuid: string): Promise { - const resultPromise = tool.get('/turn', [ + async gameTurn(apiUrl: string, gameGuid: string, witchGuid: string): Promise { + const resultPromise = tool.get(apiUrl, '/turn', [ {name: "gameGuid", value: gameGuid}, {name: "witchGuid", value: witchGuid}, ]); @@ -30,8 +30,8 @@ const service = { }); }, - async getVersion(): Promise { - return tool.get('/version', []); + async getVersion(apiUrl: string): Promise { + return tool.get(apiUrl, '/version', []); } // endregion }; diff --git a/src/store/apiStore/apiStore.ts b/src/store/apiStore/apiStore.ts index 3ba226b..04eadb1 100644 --- a/src/store/apiStore/apiStore.ts +++ b/src/store/apiStore/apiStore.ts @@ -21,23 +21,27 @@ class ApiStore { gameStage: EGameStage = EGameStage.Start; version: string = ""; currentGame: Game | undefined; - errorState: string = ""; - // endregion - // region consts - apiUrl = process.env.REACT_APP_API_URL; + errorState: string | undefined; + + apiUrl: string | undefined; // endregion + constructor() { + this.apiUrl = process.env.REACT_APP_API_URL_LOCAL; // REACT_APP_API_URL; + makeObservable(this, { /* observable properties */ currentGame: observable, mockMode: observable, + apiUrl: observable, version: observable, errorState: observable, gameStage: observable, /* actions */ setCurrentGame: action.bound, + setApiUrl: action.bound, setMockMode: action.bound, setVersion: action.bound, setErrorState: action.bound, @@ -48,7 +52,7 @@ class ApiStore { // region public methods async backgroundLoad(): Promise { - if (this.mockMode) { + if (this.mockMode || !this.apiUrl) { // mock mode const p1 = mockService.getVersion() .then((version) => { @@ -63,9 +67,9 @@ class ApiStore { }); } else { // api mode - const p1 = service.getVersion() + const p1 = service.getVersion(this.apiUrl) .then((version) => version && this.setVersion(version)); - const p2 = service.getGame() + const p2 = service.getGame(this.apiUrl) .then((game) => game && this.setCurrentGame(game)); const promise = Promise.all([p1, p2]); return promise.then(() => { @@ -77,11 +81,11 @@ class ApiStore { if (!this.currentGame) return Promise.resolve(false); if (!this.currentGame?.guid) return Promise.resolve(false); let promise; - if (this.mockMode) { + if (this.mockMode || !this.apiUrl) { // mock mode promise = mockService.gameTurn(this.currentGame.guid, witchGuid); } else { - promise = service.gameTurn(this.currentGame?.guid, witchGuid); + promise = service.gameTurn(this.apiUrl, this.currentGame?.guid, witchGuid); } return promise.then((game) => { @@ -103,6 +107,10 @@ class ApiStore { if (mockMode !== this.mockMode) this.mockMode = mockMode; } + setApiUrl(apiUrl: string) { + if (apiUrl !== this.apiUrl) this.apiUrl = apiUrl; + } + setVersion(version: string) { if (version && version !== this.version) { this.version = version; @@ -123,9 +131,9 @@ class ApiStore { //endregion startNewGame() { - const promise = (this.mockMode) + const promise = (this.mockMode || !this.apiUrl) ? mockService.getGame() - : service.getGame(); + : service.getGame(this.apiUrl); promise.then((game) => { if (game) { this.setCurrentGame(game); @@ -135,12 +143,12 @@ class ApiStore { } tryToSetMockMode(flagEnabled: boolean) { - if (!flagEnabled) { + if (!flagEnabled || !this.apiUrl) { this.setMockMode(true); this.setGameStage(EGameStage.Start); return; } - service.getVersion() + service.getVersion(this.apiUrl) .then((version) => { if (!version) { this.setMockMode(true); diff --git a/src/store/tool.ts b/src/store/tool.ts index 272bc1d..c5562c3 100644 --- a/src/store/tool.ts +++ b/src/store/tool.ts @@ -13,15 +13,14 @@ export type ParamGet = { }; const tool = { - apiUrl: process.env.REACT_APP_API_URL, - // region функции // метод взаимодействия с REST-сервисом get( + apiUrl: string, method: string, args: ParamGet[], ): Promise { - const url = this.apiUrl + method; + const url = apiUrl + method; const requestConfig = { params: _.fromPairs(args.map((v) => [v.name, v.value])) }; @@ -31,7 +30,7 @@ const tool = { requestConfig, ) .then((response) => { - const { data } = response; + const {data} = response; return data; }) .catch((err) => {