Skip to content

Commit

Permalink
fix(dashboard): handle deleted products/variants in the order domain (#…
Browse files Browse the repository at this point in the history
…9841)

* fix: optional variants and products

* fix: more cases with inventory

* fix: fulfillment creation modal

* fix: move create fulfillment actions to footer

* refactor: use properties of the LineItem

* fix: remove N/A

* fix: remove ||

* fix: show confirmed status properly

* fix: pick needed product props

* fix: typo
  • Loading branch information
fPolic authored Nov 12, 2024
1 parent 713e428 commit 81208b6
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Thumbnail } from "../../../../common/thumbnail"
import { HttpTypes } from "@medusajs/types"

type ProductCellProps = {
product: HttpTypes.AdminProduct
product: Pick<HttpTypes.AdminProduct, "thumbnail" | "title">
}

export const ProductCell = ({ product }: ProductCellProps) => {
return (
<div className="flex h-full w-full items-center gap-x-3 overflow-hidden max-w-[250px]">
<div className="flex h-full w-full max-w-[250px] items-center gap-x-3 overflow-hidden">
<div className="w-fit flex-shrink-0">
<Thumbnail src={product.thumbnail} />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function OrderAllocateItemsForm({ order }: OrderAllocateItemsFormProps) {
() =>
order.items.filter(
(item) =>
item.variant.manage_inventory &&
item.variant.inventory.length &&
item.variant?.manage_inventory &&
item.variant?.inventory.length &&
item.quantity - item.detail.fulfilled_quantity > 0
),
[order.items]
Expand All @@ -48,8 +48,8 @@ export function OrderAllocateItemsForm({ order }: OrderAllocateItemsFormProps) {
const filteredItems = useMemo(() => {
return itemsToAllocate.filter(
(i) =>
i.variant.title.toLowerCase().includes(filterTerm) ||
i.variant.product.title.toLowerCase().includes(filterTerm)
i.variant_title.toLowerCase().includes(filterTerm) ||
i.product_title.toLowerCase().includes(filterTerm)
)
}, [itemsToAllocate, filterTerm])

Expand Down Expand Up @@ -151,17 +151,17 @@ export function OrderAllocateItemsForm({ order }: OrderAllocateItemsFormProps) {

const item = itemsToAllocate.find((i) => i.id === lineItem.id)

item.variant.inventory_items.forEach((ii, ind) => {
item.variant?.inventory_items.forEach((ii, ind) => {
const num = value || 0
const inventory = item.variant.inventory[ind]
const inventory = item.variant?.inventory[ind]

form.setValue(
`quantity.${lineItem.id}-${inventory.id}`,
num * ii.required_quantity
)

if (value) {
const location = inventory.location_levels.find(
const location = inventory?.location_levels.find(
(l) => l.location_id === selectedLocationId
)
if (location) {
Expand Down Expand Up @@ -313,16 +313,16 @@ function defaultAllocations(items: OrderLineItemDTO) {
const ret = {}

items.forEach((item) => {
const hasInventoryKit = item.variant.inventory_items.length > 1
const hasInventoryKit = item.variant?.inventory_items.length > 1

ret[
hasInventoryKit
? `${item.id}-`
: `${item.id}-${item.variant.inventory[0].id}`
: `${item.id}-${item.variant?.inventory[0].id}`
] = ""

if (hasInventoryKit) {
item.variant.inventory.forEach((i) => {
item.variant?.inventory.forEach((i) => {
ret[`${item.id}-${i.id}`] = ""
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function OrderAllocateItemsItem({
const { t } = useTranslation()

const variant = item.variant
const inventory = item.variant.inventory
const inventory = item.variant?.inventory || []

const [isOpen, setIsOpen] = useState(false)

Expand All @@ -54,8 +54,6 @@ export function OrderAllocateItemsItem({
return {}
}

const { inventory } = variant

const locationInventory = inventory[0]?.location_levels?.find(
(inv) => inv.location_id === locationId
)
Expand All @@ -73,8 +71,8 @@ export function OrderAllocateItemsItem({
const hasQuantityError =
!hasInventoryKit &&
availableQuantity &&
quantityField[`${item.id}-${item.variant.inventory[0].id}`] &&
quantityField[`${item.id}-${item.variant.inventory[0].id}`] >
quantityField[`${item.id}-${item.variant?.inventory[0].id}`] &&
quantityField[`${item.id}-${item.variant?.inventory[0].id}`] >
availableQuantity

const minValue = 0
Expand All @@ -95,12 +93,12 @@ export function OrderAllocateItemsItem({
<div className="flex flex-col">
<div className="flex flex-row">
<Text className="txt-small flex" as="span" weight="plus">
{item.variant.product.title}
{item.product_title}
</Text>
{item.variant.sku && (
{item.variant_sku && (
<span className="text-ui-fg-subtle">
{" "}
({item.variant.sku})
({item.variant_sku})
</span>
)}
{hasInventoryKit && (
Expand Down Expand Up @@ -134,13 +132,13 @@ export function OrderAllocateItemsItem({
{availableQuantity &&
!hasInventoryKit &&
quantityField[
`${item.id}-${item.variant.inventory[0].id}`
`${item.id}-${item.variant?.inventory[0].id}`
] && (
<span className="text-ui-fg-error txt-small ml-1">
-
{
quantityField[
`${item.id}-${item.variant.inventory[0].id}`
`${item.id}-${item.variant?.inventory[0].id}`
]
}
</span>
Expand Down Expand Up @@ -173,7 +171,7 @@ export function OrderAllocateItemsItem({
name={
hasInventoryKit
? `quantity.${item.id}-`
: `quantity.${item.id}-${item.variant.inventory[0].id}`
: `quantity.${item.id}-${item.variant?.inventory[0].id}`
}
rules={{
required: !hasInventoryKit,
Expand All @@ -196,7 +194,7 @@ export function OrderAllocateItemsItem({
: Number(e.target.value)

onQuantityChange(
item.variant.inventory[0],
item.variant?.inventory[0],
item,
hasInventoryKit,
val,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export const AddClaimItemsTable = ({
if (q) {
results = results.filter((i) => {
return (
i.variant.product.title.toLowerCase().includes(q.toLowerCase()) ||
i.variant.title.toLowerCase().includes(q.toLowerCase()) ||
i.variant.sku?.toLowerCase().includes(q.toLowerCase())
i.product_title.toLowerCase().includes(q.toLowerCase()) ||
i.variant_title.toLowerCase().includes(q.toLowerCase()) ||
i.variant_sku?.toLowerCase().includes(q.toLowerCase())
)
})
}
Expand Down Expand Up @@ -173,14 +173,14 @@ const sortItems = (
let bValue: any

if (field === "product_title") {
aValue = a.variant.product.title
bValue = b.variant.product.title
aValue = a.product_title
bValue = b.product_title
} else if (field === "variant_title") {
aValue = a.variant.title
bValue = b.variant.title
aValue = a.variant_title
bValue = b.variant_title
} else if (field === "sku") {
aValue = a.variant.sku
bValue = b.variant.sku
aValue = a.variant_sku
bValue = b.variant_sku
} else if (field === "returnable_quantity") {
aValue = a.quantity - (a.returned_quantity || 0)
bValue = b.quantity - (b.returned_quantity || 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export const useClaimItemTableColumns = (currencyCode: string) => {
id: "product",
header: () => <ProductHeader />,
cell: ({ row }) => (
<ProductCell product={row.original.variant.product} />
<ProductCell
product={{
thumbnail: row.original.thumbnail,
title: row.original.product_title,
}}
/>
),
}),
columnHelper.accessor("variant.sku", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export const ClaimCreateForm = ({
return true
}

if (!item.variant.manage_inventory) {
if (!item.variant?.manage_inventory) {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ function ClaimInboundItem({
{item.title}{" "}
</Text>

{item.variant?.sku && <span>({item.variant.sku})</span>}
{item.variant_sku && <span>({item.variant_sku})</span>}
</div>
<Text as="div" className="text-ui-fg-subtle txt-small">
{item.variant?.product?.title}
{item.product_title}
</Text>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export const ClaimOutboundSection = ({
return true
}

if (!item.variant.manage_inventory) {
if (!item.variant?.manage_inventory) {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export const AddExchangeInboundItemsTable = ({
if (q) {
results = results.filter((i) => {
return (
i.variant.product.title.toLowerCase().includes(q.toLowerCase()) ||
i.variant.title.toLowerCase().includes(q.toLowerCase()) ||
i.variant.sku?.toLowerCase().includes(q.toLowerCase())
i.product_title.toLowerCase().includes(q.toLowerCase()) ||
i.variant_title.toLowerCase().includes(q.toLowerCase()) ||
i.variant_sku?.toLowerCase().includes(q.toLowerCase())
)
})
}
Expand Down Expand Up @@ -133,14 +133,14 @@ const sortItems = (
let bValue: any

if (field === "product_title") {
aValue = a.variant.product.title
bValue = b.variant.product.title
aValue = a.product_title
bValue = b.product_title
} else if (field === "variant_title") {
aValue = a.variant.title
bValue = b.variant.title
aValue = a.variant_title
bValue = b.variant_title
} else if (field === "sku") {
aValue = a.variant.sku
bValue = b.variant.sku
aValue = a.variant_sku
bValue = b.variant_sku
}

if (aValue < bValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export const useExchangeItemTableColumns = (currencyCode: string) => {
id: "product",
header: () => <ProductHeader />,
cell: ({ row }) => (
<ProductCell product={row.original.variant.product} />
<ProductCell
product={{
thumbnail: row.original.thumbnail,
title: row.original.product_title,
}}
/>
),
}),
columnHelper.accessor("variant.sku", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ function ExchangeInboundItem({
{item.title}{" "}
</Text>

{item.variant?.sku && <span>({item.variant.sku})</span>}
{item.variant_sku && <span>({item.variant_sku})</span>}
</div>
<Text as="div" className="text-ui-fg-subtle txt-small">
{item.variant?.product?.title}
{item.product_title}
</Text>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export const ExchangeInboundSection = ({
return true
}

if (!item.variant.manage_inventory) {
if (!item.variant?.manage_inventory) {
return true
}

Expand Down Expand Up @@ -486,7 +486,7 @@ export const ExchangeInboundSection = ({
<Text
size="small"
leading="compact"
className="text-ui-fg-muted inline ml-1"
className="text-ui-fg-muted ml-1 inline"
>
({t("fields.optional")})
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const ExchangeOutboundSection = ({
return true
}

if (!item.variant.manage_inventory) {
if (!item.variant?.manage_inventory) {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,7 @@ export function OrderCreateFulfillmentForm({
onSubmit={handleSubmit}
className="flex h-full flex-col overflow-hidden"
>
<RouteFocusModal.Header>
<div className="flex items-center justify-end gap-x-2">
<RouteFocusModal.Close asChild>
<Button size="small" variant="secondary">
{t("actions.cancel")}
</Button>
</RouteFocusModal.Close>
<Button size="small" type="submit" isLoading={isMutating}>
{t("orders.fulfillment.create")}
</Button>
</div>
</RouteFocusModal.Header>
<RouteFocusModal.Header />

<RouteFocusModal.Body className="flex h-full w-full flex-col items-center divide-y overflow-y-auto">
<div className="flex size-full flex-col items-center overflow-auto p-16">
Expand Down Expand Up @@ -309,6 +298,18 @@ export function OrderCreateFulfillmentForm({
</div>
</div>
</RouteFocusModal.Body>
<RouteFocusModal.Footer>
<div className="flex items-center justify-end gap-x-2">
<RouteFocusModal.Close asChild>
<Button size="small" variant="secondary">
{t("actions.cancel")}
</Button>
</RouteFocusModal.Close>
<Button size="small" type="submit" isLoading={isMutating}>
{t("orders.fulfillment.create")}
</Button>
</div>
</RouteFocusModal.Footer>
</KeyboundForm>
</RouteFocusModal.Form>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Thumbnail } from "../../../../../components/common/thumbnail/index"
import { useProductVariant } from "../../../../../hooks/api/products"
import { getFulfillableQuantity } from "../../../../../lib/order-item"
import { CreateFulfillmentSchema } from "./constants"
import { useReservationItems } from "../../../../../hooks/api/reservations"

type OrderEditItemProps = {
item: HttpTypes.AdminOrderLineItem
Expand All @@ -30,10 +29,13 @@ export function OrderCreateFulfillmentItem({
const { t } = useTranslation()

const { variant } = useProductVariant(
item.variant!.product_id!,
item.variant_id!,
item.product_id,
item.variant_id,
{
fields: "*inventory,*inventory.location_levels",
},
{
enabled: !!item.variant,
}
)

Expand Down Expand Up @@ -77,10 +79,10 @@ export function OrderCreateFulfillmentItem({
<Text className="txt-small" as="span" weight="plus">
{item.title}
</Text>
{item.variant?.sku && <span>({item.variant.sku})</span>}
{item.variant_sku && <span>({item.variant_sku})</span>}
</div>
<Text as="div" className="text-ui-fg-subtle txt-small">
{item.variant?.title ?? ""}
{item.variant_title}
</Text>
</div>
</div>
Expand Down
Loading

0 comments on commit 81208b6

Please sign in to comment.