-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.jsx
137 lines (123 loc) · 3.96 KB
/
page.jsx
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
130
131
132
133
134
135
136
137
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import styles from "./registerPage.module.css";
import imageCompression from "browser-image-compression";
import axios from "axios";
import Image from 'next/image'
const RegisterPage = () => {
const router = useRouter();
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [image, setImage] = useState(null);
async function handleOnChange(changeEvent) {
// compress image
const compressedFile = await imageCompression(changeEvent.target.files[0], {
maxSizeMB: 1,
maxWidthOrHeight: 150,
useWebWorker: true,
});
const reader = new FileReader();
reader.onload = function (onLoadEvent) {
setImage(onLoadEvent.target.result);
};
reader.readAsDataURL(compressedFile);
console.log(` Image Size: ${compressedFile.size / 1024 / 1024} MB`);
setImage(compressedFile);
}
const handleSubmit = async (e) => {
e.preventDefault();
const formData = new FormData();
try {
// send image to cloudinary and get url
const imgUrlResponse = await axios
.post(
"https://api.cloudinary.com/v1_1/fullstacklifeblog/image/upload/",
{
file: image,
upload_preset: "fullstacklifeblog",
tags: "profile",
}
)
.then((res) => res.data.secure_url);
// console.log(JSON.stringify({
// "username": username,
// "password": password,
// "email": email,
// "image": imgUrlResponse,
// }));
// send post data to server
const response = await fetch("/api/v1/auth/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
"username": username,
"password": password,
"email": email,
"image": imgUrlResponse,
}),
});
const data = await response.json();
if (response.status === 201) {
router.push("/#");
}
} catch (error) {
console.error("Error registering:", error);
}
};
return (
<div className={styles.container}>
<div className={styles.wrapper}>
<h1 className={styles.title}>Register</h1>
<div className={styles.loginFormContainer}>
<form className={styles.loginForm} onSubmit={handleSubmit}>
<input
type="text"
name="username"
placeholder="Username"
className={styles.input}
onChange={(e) => setUsername(e.target.value)}
/>
<input
type="email"
name="email"
placeholder="Email"
className={styles.input}
onChange={(e) => setEmail(e.target.value)}
/>
<input
type="password"
name="password"
placeholder="Password"
className={styles.input}
onChange={(e) => setPassword(e.target.value)}
/>
<input
type="file"
accept="image/*"
onChange={handleOnChange}
className={styles.imageUploadBtn}
/>
{image && (
<Image
id="output"
src={image}
width={100}
height={100}
style={{ borderRadius: "50%", objectFit: "cover"}}
alt="cover image"
/>
)}
<button type="submit" className={styles.submitButton}>
Register
</button>
</form>
</div>
</div>
</div>
);
};
export default RegisterPage;