2020-08-26 19:55:40 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
|
|
|
import { GetState } from '../../container/types';
|
2020-08-26 21:03:23 +03:00
|
|
|
import { OptionalString } from '../../utils/utils';
|
2020-08-27 19:31:56 +03:00
|
|
|
import { ShortUrlIdentifier } from '../data';
|
2020-08-27 23:09:16 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuilder';
|
2020-03-30 21:42:58 +03:00
|
|
|
|
|
|
|
/* eslint-disable padding-line-between-statements */
|
|
|
|
export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START';
|
|
|
|
export const EDIT_SHORT_URL_ERROR = 'shlink/shortUrlEdition/EDIT_SHORT_URL_ERROR';
|
|
|
|
export const SHORT_URL_EDITED = 'shlink/shortUrlEdition/SHORT_URL_EDITED';
|
|
|
|
/* eslint-enable padding-line-between-statements */
|
|
|
|
|
2020-08-26 19:55:40 +03:00
|
|
|
export interface ShortUrlEdition {
|
|
|
|
shortCode: string | null;
|
|
|
|
longUrl: string | null;
|
|
|
|
saving: boolean;
|
|
|
|
error: boolean;
|
|
|
|
}
|
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
export interface ShortUrlEditedAction extends Action<string>, ShortUrlIdentifier {
|
2020-08-26 19:55:40 +03:00
|
|
|
longUrl: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: ShortUrlEdition = {
|
2020-03-30 21:42:58 +03:00
|
|
|
shortCode: null,
|
|
|
|
longUrl: null,
|
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2020-08-26 19:55:40 +03:00
|
|
|
export default buildReducer<ShortUrlEdition, ShortUrlEditedAction>({
|
2020-03-30 21:42:58 +03:00
|
|
|
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
|
|
|
|
[EDIT_SHORT_URL_ERROR]: (state) => ({ ...state, saving: false, error: true }),
|
2020-08-26 19:55:40 +03:00
|
|
|
[SHORT_URL_EDITED]: (_, { shortCode, longUrl }) => ({ shortCode, longUrl, saving: false, error: false }),
|
2020-03-30 21:42:58 +03:00
|
|
|
}, initialState);
|
|
|
|
|
2020-08-26 19:55:40 +03:00
|
|
|
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
|
|
|
shortCode: string,
|
2020-08-26 21:03:23 +03:00
|
|
|
domain: OptionalString,
|
2020-08-26 19:55:40 +03:00
|
|
|
longUrl: string,
|
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
2020-03-30 21:42:58 +03:00
|
|
|
dispatch({ type: EDIT_SHORT_URL_START });
|
|
|
|
const { updateShortUrlMeta } = buildShlinkApiClient(getState);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await updateShortUrlMeta(shortCode, domain, { longUrl });
|
2020-08-26 19:55:40 +03:00
|
|
|
dispatch<ShortUrlEditedAction>({ shortCode, longUrl, domain, type: SHORT_URL_EDITED });
|
2020-03-30 21:42:58 +03:00
|
|
|
} catch (e) {
|
|
|
|
dispatch({ type: EDIT_SHORT_URL_ERROR });
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|