diff --git a/.env b/.env index 7d910f1..304644d 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -SKIP_PREFLIGHT_CHECK=true \ No newline at end of file +SKIP_PREFLIGHT_CHECK=true +API_URL_BASE=http://localhost:4000 \ No newline at end of file diff --git a/src/components/organisms/MainTopBar/MainTopBar.tsx b/src/components/organisms/MainTopBar/MainTopBar.tsx index 443dae0..86da8d4 100644 --- a/src/components/organisms/MainTopBar/MainTopBar.tsx +++ b/src/components/organisms/MainTopBar/MainTopBar.tsx @@ -18,7 +18,7 @@ const MainTopBar = ({ sortTerm }: { sortTerm: SortTermType }) => { const handleClickNewPatient = async () => { const patient = await dispatch(addNewPatient()); - navigate(`/patient/about/${patient.payload._id}`); + // navigate(`/patient/about/${patient.payload._id}`); }; const handleChange = (e: React.ChangeEvent) => { diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 01725d9..1af048f 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -33,7 +33,6 @@ export const AuthProvider = ({ children }: { children: ReactNode }): any => { useEffect(() => { const localStorageUser = localStorage.getItem('user'); if (!localStorageUser) return; - console.log(JSON.parse(localStorageUser)); axios.defaults.headers.common['Authorization'] = `Bearer ${JSON.parse(localStorageUser).token}`; setUser(JSON.parse(localStorageUser)); }, []); diff --git a/src/hooks/useAxiosInitialization.ts b/src/hooks/useAxiosInitialization.ts new file mode 100644 index 0000000..ee4935a --- /dev/null +++ b/src/hooks/useAxiosInitialization.ts @@ -0,0 +1,8 @@ +import axios from 'axios'; +import { useLayoutEffect } from 'react'; + +export const useAxiosInitialization = () => { + useLayoutEffect(() => { + axios.defaults.baseURL = process.env.API_URL_BASE; + }); +}; diff --git a/src/pages/Main.tsx b/src/pages/Main.tsx index fdb33ea..4ba37d6 100644 --- a/src/pages/Main.tsx +++ b/src/pages/Main.tsx @@ -26,7 +26,7 @@ const Main = () => { {patientsList.length > 0 && - patientsList.map((patient: PatientType, index: number) => )} + patientsList.map((patient: PatientType, index: number) => )} diff --git a/src/pages/Root.tsx b/src/pages/Root.tsx index 967d280..ff4835c 100644 --- a/src/pages/Root.tsx +++ b/src/pages/Root.tsx @@ -12,37 +12,39 @@ import { fetchPatients } from 'redux/patientsListSlice'; import { useAppDispatch } from 'redux/hooks'; import Login from './Login'; import Register from './Register'; +import { useAuthContext } from 'contexts/AuthContext'; +import { useAxiosInitialization } from 'hooks/useAxiosInitialization'; const Root = () => { + useAxiosInitialization(); const dispatch = useAppDispatch(); + const { user } = useAuthContext(); useEffect(() => { - dispatch(fetchPatients()); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + if (user) dispatch(fetchPatients()); + }, [dispatch, user]); return ( - {/* {user.login ? ( */} - - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - {/* } /> */} - - {/* ) : ( - <> - } /> - } /> - - )} */} + {user ? ( + + } /> + } /> + } /> + } /> + } /> + } /> + } /> + + ) : ( + <> + } /> + } /> + } /> + + )} ); diff --git a/src/redux/patientsListSlice.ts b/src/redux/patientsListSlice.ts index a00aa6b..1a473b5 100644 --- a/src/redux/patientsListSlice.ts +++ b/src/redux/patientsListSlice.ts @@ -7,7 +7,7 @@ const initialState: PatientType[] = []; export const fetchPatients = createAsyncThunk('/patients/getPatients', async () => { try { - const response = await axios.get('http://localhost:4000/api/patients'); + const response = await axios.get('/api/patients'); return response.data; } catch (err) { console.log(err); @@ -25,7 +25,7 @@ export const fetchSinglePatient = createAsyncThunk('patient/getPatient', async ( export const addNewPatient = createAsyncThunk('patients/addPatient', async (patient?: Partial) => { try { - const response = await axios.post('http://localhost:4000/api/patients', patient); + const response = await axios.post('/api/patients', patient); return response.data; } catch (err) { console.log(err); @@ -47,7 +47,7 @@ export const filterPatientsList = createAsyncThunk('patients/filterPatients', as sort: query.sortTerm, }); try { - const response = await axios.get(`http://localhost:4000/api/patients/search?${searchParams}`); + const response = await axios.get(`/api/patients/search?${searchParams}`); return response.data; } catch (err) { console.log(err);