sozsoft-platform/ui/src/views/admin/notification/CreateNotification.tsx
2026-08-11 13:00:43 +03:00

104 lines
2.8 KiB
TypeScript

import {
Button,
Dialog,
FormContainer,
FormItem,
Input,
Notification,
toast,
} from '@/components/ui'
import { postMyNotificationByNotificationRuleId } from '@/services/notification.service'
import { useLocalization } from '@/utils/hooks/useLocalization'
import { Field, Form, Formik, FormikHelpers } from 'formik'
import * as Yup from 'yup'
interface CreateNotificationValues {
id: string
message: string
}
const scheme = Yup.object().shape({
id: Yup.string().required(),
message: Yup.string().required(),
})
function CreateNotification({
open,
onDialogClose,
id,
}: {
open: boolean
onDialogClose: () => void
id: string
}) {
const { translate } = useLocalization()
const handleSubmit = async (
values: CreateNotificationValues,
{ setSubmitting }: FormikHelpers<CreateNotificationValues>,
) => {
if (!id) {
return
}
try {
await postMyNotificationByNotificationRuleId({ id, message: values.message })
toast.push(
<Notification type="success" duration={2000}>
{translate('::App.Platform.Success')}
</Notification>,
{ placement: 'bottom-end' },
)
onDialogClose()
} catch {
toast.push(
<Notification type="danger" duration={2000}>
{translate('::App.Platform.Error')}
</Notification>,
{ placement: 'bottom-end' },
)
} finally {
setSubmitting(false)
}
}
return (
<Dialog isOpen={open} onClose={onDialogClose} onRequestClose={onDialogClose}>
<h5 className="mb-4">{id}</h5>
<Formik initialValues={{ id, message: '' }} validationSchema={scheme} onSubmit={handleSubmit}>
{({ touched, errors, isSubmitting }) => {
return (
<Form>
<FormContainer size="sm">
<FormItem
label="Message"
invalid={errors.message && touched.message}
errorMessage={errors.message}
>
<Field
textArea="true"
type="text"
autoComplete="off"
name="message"
component={Input}
/>
</FormItem>
<div className="mt-6 flex flex-row justify-end gap-3">
<Button size="sm" variant="solid" loading={isSubmitting} type="submit">
{isSubmitting ? translate('::Saving') : translate('::Save')}
</Button>
<Button size="sm" type="button" variant="plain" onClick={onDialogClose}>
{translate('::Cancel')}
</Button>
</div>
</FormContainer>
</Form>
)
}}
</Formik>
</Dialog>
)
}
export default CreateNotification