Skip to content
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
67 changes: 67 additions & 0 deletions src/components/common/customtogglebutton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
alpha,
ToggleButton,
Tooltip,
type ToggleButtonProps,
} from "@mui/material";
import type { SxProps, Theme } from "@mui/material/styles";
import { Square, SquareCheckBig } from "lucide-react";
import type { ReactNode } from "react";

type CustomToggleButtonProps = ToggleButtonProps & {
activeColor?: string;
showIcon?: boolean;
tooltip?: ReactNode;
};

const customToggleButtonSx =
(activeColor?: string): SxProps<Theme> =>
(theme) => ({
height: 40,
px: 2,
textTransform: "none",
gap: 0.5,
whiteSpace: "nowrap",
backgroundColor: "transparent",
"& [data-toggle-icon='checked']": {
display: "none",
},
"&.Mui-selected [data-toggle-icon='checked']": {
display: "inline-flex",
},
"&.Mui-selected [data-toggle-icon='unchecked']": {
display: "none",
},
"&&.Mui-selected": {
backgroundColor: alpha(activeColor ?? theme.palette.primary.main, 0.5),
},
});

const CustomToggleButton = ({
children,
activeColor,
showIcon = true,
sx,
tooltip,
...props
}: CustomToggleButtonProps) => {
const button = (
<ToggleButton {...props} sx={[customToggleButtonSx(activeColor), sx]}>
{showIcon && (
<>
<span aria-hidden="true" data-toggle-icon="unchecked">
<Square className="inline-icon" />
</span>
<span aria-hidden="true" data-toggle-icon="checked">
<SquareCheckBig className="inline-icon" />
</span>
</>
)}
{children}
</ToggleButton>
);

return tooltip ? <Tooltip title={tooltip}>{button}</Tooltip> : button;
};

export default CustomToggleButton;
25 changes: 25 additions & 0 deletions src/components/common/customtogglegroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
alpha,
ToggleButtonGroup,
type ToggleButtonGroupProps,
} from "@mui/material";
import type { SxProps, Theme } from "@mui/material/styles";

const customToggleGroupSx: SxProps<Theme> = (theme) => ({
height: 40,
borderRadius: "12px",
"& .MuiToggleButtonGroup-grouped": {
px: 2,
textTransform: "none",
},
});

const CustomToggleGroup = ({
size = "small",
sx,
...props
}: ToggleButtonGroupProps) => (
<ToggleButtonGroup {...props} size={size} sx={[customToggleGroupSx, sx]} />
);

