Skip to content

Commit

Permalink
[MERX-53] Implement refunds datagrid (#4681)
Browse files Browse the repository at this point in the history
* Basic implementation of refunds datagrid

* Extract messages

* Add changeset

* Fix failing test

* Translate strings

* Move onRowClick to menuItems

* Refactor ternary

* Remove test label

* Refactor to hooks & fix test

* Group grid opts into hook

* Fix flag

* Fix test names

---------

Co-authored-by: Paweł Chyła <[email protected]>
  • Loading branch information
Droniu and poulch authored Feb 23, 2024
1 parent 9226dc9 commit 488c8e4
Show file tree
Hide file tree
Showing 13 changed files with 534 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-kangaroos-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": minor
---

Implement refunds datagrid on order details
20 changes: 16 additions & 4 deletions .featureFlags/generated.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// @ts-nocheck

import L10838 from "./images/discounts-list.png"
import W33914 from "./images/filters.png"
import E60240 from "./images/discounts-list.png"
import T97919 from "./images/filters.png"

const discounts_rules = () => (<><p><img src={L10838} alt="Discount rules"/></p>
const discounts_rules = () => (<><p><img src={E60240} alt="Discount rules"/></p>
<p>Apply the new discounts rules to narrow your promotions audience.
Set up conditions and channels that must be fulfilled to apply defined reward.</p>
</>)
const product_filters = () => (<><p><img src={W33914} alt="new filters"/></p>
const improved_refunds = () => (<><p>Experience new refund flow supporting multiple transactions.</p>

</>)
const product_filters = () => (<><p><img src={T97919} alt="new filters"/></p>
<p>Experience the new look and enhanced abilities of new fitering mechanism.
Easily combine any criteria you want, and quickly browse their values.</p>
</>)
Expand All @@ -21,6 +24,15 @@ export const AVAILABLE_FLAGS = [{
enabled: false,
payload: "default",
}
},{
name: "improved_refunds",
displayName: "Improved refunds",
component: improved_refunds,
visible: false,
content: {
enabled: false,
payload: "default",
}
},{
name: "product_filters",
displayName: "Products filtering",
Expand Down
11 changes: 11 additions & 0 deletions .featureFlags/improved-refunds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: improved_refunds
displayName: Improved refunds
enabled: false
payload: "default"
visible: false
---

Experience new refund flow supporting multiple transactions.

<!-- TODO: add image later on -->
28 changes: 28 additions & 0 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,10 @@
"context": "delete app",
"string": "Deleting this app, you will remove installation of the app. If you are paying for app subscription, remember to unsubscribe from the app in Saleor Marketplace."
},
"2v3tZ2": {
"context": "refund datagrid reason column",
"string": "Reason"
},
"2x9ZgK": {
"context": "card title",
"string": "Refund balance"
Expand Down Expand Up @@ -983,6 +987,10 @@
"context": "modal button",
"string": "Upload URL"
},
"4XRIV5": {
"context": "add new refund button",
"string": "Add new refund"
},
"4XhJY+": {
"context": "dialog header",
"string": "Add product from {channelName}"
Expand Down Expand Up @@ -1473,6 +1481,10 @@
"context": "default tax class name for new tax classes",
"string": "New tax class"
},
"8DyXiQ": {
"context": "refund datagrid status column",
"string": "Status"
},
"8EGagh": {
"context": "search box label",
"string": "Filter Countries"
Expand Down Expand Up @@ -3258,6 +3270,10 @@
"context": "select product informations to be exported",
"string": "Information exported:"
},
"JxWKvr": {
"context": "refund datagrid account column",
"string": "Account"
},
"JyQoES": {
"context": "delete attribute value",
"string": "Are you sure you want to delete \"{name}\" value?"
Expand Down Expand Up @@ -4977,6 +4993,10 @@
"context": "warehouse input",
"string": "Warehouse"
},
"W6VS7F": {
"context": "refund datagrid amount column",
"string": "Amount"
},
"W6nwjo": {
"string": "Draft"
},
Expand Down Expand Up @@ -7778,6 +7798,10 @@
"context": "order status",
"string": "Fulfilled"
},
"pnPj0b": {
"context": "refund datagrid date column",
"string": "Date"
},
"ppLwx3": {
"context": "number of categories",
"string": "Categories ({quantity})"
Expand Down Expand Up @@ -8292,6 +8316,10 @@
"context": "balance curreny missing error message",
"string": "Balance currency is missing"
},
"tZmVfa": {
"context": "refund section header",
"string": "Refund"
},
"tZnV8L": {
"context": "Header row stock label",
"string": "Stock"
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export interface AppListViewSettings {
[ListViews.ORDER_DRAFT_DETAILS_LIST]: ListSettings;
[ListViews.PRODUCT_DETAILS]: ListSettings;
[ListViews.VOUCHER_CODES]: ListSettings;
[ListViews.ORDER_REFUNDS]: ListSettings;
}
// TODO: replace with
// type AppListViewSettings = Record<ListViews, ListSettings>;

export const defaultListSettings: AppListViewSettings = {
[ListViews.APPS_LIST]: {
Expand Down Expand Up @@ -194,6 +197,10 @@ export const defaultListSettings: AppListViewSettings = {
[ListViews.VOUCHER_CODES]: {
rowNumber: PAGINATE_BY,
},
[ListViews.ORDER_REFUNDS]: {
rowNumber: PAGINATE_BY,
columns: ["status", "amount", "reason", "date", "account"],
},
};

export const APP_VERSION =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ jest.mock("react-router-dom", () => ({
Link: jest.fn(({ to, ...props }) => <a href={to} {...props} />),
}));

jest.mock("@dashboard/featureFlags", () => ({
useFlag: jest.fn(() => ({ enabled: false })),
}));

describe("OrderPaymentOrTransaction", () => {
const order = orderFixture(undefined);
const sharedProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-strict-ignore
import CardSpacer from "@dashboard/components/CardSpacer";
import { useFlag } from "@dashboard/featureFlags";
import {
OrderDetailsFragment,
OrderDetailsQuery,
Expand All @@ -11,6 +12,7 @@ import OrderAddTransaction from "../OrderAddTransaction";
import { useStyles } from "../OrderDetailsPage/styles";
import OrderGrantedRefunds from "../OrderGrantedRefunds";
import OrderPaymentSummaryCard from "../OrderPaymentSummaryCard";
import { OrderRefundDatagrid } from "../OrderRefundDatagrid";
import OrderSummaryCard from "../OrderSummaryCard";
import OrderTransaction from "../OrderTransaction";
import OrderTransactionGiftCard from "../OrderTransactionGiftCard";
Expand Down Expand Up @@ -45,19 +47,33 @@ export const OrderTransactionsWrapper: React.FC<OrderTransactionsWrapper> = ({
() => getFilteredPayments(order),
[order],
);

const { enabled } = useFlag("improved_refunds");

return (
<>
<div className={classes.cardGrid}>
<OrderSummaryCard order={order} />
<OrderPaymentSummaryCard order={order} onMarkAsPaid={onMarkAsPaid} />
</div>
<CardSpacer />
{order?.grantedRefunds?.length !== 0 ? (
<>
<OrderGrantedRefunds order={order} />
<CardSpacer />
</>
) : null}
<>
{enabled && (
<>
<OrderRefundDatagrid
orderId={order?.id}
grantedRefunds={order?.grantedRefunds}
/>
<CardSpacer />
</>
)}
{order?.grantedRefunds?.length !== 0 && !enabled && (
<>
<OrderGrantedRefunds order={order} />
<CardSpacer />
</>
)}
</>
<div>
{order?.transactions?.map(transaction => (
<OrderTransaction
Expand Down
114 changes: 114 additions & 0 deletions src/orders/components/OrderRefundDatagrid/OrderRefundDatagrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { DashboardCard } from "@dashboard/components/Card";
import { ColumnPicker } from "@dashboard/components/Datagrid/ColumnPicker/ColumnPicker";
import { useColumns } from "@dashboard/components/Datagrid/ColumnPicker/useColumns";
import Datagrid from "@dashboard/components/Datagrid/Datagrid";
import { DatagridChangeStateContext } from "@dashboard/components/Datagrid/hooks/useDatagridChange";
import { OrderDetailsFragment } from "@dashboard/graphql";
import { orderGrantRefundEditUrl } from "@dashboard/orders/urls";
import { ListViews } from "@dashboard/types";
import { Box, Button, EditIcon, PlusIcon, Text } from "@saleor/macaw-ui-next";
import React from "react";
import { FormattedMessage } from "react-intl";
import { Link } from "react-router-dom";

import {
createGetCellContent,
useDatagridOpts,
useOrderRefundStaticColumns,
} from "./datagrid";
import { refundGridMessages } from "./messages";

interface OrderRefundDatagridProps {
grantedRefunds: OrderDetailsFragment["grantedRefunds"];
orderId: string;
}

export const OrderRefundDatagrid: React.FC<OrderRefundDatagridProps> = ({
grantedRefunds,
orderId,
}) => {
const { datagrid, currentTheme, settings, handleColumnChange } =
useDatagridOpts(ListViews.ORDER_REFUNDS);

const orderDraftDetailsStaticColumns = useOrderRefundStaticColumns();

const {
handlers,
visibleColumns,
staticColumns,
selectedColumns,
recentlyAddedColumn,
} = useColumns({
staticColumns: orderDraftDetailsStaticColumns,
selectedColumns: settings?.columns ?? [],
onSave: handleColumnChange,
});

const getCellContent = createGetCellContent({
columns: visibleColumns,
refunds: grantedRefunds,
currentTheme,
});

const getMenuItems = React.useCallback(
index => [
{
label: "",
Icon: (
<Link to={orderGrantRefundEditUrl(orderId, grantedRefunds[index].id)}>
<EditIcon />
</Link>
),
onSelect: () => false,
},
],
[],
);

return (
<DashboardCard>
<Box
paddingX={6}
paddingTop={6}
display="flex"
justifyContent="space-between"
>
<Text variant="heading">
<FormattedMessage {...refundGridMessages.refundSection} />
</Text>
{/** TODO: Add modal */}
<Button variant="secondary">
<PlusIcon />
<FormattedMessage {...refundGridMessages.addNewRefund} />
</Button>
</Box>
<DatagridChangeStateContext.Provider value={datagrid}>
<Datagrid
readonly
hasRowHover
rowMarkers="none"
columnSelect="none"
freezeColumns={2}
menuItems={getMenuItems}
verticalBorder={col => col > 1}
availableColumns={visibleColumns}
emptyText={""}
getCellContent={getCellContent}
getCellError={() => false}
rows={grantedRefunds.length}
selectionActions={() => null}
onColumnResize={handlers.onResize}
onColumnMoved={handlers.onMove}
recentlyAddedColumn={recentlyAddedColumn}
renderColumnPicker={() => (
<ColumnPicker
selectedColumns={selectedColumns}
staticColumns={staticColumns}
onToggle={handlers.onToggle}
/>
)}
/>
</DatagridChangeStateContext.Provider>
</DashboardCard>
);
};
Loading

0 comments on commit 488c8e4

Please sign in to comment.