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

Add placeholder to price cells in product list #4972

Merged
merged 18 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
5 changes: 5 additions & 0 deletions .changeset/selfish-walls-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Product list no longer show a dash instead of product's price when channel is not selected. Instead 'Select channel' shortcut was added meaning that you can now use it to open filters window with 'channel' already selected.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be simpler and a bit shorter:

You can now open channel selection by clicking "Select channel" within any price cell on the product list. This means the dash "-" sign is no longer displayed there

4 changes: 4 additions & 0 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7173,6 +7173,10 @@
"jd/LWa": {
"string": "Voucher applies to all countries"
},
"jgURyO": {
"context": "product price",
"string": "Select channel"
},
"jhh/D6": {
"string": "Menu Title"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import {
useConditionalFilterContext,
} from "@dashboard/components/ConditionalFilter";
import { Box, Button, CloseIcon, DropdownButton, Popover, Text } from "@saleor/macaw-ui-next";
import React, { useState } from "react";
import React from "react";
import { useIntl } from "react-intl";

export const ExpressionFilters = () => {
const [open, setOpen] = useState(false);
const { formatMessage } = useIntl();
const { valueProvider, containerState } = useConditionalFilterContext();
const { valueProvider, containerState, filterWindow } = useConditionalFilterContext();
const clickOutside = () => {
containerState.clearEmpty();
};

return (
<Popover open={open} onOpenChange={open => setOpen(open)}>
<Popover open={filterWindow.isOpen} onOpenChange={open => filterWindow.setOpen(open)}>
<Popover.Trigger>
<DropdownButton data-test-id="filters-button">
{formatMessage(conditionalFilterMessages.popoverTrigger, {
Expand Down Expand Up @@ -46,7 +45,7 @@ export const ExpressionFilters = () => {
</Popover.Close>
</Box>
</Box>
<ConditionalFilters onClose={() => setOpen(false)} />
<ConditionalFilters onClose={() => filterWindow.setOpen(false)} />
</Box>
</Popover.Content>
</Popover>
Expand Down
2 changes: 2 additions & 0 deletions src/components/ConditionalFilter/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { FilterAPIProvider } from "../API/FilterAPIProvider";
import { FilterValueProvider } from "../FilterValueProvider";
import { LeftOperandsProvider } from "../LeftOperandsProvider";
import { useContainerState } from "../useContainerState";
import { FilterWindow } from "../useFilterWindow";

export const ConditionalFilterContext = createContext<{
apiProvider: FilterAPIProvider;
valueProvider: FilterValueProvider;
leftOperandsProvider: LeftOperandsProvider;
containerState: ReturnType<typeof useContainerState>;
filterWindow: FilterWindow;
} | null>(null);
7 changes: 7 additions & 0 deletions src/components/ConditionalFilter/context/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../constants";
import { useContainerState } from "../useContainerState";
import { useFilterLeftOperandsProvider } from "../useFilterLeftOperands";
import { useFilterWindow } from "../useFilterWindow";
import { useUrlValueProvider } from "../ValueProvider/useUrlValueProvider";
import { ConditionalFilterContext } from "./context";

Expand All @@ -23,6 +24,7 @@ export const ConditionalProductFilterProvider: FC<{
const valueProvider = useUrlValueProvider(locationSearch, "product", initialState);
const leftOperandsProvider = useFilterLeftOperandsProvider(STATIC_PRODUCT_OPTIONS);
const containerState = useContainerState(valueProvider);
const filterWindow = useFilterWindow();

return (
<ConditionalFilterContext.Provider
Expand All @@ -31,6 +33,7 @@ export const ConditionalProductFilterProvider: FC<{
valueProvider,
leftOperandsProvider,
containerState,
filterWindow,
}}
>
{children}
Expand All @@ -45,6 +48,7 @@ export const ConditionalDiscountFilterProvider: FC<{
const valueProvider = useUrlValueProvider(locationSearch, "discount");
const leftOperandsProvider = useFilterLeftOperandsProvider(STATIC_DISCOUNT_OPTIONS);
const containerState = useContainerState(valueProvider);
const filterWindow = useFilterWindow();

return (
<ConditionalFilterContext.Provider
Expand All @@ -53,6 +57,7 @@ export const ConditionalDiscountFilterProvider: FC<{
valueProvider,
leftOperandsProvider,
containerState,
filterWindow,
}}
>
{children}
Expand All @@ -69,6 +74,7 @@ export const ConditionalOrderFilterProvider: FC<{
const valueProvider = useUrlValueProvider(locationSearch, "order", initialState);
const leftOperandsProvider = useFilterLeftOperandsProvider(STATIC_ORDER_OPTIONS);
const containerState = useContainerState(valueProvider);
const filterWindow = useFilterWindow();

return (
<ConditionalFilterContext.Provider
Expand All @@ -77,6 +83,7 @@ export const ConditionalOrderFilterProvider: FC<{
valueProvider,
leftOperandsProvider,
containerState,
filterWindow,
}}
>
{children}
Expand Down
15 changes: 15 additions & 0 deletions src/components/ConditionalFilter/useFilterWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useState } from "react";

export interface FilterWindow {
isOpen: boolean;
setOpen: (open: boolean) => void;
}

export const useFilterWindow = () => {
const [open, setOpen] = useState(false);

return {
isOpen: open,
setOpen,
};
};
6 changes: 6 additions & 0 deletions src/components/Datagrid/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@glideapps/glide-data-grid/dist/index.css";

import useNavigator from "@dashboard/hooks/useNavigator";
import { usePreventHistoryBack } from "@dashboard/hooks/usePreventHistoryBack";
import { getCellAction } from "@dashboard/products/components/ProductListDatagrid/datagrid";
import DataEditor, {
CellClickedEventArgs,
DataEditorProps,
Expand Down Expand Up @@ -146,6 +147,7 @@ export const Datagrid: React.FC<DatagridProps> = ({
const { rowAnchorRef, setRowAnchorRef, setAnchorPosition } = useRowAnchor({
getRowAnchorUrl: rowAnchor,
rowMarkers,
availableColumns,
});

const { handleRowHover, hoverRow } = useRowHover({
Expand Down Expand Up @@ -258,6 +260,10 @@ export const Datagrid: React.FC<DatagridProps> = ({
onRowClick(item);
}

if (getCellAction(availableColumns, item[0])) {
return;
}

handleRowHover(args);

if (rowAnchorRef.current) {
Expand Down
7 changes: 6 additions & 1 deletion src/components/Datagrid/customCells/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ export function textCell(value: string, opts?: Partial<TextCell>): GridCell {
};
}

export function readonlyTextCell(value: string, hasCursorPointer = true): TextCell {
export function readonlyTextCell(
value: string,
hasCursorPointer = true,
style: TextCell["style"] = "normal",
): TextCell {
return {
cursor: hasCursorPointer ? "pointer" : "default",
allowOverlay: false,
readonly: true,
data: value,
displayData: value,
kind: GridCellKind.Text,
style,
};
}

Expand Down
107 changes: 107 additions & 0 deletions src/components/Datagrid/hooks/useRowAnchor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { GridMouseEventArgs } from "@glideapps/glide-data-grid";
import { render } from "@testing-library/react";
import { renderHook } from "@testing-library/react-hooks";
import React from "react";

import { useRowAnchor } from "./useRowAnchor";

jest.mock("@dashboard/hooks/useDebounce", () => jest.fn(fn => fn));

describe("useRowAnchor", () => {
const props = {
availableColumns: [
{
id: "name",
title: "Name",
width: 200,
icon: "arrowUp",
},
{
id: "description",
title: "Description",
width: 100,
icon: "arrowUp",
},
],
getRowAnchorUrl: () => "test",
rowMarkers: "checkbox-visible" as const,
};

it("should return row anchor ref", () => {
// Arrange & Act
const { result } = renderHook(() => useRowAnchor(props));

render(<a ref={result.current.setRowAnchorRef} />);

// Assert
expect(result.current.rowAnchorRef).toBeDefined();
});
it("should have anchor set", () => {
// Arrange
const { result } = renderHook(() => useRowAnchor(props));

render(<a ref={result.current.setRowAnchorRef} />);

// Act
result.current.setAnchorPosition({
kind: "cell",
location: [1, 0],
bounds: { x: 0, y: 0, width: 0, height: 0 },
isEdge: false,
shiftKey: false,
ctrlKey: false,
isFillHandle: false,
metaKey: false,
isTouch: false,
localEventX: 81,
localEventY: 39,
button: 0,
scrollEdge: [0, 0],
});

// Assert
expect(result.current.rowAnchorRef.current!).not.toBeNull();
expect(result.current.rowAnchorRef.current!.style.left).toBe("0px");
expect(result.current.rowAnchorRef.current!.href).toBe("http://localhost/test");
});
it("should not set anchor position when cell has action", () => {
// Arrange
const { result } = renderHook(() =>
useRowAnchor({
...props,
availableColumns: [
...props.availableColumns,
{
id: "price",
title: "Price",
width: 100,
icon: "arrowUp",
action: jest.fn(),
},
],
}),
);

render(<a ref={result.current.setRowAnchorRef} />);

// Act
result.current.setAnchorPosition({
kind: "cell",
location: [2, 0], // note: 2 is the index of the "price" column (with 'action')
bounds: { x: 0, y: 0, width: 0, height: 0 },
isEdge: false,
shiftKey: false,
ctrlKey: false,
isFillHandle: false,
metaKey: false,
isTouch: false,
localEventX: 81,
localEventY: 39,
button: 0,
scrollEdge: [0, 0],
} as GridMouseEventArgs);

// Assert
expect(result.current.rowAnchorRef.current!.href).toBe("");
});
});
7 changes: 6 additions & 1 deletion src/components/Datagrid/hooks/useRowAnchor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { getAppMountUri } from "@dashboard/config";
import useDebounce from "@dashboard/hooks/useDebounce";
import { getCellAction } from "@dashboard/products/components/ProductListDatagrid/datagrid";
import { DataEditorProps, GridMouseEventArgs, Item } from "@glideapps/glide-data-grid";
import { useCallback, useRef } from "react";

import { AvailableColumn } from "../types";
import { preventRowClickOnSelectionCheckbox } from "../utils";

const DEBOUNCE_TIME = 100;

export const useRowAnchor = ({
getRowAnchorUrl,
rowMarkers,
availableColumns,
}: {
getRowAnchorUrl?: (item: Item) => string;
rowMarkers?: DataEditorProps["rowMarkers"];
availableColumns?: readonly AvailableColumn[];
}) => {
const rowAnchorRef = useRef<HTMLAnchorElement | null>(null);

Expand All @@ -26,9 +30,10 @@ export const useRowAnchor = ({
return;
}

const action = getCellAction(availableColumns, args.location[0]);
const href = getRowAnchorUrl(args.location);

if (!href) {
if (!href || action) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/components/Datagrid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface AvailableColumn {
hasMenu?: boolean;
icon?: string;
themeOverride?: Partial<Theme>;
action?: (id: string) => void;
}
Loading
Loading