diff --git a/src/client/routes/manage/CreateSponsor.tsx b/src/client/routes/manage/CreateSponsor.tsx index c91e876b..c6cfae6b 100644 --- a/src/client/routes/manage/CreateSponsor.tsx +++ b/src/client/routes/manage/CreateSponsor.tsx @@ -1,10 +1,10 @@ -import React, { useState } from 'react'; +import React, { useRef, useState } from 'react'; import styled from 'styled-components'; import * as EmailValidator from 'email-validator'; +import { toast } from 'react-toastify'; import { Button } from '../../components/Buttons/Button'; import { SearchBox } from '../../components/Input/SearchBox'; import FloatingPopup from '../../components/Containers/FloatingPopup'; -import { SmallCenteredText } from '../../components/Text/SmallCenteredText'; import { FlexRow } from '../../components/Containers/FlexContainers'; import { useCreateSponsorMutation, @@ -16,7 +16,6 @@ import { TiersDocument, } from '../../generated/graphql'; import { Spinner } from '../../components/Loading/Spinner'; -import STRINGS from '../../assets/strings.json'; const StyledSelect = styled.select` margin: 0.25rem 1rem 0.25rem 0rem; @@ -40,10 +39,69 @@ const StyledOption = styled.option` min-width: 10rem; `; +const showToast = (message: string, isError = false): void => { + toast.dismiss(); + (isError ? toast.error : toast.success)( +

+ {message} +

, + { + position: 'bottom-right', + } + ); +}; + +const CreateTier: React.FC = () => { + const [tierName, setTierName] = useState(''); + const [permissions, setPermissions] = useState(['']); + + const [createTier] = useCreateTierMutation({ + variables: { input: { name: tierName, permissions } }, + refetchQueries: [{ query: TiersDocument }], + awaitRefetchQueries: true, + }); + + const onCreateTier = async (): Promise => { + try { + createTier() + .then(() => showToast(`Tier ${tierName} created successfully!`)) + .catch(res => showToast(`Sorry. ${res.graphQLErrors[0].message} Please try again :)`, true)) + .finally(() => { + setTierName(''); + setPermissions(['']); + }); + } catch (err) { + console.error(err); + } + }; + + return ( + <> + + setTierName(e.target.value)} + minWidth="15em" + /> + setPermissions(e.target.value.split(','))} + minWidth="15em" + /> + + + + ); +}; + const CreateCompany: React.FunctionComponent = (): JSX.Element => { const [companyName, setCompanyName] = useState(''); const [tierId, setTierId] = useState(''); - const [createCompanyMsg, setCreateCompanyMsg] = useState(''); + const selectTierRef = useRef(null); const [createCompany] = useCreateCompanyMutation({ variables: { input: { name: companyName, tierId } }, @@ -56,12 +114,17 @@ const CreateCompany: React.FunctionComponent = (): JSX.Element => { const onCreateCompany = async (): Promise => { try { - createCompany().catch(res => { - setCreateCompanyMsg(`Sorry. ${res.graphQLErrors[0].message} Please try again :-)`); - }); + createCompany() + .then(() => showToast(`Company ${companyName} created successfully!`)) + .catch(res => showToast(`Sorry. ${res.graphQLErrors[0].message} Please try again :)`, true)) + .finally(() => { + setCompanyName(''); + if (selectTierRef && selectTierRef.current) { + selectTierRef.current.value = ''; + } + }); } catch (err) { console.error(err); - setCreateCompanyMsg(STRINGS.GRAPHQL_ORGANIZER_ERROR_MESSAGE); } }; @@ -78,7 +141,7 @@ const CreateCompany: React.FunctionComponent = (): JSX.Element => { {loading || !data ? ( ) : ( - setTierId(e.target.value)}> + setTierId(e.target.value)} ref={selectTierRef}> Select Tier @@ -89,59 +152,8 @@ const CreateCompany: React.FunctionComponent = (): JSX.Element => { ))} )} - + - - {createCompanyMsg} - - - ); -}; - -const CreateTier: React.FC = () => { - const [tierName, setTierName] = useState(''); - const [permissions, setPermissions] = useState(['']); - const [createTierMsg, setCreateTierMsg] = useState(''); - - const [createTier] = useCreateTierMutation({ - variables: { input: { name: tierName, permissions } }, - refetchQueries: [{ query: TiersDocument }], - awaitRefetchQueries: true, - }); - - const onCreateTier = async (): Promise => { - try { - createTier().catch(res => { - setCreateTierMsg(`Sorry. ${res.graphQLErrors[0].message} Please try again :-)`); - }); - } catch (err) { - console.error(err); - setCreateTierMsg(STRINGS.GRAPHQL_ORGANIZER_ERROR_MESSAGE); - } - }; - - return ( - <> - - setTierName(e.target.value)} - minWidth="15em" - /> - setPermissions(e.target.value.split(','))} - minWidth="15em" - /> - - - - {createTierMsg} - ); }; @@ -150,31 +162,35 @@ const CreateSponsor: React.FunctionComponent = (): JSX.Element => { const [sponsorEmail, setSponsorEmail] = useState(''); const [sponsorName, setSponsorName] = useState(''); const [companyId, setCompanyId] = useState(''); - const [createSponsorMsg, setCreateSponsorMsg] = useState(''); const { loading, data } = useCompaniesQuery(); + const selectCompanyRef = useRef(null); const [createSponsor] = useCreateSponsorMutation({ variables: { input: { companyId, email: sponsorEmail, name: sponsorName } }, }); const onCreateSponsorEmail = async (): Promise => { - // validate the email entered - if (EmailValidator.validate(sponsorEmail)) { try { - console.log(sponsorEmail); - console.log(sponsorName); - createSponsor().catch(res => { - setCreateSponsorMsg(`Sorry. ${res.graphQLErrors[0].message} Please try again :-)`); - }); - // create sponsor in the database - // already created or not + createSponsor() + .then(() => + showToast(`Email ${sponsorEmail} for sponsor ${sponsorName} created successfully!`) + ) + .catch(res => + showToast(`Sorry. ${res.graphQLErrors[0].message} Please try again :)`, true) + ) + .finally(() => { + setSponsorEmail(''); + setSponsorName(''); + if (selectCompanyRef && selectCompanyRef.current) { + selectCompanyRef.current.value = ''; + } + }); } catch (err) { console.error(err); - setCreateSponsorMsg(STRINGS.GRAPHQL_ORGANIZER_ERROR_MESSAGE); } } else { - setCreateSponsorMsg(`Email '${sponsorEmail}' is not valid when creating sponsor`); + showToast(`Email ${sponsorEmail} is not valid for creating a sponsor`, true); } }; @@ -198,7 +214,7 @@ const CreateSponsor: React.FunctionComponent = (): JSX.Element => { {loading || !data ? ( ) : ( - setCompanyId(e.target.value)}> + setCompanyId(e.target.value)} ref={selectCompanyRef}> Select Company @@ -209,11 +225,8 @@ const CreateSponsor: React.FunctionComponent = (): JSX.Element => { ))} )} - + - - {createSponsorMsg} - ); };