Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
refactor: clean up code with ts-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed Jun 8, 2024
1 parent 56e2cbb commit a2195d9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include .env
export

.PHONY: publish
publish-all-dockers: publish-api publish-identity publish-storefront publish-backoffice
publish-all-dockers: publish-api publish-identity publish-storefront publish-backoffice publish-bff

.PHONY: publish-api
publish-api:
Expand All @@ -25,6 +25,13 @@ publish-storefront:
docker rmi rookieshop-storefront:latest
docker push ghcr.io/foxminchan/rookieshop/rookieshop-storefront:${VERSION}

.PHONY: publish-bff
publish-bff:
dotnet publish ./src/RookieShop.Bff/RookieShop.Bff.csproj --os linux --arch x64 /t:PublishContainer -c Release
docker tag rookieshop-bff:latest ghcr.io/foxminchan/rookieshop/rookieshop-bff:${VERSION}
docker rmi rookieshop-bff:latest
docker push ghcr.io/foxminchan/rookieshop/rookieshop-bff:${VERSION}

.PHONY: publish-backoffice
publish-backoffice:
docker build -f ./ui/backoffice/Dockerfile . --tag ghcr.io/foxminchan/rookieshop/rookieshop-backoffice:${VERSION}
Expand Down
26 changes: 8 additions & 18 deletions ui/backoffice/components/tables/order/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Image from "next/image"
import { Order, PaymentMethod } from "@/features/order/order.type"
import { ColumnDef } from "@tanstack/react-table"
import { format } from "date-fns"
import { match } from "ts-pattern"

import { Badge } from "@/components/ui/badge"
import { Icons } from "@/components/custom/icons"
Expand Down Expand Up @@ -58,24 +59,13 @@ export const columns: ColumnDef<Order>[] = [
header: "STATUS",
cell: (props) => {
const status = props.getValue() as string
let badgeClass = ""
switch (status) {
case "Pending":
badgeClass = "bg-yellow-700 text-white"
break
case "Shipping":
badgeClass = "bg-blue-700 text-white"
break
case "Completed":
badgeClass = "bg-green-700 text-white"
break
case "Cancelled":
badgeClass = "bg-red-700 text-white"
break
default:
badgeClass = "bg-gray-700 text-white"
break
}
const badgeClass = match(status)
.with("Pending", () => "bg-yellow-700 text-white")
.with("Shipping", () => "bg-blue-700 text-white")
.with("Completed", () => "bg-green-700 text-white")
.with("Cancelled", () => "bg-red-700 text-white")
.otherwise(() => "bg-gray-700 text-white")

return (
<Badge
variant="secondary"
Expand Down
35 changes: 19 additions & 16 deletions ui/backoffice/components/tables/product/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Product } from "@/features/product/product.type"
import { ColumnDef } from "@tanstack/react-table"
import { format } from "date-fns"
import parse from "html-react-parser"
import { match } from "ts-pattern"

import { Badge } from "@/components/ui/badge"
import {
Expand Down Expand Up @@ -48,14 +49,12 @@ export const columns: ColumnDef<Product>[] = [
header: "STATUS",
cell: (props) => {
const status = props.getValue() as string
let badgeClass = ""
if (status === "InStock") {
badgeClass = "bg-green-500 text-white"
} else if (status === "OutOfStock") {
badgeClass = "bg-yellow-500 text-white"
} else {
badgeClass = "bg-red-500 text-white"
}

const badgeClass = match(status)
.with("InStock", () => "bg-green-500 text-white")
.with("OutOfStock", () => "bg-yellow-500 text-white")
.otherwise(() => "bg-red-500 text-white")

return (
<Badge
variant="secondary"
Expand Down Expand Up @@ -93,15 +92,19 @@ export const columns: ColumnDef<Product>[] = [
header: "QUANTITY",
cell: (props) => {
const quantity = props.getValue() as number
let className = ""
const MEDIUM_QTY = 20
if (quantity >= MEDIUM_QTY) {
className = "text-green-500"
} else if (quantity < MEDIUM_QTY) {
className = "text-yellow-500"
} else {
className = "text-red-500"
}

const className = match(quantity)
.when(
(quantity) => quantity >= MEDIUM_QTY,
() => "text-green-500"
)
.when(
(quantity) => quantity < MEDIUM_QTY,
() => "text-yellow-500"
)
.otherwise(() => "text-red-500")

return <span className={className}>{quantity}</span>
},
},
Expand Down
4 changes: 1 addition & 3 deletions ui/backoffice/instrumentation.node.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"
import { Resource } from "@opentelemetry/resources"
import { NodeSDK } from "@opentelemetry/sdk-node"
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node"
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions"

const sdk = new NodeSDK({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: "backoffice",
}),
spanProcessor: new SimpleSpanProcessor(new OTLPTraceExporter()),
spanProcessors: [],
})

sdk.start()

0 comments on commit a2195d9

Please sign in to comment.