|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useGetPartnersQuery, useUpdatePartnerActiveMutation } from '@/lib/api'; |
| 4 | +import { |
| 5 | + UPDATE_PARTNER_ACTIVE_ERROR, |
| 6 | + UPDATE_PARTNER_ACTIVE_REQUEST, |
| 7 | + UPDATE_PARTNER_ACTIVE_SUCCESS, |
| 8 | +} from '@/lib/constants/events'; |
| 9 | +import { useTypedSelector } from '@/lib/hooks/store'; |
| 10 | +import { getErrorMessage } from '@/lib/utils/errorMessage'; |
| 11 | +import logEvent from '@/lib/utils/logEvent'; |
| 12 | +import LoadingButton from '@mui/lab/LoadingButton'; |
| 13 | +import { Box, Button, MenuItem, TextField, Typography } from '@mui/material'; |
| 14 | +import { useRollbar } from '@rollbar/react'; |
| 15 | +import { useTranslations } from 'next-intl'; |
| 16 | +import * as React from 'react'; |
| 17 | +import { useState } from 'react'; |
| 18 | + |
| 19 | +const UpdatePartnerActiveForm = () => { |
| 20 | + const partners = useTypedSelector((state) => state.partners); |
| 21 | + const rollbar = useRollbar(); |
| 22 | + |
| 23 | + useGetPartnersQuery(undefined); |
| 24 | + |
| 25 | + const t = useTranslations('Admin.updatePartner'); |
| 26 | + |
| 27 | + const [loading, setLoading] = useState<boolean>(false); |
| 28 | + const [selectedPartner, setSelectedPartner] = useState<string>(''); |
| 29 | + const [selectedPartnerName, setSelectedPartnerName] = useState<string>(''); |
| 30 | + const [isActive, setIsActive] = useState<boolean | null>(null); |
| 31 | + |
| 32 | + const [updatePartnerActive] = useUpdatePartnerActiveMutation(); |
| 33 | + const [formError, setFormError] = useState< |
| 34 | + string | React.ReactNode[] | React.ReactElement<any, string | React.JSXElementConstructor<any>> |
| 35 | + >(); |
| 36 | + const [isSuccess, setIsSuccess] = useState<boolean>(false); |
| 37 | + |
| 38 | + const selectPartner = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 39 | + setLoading(true); |
| 40 | + setSelectedPartner(event.target.value); |
| 41 | + const partner = partners.find((partner) => partner.id === event.target.value); |
| 42 | + if (partner) { |
| 43 | + setIsActive(partner.isActive); |
| 44 | + setSelectedPartnerName(partner.name); |
| 45 | + } |
| 46 | + setIsSuccess(false); |
| 47 | + setLoading(false); |
| 48 | + }; |
| 49 | + |
| 50 | + const submitHandler = async (event: React.FormEvent<HTMLFormElement>) => { |
| 51 | + event.preventDefault(); |
| 52 | + setLoading(true); |
| 53 | + |
| 54 | + logEvent(UPDATE_PARTNER_ACTIVE_REQUEST, { active: !isActive }); |
| 55 | + |
| 56 | + const partnerResponse = await updatePartnerActive({ |
| 57 | + id: selectedPartner, |
| 58 | + active: !isActive as boolean, |
| 59 | + }); |
| 60 | + |
| 61 | + if (partnerResponse.data) { |
| 62 | + logEvent(UPDATE_PARTNER_ACTIVE_SUCCESS, { active: !isActive }); |
| 63 | + } |
| 64 | + |
| 65 | + if (partnerResponse.error) { |
| 66 | + const error = partnerResponse.error; |
| 67 | + const errorMessage = getErrorMessage(error); |
| 68 | + |
| 69 | + logEvent(UPDATE_PARTNER_ACTIVE_ERROR, { |
| 70 | + active: !isActive, |
| 71 | + error: errorMessage, |
| 72 | + }); |
| 73 | + rollbar.error(t('error') + errorMessage); |
| 74 | + |
| 75 | + setFormError(t('error') + errorMessage); |
| 76 | + setLoading(false); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + setLoading(false); |
| 81 | + setIsSuccess(true); |
| 82 | + }; |
| 83 | + |
| 84 | + const resetForm = (event: React.MouseEvent<HTMLButtonElement>) => { |
| 85 | + event.preventDefault(); |
| 86 | + setIsSuccess(false); |
| 87 | + setSelectedPartner(''); |
| 88 | + setIsActive(null); |
| 89 | + }; |
| 90 | + |
| 91 | + const FormResetButton = () => ( |
| 92 | + <Box> |
| 93 | + <Button sx={{ mt: 3 }} variant="contained" color="secondary" onClick={resetForm}> |
| 94 | + {t('reset')} |
| 95 | + </Button> |
| 96 | + </Box> |
| 97 | + ); |
| 98 | + |
| 99 | + const FormSuccess = () => ( |
| 100 | + <Box> |
| 101 | + <Typography fontWeight={500} mb={1}> |
| 102 | + {t.rich('successDescription', { |
| 103 | + status: isActive ? 'inactive' : 'active', |
| 104 | + partner: selectedPartnerName || 'partner', |
| 105 | + })} |
| 106 | + </Typography> |
| 107 | + <Typography> |
| 108 | + {t.rich(isActive ? 'inactiveSuccess' : 'activeSuccess', { partner: selectedPartnerName })} |
| 109 | + </Typography> |
| 110 | + <FormResetButton /> |
| 111 | + </Box> |
| 112 | + ); |
| 113 | + |
| 114 | + if (isSuccess) { |
| 115 | + return <FormSuccess />; |
| 116 | + } |
| 117 | + |
| 118 | + return ( |
| 119 | + <form autoComplete="off" onSubmit={submitHandler}> |
| 120 | + <TextField |
| 121 | + id="selectPartner" |
| 122 | + name="selectPartner" |
| 123 | + key="select-partner" |
| 124 | + fullWidth |
| 125 | + select |
| 126 | + label={t('partnerNameLabel')} |
| 127 | + onChange={selectPartner} |
| 128 | + variant="standard" |
| 129 | + required |
| 130 | + value={selectedPartner} |
| 131 | + > |
| 132 | + {partners.map((option, index) => ( |
| 133 | + <MenuItem key={`partner-name-${index}`} value={option.id}> |
| 134 | + {option.name} |
| 135 | + </MenuItem> |
| 136 | + ))} |
| 137 | + </TextField> |
| 138 | + |
| 139 | + {formError && <Typography color="error.main">{formError}</Typography>} |
| 140 | + |
| 141 | + {selectedPartner && ( |
| 142 | + <Typography> |
| 143 | + {t.rich(isActive ? 'activeDescription' : 'inactiveDescription', { |
| 144 | + partner: selectedPartnerName, |
| 145 | + })} |
| 146 | + </Typography> |
| 147 | + )} |
| 148 | + {selectedPartner && isActive !== null && ( |
| 149 | + <LoadingButton |
| 150 | + variant="contained" |
| 151 | + color={isActive ? 'error' : 'secondary'} |
| 152 | + type="submit" |
| 153 | + loading={loading} |
| 154 | + sx={{ mt: 2 }} |
| 155 | + > |
| 156 | + {t.rich(isActive ? 'activeLabel' : 'inactiveLabel', { partner: selectedPartnerName })} |
| 157 | + </LoadingButton> |
| 158 | + )} |
| 159 | + </form> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +export default UpdatePartnerActiveForm; |
0 commit comments