-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start making the code generic by passing in functions
- Loading branch information
Showing
9 changed files
with
187 additions
and
129 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
app/javascript/src/admin/components/clustering/BrandSelector.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { useContext } from "react"; | ||
import Select from "react-select"; | ||
import _ from "lodash"; | ||
|
||
import { DispatchContext, StateContext } from "../../micro-clusters/GenericApp"; | ||
import { UPDATE_SELECTED_BRANDS } from "./actions"; | ||
import { setInBrandSelector } from "./keyDownListener"; | ||
|
||
export const BrandSelector = ({ field }) => { | ||
const dispatch = useContext(DispatchContext); | ||
const { microClusters, selectedBrands } = useContext(StateContext); | ||
const values = _.countBy(microClusters.map((c) => c[field])); | ||
const options = _.sortBy( | ||
_.map(values, (value, key) => ({ | ||
value: key, | ||
label: `${key} (${value})` | ||
})), | ||
"label" | ||
); | ||
return ( | ||
<div className="mb-3"> | ||
<Select | ||
options={options} | ||
onChange={(selected) => { | ||
dispatch({ type: UPDATE_SELECTED_BRANDS, payload: selected }); | ||
}} | ||
isMulti | ||
value={selectedBrands} | ||
onFocus={() => { | ||
setInBrandSelector(true); | ||
}} | ||
onBlur={() => { | ||
setInBrandSelector(false); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
app/javascript/src/admin/components/clustering/LoadingOverlay.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React, { useContext } from "react"; | ||
|
||
import { StateContext } from "../../micro-clusters/GenericApp"; | ||
|
||
export const LoadingOverlay = () => { | ||
const { updating } = useContext(StateContext); | ||
if (!updating) return null; | ||
const style = { | ||
position: "fixed", | ||
top: 0, | ||
left: 0, | ||
height: "100%", | ||
width: "100%", | ||
zIndex: 10, | ||
backgroundColor: "rgba(0,0,0,0.5)" | ||
}; | ||
return <div style={style}></div>; | ||
}; |
13 changes: 13 additions & 0 deletions
13
app/javascript/src/admin/components/clustering/Summary.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React, { useContext } from "react"; | ||
|
||
import { StateContext } from "../../micro-clusters/GenericApp"; | ||
|
||
export const Summary = () => { | ||
const { microClusters, selectedMicroClusters } = useContext(StateContext); | ||
return ( | ||
<div className="summary"> | ||
<b>Total:</b> {microClusters.length} <b>In Selection:</b>{" "} | ||
{selectedMicroClusters.length} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,16 @@ | ||
import React, { useEffect, useReducer, useContext } from "react"; | ||
import Select from "react-select"; | ||
import _ from "lodash"; | ||
|
||
import { Spinner } from "../components/Spinner"; | ||
import { DisplayMicroClusters } from "./DisplayMicroClusters"; | ||
import { reducer, initalState } from "../components/clustering/reducer"; | ||
import { UPDATE_SELECTED_BRANDS } from "../components/clustering/actions"; | ||
import { setInBrandSelector } from "../components/clustering/keyDownListener"; | ||
import { getMacroClusters } from "./macroClusters"; | ||
import React from "react"; | ||
import { getMacroClusters, updateMacroCluster } from "./macroClusters"; | ||
import { getMicroClusters } from "./microClusters"; | ||
import { assignCluster } from "./assignCluster"; | ||
|
||
export const StateContext = React.createContext(); | ||
export const DispatchContext = React.createContext(); | ||
|
||
export const App = () => { | ||
const [state, dispatch] = useReducer(reducer, initalState); | ||
const { loadingMacroClusters, loadingMicroClusters } = state; | ||
useEffect(() => { | ||
getMicroClusters(dispatch); | ||
}, []); | ||
useEffect(() => { | ||
getMacroClusters(dispatch); | ||
}, []); | ||
useEffect(() => { | ||
if ( | ||
loadingMicroClusters || | ||
loadingMacroClusters || | ||
state.microClusters.length > 0 | ||
) | ||
return; | ||
const intervalId = setInterval(() => { | ||
getMicroClusters(dispatch); | ||
}, 30 * 1000); | ||
return () => { | ||
clearInterval(intervalId); | ||
}; | ||
}, [loadingMicroClusters, loadingMacroClusters, state.microClusters.length]); | ||
if (!loadingMicroClusters && !loadingMacroClusters) { | ||
return ( | ||
<DispatchContext.Provider value={dispatch}> | ||
<StateContext.Provider value={state}> | ||
<div> | ||
<LoadingOverlay /> | ||
<Summary /> | ||
<BrandSelector /> | ||
<DisplayMicroClusters /> | ||
</div> | ||
</StateContext.Provider> | ||
</DispatchContext.Provider> | ||
); | ||
} else { | ||
return <Spinner text={`${state.loadingPercentage.toFixed(2)}%`} />; | ||
} | ||
}; | ||
|
||
const LoadingOverlay = () => { | ||
const { updating } = useContext(StateContext); | ||
if (!updating) return null; | ||
const style = { | ||
position: "fixed", | ||
top: 0, | ||
left: 0, | ||
height: "100%", | ||
width: "100%", | ||
zIndex: 10, | ||
backgroundColor: "rgba(0,0,0,0.5)" | ||
}; | ||
return <div style={style}></div>; | ||
}; | ||
|
||
const Summary = () => { | ||
const { microClusters, selectedMicroClusters } = useContext(StateContext); | ||
return ( | ||
<div className="summary"> | ||
<b>Total:</b> {microClusters.length} <b>In Selection:</b>{" "} | ||
{selectedMicroClusters.length} | ||
</div> | ||
); | ||
}; | ||
import { GenericApp } from "./GenericApp"; | ||
|
||
const BrandSelector = () => { | ||
const dispatch = useContext(DispatchContext); | ||
const { microClusters, selectedBrands } = useContext(StateContext); | ||
const values = _.countBy(microClusters.map((c) => c.simplified_brand_name)); | ||
const options = _.sortBy( | ||
_.map(values, (value, key) => ({ | ||
value: key, | ||
label: `${key} (${value})` | ||
})), | ||
"label" | ||
); | ||
return ( | ||
<div className="mb-3"> | ||
<Select | ||
options={options} | ||
onChange={(selected) => { | ||
dispatch({ type: UPDATE_SELECTED_BRANDS, payload: selected }); | ||
}} | ||
isMulti | ||
value={selectedBrands} | ||
onFocus={() => { | ||
setInBrandSelector(true); | ||
}} | ||
onBlur={() => { | ||
setInBrandSelector(false); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
export const App = () => ( | ||
<GenericApp | ||
brandSelectorField="simplified_brand_name" | ||
microClusterLoader={getMicroClusters} | ||
macroClusterLoader={getMacroClusters} | ||
macroClusterUpdater={updateMacroCluster} | ||
assignCluster={assignCluster} | ||
/> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.