Added Profile settings into General page

This commit is contained in:
Lucas Oliveira 2024-04-30 00:56:53 +02:00
commit ad52526642
2 changed files with 191 additions and 110 deletions

View file

@ -2,7 +2,7 @@ import { gradients } from "../../utils/styleUtils";
export default function GradientChooserComponent({ value, onChange }) { export default function GradientChooserComponent({ value, onChange }) {
return ( return (
<div className="flex flex-wrap items-center justify-center gap-4"> <div className="flex flex-wrap items-center justify-start gap-2">
{gradients.map((gradient, idx) => ( {gradients.map((gradient, idx) => (
<div <div
onClick={() => { onClick={() => {

View file

@ -1,8 +1,10 @@
import { Button } from "../../../../components/ui/button"; import * as Form from "@radix-ui/react-form";
import { cloneDeep } from "lodash";
import { ColDef, ColGroupDef } from "ag-grid-community"; import { useContext, useEffect, useState } from "react";
import { useState } from "react";
import ForwardedIconComponent from "../../../../components/genericIconComponent"; import ForwardedIconComponent from "../../../../components/genericIconComponent";
import GradientChooserComponent from "../../../../components/gradientChooserComponent";
import InputComponent from "../../../../components/inputComponent";
import { Button } from "../../../../components/ui/button";
import { import {
Card, Card,
CardContent, CardContent,
@ -11,71 +13,88 @@ import {
CardHeader, CardHeader,
CardTitle, CardTitle,
} from "../../../../components/ui/card"; } from "../../../../components/ui/card";
import { Checkbox } from "../../../../components/ui/checkbox"; import {
import { Input } from "../../../../components/ui/input"; EDIT_PASSWORD_ALERT_LIST,
EDIT_PASSWORD_ERROR_ALERT,
SAVE_ERROR_ALERT,
SAVE_SUCCESS_ALERT,
} from "../../../../constants/alerts_constants";
import { CONTROL_PATCH_USER_STATE } from "../../../../constants/constants";
import { AuthContext } from "../../../../contexts/authContext";
import { resetPassword, updateUser } from "../../../../controllers/API";
import useAlertStore from "../../../../stores/alertStore";
import useFlowsManagerStore from "../../../../stores/flowsManagerStore";
import {
inputHandlerEventType,
patchUserInputStateType,
} from "../../../../types/components";
import { gradients } from "../../../../utils/styleUtils";
export default function GeneralPage() { export default function GeneralPage() {
// Column Definitions: Defines the columns to be displayed. const setCurrentFlowId = useFlowsManagerStore(
const [colDefs, setColDefs] = useState<(ColDef<any> | ColGroupDef<any>)[]>([ (state) => state.setCurrentFlowId
{ headerName: "Variable Name", field: "name", flex: 1 }, //This column will be twice as wide as the others );
{
field: "type",
cellEditor: "agSelectCellEditor",
cellEditorParams: {
values: ["Prompt", "Credential"],
valueListGap: 10,
},
flex: 1,
editable: true,
},
{
field: "value",
cellEditor: "agLargeTextCellEditor",
cellEditorPopup: true,
flex: 2,
editable: true,
},
{
headerName: "Apply To Fields",
field: "defaultFields",
flex: 1,
editable: true,
},
]);
const [rowData, setRowData] = useState([ const [inputState, setInputState] = useState<patchUserInputStateType>(
{ CONTROL_PATCH_USER_STATE
name: "OpenAI Key", );
type: "Credential",
value: "apijpioj09u302j0982ejf",
defaultFields: "Open AI API Key",
},
{
name: "Prompt",
type: "Prompt",
value: `Answer user's questions based on the document below:
--- const { autoLogin } = useContext(AuthContext);
{Document}
---
Question:
{Question}
Answer:
`,
defaultFields: ["Prompt"],
},
{
name: "Azure Key",
type: "Credential",
value: "awowkdenvoaimojndofunoweoij0293u0n2e08n23",
defaultFields: ["Azure API Key"],
},
]);
// set null id
useEffect(() => {
setCurrentFlowId("");
}, []);
const setSuccessData = useAlertStore((state) => state.setSuccessData);
const setErrorData = useAlertStore((state) => state.setErrorData);
const { userData, setUserData } = useContext(AuthContext);
const { password, cnfPassword, gradient } = inputState;
async function handlePatchPassword() {
if (password !== cnfPassword) {
setErrorData({
title: EDIT_PASSWORD_ERROR_ALERT,
list: [EDIT_PASSWORD_ALERT_LIST],
});
return;
}
try {
if (password !== "") await resetPassword(userData!.id, { password });
handleInput({ target: { name: "password", value: "" } });
handleInput({ target: { name: "cnfPassword", value: "" } });
setSuccessData({ title: SAVE_SUCCESS_ALERT });
} catch (error) {
setErrorData({
title: SAVE_ERROR_ALERT,
list: [(error as any).response.data.detail],
});
}
}
async function handlePatchGradient() {
try {
if (gradient !== "")
await updateUser(userData!.id, { profile_image: gradient });
if (gradient !== "") {
let newUserData = cloneDeep(userData);
newUserData!.profile_image = gradient;
setUserData(newUserData);
}
setSuccessData({ title: SAVE_SUCCESS_ALERT });
} catch (error) {
setErrorData({
title: SAVE_ERROR_ALERT,
list: [(error as any).response.data.detail],
});
}
}
function handleInput({
target: { name, value },
}: inputHandlerEventType): void {
setInputState((prev) => ({ ...prev, [name]: value }));
}
return ( return (
<div className="flex h-full w-full flex-col gap-6"> <div className="flex h-full w-full flex-col gap-6">
<div className="flex w-full items-center justify-between gap-4 space-y-0.5"> <div className="flex w-full items-center justify-between gap-4 space-y-0.5">
@ -94,51 +113,113 @@ export default function GeneralPage() {
</div> </div>
<div className="grid gap-6"> <div className="grid gap-6">
<Card x-chunk="dashboard-04-chunk-1"> {!autoLogin && (
<CardHeader> <>
<CardTitle>Store Name</CardTitle> <Form.Root
<CardDescription> onSubmit={(event) => {
Used to identify your store in the marketplace. handlePatchGradient();
</CardDescription> event.preventDefault();
</CardHeader> }}
<CardContent> >
<form> <Card x-chunk="dashboard-04-chunk-1">
<Input placeholder="Store Name" /> <CardHeader>
</form> <CardTitle>Profile Gradient</CardTitle>
</CardContent> <CardDescription>
<CardFooter className="border-t px-6 py-4"> Choose the gradient that appears as your profile picture.
<Button>Save</Button> </CardDescription>
</CardFooter> </CardHeader>
</Card> <CardContent>
<Card x-chunk="dashboard-04-chunk-2"> <div className="py-2">
<CardHeader> <GradientChooserComponent
<CardTitle>Plugins Directory</CardTitle> value={
<CardDescription> gradient == ""
The directory within your project, in which your plugins are ? userData?.profile_image ??
located. gradients[
</CardDescription> parseInt(userData?.id ?? "", 30) %
</CardHeader> gradients.length
<CardContent> ]
<form className="flex flex-col gap-4"> : gradient
<Input }
placeholder="Project Name" onChange={(value) => {
defaultValue="/content/plugins" handleInput({ target: { name: "gradient", value } });
/> }}
<div className="flex items-center space-x-2"> />
<Checkbox id="include" defaultChecked /> </div>
<label </CardContent>
htmlFor="include" <CardFooter className="border-t px-6 py-4">
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" <Form.Submit asChild>
> <Button type="submit">Save</Button>
Allow administrators to change the directory. </Form.Submit>
</label> </CardFooter>
</div> </Card>
</form> </Form.Root>
</CardContent> <Form.Root
<CardFooter className="border-t px-6 py-4"> onSubmit={(event) => {
<Button>Save</Button> handlePatchPassword();
</CardFooter> event.preventDefault();
</Card> }}
>
<Card x-chunk="dashboard-04-chunk-2">
<CardHeader>
<CardTitle>Password</CardTitle>
<CardDescription>
Type your new password and confirm it.
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex w-full gap-4">
<Form.Field name="password" className="w-full">
<InputComponent
id="pasword"
onChange={(value) => {
handleInput({ target: { name: "password", value } });
}}
value={password}
isForm
password={true}
placeholder="Password"
className="w-full"
/>
<Form.Message
match="valueMissing"
className="field-invalid"
>
Please enter your password
</Form.Message>
</Form.Field>
<Form.Field name="cnfPassword" className="w-full">
<InputComponent
id="cnfPassword"
onChange={(value) => {
handleInput({
target: { name: "cnfPassword", value },
});
}}
value={cnfPassword}
isForm
password={true}
placeholder="Confirm Password"
className="w-full"
/>
<Form.Message
className="field-invalid"
match="valueMissing"
>
Please confirm your password
</Form.Message>
</Form.Field>
</div>
</CardContent>
<CardFooter className="border-t px-6 py-4">
<Form.Submit asChild>
<Button type="submit">Save</Button>
</Form.Submit>
</CardFooter>
</Card>
</Form.Root>
</>
)}
</div> </div>
</div> </div>
); );