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. 7
      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">Свой сервис: <div className="small">Свой сервис:
<ul> <ul>
<li>реализация <a href="/swagger.json">swagger</a></li> <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> <li>прием CORS от ooya.ga</li>
</ul> </ul>
</div> </div>

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

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

34
src/store/apiStore/apiStore.ts

@ -21,23 +21,27 @@ class ApiStore {
gameStage: EGameStage = EGameStage.Start; gameStage: EGameStage = EGameStage.Start;
version: string = ""; version: string = "";
currentGame: Game | undefined; currentGame: Game | undefined;
errorState: string = ""; errorState: string | undefined;
// endregion
// region consts apiUrl: string | undefined;
apiUrl = process.env.REACT_APP_API_URL;
// endregion // endregion
constructor() { constructor() {
this.apiUrl = process.env.REACT_APP_API_URL_LOCAL; // REACT_APP_API_URL;
makeObservable(this, { makeObservable(this, {
/* observable properties */ /* observable properties */
currentGame: observable, currentGame: observable,
mockMode: observable, mockMode: observable,
apiUrl: observable,
version: observable, version: observable,
errorState: observable, errorState: observable,
gameStage: observable, gameStage: observable,
/* actions */ /* actions */
setCurrentGame: action.bound, setCurrentGame: action.bound,
setApiUrl: action.bound,
setMockMode: action.bound, setMockMode: action.bound,
setVersion: action.bound, setVersion: action.bound,
setErrorState: action.bound, setErrorState: action.bound,
@ -48,7 +52,7 @@ class ApiStore {
// region public methods // region public methods
async backgroundLoad(): Promise<void> { async backgroundLoad(): Promise<void> {
if (this.mockMode) { if (this.mockMode || !this.apiUrl) {
// mock mode // mock mode
const p1 = mockService.getVersion() const p1 = mockService.getVersion()
.then((version) => { .then((version) => {
@ -63,9 +67,9 @@ class ApiStore {
}); });
} else { } else {
// api mode // api mode
const p1 = service.getVersion() const p1 = service.getVersion(this.apiUrl)
.then((version) => version && this.setVersion(version)); .then((version) => version && this.setVersion(version));
const p2 = service.getGame() const p2 = service.getGame(this.apiUrl)
.then((game) => game && this.setCurrentGame(game)); .then((game) => game && this.setCurrentGame(game));
const promise = Promise.all([p1, p2]); const promise = Promise.all([p1, p2]);
return promise.then(() => { return promise.then(() => {
@ -77,11 +81,11 @@ class ApiStore {
if (!this.currentGame) return Promise.resolve(false); if (!this.currentGame) return Promise.resolve(false);
if (!this.currentGame?.guid) return Promise.resolve(false); if (!this.currentGame?.guid) return Promise.resolve(false);
let promise; let promise;
if (this.mockMode) { if (this.mockMode || !this.apiUrl) {
// mock mode // mock mode
promise = mockService.gameTurn(this.currentGame.guid, witchGuid); promise = mockService.gameTurn(this.currentGame.guid, witchGuid);
} else { } else {
promise = service.gameTurn(this.currentGame?.guid, witchGuid); promise = service.gameTurn(this.apiUrl, this.currentGame?.guid, witchGuid);
} }
return promise.then((game) => { return promise.then((game) => {
@ -103,6 +107,10 @@ class ApiStore {
if (mockMode !== this.mockMode) this.mockMode = mockMode; if (mockMode !== this.mockMode) this.mockMode = mockMode;
} }
setApiUrl(apiUrl: string) {
if (apiUrl !== this.apiUrl) this.apiUrl = apiUrl;
}
setVersion(version: string) { setVersion(version: string) {
if (version && version !== this.version) { if (version && version !== this.version) {
this.version = version; this.version = version;
@ -123,9 +131,9 @@ class ApiStore {
//endregion //endregion
startNewGame() { startNewGame() {
const promise = (this.mockMode) const promise = (this.mockMode || !this.apiUrl)
? mockService.getGame() ? mockService.getGame()
: service.getGame(); : service.getGame(this.apiUrl);
promise.then((game) => { promise.then((game) => {
if (game) { if (game) {
this.setCurrentGame(game); this.setCurrentGame(game);
@ -135,12 +143,12 @@ class ApiStore {
} }
tryToSetMockMode(flagEnabled: boolean) { tryToSetMockMode(flagEnabled: boolean) {
if (!flagEnabled) { if (!flagEnabled || !this.apiUrl) {
this.setMockMode(true); this.setMockMode(true);
this.setGameStage(EGameStage.Start); this.setGameStage(EGameStage.Start);
return; return;
} }
service.getVersion() service.getVersion(this.apiUrl)
.then((version) => { .then((version) => {
if (!version) { if (!version) {
this.setMockMode(true); this.setMockMode(true);

7
src/store/tool.ts

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

Loading…
Cancel
Save