mirror of
https://github.com/lemeow125/EquipmentTracker-Frontend.git
synced 2024-11-17 06:09:25 +08:00
Improved registration flow
This commit is contained in:
parent
ff1d465d3f
commit
8a2c702da3
2 changed files with 57 additions and 3 deletions
|
@ -5,6 +5,7 @@ import { ActivationType, LoginType, RegisterType } from "../Types/Types";
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: "http://localhost:8000/",
|
baseURL: "http://localhost:8000/",
|
||||||
});
|
});
|
||||||
|
|
||||||
// Token Handling
|
// Token Handling
|
||||||
export async function getAccessToken() {
|
export async function getAccessToken() {
|
||||||
const accessToken = await localStorage.getItem("access_token");
|
const accessToken = await localStorage.getItem("access_token");
|
||||||
|
@ -36,6 +37,25 @@ export async function GetConfig() {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ParseError(error: { response: { data: string } }) {
|
||||||
|
if (error.response && error.response.data) {
|
||||||
|
if (error.response.data.length > 50) {
|
||||||
|
return "Error truncated (too long)";
|
||||||
|
}
|
||||||
|
return JSON.stringify(error.response.data)
|
||||||
|
.replace(/[{}]/g, " ")
|
||||||
|
.replace(/\(/g, " ")
|
||||||
|
.replace(/\)/g, " ")
|
||||||
|
.replace(/"/g, " ")
|
||||||
|
.replace(/,/g, " ")
|
||||||
|
.replace(/\[/g, "")
|
||||||
|
.replace(/\]/g, "")
|
||||||
|
.replace(/\./g, "")
|
||||||
|
.replace(/non_field_errors/g, "")
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
return "Unable to reach server";
|
||||||
|
}
|
||||||
// User APIs
|
// User APIs
|
||||||
|
|
||||||
export function RegisterAPI(register: RegisterType) {
|
export function RegisterAPI(register: RegisterType) {
|
||||||
|
@ -47,7 +67,7 @@ export function RegisterAPI(register: RegisterType) {
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Registration failed");
|
console.log("Registration failed");
|
||||||
return [false, error.response];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { AppRegistration } from "@mui/icons-material";
|
||||||
import Button from "../Button/Button";
|
import Button from "../Button/Button";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { RegisterAPI } from "../API/API";
|
import { RegisterAPI } from "../API/API";
|
||||||
|
import { toast } from "react-toastify";
|
||||||
export default function RegisterModal() {
|
export default function RegisterModal() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [showPassword, setShowPassword] = useState(false);
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
|
@ -63,6 +64,16 @@ export default function RegisterModal() {
|
||||||
value={user.last_name}
|
value={user.last_name}
|
||||||
placeholder={"Enter your last name"}
|
placeholder={"Enter your last name"}
|
||||||
/>
|
/>
|
||||||
|
<TextField
|
||||||
|
id="outlined-helperText"
|
||||||
|
label="Email"
|
||||||
|
style={styles.input_form}
|
||||||
|
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
||||||
|
setUser({ ...user, email: e.target.value })
|
||||||
|
}
|
||||||
|
value={user.email}
|
||||||
|
placeholder={"Enter your email"}
|
||||||
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
id="outlined-helperText"
|
id="outlined-helperText"
|
||||||
label="Username"
|
label="Username"
|
||||||
|
@ -143,9 +154,32 @@ export default function RegisterModal() {
|
||||||
} else {
|
} else {
|
||||||
const status = await RegisterAPI(user);
|
const status = await RegisterAPI(user);
|
||||||
if (status[0]) {
|
if (status[0]) {
|
||||||
navigate("/");
|
setError(
|
||||||
|
"Registration successful. Please activate your account using the email provided"
|
||||||
|
);
|
||||||
|
toast("Registration successful", {
|
||||||
|
position: "top-right",
|
||||||
|
autoClose: 2000,
|
||||||
|
hideProgressBar: false,
|
||||||
|
closeOnClick: true,
|
||||||
|
pauseOnHover: true,
|
||||||
|
draggable: true,
|
||||||
|
progress: undefined,
|
||||||
|
theme: "light",
|
||||||
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
navigate(0);
|
||||||
|
}, 3000);
|
||||||
|
setUser({
|
||||||
|
first_name: "",
|
||||||
|
last_name: "",
|
||||||
|
username: "",
|
||||||
|
email: "",
|
||||||
|
password: "",
|
||||||
|
confirm_password: "",
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
setError(status[1]);
|
setError(JSON.stringify(status[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
Loading…
Reference in a new issue