mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 17:57:26 +03:00
Updated EditTagsModal to be a functional component
This commit is contained in:
parent
d0b3edaa2f
commit
cd1f186e28
2 changed files with 26 additions and 53 deletions
|
@ -1,52 +1,37 @@
|
||||||
import React from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { ExternalLink } from 'react-external-link';
|
import { ExternalLink } from 'react-external-link';
|
||||||
import { pipe } from 'ramda';
|
|
||||||
import { shortUrlTagsType } from '../reducers/shortUrlTags';
|
import { shortUrlTagsType } from '../reducers/shortUrlTags';
|
||||||
import { shortUrlType } from '../reducers/shortUrlsList';
|
import { shortUrlType } from '../reducers/shortUrlsList';
|
||||||
|
|
||||||
const EditTagsModal = (TagsSelector) => class EditTagsModal extends React.Component {
|
const propTypes = {
|
||||||
static propTypes = {
|
isOpen: PropTypes.bool.isRequired,
|
||||||
isOpen: PropTypes.bool.isRequired,
|
toggle: PropTypes.func.isRequired,
|
||||||
toggle: PropTypes.func.isRequired,
|
shortUrl: shortUrlType.isRequired,
|
||||||
shortUrl: shortUrlType.isRequired,
|
shortUrlTags: shortUrlTagsType,
|
||||||
shortUrlTags: shortUrlTagsType,
|
editShortUrlTags: PropTypes.func,
|
||||||
editShortUrlTags: PropTypes.func,
|
resetShortUrlsTags: PropTypes.func,
|
||||||
resetShortUrlsTags: PropTypes.func,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
saveTags = () => {
|
const EditTagsModal = (TagsSelector) => {
|
||||||
const { editShortUrlTags, shortUrl, toggle } = this.props;
|
const EditTagsModalComp = ({ isOpen, toggle, shortUrl, shortUrlTags, editShortUrlTags, resetShortUrlsTags }) => {
|
||||||
|
const [ selectedTags, setSelectedTags ] = useState(shortUrl.tags || []);
|
||||||
|
|
||||||
editShortUrlTags(shortUrl.shortCode, shortUrl.domain, this.state.tags)
|
useEffect(() => resetShortUrlsTags, []);
|
||||||
|
|
||||||
|
const url = shortUrl && (shortUrl.shortUrl || '');
|
||||||
|
const saveTags = () => editShortUrlTags(shortUrl.shortCode, shortUrl.domain, selectedTags)
|
||||||
.then(toggle)
|
.then(toggle)
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
};
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
const { resetShortUrlsTags } = this.props;
|
|
||||||
|
|
||||||
resetShortUrlsTags();
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
this.state = { tags: props.shortUrl.tags };
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
const { isOpen, toggle, shortUrl, shortUrlTags, resetShortUrlsTags } = this.props;
|
|
||||||
const url = shortUrl && (shortUrl.shortUrl || '');
|
|
||||||
const close = pipe(resetShortUrlsTags, toggle);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal isOpen={isOpen} toggle={close} centered>
|
<Modal isOpen={isOpen} toggle={toggle} centered>
|
||||||
<ModalHeader toggle={close}>
|
<ModalHeader toggle={toggle}>
|
||||||
Edit tags for <ExternalLink href={url} />
|
Edit tags for <ExternalLink href={url} />
|
||||||
</ModalHeader>
|
</ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
<TagsSelector tags={this.state.tags} onChange={(tags) => this.setState({ tags })} />
|
<TagsSelector tags={selectedTags} onChange={(tags) => setSelectedTags(tags)} />
|
||||||
{shortUrlTags.error && (
|
{shortUrlTags.error && (
|
||||||
<div className="p-2 mt-2 bg-danger text-white text-center">
|
<div className="p-2 mt-2 bg-danger text-white text-center">
|
||||||
Something went wrong while saving the tags :(
|
Something went wrong while saving the tags :(
|
||||||
|
@ -54,19 +39,18 @@ const EditTagsModal = (TagsSelector) => class EditTagsModal extends React.Compon
|
||||||
)}
|
)}
|
||||||
</ModalBody>
|
</ModalBody>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<button className="btn btn-link" onClick={close}>Cancel</button>
|
<button className="btn btn-link" onClick={toggle}>Cancel</button>
|
||||||
<button
|
<button className="btn btn-primary" type="button" disabled={shortUrlTags.saving} onClick={saveTags}>
|
||||||
className="btn btn-primary"
|
|
||||||
type="button"
|
|
||||||
disabled={shortUrlTags.saving}
|
|
||||||
onClick={() => this.saveTags()}
|
|
||||||
>
|
|
||||||
{shortUrlTags.saving ? 'Saving tags...' : 'Save tags'}
|
{shortUrlTags.saving ? 'Saving tags...' : 'Save tags'}
|
||||||
</button>
|
</button>
|
||||||
</ModalFooter>
|
</ModalFooter>
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
EditTagsModalComp.propTypes = propTypes;
|
||||||
|
|
||||||
|
return EditTagsModalComp;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default EditTagsModal;
|
export default EditTagsModal;
|
||||||
|
|
|
@ -37,17 +37,6 @@ describe('<EditTagsModal />', () => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('resets tags when component is mounted', () => {
|
|
||||||
createWrapper({
|
|
||||||
shortCode,
|
|
||||||
tags: [],
|
|
||||||
saving: false,
|
|
||||||
error: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(resetShortUrlsTags).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('renders tags selector and save button when loaded', () => {
|
it('renders tags selector and save button when loaded', () => {
|
||||||
const wrapper = createWrapper({
|
const wrapper = createWrapper({
|
||||||
shortCode,
|
shortCode,
|
||||||
|
|
Loading…
Reference in a new issue