Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic MacroClusterRow component #2162

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions app/javascript/src/admin/components/clustering/MacroClusterRow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React, { useCallback, useContext, useEffect, useState } from "react";
import ScrollIntoViewIfNeeded from "react-scroll-into-view-if-needed";

import { DispatchContext, StateContext } from "../../micro-clusters/GenericApp";
import { EntriesList } from "./EntriesList";
import { SearchLink } from "./SearchLink";
import { ASSIGN_TO_MACRO_CLUSTER, UPDATING } from "./actions";
import { keyDownListener } from "./keyDownListener";

export const MacroClusterRow = ({
macroCluster,
afterAssign,
selected,
assignCluster,
fields,
extraColumn
}) => {
const { activeCluster, updating } = useContext(StateContext);
const dispatch = useContext(DispatchContext);
const [showInks, setShowInks] = useState(false);
const onClick = () => setShowInks(!showInks);
const assign = useCallback(() => {
dispatch({ type: UPDATING });
setTimeout(() => {
assignCluster(activeCluster.id, macroCluster.id).then((microCluster) => {
dispatch({
type: ASSIGN_TO_MACRO_CLUSTER,
payload: microCluster
});
afterAssign(microCluster);
});
}, 10);
}, [activeCluster.id, afterAssign, dispatch, macroCluster.id, assignCluster]);
useEffect(() => {
if (!selected) return;

return keyDownListener(({ keyCode }) => {
if (keyCode == 65) assign();
});
}, [macroCluster.id, activeCluster.id, selected, assign]);
return (
<>
<tr className={selected ? "selected" : ""}>
<td className="distance" onClick={onClick}>
<ScrollIntoViewIfNeeded active={selected}>
{macroCluster.distance}
</ScrollIntoViewIfNeeded>
</td>
{fields.map((field) => (
<td key={field} onClick={onClick}>
{macroCluster[field]}
</td>
))}
<td onClick={onClick}></td>
<td onClick={onClick}>{extraColumn(macroCluster)}</td>
<td>
<SearchLink e={macroCluster} fields={fields} />
</td>
<td>
<button
className="btn btn-secondary"
type="submit"
disabled={updating}
onClick={assign}
>
Assign
</button>
</td>
</tr>
{(showInks || selected) && (
<tr>
<td colSpan="7">
<table className="table macro-cluster-collected-inks">
<tbody>
<EntriesList
entries={macroCluster.micro_clusters
.map((c) => c.entries)
.flat()}
fields={fields}
extraColumn={extraColumn}
/>
</tbody>
</table>
</td>
</tr>
)}
</>
);
};
107 changes: 8 additions & 99 deletions app/javascript/src/admin/micro-clusters/DisplayMacroClusters.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import React, { useState, useContext, useEffect } from "react";
import _ from "lodash";
import levenshtein from "fast-levenshtein";
import ScrollIntoViewIfNeeded from "react-scroll-into-view-if-needed";
import _ from "lodash";
import { matchSorter } from "match-sorter";
import React, { useContext, useEffect, useState } from "react";

import { SearchLink } from "../components/clustering/SearchLink";
import { StateContext, DispatchContext } from "./GenericApp";
import { MacroClusterRow } from "../components/clustering/MacroClusterRow";
import {
ASSIGN_TO_MACRO_CLUSTER,
NEXT_MACRO_CLUSTER,
PREVIOUS_MACRO_CLUSTER,
UPDATING
PREVIOUS_MACRO_CLUSTER
} from "../components/clustering/actions";
import {
keyDownListener,
setInBrandSelector
} from "../components/clustering/keyDownListener";
import { useCallback } from "react";
import { EntriesList } from "../components/clustering/EntriesList";
import { DispatchContext, StateContext } from "./GenericApp";

export const DisplayMacroClusters = ({ afterAssign, assignCluster }) => {
const dispatch = useContext(DispatchContext);
Expand Down Expand Up @@ -76,6 +71,8 @@ const MacroClusterRows = ({ afterAssign, assignCluster }) => {
afterAssign={afterAssign}
assignCluster={assignCluster}
selected={index == selectedMacroClusterIndex}
fields={["brand_name", "line_name", "ink_name"]}
extraColumn={extraColumn}
/>
));
const inputRow = (
Expand Down Expand Up @@ -167,95 +164,7 @@ const stripped = (str) => {
.replace(/\s+/i, "");
};

const MacroClusterRow = ({
macroCluster,
afterAssign,
selected,
assignCluster
}) => {
const { activeCluster, updating } = useContext(StateContext);
const dispatch = useContext(DispatchContext);
const [showInks, setShowInks] = useState(false);
const onClick = () => setShowInks(!showInks);
const assign = useCallback(() => {
dispatch({ type: UPDATING });
setTimeout(() => {
assignCluster(activeCluster.id, macroCluster.id).then((microCluster) => {
dispatch({
type: ASSIGN_TO_MACRO_CLUSTER,
payload: microCluster
});
afterAssign(microCluster);
});
}, 10);
}, [activeCluster.id, afterAssign, dispatch, macroCluster.id, assignCluster]);
useEffect(() => {
if (!selected) return;

return keyDownListener(({ keyCode }) => {
if (keyCode == 65) assign();
});
}, [macroCluster.id, activeCluster.id, selected, assign]);
return (
<>
<tr className={selected ? "selected" : ""}>
<td className="distance" onClick={onClick}>
<ScrollIntoViewIfNeeded active={selected}>
{macroCluster.distance}
</ScrollIntoViewIfNeeded>
</td>
<td onClick={onClick}>{macroCluster.brand_name}</td>
<td onClick={onClick}>{macroCluster.line_name}</td>
<td onClick={onClick}>{macroCluster.ink_name}</td>
<td onClick={onClick}></td>
<td onClick={onClick}>
<div
style={{
backgroundColor: macroCluster.color,
height: "45px",
width: "45px"
}}
/>
</td>
<td>
<SearchLink
e={macroCluster}
fields={["brand_name", "line_name", "ink_name"]}
/>
</td>
<td>
<button
className="btn btn-secondary"
type="submit"
disabled={updating}
onClick={assign}
>
Assign
</button>
</td>
</tr>
{(showInks || selected) && (
<tr>
<td colSpan="7">
<table className="table macro-cluster-collected-inks">
<tbody>
<EntriesList
entries={macroCluster.micro_clusters
.map((c) => c.entries)
.flat()}
fields={["brand_name", "line_name", "ink_name"]}
extraColumn={extraColumn}
/>
</tbody>
</table>
</td>
</tr>
)}
</>
);
};

const extraColumn = (ci) => (
export const extraColumn = (ci) => (
<div
style={{
backgroundColor: ci.color,
Expand Down
Loading