2023-02-18 13:11:01 +03:00
|
|
|
import { faUndo } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { isEmpty, pipe } from 'ramda';
|
2020-11-25 23:05:27 +03:00
|
|
|
import { useEffect } from 'react';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { InputProps } from 'reactstrap';
|
|
|
|
import { Button, DropdownItem, Input, InputGroup, UncontrolledTooltip } from 'reactstrap';
|
2023-07-31 22:36:44 +03:00
|
|
|
import { DropdownBtn, useToggle } from '../../shlink-frontend-kit/src';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { DomainsList } from './reducers/domainsList';
|
2020-11-28 11:58:05 +03:00
|
|
|
import './DomainSelector.scss';
|
2020-11-25 23:05:27 +03:00
|
|
|
|
2020-11-28 11:58:05 +03:00
|
|
|
export interface DomainSelectorProps extends Omit<InputProps, 'onChange'> {
|
2020-11-25 23:05:27 +03:00
|
|
|
value?: string;
|
2020-11-28 14:36:40 +03:00
|
|
|
onChange: (domain: string) => void;
|
2020-11-25 23:05:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-28 11:58:05 +03:00
|
|
|
interface DomainSelectorConnectProps extends DomainSelectorProps {
|
2020-11-25 23:05:27 +03:00
|
|
|
listDomains: Function;
|
|
|
|
domainsList: DomainsList;
|
|
|
|
}
|
|
|
|
|
2020-11-28 11:58:05 +03:00
|
|
|
export const DomainSelector = ({ listDomains, value, domainsList, onChange }: DomainSelectorConnectProps) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const [inputDisplayed,, showInput, hideInput] = useToggle();
|
2020-11-25 23:05:27 +03:00
|
|
|
const { domains } = domainsList;
|
|
|
|
const valueIsEmpty = isEmpty(value);
|
|
|
|
const unselectDomain = () => onChange('');
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
listDomains();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return inputDisplayed ? (
|
|
|
|
<InputGroup>
|
|
|
|
<Input
|
2022-03-05 15:14:26 +03:00
|
|
|
value={value ?? ''}
|
2020-11-25 23:05:27 +03:00
|
|
|
placeholder="Domain"
|
|
|
|
onChange={(e) => onChange(e.target.value)}
|
|
|
|
/>
|
2022-03-05 15:14:26 +03:00
|
|
|
<Button
|
|
|
|
id="backToDropdown"
|
|
|
|
outline
|
|
|
|
type="button"
|
|
|
|
className="domains-dropdown__back-btn"
|
2022-05-09 20:03:19 +03:00
|
|
|
aria-label="Back to domains list"
|
2022-03-05 15:14:26 +03:00
|
|
|
onClick={pipe(unselectDomain, hideInput)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faUndo} />
|
|
|
|
</Button>
|
|
|
|
<UncontrolledTooltip target="backToDropdown" placement="left" trigger="hover">
|
|
|
|
Existing domains
|
|
|
|
</UncontrolledTooltip>
|
2020-11-25 23:05:27 +03:00
|
|
|
</InputGroup>
|
|
|
|
) : (
|
2020-12-25 12:43:36 +03:00
|
|
|
<DropdownBtn
|
2020-12-25 12:29:25 +03:00
|
|
|
text={valueIsEmpty ? 'Domain' : `Domain: ${value}`}
|
2021-02-27 10:23:06 +03:00
|
|
|
className={!valueIsEmpty ? 'domains-dropdown__toggle-btn--active' : 'domains-dropdown__toggle-btn'}
|
2020-12-25 12:29:25 +03:00
|
|
|
>
|
|
|
|
{domains.map(({ domain, isDefault }) => (
|
|
|
|
<DropdownItem
|
|
|
|
key={domain}
|
2022-03-26 14:17:42 +03:00
|
|
|
active={(value === domain || isDefault) && valueIsEmpty}
|
2020-12-25 12:29:25 +03:00
|
|
|
onClick={() => onChange(domain)}
|
|
|
|
>
|
|
|
|
{domain}
|
2022-03-05 15:26:28 +03:00
|
|
|
{isDefault && <span className="float-end text-muted">default</span>}
|
2020-11-25 23:05:27 +03:00
|
|
|
</DropdownItem>
|
2020-12-25 12:29:25 +03:00
|
|
|
))}
|
|
|
|
<DropdownItem divider />
|
|
|
|
<DropdownItem onClick={pipe(unselectDomain, showInput)}>
|
|
|
|
<i>New domain</i>
|
|
|
|
</DropdownItem>
|
2020-12-25 12:43:36 +03:00
|
|
|
</DropdownBtn>
|
2020-11-25 23:05:27 +03:00
|
|
|
);
|
|
|
|
};
|