From ad52526642f86a44b96b4d5988ad68fd7167e68e Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Tue, 30 Apr 2024 00:56:53 +0200 Subject: [PATCH] Added Profile settings into General page --- .../gradientChooserComponent/index.tsx | 2 +- .../SettingsPage/pages/GeneralPage/index.tsx | 299 +++++++++++------- 2 files changed, 191 insertions(+), 110 deletions(-) diff --git a/src/frontend/src/components/gradientChooserComponent/index.tsx b/src/frontend/src/components/gradientChooserComponent/index.tsx index bff1fbd51..3467aedb8 100644 --- a/src/frontend/src/components/gradientChooserComponent/index.tsx +++ b/src/frontend/src/components/gradientChooserComponent/index.tsx @@ -2,7 +2,7 @@ import { gradients } from "../../utils/styleUtils"; export default function GradientChooserComponent({ value, onChange }) { return ( -
+
{gradients.map((gradient, idx) => (
{ diff --git a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx index ceeb99697..ac2af4f75 100644 --- a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx +++ b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/index.tsx @@ -1,8 +1,10 @@ -import { Button } from "../../../../components/ui/button"; - -import { ColDef, ColGroupDef } from "ag-grid-community"; -import { useState } from "react"; +import * as Form from "@radix-ui/react-form"; +import { cloneDeep } from "lodash"; +import { useContext, useEffect, useState } from "react"; import ForwardedIconComponent from "../../../../components/genericIconComponent"; +import GradientChooserComponent from "../../../../components/gradientChooserComponent"; +import InputComponent from "../../../../components/inputComponent"; +import { Button } from "../../../../components/ui/button"; import { Card, CardContent, @@ -11,71 +13,88 @@ import { CardHeader, CardTitle, } from "../../../../components/ui/card"; -import { Checkbox } from "../../../../components/ui/checkbox"; -import { Input } from "../../../../components/ui/input"; +import { + 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() { - // Column Definitions: Defines the columns to be displayed. - const [colDefs, setColDefs] = useState<(ColDef | ColGroupDef)[]>([ - { 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 setCurrentFlowId = useFlowsManagerStore( + (state) => state.setCurrentFlowId + ); - const [rowData, setRowData] = useState([ - { - 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 [inputState, setInputState] = useState( + CONTROL_PATCH_USER_STATE + ); - --- - - {Document} - - --- - - Question: - {Question} - - Answer: - `, - defaultFields: ["Prompt"], - }, - { - name: "Azure Key", - type: "Credential", - value: "awowkdenvoaimojndofunoweoij0293u0n2e08n23", - defaultFields: ["Azure API Key"], - }, - ]); + const { autoLogin } = useContext(AuthContext); + // 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 (
@@ -94,51 +113,113 @@ export default function GeneralPage() {
- - - Store Name - - Used to identify your store in the marketplace. - - - -
- -
-
- - - -
- - - Plugins Directory - - The directory within your project, in which your plugins are - located. - - - -
- -
- - -
-
-
- - - -
+ {!autoLogin && ( + <> + { + handlePatchGradient(); + event.preventDefault(); + }} + > + + + Profile Gradient + + Choose the gradient that appears as your profile picture. + + + +
+ { + handleInput({ target: { name: "gradient", value } }); + }} + /> +
+
+ + + + + +
+
+ { + handlePatchPassword(); + event.preventDefault(); + }} + > + + + Password + + Type your new password and confirm it. + + + +
+ + { + handleInput({ target: { name: "password", value } }); + }} + value={password} + isForm + password={true} + placeholder="Password" + className="w-full" + /> + + Please enter your password + + + + { + handleInput({ + target: { name: "cnfPassword", value }, + }); + }} + value={cnfPassword} + isForm + password={true} + placeholder="Confirm Password" + className="w-full" + /> + + + Please confirm your password + + +
+
+ + + + + +
+
+ + )}
);