Skip to content

Commit

Permalink
ui: Form improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Jun 18, 2024
1 parent d78fe1c commit db1b07c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/components/admin/cities/CreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Box, Button, Grid, Typography } from '@mui/material'

import { CityFormData, CityInput, CityResponse } from 'gql/cities'
import { routes } from 'common/routes'
import { ApiErrors, handleUniqueViolation } from 'service/apiErrors'
import { ApiErrors, Message, handleUniqueViolation } from 'service/apiErrors'
import { useCreateCity } from 'service/city'
import { AlertStore } from 'stores/AlertStore'
import GenericForm from 'components/common/form/GenericForm'
Expand Down Expand Up @@ -42,7 +42,9 @@ export default function EditForm() {
const error = e.response

if (error?.status === 409) {
const message = error.data.message.map((el) => handleUniqueViolation(el.constraints, t))
const message = (error.data.message as Message[]).map((el) =>
handleUniqueViolation(el.constraints, t),
)
return AlertStore.show(message.join('/n'), 'error')
}

Expand Down
18 changes: 13 additions & 5 deletions src/components/admin/marketing/EmailConsent/SendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FormDatePicker from 'components/common/form/FormDatePicker'
import FormTextField from 'components/common/form/FormTextField'
import GenericForm from 'components/common/form/GenericForm'
import SubmitButton from 'components/common/form/SubmitButton'
import { FormikHelpers } from 'formik'
import { SendNewsLetterConsent } from 'gql/marketing'
import { useTranslation } from 'next-i18next'
import Link from 'next/link'
Expand All @@ -22,7 +23,7 @@ export default function EditForm() {
const initialValues: SendNewsLetterConsent = {
templateId: '',
listId: '',
dateThreshold: undefined,
dateThreshold: new Date().toISOString(),
}

const validationSchema: yup.SchemaOf<SendNewsLetterConsent> = yup.object().defined().shape({
Expand All @@ -34,25 +35,32 @@ export default function EditForm() {
const mutationFn = useSendConsentEmail()

const handleError = (e: AxiosError<ApiErrors>) => {
AlertStore.show(t(e.message), 'error')
const error = e.response
AlertStore.show(t(error?.data.message as string), 'error')
}

const mutation = useMutation<AxiosResponse<any>, AxiosError<ApiErrors>, SendNewsLetterConsent>({
mutationFn,
onError: (error) => handleError(error),
onSuccess: (data) => {
AlertStore.show(t(`Емайлът беше изпратен успешно на ${data.data} емайла.`), 'success')
router.push(routes.admin.marketing.index)
},
})

async function onSubmit(values: SendNewsLetterConsent) {
async function onSubmit(
values: SendNewsLetterConsent,
formikHelpers: FormikHelpers<SendNewsLetterConsent>,
) {
const data: SendNewsLetterConsent = {
templateId: values.templateId,
listId: values.listId,
dateThreshold: values.dateThreshold,
}
mutation.mutate(data)
await mutation.mutateAsync(data)
if (mutation.isSuccess && !mutation.isLoading) {
console.log(mutation.isSuccess)
formikHelpers.resetForm({ values: initialValues })
}
}

return (
Expand Down
9 changes: 5 additions & 4 deletions src/components/admin/marketing/MarketingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Card, CardContent, Container, Grid, Typography } from '@mui/material'
import { Box, Button, Card, CardContent, Container, Grid, Typography } from '@mui/material'
import AdminContainer from 'components/common/navigation/AdminContainer'
import AdminLayout from 'components/common/navigation/AdminLayout'
import React from 'react'
Expand All @@ -15,9 +15,10 @@ export default function MarketingPage() {
<AdminContainer title={t('admin.marketing')}>
<Container maxWidth={false} sx={{ py: 5 }}>
<Grid container spacing={2} rowSpacing={4} px={4} pb={4} mb={2}>
{marketingCards.map(({ label, href, icon: Icon }, index) => (
{marketingCards.map(({ label, href, icon: Icon, disabled }, index) => (
<Grid xs={12} sm={6} md={4} lg={2.4} item key={index}>
<Card
<Button
disabled={disabled}
sx={{
height: 130,
maxWidth: 345,
Expand Down Expand Up @@ -47,7 +48,7 @@ export default function MarketingPage() {
</Typography>
</CardContent>
</Link>
</Card>
</Button>
</Grid>
))}
</Grid>
Expand Down
2 changes: 2 additions & 0 deletions src/components/admin/marketing/navigation/marketingCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export const marketingCards = [
label: 'Изпращане на емайл за съгласие',
icon: ThumbUpAltIcon,
href: routes.admin.marketing.newsLetterConsent,
disabled: false,
},
{
label: 'Изпращане на маркетинг емайл',
icon: SendIcon,
href: routes.admin.marketing.newsLetterConsent,
disabled: true,
},
]
2 changes: 1 addition & 1 deletion src/service/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { isAxiosError }

export interface ApiErrors {
statusCode: number
message: Message[]
message: Message[] | string
error: string
}

Expand Down

0 comments on commit db1b07c

Please sign in to comment.