-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce intial component for catalog discounts (#4414)
Co-authored-by: Patryk Andrzejewski <[email protected]>
- Loading branch information
1 parent
c4b6085
commit 061dab8
Showing
30 changed files
with
625 additions
and
9 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"saleor-dashboard": minor | ||
--- | ||
|
||
Introduce intial component for catalog discounts |
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
65 changes: 65 additions & 0 deletions
65
src/discounts/components/DiscountRules/DiscountRules.test.tsx
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,65 @@ | ||
import { act, render, screen, waitFor } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import React from "react"; | ||
|
||
import { DiscountRules } from "./DiscountRules"; | ||
|
||
jest.mock("react-intl", () => ({ | ||
useIntl: jest.fn(() => ({ | ||
formatMessage: jest.fn(x => x.defaultMessage), | ||
})), | ||
defineMessages: jest.fn(x => x), | ||
})); | ||
|
||
describe("DiscountRules", () => { | ||
it("should render placeholder when no rules", () => { | ||
// Arrange & Act | ||
render(<DiscountRules rules={[]} onRuleAdd={jest.fn()} />); | ||
|
||
// Assert | ||
expect( | ||
screen.getByText(/add your first rule to set up a promotion/i), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render discount rules", () => { | ||
// Arrange & Act | ||
render( | ||
<DiscountRules | ||
rules={[ | ||
{ | ||
id: "Catalog rule 2", | ||
name: "Catalog rule 2", | ||
description: "", | ||
}, | ||
]} | ||
onRuleAdd={jest.fn()} | ||
/>, | ||
); | ||
|
||
// Assert | ||
expect(screen.getByText(/catalog rule/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it("should fire callback when user whant to add new rule", async () => { | ||
// Arrange | ||
const onRuleAdd = jest.fn(); | ||
render(<DiscountRules rules={[]} onRuleAdd={onRuleAdd} />); | ||
|
||
// Act | ||
await act(() => { | ||
userEvent.click(screen.getByRole("button", { name: /add rule/i })); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/^catalog$/i)).toBeInTheDocument(); | ||
}); | ||
|
||
await act(async () => { | ||
await userEvent.click(screen.getByText(/^catalog$/i)); | ||
}); | ||
|
||
// Assert | ||
expect(onRuleAdd).toHaveBeenCalled(); | ||
}); | ||
}); |
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,32 @@ | ||
import { DashboardCard } from "@dashboard/components/Card"; | ||
import { Box } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
import { useIntl } from "react-intl"; | ||
|
||
import { AddButton } from "./componenets/AddButton"; | ||
import { RulesList } from "./componenets/RulesList"; | ||
import { messages } from "./messages"; | ||
import { DiscountRule } from "./types"; | ||
|
||
interface DiscountRulesProps { | ||
rules: DiscountRule[]; | ||
onRuleAdd: () => void; | ||
} | ||
|
||
export const DiscountRules = ({ onRuleAdd, rules }: DiscountRulesProps) => { | ||
const intl = useIntl(); | ||
|
||
return ( | ||
<DashboardCard> | ||
<DashboardCard.Title> | ||
<Box display="flex" justifyContent="space-between" alignItems="center"> | ||
{intl.formatMessage(messages.title)} | ||
<AddButton onCatalogClick={onRuleAdd} /> | ||
</Box> | ||
</DashboardCard.Title> | ||
<DashboardCard.Content> | ||
<RulesList rules={rules} /> | ||
</DashboardCard.Content> | ||
</DashboardCard> | ||
); | ||
}; |
61 changes: 61 additions & 0 deletions
61
src/discounts/components/DiscountRules/componenets/AddButton/AddButton.tsx
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,61 @@ | ||
import { MenuItem, SubMenu } from "@dashboard/components/SubMenu"; | ||
import { | ||
ArrowDownIcon, | ||
Box, | ||
Button, | ||
PlusIcon, | ||
Popover, | ||
ProductsIcons, | ||
} from "@saleor/macaw-ui-next"; | ||
import React, { useMemo, useState } from "react"; | ||
import { useIntl } from "react-intl"; | ||
|
||
import { messages } from "../../messages"; | ||
|
||
interface AddButtonProps { | ||
onCatalogClick: () => void; | ||
} | ||
|
||
export const AddButton = ({ onCatalogClick }: AddButtonProps) => { | ||
const intl = useIntl(); | ||
const [isSubMenuOpen, setSubMenuOpen] = useState(false); | ||
|
||
const handleCatalogClick = () => { | ||
onCatalogClick(); | ||
setSubMenuOpen(false); | ||
}; | ||
|
||
const subMenuItems = useMemo<MenuItem[]>( | ||
() => [ | ||
{ | ||
id: "catalog", | ||
title: intl.formatMessage(messages.catalog), | ||
description: intl.formatMessage(messages.catalogDescription), | ||
icon: <ProductsIcons />, | ||
onClick: handleCatalogClick, | ||
}, | ||
], | ||
[], | ||
); | ||
|
||
return ( | ||
<Popover open={isSubMenuOpen} onOpenChange={setSubMenuOpen}> | ||
<Popover.Trigger> | ||
<Button | ||
type="button" | ||
backgroundColor="interactiveNeutralDefault" | ||
color="textNeutralContrasted" | ||
> | ||
<PlusIcon /> | ||
{intl.formatMessage(messages.addRule)} | ||
<ArrowDownIcon /> | ||
</Button> | ||
</Popover.Trigger> | ||
<Popover.Content align="end"> | ||
<Box marginTop={1}> | ||
<SubMenu menuItems={subMenuItems} /> | ||
</Box> | ||
</Popover.Content> | ||
</Popover> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
src/discounts/components/DiscountRules/componenets/AddButton/index.ts
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 @@ | ||
export * from "./AddButton"; |
51 changes: 51 additions & 0 deletions
51
src/discounts/components/DiscountRules/componenets/DiscountTypeSwitch/DiscountTypeSwitch.tsx
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,51 @@ | ||
import { Box, Switch, Text } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
|
||
import { DiscountType } from "../../types"; | ||
|
||
interface DiscountTypeSwitchProps { | ||
selected: DiscountType; | ||
currencySymbol: string; | ||
onChange: (type: string) => void; | ||
} | ||
|
||
const PERCENT_SYMBOL = "%"; | ||
|
||
export const DiscountTypeSwitch = ({ | ||
selected, | ||
currencySymbol, | ||
onChange, | ||
}: DiscountTypeSwitchProps) => { | ||
return ( | ||
<Switch | ||
defaultValue={selected} | ||
onValueChange={onChange} | ||
height={14} | ||
paddingY={1.5} | ||
__width="fit-content" | ||
> | ||
<Switch.Item id="fixed" value="fixed" marginLeft={0.5}> | ||
<Box | ||
display="flex" | ||
justifyContent="center" | ||
alignItems="center" | ||
width={9} | ||
height="100%" | ||
> | ||
<Text>{currencySymbol}</Text> | ||
</Box> | ||
</Switch.Item> | ||
<Switch.Item id="percentage" value="percentage" marginRight={0.5}> | ||
<Box | ||
display="flex" | ||
justifyContent="center" | ||
alignItems="center" | ||
width={9} | ||
height="100%" | ||
> | ||
<Text>{PERCENT_SYMBOL}</Text> | ||
</Box> | ||
</Switch.Item> | ||
</Switch> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
src/discounts/components/DiscountRules/componenets/DiscountTypeSwitch/index.ts
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 @@ | ||
export * from "./DiscountTypeSwitch"; |
27 changes: 27 additions & 0 deletions
27
src/discounts/components/DiscountRules/componenets/Placeholder/Placeholder.tsx
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,27 @@ | ||
import { Box, PlusIcon, Text } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
import { useIntl } from "react-intl"; | ||
|
||
import { messages } from "../../messages"; | ||
|
||
export const Placeholder = () => { | ||
const intl = useIntl(); | ||
|
||
return ( | ||
<Box | ||
paddingX={4} | ||
paddingY={12} | ||
display="flex" | ||
flexDirection="column" | ||
alignItems="center" | ||
gap={4} | ||
borderRadius={4} | ||
borderColor="neutralPlain" | ||
borderWidth={1} | ||
borderStyle="solid" | ||
> | ||
<PlusIcon size="large" color="iconNeutralSubdued" /> | ||
<Text size="large">{intl.formatMessage(messages.placeholder)}</Text> | ||
</Box> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
src/discounts/components/DiscountRules/componenets/Placeholder/index.ts
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 @@ | ||
export * from "./Placeholder"; |
Oops, something went wrong.