Added gradient chooser and success messages

This commit is contained in:
Lucas Oliveira 2023-09-08 13:08:39 -03:00
commit bd44500508
6 changed files with 86 additions and 72 deletions

View file

@ -0,0 +1,13 @@
import { useState } from "react";
import { gradients } from "../../utils/styleUtils";
export default function GradientChooserComponent({value, onChange}){
return (
<div className="flex flex-wrap gap-4 justify-center items-center">
{gradients.map((gradient, idx) =>
<div onClick={() => {onChange(gradient)}} className={"w-12 h-12 rounded-full transition-all duration-400 " + gradient + (value === gradient ? " shadow-lg ring-2 ring-primary" : "")} key={idx}></div>
)}
</div>
)
}

View file

@ -139,9 +139,7 @@ export default function Header(): JSX.Element {
<button <button
className={ className={
"h-7 w-7 rounded-full focus-visible:outline-0 " + "h-7 w-7 rounded-full focus-visible:outline-0 " +
gradients[ (userData!.profile_image ?? gradients[parseInt(userData!.id ?? "", 30) % gradients.length])
parseInt(userData?.id ?? "", 30) % gradients.length
]
} }
/> />
</DropdownMenuTrigger> </DropdownMenuTrigger>

View file

@ -63,7 +63,7 @@ export function AuthProvider({ children }): React.ReactElement {
.then((user) => { .then((user) => {
setUserData(user); setUserData(user);
setLoading(false); setLoading(false);
const isSuperUser = user.is_superuser; const isSuperUser = user!.is_superuser;
setIsAdmin(isSuperUser); setIsAdmin(isSuperUser);
}) })
.catch((error) => {}); .catch((error) => {});

View file

@ -463,6 +463,8 @@ export async function updateUser(user_id: string, user: changeUser) {
const res = await api.patch(`${BASE_URL_API}users/${user_id}`, user); const res = await api.patch(`${BASE_URL_API}users/${user_id}`, user);
if (res.status === 200) { if (res.status === 200) {
return res.data; return res.data;
} else {
throw new Error(res.data.toString());
} }
} catch (error) { } catch (error) {
console.log("Error:", error); console.log("Error:", error);

View file

@ -1,6 +1,7 @@
import * as Form from "@radix-ui/react-form"; import * as Form from "@radix-ui/react-form";
import { useContext, useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import IconComponent from "../../components/genericIconComponent"; import IconComponent from "../../components/genericIconComponent";
import GradientChooserComponent from "../../components/gradientChooserComponent";
import Header from "../../components/headerComponent"; import Header from "../../components/headerComponent";
import InputComponent from "../../components/inputComponent"; import InputComponent from "../../components/inputComponent";
import { Button } from "../../components/ui/button"; import { Button } from "../../components/ui/button";
@ -13,6 +14,7 @@ import {
inputHandlerEventType, inputHandlerEventType,
patchUserInputStateType, patchUserInputStateType,
} from "../../types/components"; } from "../../types/components";
import { gradients } from "../../utils/styleUtils";
export default function ProfileSettingsPage(): JSX.Element { export default function ProfileSettingsPage(): JSX.Element {
const { setTabId } = useContext(TabsContext); const { setTabId } = useContext(TabsContext);
@ -24,7 +26,7 @@ export default function ProfileSettingsPage(): JSX.Element {
useEffect(() => { useEffect(() => {
setTabId(""); setTabId("");
}, []); }, []);
const { setErrorData } = useContext(alertContext); const { setErrorData, setSuccessData } = useContext(alertContext);
const { userData } = useContext(AuthContext); const { userData } = useContext(AuthContext);
const { password, cnfPassword, gradient } = inputState; const { password, cnfPassword, gradient } = inputState;
@ -36,8 +38,12 @@ export default function ProfileSettingsPage(): JSX.Element {
}); });
return; return;
} }
try{
updateUser(userData!.id, { password, profile_image: gradient }).then(() => {setSuccessData({title: "Changes saved successfully!"})});
} catch (error) {
setErrorData({title: "Error saving changes", list: [error as string]});
}
updateUser(userData!.id, {password, profile_image: gradient});
} }
function handleInput({ function handleInput({
@ -67,87 +73,81 @@ export default function ProfileSettingsPage(): JSX.Element {
const data = Object.fromEntries(new FormData(event.currentTarget)); const data = Object.fromEntries(new FormData(event.currentTarget));
event.preventDefault(); event.preventDefault();
}} }}
className="flex justify-between px-6" className="flex h-full flex-col px-6 pb-16"
> >
<div className="flex gap-4"> <div className="flex h-full flex-col gap-4">
<div className="mb-3 w-96"> <div className="flex gap-4">
<Form.Field name="password"> <div className="mb-3 w-96">
<Form.Label className="data-[invalid]:label-invalid"> <Form.Field name="password">
Password <span className="font-medium text-destructive">*</span> <Form.Label className="data-[invalid]:label-invalid">
</Form.Label> Password{" "}
</Form.Label>
<Form.Control asChild> <Form.Control asChild>
<InputComponent <InputComponent
onChange={(value) => { onChange={(value) => {
handleInput({ target: { name: "password", value } }); handleInput({ target: { name: "password", value } });
}} }}
value={password} value={password}
isForm isForm
password={true} password={true}
required placeholder="Password"
placeholder="Password" className="w-full"
className="w-full" />
/> </Form.Control>
</Form.Control>
<Form.Message match="valueMissing" className="field-invalid"> <Form.Message match="valueMissing" className="field-invalid">
Please enter your password Please enter your password
</Form.Message> </Form.Message>
</Form.Field> </Form.Field>
</div> </div>
<div className="mb-3 w-96"> <div className="mb-3 w-96">
<Form.Field name="cnfPassword"> <Form.Field name="cnfPassword">
<Form.Label className="data-[invalid]:label-invalid"> <Form.Label className="data-[invalid]:label-invalid">
Confirm Password{" "} Confirm Password{" "}
<span className="font-medium text-destructive">*</span> </Form.Label>
</Form.Label>
<InputComponent <InputComponent
onChange={(value) => { onChange={(value) => {
handleInput({ target: { name: "cnfPassword", value } }); handleInput({ target: { name: "cnfPassword", value } });
}} }}
value={cnfPassword} value={cnfPassword}
isForm isForm
password={true} password={true}
required placeholder="Confirm Password"
placeholder="Confirm Password" className="w-full"
className="w-full" />
/>
<Form.Message className="field-invalid" match="valueMissing"> <Form.Message className="field-invalid" match="valueMissing">
Please confirm your password Please confirm your password
</Form.Message> </Form.Message>
</Form.Field> </Form.Field>
</div>
</div>
<Form.Field name="gradient"> <Form.Field name="gradient">
<Form.Label className="data-[invalid]:label-invalid"> <Form.Label className="data-[invalid]:label-invalid">
Insert Gradient{" "} Profile Gradient{" "}
<span className="font-medium text-destructive">*</span>
</Form.Label> </Form.Label>
<InputComponent <div className="mt-4 w-[1010px]">
<GradientChooserComponent
value={gradient == "" ? (userData!.profile_image ?? gradients[parseInt(userData!.id ?? "", 30) % gradients.length]) : gradient}
onChange={(value) => { onChange={(value) => {
handleInput({ target: { name: "gradient", value } }); handleInput({ target: { name: "gradient", value } });
}} }}
value={gradient}
isForm
password={true}
required
placeholder="Insert Gradient"
className="w-full"
/> />
</div>
<Form.Message className="field-invalid" match="valueMissing">
Please insert gradient
</Form.Message>
</Form.Field> </Form.Field>
</div> </div>
</div>
<div className="w-40"> <div className="flex w-full justify-end">
<Form.Submit asChild> <div className="w-32">
<Button className="mr-3 mt-6 w-full" type="submit"> <Form.Submit asChild>
Save <Button className="mr-3 mt-6 w-full" type="submit">
</Button> Save
</Form.Submit> </Button>
</Form.Submit>
</div>
</div> </div>
</Form.Root> </Form.Root>
</div> </div>

View file

@ -91,6 +91,7 @@ export type Users = {
username: string; username: string;
is_active: boolean; is_active: boolean;
is_superuser: boolean; is_superuser: boolean;
profile_image: string;
create_at: Date; create_at: Date;
updated_at: Date; updated_at: Date;
}; };