|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { useAuth } from "../lib/auth"; |
| 3 | +import { getCurrentUser, searchUsers, signOut } from "../lib/connection"; |
| 4 | +import Image from "next/image"; |
| 5 | +import { useRouter } from "next/dist/client/router"; |
| 6 | +import { supabase } from "../lib/supabase"; |
| 7 | +import toast from "react-hot-toast"; |
| 8 | + |
| 9 | +export default function EditUser() { |
| 10 | + const [thisUser, setThisUser] = useState(null); |
| 11 | + const { currentUser } = useAuth(); |
| 12 | + const router = useRouter(); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + if (currentUser) { |
| 16 | + searchUsers(currentUser.email).then((res) => { |
| 17 | + if (res == -1) { |
| 18 | + router.push("/onboarding"); |
| 19 | + } else { |
| 20 | + getCurrentUser(currentUser.email).then((user) => { |
| 21 | + setThisUser(user); |
| 22 | + }); |
| 23 | + } |
| 24 | + }); |
| 25 | + } else { |
| 26 | + router.push("/signup"); |
| 27 | + } |
| 28 | + console.log(currentUser); |
| 29 | + }, [currentUser, router]); |
| 30 | + |
| 31 | + const [userObject, setUserObject] = useState({ |
| 32 | + fullName: "", |
| 33 | + tags: "", |
| 34 | + username: "", |
| 35 | + }); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + if (thisUser) { |
| 39 | + setUserObject({ |
| 40 | + fullName: thisUser.fullName, |
| 41 | + tags: thisUser.tags.join(","), |
| 42 | + username: thisUser.username, |
| 43 | + }); |
| 44 | + } |
| 45 | + }, [thisUser]); |
| 46 | + |
| 47 | + const handleChange = (e) => { |
| 48 | + setUserObject({ ...userObject, [e.target.name]: e.target.value }); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleSubmit = async (e) => { |
| 52 | + e.preventDefault(); |
| 53 | + let tagsToBeSent = []; |
| 54 | + console.log(userObject); |
| 55 | + tagsToBeSent = userObject.tags.split(","); |
| 56 | + try { |
| 57 | + let res = await supabase |
| 58 | + .from("users") |
| 59 | + .update({ |
| 60 | + fullName: userObject.fullName, |
| 61 | + tags: tagsToBeSent, |
| 62 | + username: userObject.username, |
| 63 | + }) |
| 64 | + .match({ email: currentUser.email }); |
| 65 | + console.log(res); |
| 66 | + |
| 67 | + if (res.error) { |
| 68 | + throw res.error; |
| 69 | + } |
| 70 | + toast.success("User Updated"); |
| 71 | + router.push("/profile"); |
| 72 | + } catch (error) { |
| 73 | + console.log(error); |
| 74 | + if (error.code == 23505) { |
| 75 | + toast.error("Username already taken"); |
| 76 | + } else { |
| 77 | + toast.error("Something went wrong, check your details"); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <div className="flex justify-center"> |
| 84 | + <div className="mt-40 w-5/12 flex flex-col justify-center p-10 bg-gradient-to-br from-primary-start to-primary-end rounded-lg shadow-lg"> |
| 85 | + {thisUser ? ( |
| 86 | + <> |
| 87 | + <Image |
| 88 | + className="pr-20" |
| 89 | + src={ |
| 90 | + "https://avatars.dicebear.com/api/miniavs/" + |
| 91 | + userObject.username + |
| 92 | + ".svg" |
| 93 | + } |
| 94 | + alt="Avatar" |
| 95 | + height="120" |
| 96 | + width="120" |
| 97 | + /> |
| 98 | + <form |
| 99 | + className="text-left flex flex-col items-center mt-10 mx-20" |
| 100 | + onSubmit={handleSubmit} |
| 101 | + > |
| 102 | + <div className="my-5 flex flex-row justify-between w-full"> |
| 103 | + <label className="text-black text-2xl font-thin mr-10"> |
| 104 | + Full Name (real or Fake) |
| 105 | + </label> |
| 106 | + <input |
| 107 | + className="rounded-lg p-2 focus:outline-none" |
| 108 | + name="fullName" |
| 109 | + value={userObject.fullName} |
| 110 | + onChange={handleChange} |
| 111 | + required |
| 112 | + placeholder="Full Name" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + <div className="my-5 flex flex-row justify-between w-full"> |
| 116 | + <label className="text-black text-2xl font-thin mr-10"> |
| 117 | + Your tags (separated by commas) |
| 118 | + </label> |
| 119 | + <input |
| 120 | + className="rounded-lg p-2 focus:outline-none" |
| 121 | + name="tags" |
| 122 | + value={userObject.tags} |
| 123 | + onChange={handleChange} |
| 124 | + required |
| 125 | + placeholder="Enter , separated values" |
| 126 | + /> |
| 127 | + </div> |
| 128 | + <div className="my-5 flex flex-row justify-between w-full"> |
| 129 | + <label className="text-black text-2xl font-thin mr-10"> |
| 130 | + Username (does <b>COOL</b> things to your dp) |
| 131 | + </label> |
| 132 | + <input |
| 133 | + className="rounded-lg p-2 focus:outline-none" |
| 134 | + name="username" |
| 135 | + value={userObject.username} |
| 136 | + onChange={handleChange} |
| 137 | + required |
| 138 | + placeholder="Username" |
| 139 | + /> |
| 140 | + </div> |
| 141 | + <button |
| 142 | + type="submit" |
| 143 | + className=" w-5/12 my-10 mx-5 bg-gradient-to-tr from-secondary-start to-secondary-end shadow-xl hover:bg-opacity-70 text-white font-bold font-mono py-2 px-10 rounded-lg" |
| 144 | + > |
| 145 | + Update |
| 146 | + </button> |
| 147 | + <button |
| 148 | + type="button" |
| 149 | + className=" w-5/12 mx-5 bg-gradient-to-tr from-danger-start to-danger-end shadow-xl hover:bg-opacity-70 text-white font-bold font-mono py-2 px-10 rounded-lg" |
| 150 | + onClick={() => router.push("/profile")} |
| 151 | + > |
| 152 | + Cancel |
| 153 | + </button> |
| 154 | + </form> |
| 155 | + </> |
| 156 | + ) : null} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + ); |
| 160 | +} |
0 commit comments