Skip to content

Commit

Permalink
adjust routes
Browse files Browse the repository at this point in the history
lukbar48 committed Oct 14, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 358282b commit dc5bd95
Showing 7 changed files with 38 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
SKIP_PREFLIGHT_CHECK=true
SKIP_PREFLIGHT_CHECK=true
API_URL_BASE=http://localhost:4000
2 changes: 1 addition & 1 deletion src/components/organisms/MainTopBar/MainTopBar.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLInputElement>) => {
1 change: 0 additions & 1 deletion src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -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));
}, []);
8 changes: 8 additions & 0 deletions src/hooks/useAxiosInitialization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import axios from 'axios';
import { useLayoutEffect } from 'react';

export const useAxiosInitialization = () => {
useLayoutEffect(() => {
axios.defaults.baseURL = process.env.API_URL_BASE;
});
};
2 changes: 1 addition & 1 deletion src/pages/Main.tsx
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ const Main = () => {
<MainTopBar sortTerm={sortTerm} />
<Wrapper>
{patientsList.length > 0 &&
patientsList.map((patient: PatientType, index: number) => <MainPatientInfo index={index} key={patient._id} {...patient} />)}
patientsList.map((patient: PatientType, index: number) => <MainPatientInfo index={index} key={patient?._id} {...patient} />)}
</Wrapper>
<MainBottomBar setSortTerm={setSortTerm} />
</>
44 changes: 23 additions & 21 deletions src/pages/Root.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Router basename={process.env.PUBLIC_URL}>
<GlobalStyle />
<Routes>
{/* {user.login ? ( */}
<Wrapper>
<Route path="/register" element={<Register />} />
<Route path="/login" element={<Login />} />
<Route path="/patient/appointments/:id" element={<Appointments />} />
<Route path="/patient/blood-tests/:id" element={<BloodTests />} />
<Route path="/patient/allergens/:id" element={<Allergens />} />
<Route path="/patient/diet/:id" element={<Diet />} />
<Route path="/patient/about/:id" element={<About />} />
<Route path="/" element={<Main />} />
{/* <Route path="*" element={<Navigate to="/" />} /> */}
</Wrapper>
{/* ) : (
<>
<Route path="*" element={<Navigate to="/login" />} />
<Route path="/login" element={<Login />} />
</>
)} */}
{user ? (
<Wrapper>
<Route path="/patient/appointments/:id" element={<Appointments />} />
<Route path="/patient/blood-tests/:id" element={<BloodTests />} />
<Route path="/patient/allergens/:id" element={<Allergens />} />
<Route path="/patient/diet/:id" element={<Diet />} />
<Route path="/patient/about/:id" element={<About />} />
<Route path="/" element={<Main />} />
<Route path="*" element={<Navigate to="/" />} />
</Wrapper>
) : (
<>
<Route path="*" element={<Navigate to="/login" />} />
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
</>
)}
</Routes>
</Router>
);
6 changes: 3 additions & 3 deletions src/redux/patientsListSlice.ts
Original file line number Diff line number Diff line change
@@ -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<PatientType>) => {
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);

0 comments on commit dc5bd95

Please sign in to comment.