chore: some refactor and cleaning

This commit is contained in:
2024-12-30 06:54:01 +01:00
parent 5c75e9390e
commit e39026c259
15 changed files with 5029 additions and 7233 deletions

View File

@@ -16,6 +16,8 @@ import {
UnsubscribeFormSchema,
UnsubscribeFormType
} from '@utils/validationSchemas';
import axios from 'axios';
import { useEffect, useRef, useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
@@ -52,29 +54,28 @@ const Unsubscribe = () => {
async function handleSubmit(values: UnsubscribeFormType) {
setIsLoading(true);
try {
const response = await fetch('/api/unsubscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
const { data } = await axios.post<ResponseType>(
'/api/unsubscribe',
{
email: values.email
})
});
},
{
headers: {
'Content-Type': 'application/json'
}
}
);
if (!response?.ok) {
throw new Error(`Invalid response: ${response.status}`);
if (!data.success) {
throw new Error(data.message);
}
const formResponse: ResponseType = await response.json();
if (!formResponse.success) {
throw Error(formResponse.message);
}
setMessage(formResponse.message);
setMessage(data.message);
setCompleted(true);
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Axios error:', error.response?.data || error.message);
}
setError(true);
} finally {
setIsLoading(false);