Skip to content

Commit c282424

Browse files
committed
feat: add deny and offer actions
1 parent 2dcd150 commit c282424

File tree

4 files changed

+106
-16
lines changed

4 files changed

+106
-16
lines changed

src/components/Breadcrumbs.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ export function Breadcrumbs({ parents, children }: PropsWithChildren<{ parents?:
2222
{parents && (
2323
<>
2424
{parents.map((it, index) => (
25-
<>
26-
<BreadcrumbItem key={index}>
27-
<BreadcrumbLink asChild>
28-
<Link to="/">{it}</Link>
29-
</BreadcrumbLink>
25+
<div className="flex items-center gap-2" key={index}>
26+
<BreadcrumbItem>
27+
<>{it}</>
3028
</BreadcrumbItem>
3129
<BreadcrumbSeparator />
32-
</>
30+
</div>
3331
))}
3432
</>
3533
)}

src/mocks/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { fetchAdminLookupQuote, fetchAdminQuotePending } from "./handlers/admin_quotes"
1+
import { fetchAdminLookupQuote, fetchAdminQuotePending, updateAdminQuote } from "./handlers/admin_quotes"
22
import { fetchBalances } from "./handlers/balances"
33
import { fetchInfo } from "./handlers/info"
44

5-
export const handlers = [fetchInfo, fetchBalances, fetchAdminQuotePending, fetchAdminLookupQuote]
5+
export const handlers = [fetchInfo, fetchBalances, fetchAdminQuotePending, fetchAdminLookupQuote, updateAdminQuote]

src/mocks/handlers/admin_quotes.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { http, delay, HttpResponse, StrictResponse } from "msw"
22
import { API_URL } from "@/constants/api"
33
import { ADMIN_QUOTE_BY_ID, ADMIN_QUOTE_PENDING } from "@/constants/endpoints"
4-
import { AdminLookupQuoteResponse, InfoReply, ListPendingQuotesResponse } from "@/generated/client"
4+
import { AdminLookupQuoteResponse, InfoReply, ListPendingQuotesResponse, ResolveRequest } from "@/generated/client"
55
import { db } from "../db"
66

77
export const fetchAdminQuotePending = http.get<never, never, ListPendingQuotesResponse>(
@@ -25,11 +25,42 @@ export const fetchAdminLookupQuote = http.get<never, never, AdminLookupQuoteResp
2525
await delay(1_000)
2626

2727
const data = db.quotes.getAll().filter((it) => it.id === id)
28-
2928
if (data.length === 0) {
3029
return HttpResponse.json(null, { status: 404 }) as unknown as StrictResponse<InfoReply>
3130
}
3231

3332
return HttpResponse.json(data[0] as InfoReply)
3433
},
3534
)
35+
36+
export const updateAdminQuote = http.post<never, ResolveRequest>(
37+
`${API_URL}${ADMIN_QUOTE_BY_ID}`,
38+
async ({ params, request }) => {
39+
const { id } = params
40+
const body = await request.json()
41+
42+
await delay(1_000)
43+
44+
const data = db.quotes.getAll().filter((it) => it.id === id)
45+
if (data.length === 0) {
46+
return HttpResponse.json(null, { status: 404 }) as unknown as StrictResponse<InfoReply>
47+
}
48+
49+
const quote = data[0]
50+
51+
if (body.action === "deny") {
52+
quote.status = "denied"
53+
}
54+
if (body.action === "offer") {
55+
quote.status = "offered"
56+
quote.ttl = body.ttl ?? null
57+
}
58+
59+
const updated = db.quotes.update({
60+
where: { id: { equals: quote.id } },
61+
data: quote,
62+
})
63+
64+
return HttpResponse.json(updated as InfoReply)
65+
},
66+
)

src/pages/quotes/QuotePage.tsx

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { Breadcrumbs } from "@/components/Breadcrumbs"
22
import { PageTitle } from "@/components/PageTitle"
3+
import { Button } from "@/components/ui/button"
34
import { Skeleton } from "@/components/ui/skeleton"
45
import { Table, TableBody, TableCell, TableRow } from "@/components/ui/table"
56
import { InfoReply } from "@/generated/client"
6-
import { adminLookupQuoteOptions } from "@/generated/client/@tanstack/react-query.gen"
7+
import {
8+
adminLookupQuoteOptions,
9+
adminLookupQuoteQueryKey,
10+
resolveQuoteMutation,
11+
} from "@/generated/client/@tanstack/react-query.gen"
712
import useLocalStorage from "@/hooks/use-local-storage"
8-
import { useSuspenseQuery } from "@tanstack/react-query"
13+
import { useMutation, useQueryClient, useSuspenseQuery } from "@tanstack/react-query"
914
import { Suspense } from "react"
1015
import { Link, useParams } from "react-router"
1116

@@ -17,6 +22,64 @@ function Loader() {
1722
)
1823
}
1924

25+
function QuoteActions({ value }: { value: InfoReply }) {
26+
const queryClient = useQueryClient()
27+
28+
const resolveQuote = useMutation({
29+
...resolveQuoteMutation(),
30+
onError: (error) => {
31+
console.log(error)
32+
},
33+
onSuccess: (data) => {
34+
console.log(data)
35+
void queryClient.invalidateQueries({
36+
queryKey: adminLookupQuoteQueryKey({
37+
path: {
38+
id: value.id,
39+
},
40+
}),
41+
})
42+
},
43+
})
44+
45+
const onDenyQuote = () => {
46+
resolveQuote.mutate({
47+
path: {
48+
id: value.id,
49+
},
50+
body: {
51+
action: "deny",
52+
},
53+
})
54+
}
55+
56+
const onOfferQuote = () => {
57+
resolveQuote.mutate({
58+
path: {
59+
id: value.id,
60+
},
61+
body: {
62+
action: "offer",
63+
discount: "1",
64+
ttl: "1",
65+
},
66+
})
67+
}
68+
69+
return (
70+
<>
71+
<div className="flex items-center gap-2">
72+
<Button className="flex-1" onClick={onDenyQuote} disabled={value.status === "denied"}>
73+
Deny
74+
</Button>
75+
<Button className="flex-1" onClick={onOfferQuote} disabled={value.status === "offered"}>
76+
Offer
77+
</Button>
78+
</div>
79+
</>
80+
)
81+
}
82+
2083
function Quote({ value }: { value: InfoReply }) {
2184
return (
2285
<>
@@ -31,16 +94,14 @@ function Quote({ value }: { value: InfoReply }) {
3194
<TableCell>status: </TableCell>
3295
<TableCell>{value.status}</TableCell>
3396
</TableRow>
34-
<TableRow>
35-
<TableCell>endorser: </TableCell>
36-
<TableCell>{value.endorser || "(empty)"}</TableCell>
37-
</TableRow>
3897
<TableRow>
3998
<TableCell>bill: </TableCell>
4099
<TableCell>{value.bill || "(empty)"}</TableCell>
41100
</TableRow>
42101
</TableBody>
43102
</Table>
103+
104+
<QuoteActions value={value} />
44105
</div>
45106
</>
46107
)

0 commit comments

Comments
 (0)