diff --git a/packages/admin-ui/src/pages/LoyaltyPanel/IndividualDiscounts.jsx b/packages/admin-ui/src/pages/LoyaltyPanel/IndividualDiscounts.jsx
index bd151fc4..952b2a84 100644
--- a/packages/admin-ui/src/pages/LoyaltyPanel/IndividualDiscounts.jsx
+++ b/packages/admin-ui/src/pages/LoyaltyPanel/IndividualDiscounts.jsx
@@ -1,18 +1,20 @@
-import IconButton from '@mui/material/IconButton'
-import SvgIcon from '@mui/material/SvgIcon'
import { useQuery, useMutation, gql } from '@apollo/client'
import * as R from 'ramda'
-import React, { useState } from 'react'
+import React, { useState, useMemo } from 'react'
+import {
+ MaterialReactTable,
+ MRT_ActionMenuItem,
+ useMaterialReactTable,
+} from 'material-react-table'
+import Delete from '@mui/icons-material/Delete'
import { Link, Button } from '../../components/buttons'
import { DeleteDialog } from '../../components/DeleteDialog'
-import DataTable from '../../components/tables/DataTable'
import { Label3, TL1 } from '../../components/typography'
import PhoneIdIcon from '../../styling/icons/ID/phone/zodiac.svg?react'
-import DeleteIcon from '../../styling/icons/action/delete/enabled.svg?react'
+import { defaultMaterialTableOpts } from '../../utils/materialReactTableOpts'
import IndividualDiscountModal from './IndividualDiscountModal'
-import classnames from 'classnames'
const GET_INDIVIDUAL_DISCOUNTS = gql`
query individualDiscounts {
@@ -44,16 +46,6 @@ const CREATE_DISCOUNT = gql`
}
`
-const GET_CUSTOMERS = gql`
- {
- customers {
- id
- phone
- idCardData
- }
- }
-`
-
const IndividualDiscounts = () => {
const [deleteDialog, setDeleteDialog] = useState(false)
const [toBeDeleted, setToBeDeleted] = useState()
@@ -62,9 +54,11 @@ const IndividualDiscounts = () => {
const [showModal, setShowModal] = useState(false)
const toggleModal = () => setShowModal(!showModal)
- const { data: discountResponse, loading } = useQuery(GET_INDIVIDUAL_DISCOUNTS)
- const { data: customerData, loading: customerLoading } =
- useQuery(GET_CUSTOMERS)
+ const { data: discountResponse, loading } = useQuery(
+ GET_INDIVIDUAL_DISCOUNTS,
+ { notifyOnNetworkStatusChange: true },
+ )
+ const discounts = discountResponse?.individualDiscounts || []
const [createDiscount, { error: creationError }] = useMutation(
CREATE_DISCOUNT,
@@ -82,88 +76,86 @@ const IndividualDiscounts = () => {
refetchQueries: () => ['individualDiscounts'],
})
- const elements = [
- {
- header: 'Identification',
- width: 312,
- textAlign: 'left',
- size: 'sm',
- view: t => {
- return (
+ const columns = useMemo(
+ () => [
+ {
+ id: 'identification',
+ header: 'Identification',
+ size: 312,
+ accessorFn: row => row.customer.phone,
+ Cell: ({ row }) => (
-
{t.customer.phone}
+
{row.original.customer.phone}
- )
+ ),
},
- },
- {
- header: 'Name',
- width: 300,
- textAlign: 'left',
- size: 'sm',
- view: t => {
- const customer = t.customer
- if (R.isNil(customer.idCardData)) {
- return <>{'-'}>
- }
-
- return (
- <>{`${customer.idCardData.firstName ?? ``}${
+ {
+ id: 'name',
+ header: 'Name',
+ size: 300,
+ accessorFn: row => {
+ const customer = row.customer
+ if (R.isNil(customer.idCardData)) {
+ return '-'
+ }
+ return `${customer.idCardData.firstName ?? ''}${
customer.idCardData.firstName && customer.idCardData.lastName
- ? ` `
- : ``
- }${customer.idCardData.lastName ?? ``}`}>
- )
+ ? ' '
+ : ''
+ }${customer.idCardData.lastName ?? ''}`
+ },
},
+ {
+ id: 'discount',
+ header: 'Discount rate',
+ size: 220,
+ accessorKey: 'discount',
+ Cell: ({ cell }) => (
+ <>
+
{cell.getValue()} %
+ >
+ ),
+ },
+ ],
+ [],
+ )
+
+ const table = useMaterialReactTable({
+ ...defaultMaterialTableOpts,
+ columns,
+ data: discounts,
+ state: { isLoading: loading },
+ getRowId: row => row.id,
+ enableRowActions: true,
+ renderRowActionMenuItems: ({ row }) => [
+
}
+ key="delete"
+ label="Revoke"
+ onClick={() => {
+ setDeleteDialog(true)
+ setToBeDeleted({ variables: { discountId: row.original.id } })
+ }}
+ table={table}
+ />,
+ ],
+ initialState: {
+ ...defaultMaterialTableOpts.initialState,
+ columnPinning: { right: ['mrt-row-actions'] },
},
- {
- header: 'Discount rate',
- width: 220,
- textAlign: 'left',
- size: 'sm',
- view: t => (
- <>
-
{t.discount} %
- >
- ),
- },
- {
- header: 'Revoke',
- width: 100,
- textAlign: 'center',
- size: 'sm',
- view: t => (
-
{
- setDeleteDialog(true)
- setToBeDeleted({ variables: { discountId: t.id } })
- }}>
-
-
-
-
- ),
- },
- ]
+ })
return (
<>
- {!loading && !R.isEmpty(discountResponse.individualDiscounts) && (
+ {!loading && !R.isEmpty(discounts) && (
<>
-
+
Add new code
-
+
{
@@ -178,7 +170,7 @@ const IndividualDiscounts = () => {
/>
>
)}
- {!loading && R.isEmpty(discountResponse.individualDiscounts) && (
+ {!loading && R.isEmpty(discounts) && (
It seems there are no active individual customer discounts on your
@@ -195,7 +187,6 @@ const IndividualDiscounts = () => {
}}
creationError={creationError}
addDiscount={createDiscount}
- customers={R.path(['customers'])(customerData)}
/>
>
)
diff --git a/packages/server/lib/customers.js b/packages/server/lib/customers.js
index 5e768e6d..7b66b8af 100644
--- a/packages/server/lib/customers.js
+++ b/packages/server/lib/customers.js
@@ -488,7 +488,10 @@ function getSlimCustomerByIdBatch(ids) {
const sql = `SELECT id, phone, id_card_data
FROM customers
WHERE id = ANY($1::uuid[])`
- return db.any(sql, [ids]).then(customers => _.map(camelize, customers))
+ return db.any(sql, [ids]).then(customers => {
+ const customersById = _.keyBy('id', _.map(camelize, customers))
+ return ids.map(id => customersById[id] || null)
+ })
}
function getCustomersList() {