Skip to content

Commit

Permalink
fix: #337 / labels selection
Browse files Browse the repository at this point in the history
  • Loading branch information
jacovinus committed Jun 13, 2023
1 parent 8d1ea25 commit 82ce2a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {
const dispatch = useDispatch();

const { name, data, labelsSelected, label, queries } = props;
const { dataSourceId, start, stop } = data;
const { dataSourceId, start, stop, labels: propsLabels } = data;

const [filterState, setFilterState] = useState("");

// get values hook
const { response: labelValuesResponse, loading }: any = useLabelValues(
dataSourceId,
label,
Expand Down Expand Up @@ -174,21 +173,17 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {
}, [props.data.labels]);

const resp = useMemo(() => {


if (labelValuesResponse?.data?.data?.length > 0) {
const labelFromResponse = data?.labels?.find(
(l: any) => l.name === label
);

const values = labelFromResponse?.values;
const valuesMap = new Map();
const valuesMap = new Map();

values?.forEach((value: any) => {
valuesMap.set(value.name, {...value,label});
valuesMap.set(value.name, { ...value, label });
});


return labelValuesResponse?.data?.data?.map((val: any) => ({
label,
name: val,
Expand Down Expand Up @@ -266,32 +261,26 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {
}
};

const onValueClick = (val: any, isAll = false) => {
const onValueClick = (val: any) => {
let initialValues: any = [];
if (isAll) {
setValsSelection([]);
}
if (valsSelection.length > 0) {
initialValues = onValueFilter(val, valsSelection);
if (val.type === "metrics") {
setValuesState((prev: any) => {
const found = prev.some((s: any) => s.id === val.id);

let prevCp = [...prev];
if (found) {
return prev.map((m: any) => {
if (m.id === val.id) {
return { ...m, selected: false };
}
return { ...m, selected: true };
return prevCp?.map((m: any) => {
return m.id === val.id
? { ...m, selected: false }
: { ...m, selected: true };
});
}
});
}
} else if (!isAll) {
initialValues = [...initialValues, { ...val }];
}

let propsLabels = JSONClone(props.data.labels) || [];
initialValues = [...initialValues, { ...val }];

let labelsMod: any = [];

Expand All @@ -308,18 +297,18 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {
];
} else {
if (propsLabels.some((s: any) => s.name === label)) {
for (let LCP of propsLabels) {
if (LCP.name === label) {
LCP = {
for (let propsLabel of propsLabels) {
if (propsLabel.name === label) {
propsLabel = {
name: label,
selected:
labelsSelected.includes(label) &&
initialValues.length > 0,
values: [...initialValues],
};
labelsMod.push(LCP);
labelsMod.push(propsLabel);
} else {
labelsMod.push(LCP);
labelsMod.push(propsLabel);
}
}
} else {
Expand All @@ -338,8 +327,7 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {

const finalLabels = labelsSelected?.map((m: any) => {
if (labelsMod.some((s: any) => s.name === m)) {
let found = labelsMod.find((f: any) => f.name === m);
return found;
return labelsMod.find((f: any) => f.name === m);
} else {
return { name: m, selected: true, values: [] };
}
Expand All @@ -362,13 +350,13 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {
setFilterState((prev) => value);

if (e !== "") {
setFilterValuesState((prev: any) =>
setFilterValuesState(() =>
valuesState.filter((f: any) =>
f.name.toLowerCase().includes(value.toLowerCase())
)
);
} else {
setFilterValuesState((prev: any) => valuesState);
setFilterValuesState(() => valuesState);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -393,7 +381,7 @@ const ValuesList: React.FC<ValuesListProps> = (props) => {
</LoaderCont>
)}
{valuesState?.length > 0 &&
filterValuesState?.map((value: any, key: any) => (
filterValuesState?.map((value: any, key: number) => (
<LabelValue
{...props}
key={key}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const sendLabels = async (

if (response?.data?.data?.length > 0) {
const labels = response?.data?.data
.sort()

.map((label: any) => ({
name: label,
selected: false,
Expand Down

0 comments on commit 82ce2a6

Please sign in to comment.