Skip to content

Commit

Permalink
Remove dispatch catch useless blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Jud committed May 17, 2024
1 parent 751ba66 commit e5e0f69
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 79 deletions.
26 changes: 6 additions & 20 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useLocation } from 'react-router';
import NavigationDrawer from './components/menu/NavigationDrawer';
import { ThemeContext } from './contexts/ThemeContextProvider';
import { auth } from './firebase/Firebase';
import useNotification from './hooks/useNotification';
import { useAppDispatch, useAppSelector } from './store/hooks';
import { setUser } from './store/slices/authSlice';
import { getAbsences } from './store/thunks/absencesThunks';
Expand All @@ -17,8 +16,6 @@ import AnalyticsTracker from './utils/AnalyticsTracker';
import { darkThemeOptions, lightThemeOptions } from './utils/MyThemeOptions';

const App = () => {
const notifyContext = useNotification().notifyContext;

const dispatch = useAppDispatch();
const user = useAppSelector((state) => state.auth.user);

Expand All @@ -41,24 +38,13 @@ const App = () => {
useEffect(() => {
if (user && user.uid) {
// Check if user is logged in
dispatch(loadProfile(undefined))
.catch((error) => {
console.error('Error loading profile: ', error);
notifyContext.addNotification('Error loading profile: ' + error, 'error');
})
.then(() => {
// Load times from firestore
dispatch(getTimes(undefined)).catch((error) => {
console.error('Error loading data: ', error);
notifyContext.addNotification('Error loading data: ' + error, 'error');
});
dispatch(loadProfile(undefined)).then(() => {
// Load times from firestore
dispatch(getTimes(undefined));

// Load absences from firestore
dispatch(getAbsences(undefined)).catch((error) => {
console.error('Error loading absences: ', error);
notifyContext.addNotification('Error loading absences: ' + error, 'error');
});
});
// Load absences from firestore
dispatch(getAbsences(undefined));
});
}
}, [user]);

Expand Down
7 changes: 1 addition & 6 deletions src/components/UnautheticatedHomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import GoogleIcon from '@mui/icons-material/Google';
import { Box, Button, Link, Stack, Typography } from '@mui/material';
import useNotification from '../hooks/useNotification';
import { useAppDispatch } from '../store/hooks';
import { googleSignIn } from '../store/thunks/authThunks';
import { ReactComponent as HomeSvg } from '../svg/home.svg';
import Item from './common/Item';

const UnautheticatedHomePage = () => {
const dispatch = useAppDispatch();
const { notifyContext } = useNotification();

/*
Handle Google Signup with Firebase Auth
*/
const handleGoogleSignup = () => {
dispatch(googleSignIn(undefined)).catch((error) => {
console.error(error);
notifyContext.addNotification(error, 'error');
});
dispatch(googleSignIn(undefined));
};

return (
Expand Down
6 changes: 2 additions & 4 deletions src/components/absences/AbsencesCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ const AbsencesCard = ({ days }: AbsencesCardProperties) => {

// Remove an absence from the list
const remove = (absence: IAbsence) => {
dispatch(deleteAbsence(absence)).catch((error) => {
console.error(error);
notifyContext.addNotification(error, 'error');
});
dispatch(deleteAbsence(absence));
notifyContext.addNotification('Abwesenheit wurde gelöscht', 'success');

setAbsences((prevState) => {
if (prevState) {
Expand Down
5 changes: 2 additions & 3 deletions src/components/absences/NewAbsence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Button, Card, CardContent, FormControl, InputLabel, MenuItem, Select, S
import { Dayjs } from 'dayjs';
import locale from 'dayjs/locale/de';
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router';
import useNotification from '../../hooks/useNotification';
import { IAbsence, absenceTypes } from '../../interfaces/Types';
import { useAppDispatch, useAppSelector } from '../../store/hooks';
Expand All @@ -16,7 +15,7 @@ const NewAbsence = () => {
const [absenceType, setAbsenceType] = useState<string | null>(null);
const [absences, setAbsences] = useState<IAbsence[]>([]);
const { notifyContext } = useNotification();
const navigate = useNavigate();

const dispatch = useAppDispatch();

// The DateRangePicker component is used to select a range of dates.
Expand All @@ -42,8 +41,8 @@ const NewAbsence = () => {
} else {
if (absences.length > 0) {
dispatch(addAbsences(absences));
notifyContext.addNotification('Abwesenheit wurde gespeichert', 'success');
}
navigate('/absences/view');
}
};

Expand Down
19 changes: 0 additions & 19 deletions src/components/common/ProtectedRoute.tsx

This file was deleted.

6 changes: 2 additions & 4 deletions src/components/settings/TabMyDataCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ const TabMyDataCard = () => {
if (availableTimePerDay) newProfile.availabletime = TimeUtils.minutesFromTime(availableTimePerDay);
if (numWorkday) newProfile.numWorkday = TimeUtils.numWorkdays(wd);

dispatch(updateProfile(newProfile)).catch((error) => {
console.error(error);
notifyContext.addNotification(error.message, 'error');
});
dispatch(updateProfile(newProfile));
notifyContext.addNotification('Profil wurde gespeichert', 'success');
};

return (
Expand Down
6 changes: 2 additions & 4 deletions src/components/settings/TabMyProfileCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ const TabMyProfileCard = () => {
Handle Delete Account
*/
const deleteAccount = () => {
dispatch(deleteMyUser(undefined)).catch((error) => {
console.error(error);
notifyContext.addNotification(error, 'error');
});
dispatch(deleteMyUser(undefined));
notifyContext.addNotification('Dein Account wurde gelöscht', 'success');
};

return (
Expand Down
10 changes: 8 additions & 2 deletions src/components/time/TimeEditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import BeachAccessIcon from '@mui/icons-material/BeachAccess';
import { Box, Button, Card, CardContent, IconButton, Stack, TextField, Typography } from '@mui/material';
import { useEffect, useState } from 'react';
import useNotification from '../../hooks/useNotification';
import { ITime, IWorkingdays } from '../../interfaces/Types';
import { useAppDispatch, useAppSelector } from '../../store/hooks';
import { addTimes, deleteTime } from '../../store/thunks/timesThunks';
Expand All @@ -20,6 +21,7 @@ const TimeEditPage = ({ editDate, handleClose }: TimeEditPageProps) => {
const times = useAppSelector((state) => state.times.times);

const dispatch = useAppDispatch();
const { notifyContext } = useNotification();

const day = editDate;

Expand Down Expand Up @@ -77,6 +79,7 @@ const TimeEditPage = ({ editDate, handleClose }: TimeEditPageProps) => {
const save = () => {
if (time) {
dispatch(addTimes([time]));
notifyContext.addNotification('Eintrag wurde gespeichert', 'success');
handleClose();
}
};
Expand All @@ -85,8 +88,11 @@ const TimeEditPage = ({ editDate, handleClose }: TimeEditPageProps) => {
If the user has entered all the data and clicks on the delete button, the data is deleted from the Firestore DB.
*/
const deleteThisItem = () => {
if (time) dispatch(deleteTime(DayjsUtils.formatStringAsDay(day)));
handleClose;
if (time) {
dispatch(deleteTime(DayjsUtils.formatStringAsDay(day)));
notifyContext.addNotification('Eintrag wurde gelöscht', 'info');
handleClose();
}
};

return (
Expand Down
6 changes: 2 additions & 4 deletions src/components/time/TimeInputPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ const TimeInputPage = () => {
At the end only one batch is sent and its success or failure is reported.
*/
const save = () => {
dispatch(addTimes(myTimes)).catch((error) => {
console.error(error);
notifyContext.addNotification(error, 'error');
});
dispatch(addTimes(myTimes));
notifyContext.addNotification('Zeiten wurden gespeichert', 'success');
};

/*
Expand Down
21 changes: 8 additions & 13 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
import Notification from './components/common/Notification';
import { NotificationContextProvider } from './contexts/NotificationContextProvider';
Expand All @@ -11,18 +10,14 @@ import { store } from './store/store';

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<>
<BrowserRouter>
<ThemeContextProvider>
<NotificationContextProvider>
<Provider store={store}>
<App />
</Provider>
<Notification />
</NotificationContextProvider>
</ThemeContextProvider>
</BrowserRouter>
</>
<ThemeContextProvider>
<NotificationContextProvider>
<Provider store={store}>
<App />
</Provider>
<Notification />
</NotificationContextProvider>
</ThemeContextProvider>
);

serviceWorkerRegistration.register();
1 change: 1 addition & 0 deletions src/store/thunks/authThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const deleteMyUser = createAsyncThunk('auth/delete', async (action: any,
deleteUser(user).catch((error) => {
console.error(error);
});
logout(undefined);
}
});

Expand Down

0 comments on commit e5e0f69

Please sign in to comment.