export default CustomToggleGroup;
114 changes: 66 additions & 48 deletions src/cosm/vendors/components/locationfilter.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { useEffect, useMemo, useState } from "react";
import React, { useMemo, useState } from "react";
import {
alpha,
Autocomplete,
Box,
createFilterOptions,
TextField,
Typography,
useTheme,
type SxProps,
type Theme,
} from "@mui/material";
import { RotateCcw } from "lucide-react";
import {
formatLocation as formatLocationName,
useLocationFormatVersion,
Expand All @@ -35,6 +37,9 @@ const formatLocation = (option: LocationOption) =>
option.id === NOT_HORTUS_LOCATION
? option.name || option.id
: formatLocationName(option.name, option.id);
const filterOptions = createFilterOptions<LocationOption>({
stringify: formatLocation,
});

const LocationFilter = ({
locations,
Expand All @@ -51,68 +56,81 @@ const LocationFilter = ({
}) => {
const theme = useTheme();
const locationFormatVersion = useLocationFormatVersion();
const [inputValue, setInputValue] = useState("");
const [inputValue, setInputValue] = useState<string | null>(null);
const [open, setOpen] = useState(false);
const options = useMemo(() => {
const sortedLocations = [...locations].sort((a, b) => {
if (a.id === HORTUS_LOCATION_CODE) return -1;
if (b.id === HORTUS_LOCATION_CODE) return 1;
return formatLocation(a).localeCompare(formatLocation(b));
});
const hortus = sortedLocations.find(
const hortus = locations.find(
(location) => location.id === HORTUS_LOCATION_CODE,
);
const otherLocations = locations
.filter((location) => location.id !== HORTUS_LOCATION_CODE)
.sort((a, b) => formatLocation(a).localeCompare(formatLocation(b)));
return hortus
? [
ALL_LOCATIONS,
hortus,
NOT_HORTUS,
...sortedLocations.filter((location) => location !== hortus),
]
: [ALL_LOCATIONS, ...sortedLocations];
? [hortus, NOT_HORTUS, ALL_LOCATIONS, ...otherLocations]
: [ALL_LOCATIONS, ...otherLocations];
}, [locationFormatVersion, locations]);

useEffect(() => {
setInputValue(
value
? formatLocation(
options.find((option) => option.id === value) || { id: value },
)
: "",
);
}, [options, value]);
const selectedOption =
value === null
? ALL_LOCATIONS
: options.find((option) => option.id === value) || null;
const selectedLabel = selectedOption
? formatLocation(selectedOption)
: value || "";
const canClear = value !== HORTUS_LOCATION_CODE;

return (
<Autocomplete<LocationOption, false, false, false>
size="small"
clearIcon={<RotateCcw size={18} />}
options={options}
value={
value ? options.find((option) => option.id === value) || null : null
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
filterOptions={(locations, state) =>
state.inputValue === selectedLabel
? locations
: filterOptions(locations, state)
}
onChange={(_event, option) => {
value={selectedOption}
onChange={(event, option) => {
// MUI also reports erased text as a clear change; only the button resets.
if (!option && event.type !== "click") return;
if (!option) setOpen(false);
const nextValue =
option?.id === ALL_LOCATIONS.id ? null : option?.id || null;
setInputValue(nextValue ? formatLocation(option!) : "");
option?.id === ALL_LOCATIONS.id
? null
: option?.id || HORTUS_LOCATION_CODE;
setInputValue(null);
onChange(nextValue);
}}
inputValue={inputValue}
inputValue={inputValue ?? selectedLabel}
onInputChange={(_event, nextInputValue, reason) => {
if (reason === "input") setInputValue(nextInputValue);
if (reason !== "input") return;
setInputValue(nextInputValue);
}}
onBlur={() => setInputValue(null)}
getOptionLabel={formatLocation}
isOptionEqualToValue={(option, selected) => option.id === selected.id}
renderOption={(props, option) => (
<Box
component="li"
{...props}
sx={
option.id === NOT_HORTUS_LOCATION
? { borderBottom: `1px solid ${theme.palette.divider}`, mb: 0.5 }
: undefined
}
>
<Typography variant="body2">{formatLocation(option)}</Typography>
</Box>
)}
renderOption={(props, option) => {
const { key, ...optionProps } = props;
return (
<Box
component="li"
key={key}
{...optionProps}
sx={
option.id === ALL_LOCATIONS_ID
? {
borderBottom: `1px solid ${theme.palette.divider}`,
mb: 0.5,
}
: undefined
}
>
<Typography variant="body2">{formatLocation(option)}</Typography>
</Box>
);
}}
slotProps={{
paper: {
sx: {
Expand All @@ -125,8 +143,8 @@ const LocationFilter = ({
sx={[
{
"& .MuiAutocomplete-clearIndicator": {
visibility: value ? "visible" : "hidden",
opacity: value ? 1 : 0,
visibility: canClear ? "visible" : "hidden",
opacity: canClear ? 1 : 0,
},
"& .MuiOutlinedInput-root": {
height: 40,
Expand All @@ -146,7 +164,7 @@ const LocationFilter = ({
...(Array.isArray(sx) ? sx : sx ? [sx] : []),
]}
renderInput={(params) => (
<TextField {...params} variant="outlined" placeholder="All Locations" />
<TextField {...params} variant="outlined" placeholder="Location" />
)}
/>
);
Expand Down
14 changes: 14 additions & 0 deletions src/cosm/vendors/components/locationicon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useTheme } from "@mui/material";
import { Globe, Warehouse } from "lucide-react";

const LocationIcon = ({ locationCode }: { locationCode?: string }) => {
const theme = useTheme();

return locationCode === "HRT" ? (
<Warehouse className="inline-icon" color={theme.palette.success.light} />
) : (
<Globe className="inline-icon" color={theme.palette.error.light} />
);
};

export default LocationIcon;
41 changes: 6 additions & 35 deletions src/cosm/vendors/shoppinglistmodal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import {
ArrowUp,
ArrowDown,
Store,
Warehouse,
Globe,
FileUp,
} from "lucide-react";
import { ContentCopy } from "@mui/icons-material";
Expand All @@ -58,6 +56,7 @@ import LocationFilter, {
import InlineConfirmButton from "../components/inlineconfirmbutton";
import { Modal } from "./modal";
import VendorLastActiveIndicator from "./components/vendorlastactiveindicator";
import LocationIcon from "./components/locationicon";
import { useGlobalData } from "../../context/globaldatacontext";
import {
formatLocation as formatLocationLabel,
Expand Down Expand Up @@ -388,17 +387,7 @@ const VendorPrioritySelector: React.FC<{
<Typography variant="caption" color="text.secondary">
{location ? (
<>
{location.location_code === "HRT" ? (
<Warehouse
className="inline-icon"
color={theme.palette.success.light}
/>
) : (
<Globe
className="inline-icon"
color={theme.palette.warning.main}
/>
)}{" "}
<LocationIcon locationCode={location.location_code} />{" "}
{formatLocationLabel(
location.location_name ||
location.location_code ||
Expand Down Expand Up @@ -730,17 +719,9 @@ const CompactListItem: React.FC<{
key={location.label}
sx={{ whiteSpace: "nowrap" }}
>
{location.isHortus ? (
<Warehouse
className="inline-icon"
color={theme.palette.success.light}
/>
) : (
<Globe
className="inline-icon"
color={theme.palette.warning.main}
/>
)}{" "}
<LocationIcon
locationCode={location.isHortus ? "HRT" : undefined}
/>{" "}
{location.label}
</Box>,
" ",
Expand Down Expand Up @@ -1059,17 +1040,7 @@ const OrderSummaryPanel: React.FC<{
mb: 0.75,
}}
>
{location?.location_code === "HRT" ? (
<Warehouse
className="inline-icon"
color={theme.palette.success.light}
/>
) : (
<Globe
className="inline-icon"
color={theme.palette.warning.main}
/>
)}
<LocationIcon locationCode={location?.location_code} />
<Typography variant="subtitle2" sx={{ fontWeight: "bold" }}>
{formatLocationLabel(locationName, locationCode)}
</Typography>
Expand Down
Loading
Loading