Skip to content

Commit 93de729

Browse files
committed
fix: Use LoadingButtons instead of whole loader when possible
1 parent e31af74 commit 93de729

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

client/src/components/forms/UserForm.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import UserRoutes from '../../api/UserRoutes';
99
import { useAlert } from '../../hooks/useAlert';
1010
import { useAuth } from '../../hooks/useAuth';
1111
import useForm from '../../hooks/useForm';
12-
import { useLoader } from '../../hooks/useLoader';
1312
import catchError from '../../utils/catchError';
1413

1514
interface Data {
@@ -32,12 +31,12 @@ interface Props {
3231
*/
3332
const UserForm = ({ data }: Props) => {
3433
const Alert = useAlert();
35-
const Loader = useLoader();
3634
const { t } = useTranslation('user');
3735
const navigate = useNavigate();
3836
const { user, updateData } = useAuth();
3937

40-
const { register, handleSubmit, formState: { isSubmitting }, reset } = useForm<Data>('user', {
38+
const [loading, setLoading] = React.useState(false);
39+
const { register, handleSubmit, reset } = useForm<Data>('user', {
4140
defaultValues: data,
4241
});
4342

@@ -57,7 +56,7 @@ const UserForm = ({ data }: Props) => {
5756
phone: formData.phone,
5857
};
5958

60-
Loader.open();
59+
setLoading(true);
6160
if (formData.id) { // Edition
6261
if (formData.password.length) {
6362
await UserRoutes.changePassword(formData.id, formData.password);
@@ -83,7 +82,7 @@ const UserForm = ({ data }: Props) => {
8382

8483
Alert.open('success', t('common:saved'));
8584
}).catch(catchError(Alert));
86-
Loader.close();
85+
setLoading(false);
8786
} else { // Addition
8887
await UserRoutes.insert({
8988
...processedData,
@@ -97,7 +96,7 @@ const UserForm = ({ data }: Props) => {
9796
navigate('/app/admin/user/list');
9897
reset();
9998
}).catch(catchError(Alert));
100-
Loader.close();
99+
setLoading(false);
101100
}
102101
};
103102

@@ -147,7 +146,7 @@ const UserForm = ({ data }: Props) => {
147146
>
148147
<LoadingButton
149148
color="primary"
150-
loading={isSubmitting}
149+
loading={loading}
151150
type="submit"
152151
variant="contained"
153152
>

client/src/views/account/AccountView/ProfileDetails.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import UserRoutes, { UserWithPerson } from '../../../api/UserRoutes';
99
import { useAlert } from '../../../hooks/useAlert';
1010
import { useAuth } from '../../../hooks/useAuth';
1111
import useForm from '../../../hooks/useForm';
12-
import { useLoader } from '../../../hooks/useLoader';
1312
import catchError from '../../../utils/catchError';
1413

1514
type FormData = {
@@ -25,10 +24,10 @@ type FormData = {
2524
const ProfileDetails = ({ ...rest }) => {
2625
const auth = useAuth();
2726
const Alert = useAlert();
28-
const Loader = useLoader();
2927
const { t } = useTranslation('user');
3028

31-
const { register, handleSubmit, formState: { isSubmitting }, control } = useForm<FormData>('user', {
29+
const [loading, setLoading] = React.useState(false);
30+
const { register, handleSubmit, control } = useForm<FormData>('user', {
3231
defaultValues: {
3332
firstName: auth.user.person.firstName,
3433
lastName: auth.user.person.lastName,
@@ -55,20 +54,20 @@ const ProfileDetails = ({ ...rest }) => {
5554

5655
if (data.password) {
5756
if (data.passwordConfirm) {
58-
Loader.open();
57+
setLoading(true);
5958
await UserRoutes.changePassword(auth.user.id, data.password).catch(catchError(Alert));
6059
}
6160
}
6261

63-
Loader.open();
62+
setLoading(true);
6463
await UserRoutes.update(auth.user.id, {
6564
lang: data.lang,
6665
person: { update: processedData }
6766
}, { person: true }).then((newData) => {
6867
auth.updateData(newData as UserWithPerson);
6968
Alert.open('success', t('common:saved'));
7069
}).catch(catchError(Alert));
71-
Loader.close();
70+
setLoading(false);
7271
};
7372

7473
return (
@@ -122,7 +121,7 @@ const ProfileDetails = ({ ...rest }) => {
122121
>
123122
<LoadingButton
124123
color="primary"
125-
loading={isSubmitting}
124+
loading={loading}
126125
type="submit"
127126
variant="contained"
128127
>

client/src/views/auth/LoginView.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Text from '../../components/Text';
1111
import { useAlert } from '../../hooks/useAlert';
1212
import { useAuth } from '../../hooks/useAuth';
1313
import useForm from '../../hooks/useForm';
14-
import { useLoader } from '../../hooks/useLoader';
1514
import catchError from '../../utils/catchError';
1615
import { ErrorType } from '../../utils/fetcher';
1716

@@ -28,23 +27,24 @@ const LoginView = () => {
2827
const navigate = useNavigate();
2928
const auth = useAuth();
3029
const Alert = useAlert();
31-
const Loader = useLoader();
3230
const { t } = useTranslation('login');
3331

34-
const { register, handleSubmit, formState: { isSubmitting }, control, setValue, setFocus } = useForm<FormData>('user');
32+
const [loading, setLoading] = useState(false);
33+
const [resetLoading, setResetLoading] = useState(false);
34+
const { register, handleSubmit, control, setValue, setFocus } = useForm<FormData>('user');
3535
const login = useWatch({ name: 'login', control });
3636

3737
/**
3838
* Login handler
3939
*/
4040
const onSubmit = (formData: FormData) => {
41-
Loader.open();
41+
setLoading(true);
4242
auth.signin(formData.login, formData.password).then(() => {
43-
Loader.close();
43+
setLoading(false);
4444
navigate('/app/home', { replace: true });
4545
}).catch((response: string) => {
4646
catchError(Alert)(response);
47-
Loader.close();
47+
setLoading(false);
4848
});
4949
};
5050

@@ -53,40 +53,39 @@ const LoginView = () => {
5353
const user = localStorage.getItem('user');
5454
const token = localStorage.getItem('token') || '';
5555
if (!auth.authed && user) {
56-
Loader.open();
56+
setLoading(true);
5757
auth.signin(user, token).catch((error: ErrorType) => {
5858
localStorage.removeItem('user');
5959
localStorage.removeItem('token');
6060
navigate('/login', { replace: true });
6161
catchError(Alert)(error);
6262
}).finally(() => {
63-
Loader.close();
63+
setLoading(false);
6464
});
6565
}
6666
if (auth.authed) {
6767
navigate('/app/home', { replace: true });
6868
}
69-
}, [Alert, Loader, auth, navigate, t]);
69+
}, [Alert, auth, navigate, t]);
7070

7171
// Password reset
7272
const resetPassword = useCallback(async () => {
7373
if (!login) {
7474
Alert.open('error', t('pleaseEnterLogin'));
7575
return;
7676
}
77-
Loader.open();
77+
setLoading(true);
7878
await UserRoutes.sendPasswordResetMail(login).then(() => {
7979
Alert.open('success', t('passwordResetMailSent'));
8080
}).catch(catchError(Alert));
81-
Loader.close();
82-
}, [Alert, Loader, login, t]);
81+
setLoading(false);
82+
}, [Alert, login, t]);
8383

8484
// Password reset form
8585
const [resetDialogOpen, setResetDialogOpen] = useState(false);
8686
const {
8787
register: resetRegister,
8888
handleSubmit: resetHandleSubmit,
89-
formState: { isSubmitting: resetIsSubmitting },
9089
control: resetControl,
9190
reset: resetForm,
9291
} = useForm<ResetFormData>('user');
@@ -104,9 +103,11 @@ const LoginView = () => {
104103
const url = new URL(window.location.href);
105104
const code = url.searchParams.get('reset');
106105

106+
setResetLoading(true);
107107
UserRoutes.resetPassword(login, code || '', formData.password).then(() => {
108108
Alert.open('success', t('passwordResetSuccess'));
109109
}).catch(catchError(Alert)).finally(() => {
110+
setResetLoading(false);
110111
setResetDialogOpen(false);
111112
resetForm();
112113
});
@@ -175,7 +176,7 @@ const LoginView = () => {
175176
<Box my={2}>
176177
<LoadingButton
177178
color="primary"
178-
loading={isSubmitting}
179+
loading={loading}
179180
fullWidth
180181
size="large"
181182
type="submit"
@@ -210,7 +211,7 @@ const LoginView = () => {
210211
<Button onClick={closeResetDialog}>{t('common:cancel')}</Button>
211212
<LoadingButton
212213
color="primary"
213-
loading={resetIsSubmitting}
214+
loading={resetLoading}
214215
type="submit"
215216
variant="contained"
216217
>

0 commit comments

Comments
 (0)