-
Notifications
You must be signed in to change notification settings - Fork 0
/
componentsFunctions.tsx
88 lines (84 loc) · 2.79 KB
/
componentsFunctions.tsx
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
import { NavigateFunction } from "react-router-dom";
import api from "./api";
import { fetchBuildings } from "./reducers/buildings";
import { userPreference } from "./reducers/preference";
import { login } from "./reducers/user";
import { AppDispatch } from "./store";
import { message } from "antd";
export const signUpButton = (setError: (arg: Array<any>) => void, setSwapPanel: (arg: boolean) => void) => {
setError([])
setSwapPanel(true);
};
export const signInButton = (setError: (arg: Array<any>) => void, setSwapPanel: (arg: boolean) => void) => {
setError([])
setSwapPanel(false);
};
export const handleLoginSubmit = (email: string, password: string, setError: (arg: Array<any>) => void, navigate: NavigateFunction, dispatch: AppDispatch) => {
if (!email || !password) {
setError(["Fill the form to continue"])
return
}
const data = { email, password }
api.user
.login(data)
.then((user) => {
localStorage.setItem("token", user.token)
api.preference.fetchPreference(user._id).then(async (res) => {
dispatch(userPreference(res))
if (res.activityLog) await api.activity.updateActivity(user._id)
})
if (user.type !== "Vendor")
api.buildings.fetchBuildingsByUserId(user._id).then((res) => {
dispatch(fetchBuildings(res))
})
dispatch(login(user))
navigate("/Dashboard")
})
.catch((err) => {
message.error("Error! " + err.message)
});
}
export const handleSignUpSubmit = async (
event: any,
name: string,
surname: string,
password: string,
passwordConf: string,
email: string,
type: string,
company: string,
setError: (arg: Array<any>) => void,
dispatch: AppDispatch
) => {
if (!name || !surname || !password || !passwordConf || !email || event == null) {
setError(["Fill the form to continue"])
return
}
if (password !== passwordConf) {
setError(["Typed Password are different"])
return
}
const data = {
type,
email,
password,
name,
surname,
}
await api.user
.signUp(data)
.then(async (data) => {
if (type === "Vendor")
await api.organization.create({ userId: data._id, name: company })
await api.preference.createPreference(data._id).then((res) => {
dispatch(userPreference(res))
dispatch(login(data))
localStorage.setItem("token", data.token)
})
api.activity.updateActivity(data._id)
})
.catch((err) => {
return new Error(err?.response?.data?.errors?.email);
});
event.preventDefault();
}