forked from Groway-Studio/python-course-landing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseUserInfo.ts
More file actions
129 lines (108 loc) · 3.38 KB
/
useUserInfo.ts
File metadata and controls
129 lines (108 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { State, TopLevel } from "../interfaces";
import {
keystore,
validPaths,
getParameterByName,
validateEmail,
} from "../utils";
import { API } from "../api";
const useUserInfo = () => {
const [state, setState] = useState<State>({
firstName: "",
lastName: "",
email: "",
phoneNumber: "",
});
const [loading, setLoading] = useState<boolean>(false);
const [response, setResponse] = useState<TopLevel>();
const [error, setError] = useState<string | null>(null);
const [code, setCode] = useState<string>('');
const { firstName, lastName, email, phoneNumber } = state;
const pathnameOrigin: string = window.location.origin;
const navigate = useNavigate();
const handleInputChange = ({
target,
}: React.ChangeEvent<HTMLInputElement>): void => {
setState((state) => ({ ...state, [target.name]: target.value }));
};
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
formValidations();
window.dataLayer.push({
event: "checkout_initiated",
});
if (
Object.values(state).every((item) => item.trim() !== "") &&
!isNaN(+phoneNumber) &&
phoneNumber.trim().length >= 9
) {
setLoading(true);
try {
let checkoutUrl = `${API}/checkout.py`;
if (process.env.NODE_ENV === "development") {
checkoutUrl += "?sandbox=true";
} else {
checkoutUrl += "?sandbox=false";
}
const response = await fetch(checkoutUrl, {
method: "POST",
body: JSON.stringify({
title: "Private Sale",
success_url: `${pathnameOrigin}${"/#"}${validPaths.success}`,
pending_url: `${pathnameOrigin}${"/#"}${validPaths.pending}`,
failure_url: `${pathnameOrigin}${"/#"}${validPaths.failed}`,
user_first_name: firstName,
user_last_name: lastName,
user_email: email,
user_phone: `+51${phoneNumber}`,
invitation_code: getParameterByName("invitation_code"),
price: 29.0,
}),
});
const data = await response.json();
setResponse(data);
const user = {
user_first_name: firstName,
user_last_name: lastName,
user_email: email,
user_phone: `+51${phoneNumber}`,
invitation_code: getParameterByName("invitation_code"),
};
localStorage.setItem(keystore.USER_DATA, JSON.stringify(user));
setTimeout(() => {
setLoading(false);
}, 1000);
} catch (error: any) {
setLoading(false);
navigate("/server-error");
throw new Error(error);
}
}
};
const formValidations = () => {
if (!firstName.trim()) setError("¡Nombre no válido!");
else if (!lastName.trim()) setError("¡Apellido no válido!");
else if (!validateEmail(email)) setError("¡Correo electrónico no válido!");
else if (!phoneNumber.match(/^\d+$/) || phoneNumber.trim().length < 9)
setError("¡Número de teléfono no válido!");
setTimeout(() => {
setError(null);
}, 3000);
};
return {
firstName,
lastName,
email,
phoneNumber,
loading,
response,
handleInputChange,
handleSubmit,
error,
code,
setCode,
};
};
export default useUserInfo;