|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import Button from './Buttons'; |
| 4 | + |
| 5 | +interface DropOrUndropUserProps { |
| 6 | + subject: string; |
| 7 | + title: string; |
| 8 | + drop?: boolean; |
| 9 | + loading: boolean; |
| 10 | + setRemovalReason?: React.Dispatch<React.SetStateAction<string>>; |
| 11 | + onSubmit: (data: React.MouseEvent<HTMLButtonElement>) => void; |
| 12 | + onClose: () => void; |
| 13 | +} |
| 14 | + |
| 15 | +function DropOrUndropUser({ |
| 16 | + subject, |
| 17 | + title, |
| 18 | + drop, |
| 19 | + loading, |
| 20 | + setRemovalReason, |
| 21 | + onSubmit, |
| 22 | + onClose, |
| 23 | +}: DropOrUndropUserProps) { |
| 24 | + const { t } = useTranslation(); |
| 25 | + const [reason, setReason] = useState(''); |
| 26 | + |
| 27 | + const handleConfirm = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 28 | + e.stopPropagation(); |
| 29 | + if (onSubmit) onSubmit(e); |
| 30 | + }; |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="h-screen w-screen bg-black bg-opacity-30 backdrop-blur-sm fixed top-0 left-0 z-20 flex items-center justify-center px-4"> |
| 34 | + <div className="w-full p-4 pb-8 bg-white rounded-lg dark:bg-dark-bg sm:w-3/4 xl:w-4/12"> |
| 35 | + <div className="flex flex-wrap items-center justify-center w-full card-title "> |
| 36 | + <h3 className="w-11/12 text-sm font-bold text-center dark:text-white "> |
| 37 | + {t(subject)} |
| 38 | + </h3> |
| 39 | + <hr className="w-full my-3 border-b bg-primary" /> |
| 40 | + </div> |
| 41 | + <div className="card-body w-full"> |
| 42 | + <form className="px-8 py-3 w-full"> |
| 43 | + <div className="flex flex-wrap items-center justify-center w-full card-title "> |
| 44 | + <h3 className="w-11/12 text-sm font-bold text-center dark:text-white "> |
| 45 | + {t(title)} |
| 46 | + </h3> |
| 47 | + </div> |
| 48 | + {/* Reason input field */} |
| 49 | + {drop && ( |
| 50 | + <div className="mt-4"> |
| 51 | + <input |
| 52 | + type="text" |
| 53 | + className="mt-1 p-2 block w-full border rounded-md shadow-sm focus:ring focus:ring-opacity-50 focus:ring-primary dark:bg-dark-bg dark:border-gray-600 dark:text-white" |
| 54 | + placeholder={t('Enter reason')} |
| 55 | + value={reason} |
| 56 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => |
| 57 | + setReason(e.target.value) |
| 58 | + } |
| 59 | + id="removalReason" |
| 60 | + /> |
| 61 | + <p |
| 62 | + id="errorMessage" |
| 63 | + className="text-red-500 text-xs mt-1 hidden" |
| 64 | + > |
| 65 | + Reason is required! |
| 66 | + </p> |
| 67 | + </div> |
| 68 | + )} |
| 69 | + |
| 70 | + <div className="flex justify-between w-full pt-3"> |
| 71 | + <Button |
| 72 | + data-testid="removeModel2" |
| 73 | + variant="info" |
| 74 | + size="sm" |
| 75 | + style="w-[8rem] h-[2.3rem] text-sm p-0 mx-0 flex justify-center items-center" |
| 76 | + onClick={() => { |
| 77 | + if (drop) { |
| 78 | + setReason(''); |
| 79 | + document |
| 80 | + .getElementById('errorMessage')! |
| 81 | + .classList.add('hidden'); |
| 82 | + } |
| 83 | + onClose(); |
| 84 | + }} |
| 85 | + > |
| 86 | + {t('Cancel')} |
| 87 | + </Button> |
| 88 | + <Button |
| 89 | + variant="primary" |
| 90 | + size="sm" |
| 91 | + style="w-[8rem] h-[2.3rem] text-sm p-0 mx-0 flex justify-center items-center" |
| 92 | + onClick={() => { |
| 93 | + if (!reason.trim() && drop) { |
| 94 | + document |
| 95 | + .getElementById('errorMessage')! |
| 96 | + .classList.remove('hidden'); |
| 97 | + } else { |
| 98 | + if (drop && setRemovalReason) { |
| 99 | + setRemovalReason(reason); |
| 100 | + } |
| 101 | + handleConfirm( |
| 102 | + new MouseEvent( |
| 103 | + 'click', |
| 104 | + ) as unknown as React.MouseEvent<HTMLButtonElement>, |
| 105 | + ); |
| 106 | + } |
| 107 | + }} |
| 108 | + loading={loading} |
| 109 | + > |
| 110 | + {loading ? t('Loading') : t('Proceed')} |
| 111 | + </Button> |
| 112 | + </div> |
| 113 | + </form> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +export default DropOrUndropUser; |
0 commit comments