2023-03-09 21:42:12 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
|
|
|
import { useRef, useState } from 'react';
|
|
|
|
import { Button, Dropdown, Form, FormButtons, FormRow, Item, SubmitButton } from 'react-basics';
|
|
|
|
import WebsiteTags from './WebsiteTags';
|
2023-03-22 22:05:55 +01:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2023-03-09 21:42:12 +01:00
|
|
|
|
2023-04-10 01:04:28 +02:00
|
|
|
export default function TeamAddWebsiteForm({ teamId, onSave, onClose }) {
|
2023-03-22 22:05:55 +01:00
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-03-09 21:42:12 +01:00
|
|
|
const { get, post, useQuery, useMutation } = useApi();
|
|
|
|
const { mutate, error } = useMutation(data => post(`/teams/${teamId}/websites`, data));
|
|
|
|
const { data: websites } = useQuery(['websites'], () => get('/websites'));
|
|
|
|
const [newWebsites, setNewWebsites] = useState([]);
|
|
|
|
const formRef = useRef();
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
|
|
|
mutate(
|
|
|
|
{ websiteIds: newWebsites },
|
|
|
|
{
|
|
|
|
onSuccess: async () => {
|
|
|
|
onSave();
|
|
|
|
onClose();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleAddWebsite = value => {
|
|
|
|
if (!newWebsites.some(a => a === value)) {
|
|
|
|
const nextValue = [...newWebsites];
|
|
|
|
|
|
|
|
nextValue.push(value);
|
|
|
|
|
|
|
|
setNewWebsites(nextValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveWebsite = value => {
|
|
|
|
const newValue = newWebsites.filter(a => a !== value);
|
|
|
|
|
|
|
|
setNewWebsites(newValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Form onSubmit={handleSubmit} error={error} ref={formRef}>
|
|
|
|
<FormRow label={formatMessage(labels.websites)}>
|
|
|
|
<Dropdown items={websites} onChange={handleAddWebsite}>
|
|
|
|
{({ id, name }) => <Item key={id}>{name}</Item>}
|
|
|
|
</Dropdown>
|
|
|
|
</FormRow>
|
|
|
|
<WebsiteTags items={websites} websites={newWebsites} onClick={handleRemoveWebsite} />
|
|
|
|
<FormButtons flex>
|
|
|
|
<SubmitButton disabled={newWebsites && newWebsites.length === 0}>
|
2023-04-08 22:14:22 +02:00
|
|
|
{formatMessage(labels.addWebsite)}
|
2023-03-09 21:42:12 +01:00
|
|
|
</SubmitButton>
|
|
|
|
<Button onClick={onClose}>{formatMessage(labels.cancel)}</Button>
|
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|