generic return type
This commit is contained in:
1 parent
7c2f789a31
commit
a8f111b399
2 files changed
+7
-8
No files matched your search
@@ -34,19 +34,16 @@ export const useGet = <T,>(
|
||||
};
|
||||
};
|
||||
|
||||
export const usePost = <B = any,>(endpoint: string) => {
|
||||
export const usePost = <B = any, R = any>(endpoint: string) => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const postFn = async (
|
||||
body: B,
|
||||
config?: AxiosRequestConfig
|
||||
): Promise<ApiResponse | null> => {
|
||||
const postFn = async (body: B, config?: AxiosRequestConfig): Promise<R | null> => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const res = await post<ApiResponse>(endpoint, body, config);
|
||||
const res = await post<R>(endpoint, body, config);
|
||||
return res.data;
|
||||
} catch (err: any) {
|
||||
const errMsg = err?.response?.data?.msg || err.message || "An error occurred";
|
||||
|
||||
@@ -8,6 +8,8 @@ import { TextInput } from "@/Components/Inputs/TextInput/indexV2.tsx";
|
||||
import Button from "@mui/material/Button";
|
||||
import Stack from "@mui/material/Stack";
|
||||
import { usePost } from "@/Hooks/v2/UseApi";
|
||||
import type { ApiResponse } from "@/Hooks/v2/UseApi";
|
||||
|
||||
const schema = z.object({
|
||||
email: z.email("Invalid email address"),
|
||||
password: z.string().min(6, "Password must be at least 6 characters"),
|
||||
@@ -18,14 +20,14 @@ type FormData = z.infer<typeof schema>;
|
||||
const Login = () => {
|
||||
const { t } = useTranslation();
|
||||
const theme = useTheme();
|
||||
const { post, loading, error } = usePost<FormData>("/auth/login");
|
||||
const { post, loading, error } = usePost<FormData, ApiResponse>("/auth/login");
|
||||
|
||||
const {
|
||||
handleSubmit,
|
||||
control,
|
||||
formState: { errors },
|
||||
} = useForm<FormData>({
|
||||
resolver: zodResolver(schema), // ⬅️ connect Zod
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: {
|
||||
email: "",
|
||||
password: "",
|
||||
|
||||
Reference in new issue
Block a user