source: sources/client/src/hooks/useUserPayForSession.js

Last change on this file was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 2 years ago

Push before video

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import { useState, useContext } from 'react';
2import axios from 'axios';
3import { AccessoriesContext } from '../context/AccessoriesContext';
4import { useHistory } from 'react-router-dom';
5import { UserContext } from '../context/UserContext';
6import useLogoutUser from './useLogoutUser';
7import { roles } from '../config/enums';
8
9const useUserPayForSession = () => {
10 const {user} = useContext(UserContext);
11 const {logoutUser} = useLogoutUser();
12 const [isLoading, setIsLoading] = useState(false);
13 const { setAlert } = useContext(AccessoriesContext);
14 const history = useHistory();
15 const userPayForSession = async ({ method, paymentCredentials }) => {
16 setIsLoading(true);
17 await axios
18 .put(
19 `/parkingSession/pay?expireDate=${paymentCredentials?.cardExpDate ?? ''}`,
20 {
21 method,
22 paymentCredentials,
23 }
24 )
25 .then((res) => {
26 setAlert({
27 type: 'success',
28 msg: `Успешно плаќање!`,
29 });
30 if(user.role === roles.guest) {
31 logoutUser();
32 } else {
33 history.push('/');
34 }
35 })
36 .catch((err) => {
37 // ALERT FOR ERROR WITH ERROR MSG
38 setAlert({
39 type: 'error',
40 msg: 'Проблеми со серверот!', // TODO change msg to err.message
41 });
42 setIsLoading(false);
43 });
44 };
45
46 return {
47 userPayForSession,
48 isLoading,
49 };
50};
51
52export default useUserPayForSession;
Note: See TracBrowser for help on using the repository browser.