Made it so that students, connected to a corporate, if they change their e-mail, they get unassigned

This commit is contained in:
Tiago Ribeiro
2024-01-02 11:07:18 +00:00
parent 1c2c3fe402
commit d2276eba1d
4 changed files with 70 additions and 40 deletions

View File

@@ -19,6 +19,8 @@ import {shouldRedirectHome} from "@/utils/navigation.disabled";
import moment from "moment";
import {BsCamera, BsCameraFill} from "react-icons/bs";
import {USER_TYPE_LABELS} from "@/resources/user";
import useGroups from "@/hooks/useGroups";
import useUsers from "@/hooks/useUsers";
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
const user = req.session.user;
@@ -52,28 +54,32 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => {
interface Props {
user: User;
mutateUser: Function,
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 || '');
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(user.profilePicture);
const [country, setCountry] = useState<string>(user.demographicInformation?.country || '');
const [phone, setPhone] = useState<string>(user.demographicInformation?.phone || '');
const [country, setCountry] = useState<string>(user.demographicInformation?.country || "");
const [phone, setPhone] = useState<string>(user.demographicInformation?.phone || "");
const [gender, setGender] = useState<Gender | undefined>(user.demographicInformation?.gender || undefined);
const [employment, setEmployment] = useState<EmploymentStatus | undefined>(user.type === "corporate" ? undefined : user.demographicInformation?.employment);
const [employment, setEmployment] = useState<EmploymentStatus | undefined>(
user.type === "corporate" ? undefined : user.demographicInformation?.employment,
);
const [position, setPosition] = useState<string | undefined>(user.type === "corporate" ? user.demographicInformation?.position : undefined);
const [companyName, setCompanyName] = useState<string | undefined>(user.type === 'agent' ? user.agentInformation?.companyName : undefined);
const [commercialRegistration, setCommercialRegistration] = useState<string | undefined>(user.type === 'agent' ? user.agentInformation?.commercialRegistration : undefined);
const [companyName, setCompanyName] = useState<string | undefined>(user.type === "agent" ? user.agentInformation?.companyName : undefined);
const [commercialRegistration, setCommercialRegistration] = useState<string | undefined>(
user.type === "agent" ? user.agentInformation?.commercialRegistration : undefined,
);
const {groups} = useGroups();
const {users} = useUsers();
const profilePictureInput = useRef(null);
const expirationDateColor = (date: Date) => {
@@ -120,6 +126,19 @@ function UserProfile({
return;
}
if (email !== user?.email) {
const userAdmins = groups.filter((x) => x.participants.includes(user.id)).map((x) => x.admin);
const message =
users.filter((x) => userAdmins.includes(x.id) && x.type === "corporate").length > 0
? "If you change your e-mail address, you will lose all benefits from your university/institute. Are you sure you want to continue?"
: "Are you sure you want to update your e-mail address?";
if (!confirm(message)) {
setIsLoading(false);
return;
}
}
const request = await axios.post("/api/users/update", {
bio,
name,
@@ -176,7 +195,7 @@ function UserProfile({
</div>
<div className="flex flex-col md:flex-row gap-8 w-full">
<Input
label="Old Password"
label="Current Password"
type="password"
name="password"
onChange={(e) => setPassword(e)}
@@ -358,10 +377,10 @@ function UserProfile({
</span>
<h6 className="font-normal text-base text-mti-gray-taupe">{USER_TYPE_LABELS[user.type]}</h6>
</div>
{user.type === 'agent' && (
{user.type === "agent" && (
<div className="flag items-center h-fit">
<img
alt={user.demographicInformation?.country.toLowerCase() + '_flag'}
alt={user.demographicInformation?.country.toLowerCase() + "_flag"}
src={`https://flagcdn.com/w320/${user.demographicInformation?.country.toLowerCase()}.png`}
width="320"
/>
@@ -394,9 +413,8 @@ function UserProfile({
);
}
export default function Home() {
const {user, mutateUser } = useUser({redirectTo: "/login"});
const {user, mutateUser} = useUser({redirectTo: "/login"});
return (
<>
@@ -410,9 +428,7 @@ export default function Home() {
<link rel="icon" href="/favicon.ico" />
</Head>
<ToastContainer />
{user && <UserProfile user={user} mutateUser={mutateUser} /> }
{user && <UserProfile user={user} mutateUser={mutateUser} />}
</>
);
}
}