accounts-frontend/packages/app/components/contact/ContactForm.tsx
ErickSkrauch 82abe0a746
Extract general popups markup to its own component
Split popups controllers into separate components
Implemented storybooks for all project's popups
2020-07-06 19:29:56 +03:00

44 lines
1.4 KiB
TypeScript

import React, { ComponentType, useCallback, useRef, useState } from 'react';
import { useSelector } from 'react-redux';
import { send as sendFeedback } from 'app/services/api/feedback';
import { RootState } from 'app/reducers';
import logger from 'app/services/logger';
import ContactFormPopup from './ContactFormPopup';
import SuccessContactFormPopup from './SuccessContactFormPopup';
interface Props {
onClose?: () => void;
}
const ContactForm: ComponentType<Props> = ({ onClose }) => {
const userEmail = useSelector((state: RootState) => state.user.email);
const usedEmailRef = useRef(userEmail); // Use ref to avoid unneeded redraw
const [isSent, setIsSent] = useState<boolean>(false);
const onSubmit = useCallback(
(params: Parameters<typeof sendFeedback>[0]): Promise<void> =>
sendFeedback(params)
.then(() => {
setIsSent(true);
usedEmailRef.current = params.email;
})
.catch((resp) => {
if (!resp.errors) {
logger.warn('Error sending feedback', resp);
}
throw resp;
}),
[],
);
return isSent ? (
<SuccessContactFormPopup email={usedEmailRef.current} onClose={onClose} />
) : (
<ContactFormPopup initEmail={userEmail} onSubmit={onSubmit} onClose={onClose} />
);
};
export default ContactForm;