From b38e2c094539256e0b9e1e9fc91dcac100b89693 Mon Sep 17 00:00:00 2001 From: Emil Widlund Date: Mon, 3 Feb 2025 23:18:11 +0100 Subject: [PATCH] remove unnecessary import --- .../(header)/sales/ClientPage.tsx | 489 +++++++++--------- 1 file changed, 244 insertions(+), 245 deletions(-) diff --git a/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/sales/ClientPage.tsx b/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/sales/ClientPage.tsx index 8002fb2e23..5cc1f90a82 100644 --- a/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/sales/ClientPage.tsx +++ b/clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/sales/ClientPage.tsx @@ -1,253 +1,252 @@ -"use client"; - -import { DashboardBody } from "@/components/Layout/DashboardLayout"; -import MetricChartBox from "@/components/Metrics/MetricChartBox"; -import { MiniMetricChartBox } from "@/components/Metrics/MiniMetricChartBox"; -import ProductSelect from "@/components/Products/ProductSelect"; -import { OrderAmountWithRefund } from "@/components/Refunds/OrderAmountWithRefund"; -import { useMetrics } from "@/hooks/queries/metrics"; -import { useOrders } from "@/hooks/queries/orders"; +'use client' + +import { DashboardBody } from '@/components/Layout/DashboardLayout' +import { MiniMetricChartBox } from '@/components/Metrics/MiniMetricChartBox' +import ProductSelect from '@/components/Products/ProductSelect' +import { OrderAmountWithRefund } from '@/components/Refunds/OrderAmountWithRefund' +import { useMetrics } from '@/hooks/queries/metrics' +import { useOrders } from '@/hooks/queries/orders' import { - DataTablePaginationState, - DataTableSortingState, - getAPIParams, - serializeSearchParams, -} from "@/utils/datatable"; -import { dateToInterval } from "@/utils/metrics"; -import { Order, OrderCustomer, Organization, Product } from "@polar-sh/api"; -import Avatar from "@polar-sh/ui/components/atoms/Avatar"; + DataTablePaginationState, + DataTableSortingState, + getAPIParams, + serializeSearchParams, +} from '@/utils/datatable' +import { dateToInterval } from '@/utils/metrics' +import { Order, OrderCustomer, Organization, Product } from '@polar-sh/api' +import Avatar from '@polar-sh/ui/components/atoms/Avatar' import { - DataTable, - DataTableColumnDef, - DataTableColumnHeader, -} from "@polar-sh/ui/components/atoms/DataTable"; -import FormattedDateTime from "@polar-sh/ui/components/atoms/FormattedDateTime"; -import { RowSelectionState } from "@tanstack/react-table"; -import { useRouter } from "next/navigation"; -import React, { useEffect, useState } from "react"; + DataTable, + DataTableColumnDef, + DataTableColumnHeader, +} from '@polar-sh/ui/components/atoms/DataTable' +import FormattedDateTime from '@polar-sh/ui/components/atoms/FormattedDateTime' +import { RowSelectionState } from '@tanstack/react-table' +import { useRouter } from 'next/navigation' +import React, { useEffect, useState } from 'react' interface ClientPageProps { - organization: Organization; - pagination: DataTablePaginationState; - sorting: DataTableSortingState; - productId?: string[]; + organization: Organization + pagination: DataTablePaginationState + sorting: DataTableSortingState + productId?: string[] } const ClientPage: React.FC = ({ - organization, - pagination, - sorting, - productId, + organization, + pagination, + sorting, + productId, }) => { - const [selectedOrderState, setSelectedOrderState] = - useState({}); - - const getSearchParams = ( - pagination: DataTablePaginationState, - sorting: DataTableSortingState, - productId?: string[], - ) => { - const params = serializeSearchParams(pagination, sorting); - - if (productId) { - productId.forEach((id) => params.append("product_id", id)); - } - - return params; - }; - - const router = useRouter(); - - const setPagination = ( - updaterOrValue: - | DataTablePaginationState - | ((old: DataTablePaginationState) => DataTablePaginationState), - ) => { - const updatedPagination = - typeof updaterOrValue === "function" - ? updaterOrValue(pagination) - : updaterOrValue; - - router.push( - `/dashboard/${organization.slug}/sales?${getSearchParams( - updatedPagination, - sorting, - productId, - )}`, - ); - }; - - const setSorting = ( - updaterOrValue: - | DataTableSortingState - | ((old: DataTableSortingState) => DataTableSortingState), - ) => { - const updatedSorting = - typeof updaterOrValue === "function" - ? updaterOrValue(sorting) - : updaterOrValue; - - router.push( - `/dashboard/${organization.slug}/sales?${getSearchParams( - pagination, - updatedSorting, - productId, - )}`, - ); - }; - - const onProductSelect = (value: string[]) => { - router.push( - `/dashboard/${organization.slug}/sales?${getSearchParams( - pagination, - sorting, - value, - )}`, - ); - }; - - const ordersHook = useOrders(organization.id, { - ...getAPIParams(pagination, sorting), - productId, - }); - - const orders = ordersHook.data?.items || []; - const pageCount = ordersHook.data?.pagination.max_page ?? 1; - - const columns: DataTableColumnDef[] = [ - { - accessorKey: "customer", - enableSorting: true, - header: ({ column }) => ( - - ), - cell: (props) => { - const customer = props.getValue() as OrderCustomer; - return ( -
- -
{customer.email}
-
- ); - }, - }, - { - accessorKey: "amount", - enableSorting: true, - header: ({ column }) => ( - - ), - cell: ({ row: { original: order } }) => ( - - ), - }, - { - accessorKey: "product", - enableSorting: true, - header: ({ column }) => ( - - ), - cell: (props) => { - const product = props.getValue() as Product; - return ( - <> - {product.name} - {product.is_archived && ( - - Archived - - )} - - ); - }, - }, - { - accessorKey: "created_at", - enableSorting: true, - header: ({ column }) => ( - - ), - cell: (props) => ( - - ), - }, - ]; - - const selectedOrder = orders.find((order) => selectedOrderState[order.id]); - - useEffect(() => { - if (selectedOrder) { - router.push(`/dashboard/${organization.slug}/sales/${selectedOrder.id}`); - } - }, [selectedOrder, router, organization]); - - const { data: metricsData } = useMetrics({ - organizationId: organization.id, - startDate: new Date(organization.created_at), - endDate: new Date(), - interval: dateToInterval(new Date(organization.created_at)), - productId, - }); - - return ( - -
-
-
- -
-
-
- acc + current.orders, - 0, - )} - metric={metricsData?.metrics.orders} - /> - - -
- {orders && pageCount !== undefined && ( - { - setSelectedOrderState(row); - }} - rowSelection={selectedOrderState} - getRowId={(row) => row.id.toString()} - enableRowSelection - /> - )} -
-
- ); -}; - -export default ClientPage; + const [selectedOrderState, setSelectedOrderState] = + useState({}) + + const getSearchParams = ( + pagination: DataTablePaginationState, + sorting: DataTableSortingState, + productId?: string[], + ) => { + const params = serializeSearchParams(pagination, sorting) + + if (productId) { + productId.forEach((id) => params.append('product_id', id)) + } + + return params + } + + const router = useRouter() + + const setPagination = ( + updaterOrValue: + | DataTablePaginationState + | ((old: DataTablePaginationState) => DataTablePaginationState), + ) => { + const updatedPagination = + typeof updaterOrValue === 'function' + ? updaterOrValue(pagination) + : updaterOrValue + + router.push( + `/dashboard/${organization.slug}/sales?${getSearchParams( + updatedPagination, + sorting, + productId, + )}`, + ) + } + + const setSorting = ( + updaterOrValue: + | DataTableSortingState + | ((old: DataTableSortingState) => DataTableSortingState), + ) => { + const updatedSorting = + typeof updaterOrValue === 'function' + ? updaterOrValue(sorting) + : updaterOrValue + + router.push( + `/dashboard/${organization.slug}/sales?${getSearchParams( + pagination, + updatedSorting, + productId, + )}`, + ) + } + + const onProductSelect = (value: string[]) => { + router.push( + `/dashboard/${organization.slug}/sales?${getSearchParams( + pagination, + sorting, + value, + )}`, + ) + } + + const ordersHook = useOrders(organization.id, { + ...getAPIParams(pagination, sorting), + productId, + }) + + const orders = ordersHook.data?.items || [] + const pageCount = ordersHook.data?.pagination.max_page ?? 1 + + const columns: DataTableColumnDef[] = [ + { + accessorKey: 'customer', + enableSorting: true, + header: ({ column }) => ( + + ), + cell: (props) => { + const customer = props.getValue() as OrderCustomer + return ( +
+ +
{customer.email}
+
+ ) + }, + }, + { + accessorKey: 'amount', + enableSorting: true, + header: ({ column }) => ( + + ), + cell: ({ row: { original: order } }) => ( + + ), + }, + { + accessorKey: 'product', + enableSorting: true, + header: ({ column }) => ( + + ), + cell: (props) => { + const product = props.getValue() as Product + return ( + <> + {product.name} + {product.is_archived && ( + + Archived + + )} + + ) + }, + }, + { + accessorKey: 'created_at', + enableSorting: true, + header: ({ column }) => ( + + ), + cell: (props) => ( + + ), + }, + ] + + const selectedOrder = orders.find((order) => selectedOrderState[order.id]) + + useEffect(() => { + if (selectedOrder) { + router.push(`/dashboard/${organization.slug}/sales/${selectedOrder.id}`) + } + }, [selectedOrder, router, organization]) + + const { data: metricsData } = useMetrics({ + organizationId: organization.id, + startDate: new Date(organization.created_at), + endDate: new Date(), + interval: dateToInterval(new Date(organization.created_at)), + productId, + }) + + return ( + +
+
+
+ +
+
+
+ acc + current.orders, + 0, + )} + metric={metricsData?.metrics.orders} + /> + + +
+ {orders && pageCount !== undefined && ( + { + setSelectedOrderState(row) + }} + rowSelection={selectedOrderState} + getRowId={(row) => row.id.toString()} + enableRowSelection + /> + )} +
+
+ ) +} + +export default ClientPage