mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-01 21:26:46 +00:00
Removed duplicated code when building ShlinkApiClient
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
import * as axios from 'axios';
|
||||
import { wait } from '../utils';
|
||||
import ShlinkApiClient from './ShlinkApiClient';
|
||||
|
||||
const apiClients = {};
|
||||
|
||||
const buildShlinkApiClient = (axios) => ({ url, apiKey }) => {
|
||||
const getSelectedServerFromState = async (getState) => {
|
||||
const { selectedServer } = getState();
|
||||
|
||||
if (!selectedServer) {
|
||||
return wait(250).then(() => getSelectedServerFromState(getState));
|
||||
}
|
||||
|
||||
return selectedServer;
|
||||
};
|
||||
|
||||
const buildShlinkApiClient = (axios) => async (getState) => {
|
||||
const { url, apiKey } = await getSelectedServerFromState(getState);
|
||||
const clientKey = `${url}_${apiKey}`;
|
||||
|
||||
if (!apiClients[clientKey]) {
|
||||
@@ -14,5 +25,3 @@ const buildShlinkApiClient = (axios) => ({ url, apiKey }) => {
|
||||
};
|
||||
|
||||
export default buildShlinkApiClient;
|
||||
|
||||
export const buildShlinkApiClientWithAxios = buildShlinkApiClient(axios);
|
||||
|
||||
@@ -51,3 +51,5 @@ export const useToggle = (initialValue = false) => {
|
||||
|
||||
return [ flag, () => setFlag(!flag) ];
|
||||
};
|
||||
|
||||
export const wait = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds));
|
||||
|
||||
Reference in New Issue
Block a user