Last change
on this file since bc20307 was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 3 years ago |
Push before video
|
-
Property mode
set to
100644
|
File size:
1.7 KB
|
Rev | Line | |
---|
[bc20307] | 1 | import { useState, useContext } from 'react';
|
---|
| 2 | import axios from 'axios';
|
---|
| 3 | import { AccessoriesContext } from '../context/AccessoriesContext';
|
---|
| 4 | import { useHistory } from 'react-router-dom';
|
---|
| 5 | import { UserContext } from '../context/UserContext';
|
---|
| 6 | import useLogoutUser from './useLogoutUser';
|
---|
| 7 | import { roles } from '../config/enums';
|
---|
| 8 |
|
---|
| 9 | const 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 |
|
---|
| 52 | export default useUserPayForSession;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.