mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 18:29:15 +03:00
refactor: refactored fetch course hook
This commit is contained in:
parent
bf55565eed
commit
aa5bf76cc7
@ -18,6 +18,10 @@ import { observer } from 'mobx-react';
|
|||||||
import { useRootStore } from '@educt/hooks/useRootStore';
|
import { useRootStore } from '@educt/hooks/useRootStore';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import UserBadge from '@educt/components/UserBadge';
|
import UserBadge from '@educt/components/UserBadge';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks
|
||||||
|
*/
|
||||||
import useLogoutQuery from '@educt/hooks/useLogoutQuery';
|
import useLogoutQuery from '@educt/hooks/useLogoutQuery';
|
||||||
|
|
||||||
const UserInfo: React.FC = () => {
|
const UserInfo: React.FC = () => {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import useIsMountedRef from './useIsMountedRef';
|
import useIsMountedRef from './useIsMountedRef';
|
||||||
|
|
||||||
type StateType<DataResultT> = {
|
type StateType<DataResultT> = {
|
||||||
|
|||||||
@ -2,56 +2,38 @@
|
|||||||
* Types
|
* Types
|
||||||
*/
|
*/
|
||||||
import { ICourse } from '@educt/interfaces';
|
import { ICourse } from '@educt/interfaces';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hooks
|
* Hooks
|
||||||
*/
|
*/
|
||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { useErrorHandler } from 'react-error-boundary';
|
import { useErrorHandler } from 'react-error-boundary';
|
||||||
import useIsMountedRef from './useIsMountedRef';
|
import useAsync from './useAsync';
|
||||||
import { useRootStore } from './useRootStore';
|
import { useRootStore } from './useRootStore';
|
||||||
|
|
||||||
type CourseStateType = {
|
type QueryResponseDataType = Omit<ICourse, 'students_count' | 'likes_count' | 'lessons_count'> | undefined;
|
||||||
data: Omit<ICourse, 'students_count' | 'likes_count' | 'lessons_count'> | null;
|
|
||||||
error: any;
|
|
||||||
loading: boolean;
|
|
||||||
fetched: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
const useFetchCourseQuery = (id: string) => {
|
const useFetchCourseQuery = (id: string) => {
|
||||||
const {
|
const {
|
||||||
courseStore: { courseService },
|
courseStore: { courseService },
|
||||||
} = useRootStore();
|
} = useRootStore();
|
||||||
const [state, setState] = useState<CourseStateType>({ data: null, error: null, loading: false, fetched: false });
|
|
||||||
const isMountedRef = useIsMountedRef();
|
|
||||||
const handleError = useErrorHandler();
|
const handleError = useErrorHandler();
|
||||||
|
|
||||||
/**
|
const fetch = async (id: string) => {
|
||||||
* Fetch course handler
|
try {
|
||||||
*/
|
const result = await courseService.fetchById(id);
|
||||||
|
return result.data;
|
||||||
|
} catch (error: any) {
|
||||||
|
if (!error.response) {
|
||||||
|
handleError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const { execute: fetchCourseById, ...state } = useAsync<QueryResponseDataType, Parameters<typeof fetch>>(fetch);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setState(s => ({ ...s, loading: true }));
|
fetchCourseById(id);
|
||||||
courseService
|
|
||||||
.fetchById(id)
|
|
||||||
.then(data => {
|
|
||||||
if (isMountedRef.current) {
|
|
||||||
setState(s => ({ ...s, data: data.data }));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
if (error.response) {
|
|
||||||
if (isMountedRef.current) {
|
|
||||||
setState(s => ({ ...s, error }));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handleError(error);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
if (isMountedRef.current) {
|
|
||||||
setState(s => ({ ...s, loading: false, fetched: true }));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
return { ...state };
|
return { ...state };
|
||||||
|
|||||||
@ -18,7 +18,7 @@ const useLoginQuery = () => {
|
|||||||
isClosable: true,
|
isClosable: true,
|
||||||
status: 'success',
|
status: 'success',
|
||||||
});
|
});
|
||||||
return result;
|
return result.data;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
/**
|
/**
|
||||||
@ -41,7 +41,7 @@ const useLoginQuery = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const { execute, ...state } = useAsync<IApiRespose<IToken> | undefined, Parameters<typeof login>>(login);
|
const { execute, ...state } = useAsync<IToken | undefined, Parameters<typeof login>>(login);
|
||||||
|
|
||||||
return { login: execute, ...state };
|
return { login: execute, ...state };
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,49 +1,28 @@
|
|||||||
import { useState } from 'react';
|
|
||||||
import { useToast } from '@chakra-ui/react';
|
import { useToast } from '@chakra-ui/react';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
import { useRootStore } from './useRootStore';
|
import { useRootStore } from './useRootStore';
|
||||||
import { IApiRespose } from '@educt/interfaces';
|
|
||||||
import { useErrorHandler } from 'react-error-boundary';
|
import { useErrorHandler } from 'react-error-boundary';
|
||||||
import useIsMountedRef from './useIsMountedRef';
|
import useAsync from './useAsync';
|
||||||
|
|
||||||
type LogoutStateType = {
|
|
||||||
result: IApiRespose<{}> | null;
|
|
||||||
error: any;
|
|
||||||
loading: boolean;
|
|
||||||
fetched: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
const useLogoutQuery = () => {
|
const useLogoutQuery = () => {
|
||||||
const { authStore } = useRootStore();
|
const { authStore } = useRootStore();
|
||||||
const [state, setState] = useState<LogoutStateType>({ result: null, error: null, loading: false, fetched: false });
|
|
||||||
const isMountedRef = useIsMountedRef();
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const handleError = useErrorHandler();
|
const handleError = useErrorHandler();
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
setState(s => ({ ...s, loading: true }));
|
|
||||||
try {
|
try {
|
||||||
const data = await authStore.logout();
|
await authStore.logout();
|
||||||
history.push('/auth');
|
history.push('/auth');
|
||||||
|
|
||||||
toast({ title: 'You are logged out.', isClosable: true, status: 'info' });
|
toast({ title: 'You are logged out.', isClosable: true, status: 'info' });
|
||||||
if (isMountedRef.current) {
|
|
||||||
setState(s => ({ ...s, result: data }));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (isMountedRef.current) {
|
|
||||||
setState(s => ({ ...s, error }));
|
|
||||||
}
|
|
||||||
handleError(error);
|
handleError(error);
|
||||||
} finally {
|
|
||||||
if (isMountedRef.current) {
|
|
||||||
setState(s => ({ ...s, loading: false, fetched: true }));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return { ...state, logout };
|
const { execute, ...state } = useAsync<void, Parameters<typeof logout>>(logout);
|
||||||
|
|
||||||
|
return { logout: execute, ...state };
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useLogoutQuery;
|
export default useLogoutQuery;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import Helmet from 'react-helmet';
|
import Helmet from 'react-helmet';
|
||||||
import { Redirect } from 'react-router-dom';
|
import { Redirect } from 'react-router-dom';
|
||||||
import { Box, Heading, Text, Tab, TabList, Flex, Tabs, TabPanels, TabPanel } from '@chakra-ui/react';
|
import { Box, Heading, Text, Tab, TabList, Flex, Tabs, TabPanels, TabPanel } from '@chakra-ui/react';
|
||||||
@ -27,7 +27,7 @@ import useFetchCourseQuery from '@educt/hooks/useFetchCourseQuery';
|
|||||||
*/
|
*/
|
||||||
const EditCoursePage: React.FC<IPageProps> = () => {
|
const EditCoursePage: React.FC<IPageProps> = () => {
|
||||||
const params = useParams<{ id: string }>();
|
const params = useParams<{ id: string }>();
|
||||||
const { data: course, loading, error } = useFetchCourseQuery(params.id);
|
const { data: course, error, isLoading } = useFetchCourseQuery(params.id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Not Found
|
* Not Found
|
||||||
@ -37,7 +37,7 @@ const EditCoursePage: React.FC<IPageProps> = () => {
|
|||||||
/**
|
/**
|
||||||
* Loading
|
* Loading
|
||||||
*/
|
*/
|
||||||
if (loading || course === null) return <LoadingPage />;
|
if (isLoading || !course) return <LoadingPage />;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Box, Heading, Breadcrumb, BreadcrumbItem, BreadcrumbLink, Grid } from '@chakra-ui/react';
|
import { Box, Heading } from '@chakra-ui/react';
|
||||||
|
|
||||||
import { IPageProps } from '@educt/interfaces';
|
import { IPageProps } from '@educt/interfaces';
|
||||||
import { observer } from 'mobx-react';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main page
|
* Main page
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user