Skip to content

Commit

Permalink
fix: Build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Jun 19, 2024
1 parent db1b07c commit ef43c2b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Button, Grid, Typography } from '@mui/material'
import { Button, Grid, Typography } from '@mui/material'
import { useMutation } from '@tanstack/react-query'
import { AxiosError, AxiosResponse } from 'axios'
import { routes } from 'common/routes'
Expand All @@ -7,17 +7,15 @@ 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 { NewsLetterConsentResponse, SendNewsLetterConsent } from 'gql/marketing'
import { useTranslation } from 'next-i18next'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { ApiErrors } from 'service/apiErrors'
import { ApiError } from 'service/apiErrors'
import { useSendConsentEmail } from 'service/marketing'
import { AlertStore } from 'stores/AlertStore'
import * as yup from 'yup'

export default function EditForm() {
const router = useRouter()
export default function SendConsentEmailForm() {
const { t } = useTranslation('marketing')

const initialValues: SendNewsLetterConsent = {
Expand All @@ -34,16 +32,24 @@ export default function EditForm() {

const mutationFn = useSendConsentEmail()

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

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

Expand All @@ -58,7 +64,6 @@ export default function EditForm() {
}
await mutation.mutateAsync(data)
if (mutation.isSuccess && !mutation.isLoading) {
console.log(mutation.isSuccess)
formikHelpers.resetForm({ values: initialValues })
}
}
Expand Down Expand Up @@ -88,13 +93,13 @@ export default function EditForm() {
direction={'row'}
justifyContent={'space-between'}
alignItems={'center'}>
<Grid item xs={6}>
<Grid item xs={12} md={6}>
<Typography>Премахване от списък на потребители регистрирани след: </Typography>
</Grid>
<FormDatePicker name="dateThreshold" label="" />
</Grid>
<Grid item xs={12}>
<SubmitButton label="Изпрати" fullWidth loading={mutation.isLoading}></SubmitButton>
<SubmitButton label="Изпрати" fullWidth loading={mutation.isLoading} />
<Link href={routes.admin.marketing.index} passHref>
<Button fullWidth>{t('Откажи')}</Button>
</Link>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AdminContainer from 'components/common/navigation/AdminContainer'
import AdminLayout from 'components/common/navigation/AdminLayout'
import React from 'react'
import SendForm from './SendForm'
import SendEmailConsentForm from './SendEmailConsentForm'
import { useTranslation } from 'next-i18next'
import { Container } from '@mui/material'

Expand All @@ -11,7 +11,7 @@ export default function SendEmailConsentPage() {
<AdminLayout>
<AdminContainer title={t('admin.sendConsentEmail')}>
<Container maxWidth={'sm'} sx={{ py: 5 }}>
<SendForm />
<SendEmailConsentForm />
</Container>
</AdminContainer>
</AdminLayout>
Expand Down
3 changes: 1 addition & 2 deletions src/components/admin/marketing/MarketingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Box, Button, Card, CardContent, Container, Grid, Typography } from '@mui/material'
import { Box, Button, 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'
import { useTranslation } from 'next-i18next'
import { adminCards } from 'components/common/navigation/adminMenu'
import Link from 'next/link'
import { marketingCards } from './navigation/marketingCards'

Expand Down
4 changes: 4 additions & 0 deletions src/gql/marketing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export type SendMarketingEmail = {
export type SendNewsLetterConsent = SendMarketingEmail & {
dateThreshold?: string
}

export type NewsLetterConsentResponse = {
contactCount: number
}
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[] | string
message: Message[]
error: string
}

Expand Down
6 changes: 2 additions & 4 deletions src/service/marketing.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { SendNewsLetterConsent } from 'gql/marketing'
import { SendConfirmationEmailInput } from 'gql/notification'
import { NewsLetterConsentResponse, SendNewsLetterConsent } from 'gql/marketing'
import { useSession } from 'next-auth/react'
import { authConfig } from './restRequests'
import { endpoints } from './apiEndpoints'
import { apiClient } from './apiClient'
import { AxiosResponse } from 'axios'

export function useSendConsentEmail() {
const { data: session } = useSession()
return async (data: SendNewsLetterConsent) => {
return await apiClient.post<SendNewsLetterConsent, AxiosResponse<SendConfirmationEmailInput>>(
return await apiClient.post<NewsLetterConsentResponse>(
endpoints.notifications.sendNewsLetterConsentEmail.url,
data,
authConfig(session?.accessToken),
Expand Down

0 comments on commit ef43c2b

Please sign in to comment.