From e81c3cd3bbb63a5fe098ad06a045f4c806ef0893 Mon Sep 17 00:00:00 2001 From: BitterKanegul <15769643+BitterKanegul@users.noreply.github.com> Date: Sat, 31 Dec 2022 18:23:33 +0530 Subject: [PATCH] Incorporate review comments --- website/src/components/FlaggableElement.tsx | 175 +++++++++++--------- 1 file changed, 95 insertions(+), 80 deletions(-) diff --git a/website/src/components/FlaggableElement.tsx b/website/src/components/FlaggableElement.tsx index 556df44e..f5717350 100644 --- a/website/src/components/FlaggableElement.tsx +++ b/website/src/components/FlaggableElement.tsx @@ -1,42 +1,29 @@ -import poster from "src/lib/poster"; import { - Popover, - useBoolean, - PopoverAnchor, Button, - PopoverTrigger, - PopoverContent, - PopoverBody, Checkbox, - PopoverCloseButton, - PopoverArrow, Flex, - Spacer, + Popover, + PopoverAnchor, + PopoverArrow, + PopoverBody, + PopoverCloseButton, + PopoverContent, + PopoverTrigger, Slider, - SliderTrack, SliderFilledTrack, SliderThumb, + SliderTrack, + Spacer, + useBoolean, } from "@chakra-ui/react"; -import useSWRMutation from "swr/mutation"; -import { QuestionMarkCircleIcon, FlagIcon } from "@heroicons/react/20/solid"; -import { useState } from "react"; +import { FlagIcon, QuestionMarkCircleIcon } from "@heroicons/react/20/solid"; -export type TextFlagProps = { - text: string; - label_map; - post_id: string; -}; -type FlagRefs = { - label: string; - value: number; - setValue; - isChecked: boolean; - setChecked; -}; +import poster from "src/lib/poster"; +import useSWRMutation from "swr/mutation"; +import { useState } from "react"; export const FlaggableElement = (props) => { const [isEditing, setIsEditing] = useBoolean(); - const [hasCheck, setHasCheck] = useBoolean(); const { trigger, isMutating } = useSWRMutation("/api/v1/text_labels", poster, { onSuccess: () => { setIsEditing.off; @@ -46,27 +33,32 @@ export const FlaggableElement = (props) => { const submitResponse = (label_data) => { trigger(label_data); }; - const label_refs: FlagRefs[] = TEXT_LABEL_FLAGS.map((flag) => { - const [isChecked, setChecked] = useState(false); - const [value, setValue] = useState(1); - return { - label: flag.attributeName, - value: value, - setValue: setValue, - isChecked: isChecked, - setChecked: setChecked, - }; - }); + const [checkboxValues, setCheckboxValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(false)); + const [sliderValues, setSliderValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(1)); + const fetchData = () => { let label_map: Map = new Map(); - label_refs - .filter((flagRef) => flagRef.isChecked) - .map((refs) => { - label_map.set(refs.label, refs.value); - }); - console.log(label_map); + TEXT_LABEL_FLAGS.forEach((flag, i) => { + if (checkboxValues[i]) { + label_map.set(flag.attributeName, sliderValues[i]); + } + }); return { post_id: props.post_id, label_map: Object.fromEntries(label_map), text: props.text }; }; + const handleCheckboxState = (isChecked, idx) => { + setCheckboxValues( + checkboxValues.map((val, i) => { + return i == idx ? isChecked : val; + }) + ); + }; + const handleSliderState = (newVal, idx) => { + setSliderValues( + sliderValues.map((val, i) => { + return i == idx ? newVal : val; + }) + ); + }; return ( {
{props.children} -
@@ -93,11 +88,26 @@ export const FlaggableElement = (props) => {
    {TEXT_LABEL_FLAGS.map((option, i) => { - return ; + return ( + + ); })}