Simplified ShlinkApiClient and moved runtime creation logic to external service

This commit is contained in:
Alejandro Celaya
2018-12-18 10:14:25 +01:00
parent 7bd4b39b5a
commit 4f54e3315f
14 changed files with 90 additions and 57 deletions

View File

@@ -1,6 +1,6 @@
import { curry } from 'ramda';
import PropTypes from 'prop-types';
import shlinkApiClient from '../../api/ShlinkApiClient';
import { buildShlinkApiClientWithAxios as buildShlinkApiClient } from '../../api/ShlinkApiClientBuilder';
/* eslint-disable padding-line-between-statements, newline-after-var */
export const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START';
@@ -50,9 +50,12 @@ export default function reducer(state = defaultState, action) {
}
}
export const _createShortUrl = (shlinkApiClient, data) => async (dispatch) => {
export const _createShortUrl = (buildShlinkApiClient, data) => async (dispatch, getState) => {
dispatch({ type: CREATE_SHORT_URL_START });
const { selectedServer } = getState();
const shlinkApiClient = buildShlinkApiClient(selectedServer);
try {
const result = await shlinkApiClient.createShortUrl(data);
@@ -62,6 +65,6 @@ export const _createShortUrl = (shlinkApiClient, data) => async (dispatch) => {
}
};
export const createShortUrl = curry(_createShortUrl)(shlinkApiClient);
export const createShortUrl = curry(_createShortUrl)(buildShlinkApiClient);
export const resetCreateShortUrl = () => ({ type: RESET_CREATE_SHORT_URL });

View File

@@ -1,6 +1,6 @@
import { curry } from 'ramda';
import PropTypes from 'prop-types';
import shlinkApiClient from '../../api/ShlinkApiClient';
import { buildShlinkApiClientWithAxios as buildShlinkApiClient } from '../../api/ShlinkApiClientBuilder';
/* eslint-disable padding-line-between-statements, newline-after-var */
const DELETE_SHORT_URL_START = 'shlink/deleteShortUrl/DELETE_SHORT_URL_START';
@@ -56,9 +56,12 @@ export default function reducer(state = defaultState, action) {
}
}
export const _deleteShortUrl = (shlinkApiClient, shortCode) => async (dispatch) => {
export const _deleteShortUrl = (buildShlinkApiClient, shortCode) => async (dispatch, getState) => {
dispatch({ type: DELETE_SHORT_URL_START });
const { selectedServer } = getState();
const shlinkApiClient = buildShlinkApiClient(selectedServer);
try {
await shlinkApiClient.deleteShortUrl(shortCode);
dispatch({ type: DELETE_SHORT_URL, shortCode });
@@ -69,7 +72,7 @@ export const _deleteShortUrl = (shlinkApiClient, shortCode) => async (dispatch)
}
};
export const deleteShortUrl = curry(_deleteShortUrl)(shlinkApiClient);
export const deleteShortUrl = curry(_deleteShortUrl)(buildShlinkApiClient);
export const resetDeleteShortUrl = () => ({ type: RESET_DELETE_SHORT_URL });

View File

@@ -50,8 +50,10 @@ export default function reducer(state = defaultState, action) {
}
}
export const editShortUrlTags = (shlinkApiClient) => (shortCode, tags) => async (dispatch) => {
export const editShortUrlTags = (buildShlinkApiClient) => (shortCode, tags) => async (dispatch, getState) => {
dispatch({ type: EDIT_SHORT_URL_TAGS_START });
const { selectedServer } = getState();
const shlinkApiClient = buildShlinkApiClient(selectedServer);
try {
const normalizedTags = await shlinkApiClient.updateShortUrlTags(shortCode, tags);

View File

@@ -1,6 +1,6 @@
import { assoc, assocPath, reject } from 'ramda';
import PropTypes from 'prop-types';
import shlinkApiClient from '../../api/ShlinkApiClient';
import { buildShlinkApiClientWithAxios as buildShlinkApiClient } from '../../api/ShlinkApiClientBuilder';
import { SHORT_URL_TAGS_EDITED } from './shortUrlTags';
import { SHORT_URL_DELETED } from './shortUrlDeletion';
@@ -55,9 +55,12 @@ export default function reducer(state = initialState, action) {
}
}
export const _listShortUrls = (shlinkApiClient, params = {}) => async (dispatch) => {
export const _listShortUrls = (buildShlinkApiClient, params = {}) => async (dispatch, getState) => {
dispatch({ type: LIST_SHORT_URLS_START });
const { selectedServer } = getState();
const shlinkApiClient = buildShlinkApiClient(selectedServer);
try {
const shortUrls = await shlinkApiClient.listShortUrls(params);
@@ -67,4 +70,4 @@ export const _listShortUrls = (shlinkApiClient, params = {}) => async (dispatch)
}
};
export const listShortUrls = (params = {}) => _listShortUrls(shlinkApiClient, params);
export const listShortUrls = (params = {}) => _listShortUrls(buildShlinkApiClient, params);