diff --git a/web/components/templates/requestsV2/requestRow.tsx b/web/components/templates/requestsV2/requestRow.tsx index 8ab115ca15..b9cadb2ab8 100644 --- a/web/components/templates/requestsV2/requestRow.tsx +++ b/web/components/templates/requestsV2/requestRow.tsx @@ -19,10 +19,13 @@ import { } from "@heroicons/react/24/solid"; import { addRequestLabel, + addRequestScore, updateRequestFeedback, } from "../../../services/lib/requests"; import useNotification from "../../shared/notification/useNotification"; import { useOrg } from "../../layout/organizationContext"; +import HcButton from "../../ui/hcButton"; +import { TextInput } from "@tremor/react"; function getPathName(url: string) { try { @@ -57,6 +60,8 @@ const RequestRow = (props: { const org = useOrg(); const [isAddingLabel, setIsAddingLabel] = useState(false); + const [isScoresAddingLabel, setIsScoresAddingLabel] = useState(false); + const [isScoresAdding, setIsScoresAdding] = useState(false); const [isAdding, setIsAdding] = useState(false); const [currentProperties, setCurrentProperties] = useState< { @@ -64,6 +69,8 @@ const RequestRow = (props: { }[] >(); + const [currentScores, setCurrentScores] = useState>(); + const router = useRouter(); const { setNotification } = useNotification(); @@ -85,7 +92,9 @@ const RequestRow = (props: { }); setCurrentProperties(currentProperties); - }, [properties, request.customProperties]); + const currentScores: Record = request.scores || {}; + setCurrentScores(currentScores); + }, [properties, request.customProperties, request.scores]); const updateFeedbackHandler = async (requestId: string, rating: boolean) => { updateRequestFeedback(requestId, rating) @@ -151,6 +160,57 @@ const RequestRow = (props: { } }; + const onAddScoreHandler = async (e: React.FormEvent) => { + e.preventDefault(); + setIsScoresAdding(true); + + const formData = new FormData(e.currentTarget); + const key = formData.get("key") as string; + const value = formData.get("value") as any as number; + + if (currentScores && currentScores[key]) { + setNotification("Score already exists", "error"); + setIsScoresAdding(false); + return; + } + + if (!key || !value || org?.currentOrg?.id === undefined) { + setNotification("Error adding score", "error"); + setIsScoresAdding(false); + return; + } + try { + const res = await addRequestScore( + request.id, + org?.currentOrg?.id, + key, + value + ); + + if (res?.status === 201) { + setNotification("Score added", "success"); + setCurrentScores( + currentScores + ? { + ...currentScores, + [key]: value, + } + : { [key]: value } + ); + + setIsScoresAdding(false); + } else { + setNotification("Error adding score", "error"); + setIsScoresAdding(false); + } + } catch (err) { + console.error(err); + setNotification(`Error adding score: ${err}`, "error"); + setIsScoresAdding(false); + return; + } + }; + return (
@@ -297,13 +357,13 @@ const RequestRow = (props: { Key
- @@ -317,27 +377,29 @@ const RequestRow = (props: { Value
-
- + )}
@@ -358,13 +420,89 @@ const RequestRow = (props: { })}
- {request.scores && ( -
-
- Scores{" "} -
-
- {Object.entries(request?.scores).map(([key, value]) => ( + +
+
+ Scores{" "} + + + +
+ {isScoresAddingLabel && ( +
+
+ +
+ +
+
+
+ +
+ +
+
+ + {isAdding && ( + + )} + Add + +
+ )} + +
+ {currentScores && + Object.entries(currentScores).map(([key, value]) => (
  • {value}

  • ))} -
    - )} +
    + {displayPreview && (
    diff --git a/web/services/lib/requests.ts b/web/services/lib/requests.ts index 58d14f266c..6165c6d88c 100644 --- a/web/services/lib/requests.ts +++ b/web/services/lib/requests.ts @@ -39,3 +39,26 @@ export const addRequestLabel = async ( }) ).response; }; + +export const addRequestScore = async ( + requestId: string, + orgId: string, + key: string, + value: number +) => { + const jawn = getJawnClient(orgId); + return ( + await jawn.POST("/v1/request/{requestId}/score", { + body: { + scores: { + [key]: value, + }, + }, + params: { + path: { + requestId, + }, + }, + }) + ).response; +};