diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index 3d142ed4..0c0ed4cc 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -50,27 +50,32 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => { }; }, sessionOptions); -export default function Home() { - const [bio, setBio] = useState(""); - const [name, setName] = useState(""); - const [email, setEmail] = useState(""); +interface Props { + user: User; + mutateUser: Function, +} + +function UserProfile({ + user, + mutateUser, +}: Props) { + const [bio, setBio] = useState(user.bio || ''); + const [name, setName] = useState(user.name || ''); + const [email, setEmail] = useState(user.email || ''); const [password, setPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); - const [profilePicture, setProfilePicture] = useState(""); + const [profilePicture, setProfilePicture] = useState(user.profilePicture); - const [country, setCountry] = useState(); - const [phone, setPhone] = useState(); - const [gender, setGender] = useState(); - const [employment, setEmployment] = useState(); - const [position, setPosition] = useState(); - const [companyName, setCompanyName] = useState(""); - const [commercialRegistration, setCommercialRegistration] = useState(""); + const [country, setCountry] = useState(user.demographicInformation?.country || ''); + const [phone, setPhone] = useState(user.demographicInformation?.phone || ''); + const [gender, setGender] = useState(user.demographicInformation?.gender || undefined); + const [employment, setEmployment] = useState(user.type === "corporate" ? undefined : user.demographicInformation?.employment); + const [position, setPosition] = useState(user.type === "corporate" ? user.demographicInformation?.position : undefined); + const [companyName, setCompanyName] = useState(user.type === 'agent' ? user.agentInformation?.companyName : undefined); + const [commercialRegistration, setCommercialRegistration] = useState(user.type === 'agent' ? user.agentInformation?.commercialRegistration : undefined); const profilePictureInput = useRef(null); - - const {user, mutateUser} = useUser({redirectTo: "/login"}); - const expirationDateColor = (date: Date) => { const momentDate = moment(date); const today = moment(new Date()); @@ -80,24 +85,6 @@ export default function Home() { if (today.add(7, "days").isAfter(momentDate)) return "!bg-mti-orange-ultralight border-mti-orange-light"; }; - useEffect(() => { - if (user) { - setName(user.name); - setEmail(user.email); - setBio(user.bio); - setProfilePicture(user.profilePicture); - setCountry(user.demographicInformation?.country); - setPhone(user.demographicInformation?.phone); - setGender(user.demographicInformation?.gender); - setEmployment(user.type === "corporate" ? undefined : user.demographicInformation?.employment); - setPosition(user.type === "corporate" ? user.demographicInformation?.position : undefined); - if(user.type === 'agent') { - setCompanyName(user.agentInformation?.companyName) - setCommercialRegistration(user.agentInformation?.commercialRegistration) - } - } - }, [user]); - const convertBase64 = (file: File) => { return new Promise((resolve, reject) => { const fileReader = new FileReader(); @@ -159,6 +146,258 @@ export default function Home() { setIsLoading(false); }; + return ( + +
+

Edit Profile

+
+
+

Edit Profile

+
+
+ setName(e)} + placeholder="Enter your name" + defaultValue={name} + required + /> + setEmail(e)} + placeholder="Enter email address" + defaultValue={email} + required + /> +
+
+ setPassword(e)} + placeholder="Enter your password" + required + /> + setNewPassword(e)} + placeholder="Enter your new password (optional)" + /> +
+ + {user.type === "agent" && ( +
+ null} + placeholder="Enter corporate name" + defaultValue={companyName} + disabled + /> + null} + placeholder="Enter commercial registration" + defaultValue={commercialRegistration} + disabled + /> +
+ )} + +
+
+ + +
+ setPhone(e)} + placeholder="Enter phone number" + defaultValue={phone} + required + /> +
+
+ {user.type === "corporate" && ( + + )} + {user.type !== "corporate" && ( +
+ + + {EMPLOYMENT_STATUS.map(({status, label}) => ( + + {({checked}) => ( + + {label} + + )} + + ))} + +
+ )} +
+
+ + + + {({checked}) => ( + + Male + + )} + + + {({checked}) => ( + + Female + + )} + + + {({checked}) => ( + + Other + + )} + + +
+
+ + + {!user.subscriptionExpirationDate && "Unlimited"} + {user.subscriptionExpirationDate && moment(user.subscriptionExpirationDate).format("DD/MM/YYYY")} + +
+
+
+
+
+
+
(profilePictureInput.current as any)?.click()}> +
+
+ +
+ {user.name} +
+ + (profilePictureInput.current as any)?.click()} + className="cursor-pointer text-mti-purple-light text-sm"> + Change picture + +
{USER_TYPE_LABELS[user.type]}
+
+ {user.type === 'agent' && ( +
+ {user.demographicInformation?.country.toLowerCase() +
+ )} +
+
+
+ Bio +