Browse Source

добавлена возможность смены тестового URL

pull/1/head
Sergey Verevkin 4 years ago
parent
commit
dbe57a7812
  1. 3
      .env
  2. 6
      src/components/GamePage/GamePage.tsx
  3. 12
      src/store/apiStore/apiStore.service.ts
  4. 34
      src/store/apiStore/apiStore.ts
  5. 5
      src/store/tool.ts

3
.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

6
src/components/GamePage/GamePage.tsx

@ -75,7 +75,11 @@ const GamePage: React.FC<GameProps> = (props) => {
<div className="small">Свой сервис:
<ul>
<li>реализация <a href="/swagger.json">swagger</a></li>
<li>хостинг https://localhost:7115/api/v1</li>
<li>хостинг
<input
type='text'
value={apiStore.apiUrl}
onChange={(e) => apiStore.setApiUrl(e.target.value)}/></li>
<li>прием CORS от ooya.ga</li>
</ul>
</div>

12
src/store/apiStore/apiStore.service.ts

@ -11,16 +11,16 @@ import { Mapper } from '../mapper';
const service = {
// region Публичные функции
async getGame(): Promise<Game | undefined> {
const resultPromise = tool.get<IGameDto>('/game', []);
async getGame(apiUrl: string): Promise<Game | undefined> {
const resultPromise = tool.get<IGameDto>(apiUrl, '/game', []);
return resultPromise.then((response: IGameDto | undefined): (Game | undefined) => {
if (!response) return;
return Mapper.fromGame(response);
});
},
async gameTurn(gameGuid: string, witchGuid: string): Promise<Game | undefined> {
const resultPromise = tool.get<IGameDto>('/turn', [
async gameTurn(apiUrl: string, gameGuid: string, witchGuid: string): Promise<Game | undefined> {
const resultPromise = tool.get<IGameDto>(apiUrl, '/turn', [
{name: "gameGuid", value: gameGuid},
{name: "witchGuid", value: witchGuid},
]);
@ -30,8 +30,8 @@ const service = {
});
},
async getVersion(): Promise<string | undefined> {
return tool.get<string>('/version', []);
async getVersion(apiUrl: string): Promise<string | undefined> {
return tool.get<string>(apiUrl, '/version', []);
}
// endregion
};

34
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<void> {
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);

5
src/store/tool.ts

@ -13,15 +13,14 @@ export type ParamGet = {
};
const tool = {
apiUrl: process.env.REACT_APP_API_URL,
// region функции
// метод взаимодействия с REST-сервисом
get<TResult>(
apiUrl: string,
method: string,
args: ParamGet[],
): Promise<TResult | undefined> {
const url = this.apiUrl + method;
const url = apiUrl + method;
const requestConfig = {
params: _.fromPairs(args.map((v) => [v.name, v.value]))
};

Loading…
Cancel
Save