mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-11 02:37:22 +03:00
Improved short URL creation
This commit is contained in:
parent
eb65e99024
commit
2a2bae6d1a
4 changed files with 34 additions and 23 deletions
|
@ -23,22 +23,36 @@ const propTypes = {
|
||||||
selectedServer: serverType,
|
selectedServer: serverType,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
longUrl: '',
|
||||||
|
tags: [],
|
||||||
|
customSlug: '',
|
||||||
|
shortCodeLength: '',
|
||||||
|
domain: '',
|
||||||
|
validSince: undefined,
|
||||||
|
validUntil: undefined,
|
||||||
|
maxVisits: '',
|
||||||
|
findIfExists: false,
|
||||||
|
};
|
||||||
|
|
||||||
const CreateShortUrl = (TagsSelector, CreateShortUrlResult, ForServerVersion) => {
|
const CreateShortUrl = (TagsSelector, CreateShortUrlResult, ForServerVersion) => {
|
||||||
const CreateShortUrlComp = ({ createShortUrl, shortUrlCreationResult, resetCreateShortUrl, selectedServer }) => {
|
const CreateShortUrlComp = ({ createShortUrl, shortUrlCreationResult, resetCreateShortUrl, selectedServer }) => {
|
||||||
const [ shortUrlCreation, setShortUrlCreation ] = useState({
|
const [ shortUrlCreation, setShortUrlCreation ] = useState(initialState);
|
||||||
longUrl: '',
|
|
||||||
tags: [],
|
|
||||||
customSlug: undefined,
|
|
||||||
shortCodeLength: undefined,
|
|
||||||
domain: undefined,
|
|
||||||
validSince: undefined,
|
|
||||||
validUntil: undefined,
|
|
||||||
maxVisits: undefined,
|
|
||||||
findIfExists: false,
|
|
||||||
});
|
|
||||||
const [ moreOptionsVisible, toggleMoreOptionsVisible ] = useToggle();
|
const [ moreOptionsVisible, toggleMoreOptionsVisible ] = useToggle();
|
||||||
|
|
||||||
const changeTags = (tags) => setShortUrlCreation({ ...shortUrlCreation, tags: tags.map(normalizeTag) });
|
const changeTags = (tags) => setShortUrlCreation({ ...shortUrlCreation, tags: tags.map(normalizeTag) });
|
||||||
|
const reset = () => setShortUrlCreation(initialState);
|
||||||
|
const save = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const shortUrlData = {
|
||||||
|
...shortUrlCreation,
|
||||||
|
validSince: formatDate(shortUrlCreation.validSince),
|
||||||
|
validUntil: formatDate(shortUrlCreation.validUntil),
|
||||||
|
};
|
||||||
|
|
||||||
|
createShortUrl(shortUrlData).then(reset).catch(() => {});
|
||||||
|
};
|
||||||
const renderOptionalInput = (id, placeholder, type = 'text', props = {}) => (
|
const renderOptionalInput = (id, placeholder, type = 'text', props = {}) => (
|
||||||
<FormGroup>
|
<FormGroup>
|
||||||
<Input
|
<Input
|
||||||
|
@ -62,14 +76,7 @@ const CreateShortUrl = (TagsSelector, CreateShortUrlResult, ForServerVersion) =>
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
const save = (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
createShortUrl({
|
|
||||||
...shortUrlCreation,
|
|
||||||
validSince: formatDate(shortUrlCreation.validSince),
|
|
||||||
validUntil: formatDate(shortUrlCreation.validUntil),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const currentServerVersion = selectedServer && selectedServer.version;
|
const currentServerVersion = selectedServer && selectedServer.version;
|
||||||
const disableDomain = !versionMatch(currentServerVersion, { minVersion: '1.19.0-beta.1' });
|
const disableDomain = !versionMatch(currentServerVersion, { minVersion: '1.19.0-beta.1' });
|
||||||
const disableShortCodeLength = !versionMatch(currentServerVersion, { minVersion: '2.1.0' });
|
const disableShortCodeLength = !versionMatch(currentServerVersion, { minVersion: '2.1.0' });
|
||||||
|
@ -147,9 +154,9 @@ const CreateShortUrl = (TagsSelector, CreateShortUrlResult, ForServerVersion) =>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="btn btn-outline-primary float-right"
|
className="btn btn-outline-primary float-right"
|
||||||
disabled={shortUrlCreationResult.loading || isEmpty(shortUrlCreation.longUrl)}
|
disabled={shortUrlCreationResult.saving || isEmpty(shortUrlCreation.longUrl)}
|
||||||
>
|
>
|
||||||
{shortUrlCreationResult.loading ? 'Creating...' : 'Create'}
|
{shortUrlCreationResult.saving ? 'Creating...' : 'Create'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,8 @@ export const createShortUrl = (buildShlinkApiClient) => (data) => async (dispatc
|
||||||
dispatch({ type: CREATE_SHORT_URL, result });
|
dispatch({ type: CREATE_SHORT_URL, result });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dispatch({ type: CREATE_SHORT_URL_ERROR });
|
dispatch({ type: CREATE_SHORT_URL_ERROR });
|
||||||
|
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ describe('<CreateShortUrl />', () => {
|
||||||
const shortUrlCreationResult = {
|
const shortUrlCreationResult = {
|
||||||
loading: false,
|
loading: false,
|
||||||
};
|
};
|
||||||
const createShortUrl = jest.fn();
|
const createShortUrl = jest.fn(() => Promise.resolve());
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => '', () => '');
|
const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => '', () => '');
|
||||||
|
@ -22,7 +22,7 @@ describe('<CreateShortUrl />', () => {
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
wrapper.unmount();
|
wrapper.unmount();
|
||||||
createShortUrl.mockReset();
|
createShortUrl.mockClear();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('saves short URL with data set in form controls', () => {
|
it('saves short URL with data set in form controls', () => {
|
||||||
|
|
|
@ -72,6 +72,8 @@ describe('shortUrlCreationReducer', () => {
|
||||||
const apiClientMock = createApiClientMock(Promise.reject(error));
|
const apiClientMock = createApiClientMock(Promise.reject(error));
|
||||||
const dispatchable = createShortUrl(() => apiClientMock)({});
|
const dispatchable = createShortUrl(() => apiClientMock)({});
|
||||||
|
|
||||||
|
expect.assertions(5);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await dispatchable(dispatch, getState);
|
await dispatchable(dispatch, getState);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
Loading…
Reference in a new issue