Connected creation form with redux, and created reducer for short URL creation

This commit is contained in:
Alejandro Celaya
2018-07-28 10:41:05 +02:00
parent c51bf5b9a0
commit 0a5c20e3ee
13 changed files with 116 additions and 48 deletions

View File

@@ -0,0 +1,46 @@
import ShlinkApiClient from '../../api/ShlinkApiClient';
const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START';
const CREATE_SHORT_URL_ERROR = 'shlink/createShortUrl/CREATE_SHORT_URL_ERROR';
const CREATE_SHORT_URL = 'shlink/createShortUrl/CREATE_SHORT_URL';
const defaultState = {
result: null,
saving: false,
error: false,
};
export default function reducer(state = defaultState, action) {
switch (action.type) {
case CREATE_SHORT_URL_START:
return {
...state,
saving: true,
};
case CREATE_SHORT_URL_ERROR:
return {
...state,
saving: false,
error: true,
};
case CREATE_SHORT_URL:
return {
result: action.result,
saving: false,
error: true,
};
default:
return state;
}
}
export const createShortUrl = data => async dispatch => {
dispatch({ type: CREATE_SHORT_URL_START });
try {
const result = await ShlinkApiClient.createShortUrl(data);
dispatch({ type: CREATE_SHORT_URL, result });
} catch (e) {
dispatch({ type: CREATE_SHORT_URL_ERROR });
}
};

View File

@@ -41,15 +41,13 @@ export const listShortUrls = (serverId, params = {}) => {
return updateShortUrlsList(params);
};
export const updateShortUrlsList = (params = {}) => {
return async dispatch => {
dispatch({ type: LIST_SHORT_URLS_START });
export const updateShortUrlsList = (params = {}) => async dispatch => {
dispatch({ type: LIST_SHORT_URLS_START });
try {
const shortUrls = await ShlinkApiClient.listShortUrls(params);
dispatch({ type: LIST_SHORT_URLS, shortUrls, params });
} catch (e) {
dispatch({ type: LIST_SHORT_URLS_ERROR, params });
}
};
try {
const shortUrls = await ShlinkApiClient.listShortUrls(params);
dispatch({ type: LIST_SHORT_URLS, shortUrls, params });
} catch (e) {
dispatch({ type: LIST_SHORT_URLS_ERROR, params });
}
};