Skip to content

Commit 699937f

Browse files
committed
chore: remove unneeded hardcoded api methods
1 parent 57189a3 commit 699937f

File tree

8 files changed

+55
-108
lines changed

8 files changed

+55
-108
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
src/components/ui/*
2+
src/generated/*

src/hooks/use-api-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as client from "@/generated/client";
1+
import * as client from "@/generated/client"
22

33
export default function useApiClient() {
4-
return client;
4+
return client
55
}

src/hooks/use-local-storage.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { useState } from "react";
2-
import { getItem, setItem, removeItem } from "@/utils/local-storage";
1+
import { useState } from "react"
2+
import { getItem, setItem, removeItem } from "@/utils/local-storage"
33

4-
type DispatchAction<T> = T | ((prevState: T) => T);
4+
type DispatchAction<T> = T | ((prevState: T) => T)
55

66
export default function useLocalStorage<T>(key: string, initialValue: T) {
77
const [value, setValue] = useState(() => {
8-
const data = getItem(key);
9-
return (data || initialValue) as T;
10-
});
8+
const data = getItem(key)
9+
return (data || initialValue) as T
10+
})
1111

1212
function handleDispatch(action: DispatchAction<T>) {
1313
if (typeof action === "function") {
1414
setValue((prevState) => {
15-
const newValue = (action as (prevState: T) => T)(prevState);
16-
setItem(key, newValue);
17-
return newValue;
18-
});
15+
const newValue = (action as (prevState: T) => T)(prevState)
16+
setItem(key, newValue)
17+
return newValue
18+
})
1919
} else {
20-
setValue(action);
21-
setItem(key, action);
20+
setValue(action)
21+
setItem(key, action)
2222
}
2323
}
2424

2525
function clearState() {
26-
setValue(undefined as T);
27-
removeItem(key);
26+
setValue(undefined as T)
27+
removeItem(key)
2828
}
2929

30-
return [value, handleDispatch, clearState] as const;
30+
return [value, handleDispatch, clearState] as const
3131
}

src/lib/api.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ADMIN_QUOTE_ACCEPTED, ADMIN_QUOTE_BY_ID, ADMIN_QUOTE_PENDING, BALANCES, INFO } from "@/constants/endpoints"
1+
import { BALANCES, INFO } from "@/constants/endpoints"
22
import { apiFetch } from "@/utils/api"
33

44
export interface InfoResponse {
@@ -59,55 +59,3 @@ export async function fetchBalances(): Promise<BalancesResponse> {
5959
},
6060
})
6161
}
62-
63-
interface QuoteBase {
64-
id: string
65-
bill: string
66-
endorser: string
67-
}
68-
69-
interface QuotePending extends QuoteBase {
70-
submitted: number
71-
suggested_expiration: number
72-
}
73-
interface QuoteOffered extends QuoteBase {
74-
ttl: number
75-
signatures: unknown[]
76-
}
77-
interface QuoteDenied extends QuoteBase {
78-
tstamp: number
79-
}
80-
interface QuoteAccepted extends QuoteBase {
81-
signatures: unknown[]
82-
}
83-
interface QuoteRejected extends QuoteBase {
84-
tstamp: number
85-
}
86-
87-
export type QuoteInfoReply = QuotePending | QuoteOffered | QuoteDenied | QuoteAccepted | QuoteRejected
88-
89-
export interface QuoteListResponse {
90-
quotes: string[]
91-
}
92-
93-
export async function fetchAdminQuoteById(id: string): Promise<QuoteInfoReply> {
94-
return apiFetch<QuoteInfoReply>(ADMIN_QUOTE_BY_ID.replace(":id", id), {
95-
headers: {
96-
"Content-Type": "application/json",
97-
},
98-
})
99-
}
100-
export async function fetchAdminQuotePending(): Promise<QuoteListResponse> {
101-
return apiFetch<QuoteListResponse>(ADMIN_QUOTE_PENDING, {
102-
headers: {
103-
"Content-Type": "application/json",
104-
},
105-
})
106-
}
107-
export async function fetchAdminQuoteAccepted(): Promise<QuoteListResponse> {
108-
return apiFetch<QuoteListResponse>(ADMIN_QUOTE_ACCEPTED, {
109-
headers: {
110-
"Content-Type": "application/json",
111-
},
112-
})
113-
}

src/mocks/handlers/admin_quotes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { http, delay, HttpResponse } from "msw"
22
import { API_URL } from "@/constants/api"
3-
import type { QuoteListResponse } from "@/lib/api"
43
import { ADMIN_QUOTE_PENDING } from "@/constants/endpoints"
4+
import { ListPendingQuotesResponse } from "@/generated/client"
55

6-
export const fetchAdminQuotePending = http.get<never, never, QuoteListResponse>(
6+
export const fetchAdminQuotePending = http.get<never, never, ListPendingQuotesResponse>(
77
`${API_URL}${ADMIN_QUOTE_PENDING}`,
88
async () => {
99
await delay(1_000)

src/pages/balances/BalancesPage.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ function PageBody() {
180180
)
181181
}
182182

183-
184183
function DevSection() {
185184
const [devMode] = useLocalStorage("devMode", false)
186185
const { data } = useSuspenseQuery({
@@ -191,10 +190,9 @@ function DevSection() {
191190
return (
192191
<>
193192
{devMode && (
194-
195-
<pre className="text-sm bg-accent text-accent-foreground rounded-lg p-2 my-2">
196-
{JSON.stringify(data, null, 2)}
197-
</pre>
193+
<pre className="text-sm bg-accent text-accent-foreground rounded-lg p-2 my-2">
194+
{JSON.stringify(data, null, 2)}
195+
</pre>
198196
)}
199197
</>
200198
)

src/pages/quotes/QuotesPage.tsx

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Button } from "@/components/ui/button"
55
import { Skeleton } from "@/components/ui/skeleton"
66
import useApiClient from "@/hooks/use-api-client"
77
import useLocalStorage from "@/hooks/use-local-storage"
8-
import { fetchAdminQuotePending } from "@/lib/api"
98
import { useSuspenseQuery } from "@tanstack/react-query"
109
import { ViewIcon } from "lucide-react"
1110
import { Suspense } from "react"
@@ -19,22 +18,26 @@ function Loader() {
1918
}
2019

2120
function QuoteListPendingRaw() {
21+
const client = useApiClient()
22+
2223
const { data } = useSuspenseQuery({
2324
queryKey: ["quotes-pending"],
24-
queryFn: fetchAdminQuotePending,
25+
queryFn: () => client.listPendingQuotes(),
2526
})
2627

2728
return (
2829
<>
29-
<pre className="text-sm bg-accent text-accent-foreground rounded-lg p-2 my-2">
30-
{JSON.stringify(data, null, 2)}
31-
</pre>
30+
{data.data && (
31+
<pre className="text-sm bg-accent text-accent-foreground rounded-lg p-2 my-2">
32+
{JSON.stringify(data.data, null, 2)}
33+
</pre>
34+
)}
3235
</>
3336
)
3437
}
3538

3639
function QuoteListPending() {
37-
const client = useApiClient();
40+
const client = useApiClient()
3841

3942
const { data } = useSuspenseQuery({
4043
queryKey: ["quotes-pending"],
@@ -44,16 +47,20 @@ function QuoteListPending() {
4447
return (
4548
<>
4649
<div className="flex flex-col gap-1">
47-
{data.data ? data.data.quotes.map((it, index) => {
48-
return (
49-
<div key={index} className="flex gap-1 items-center text-sm">
50-
<span>{it}</span>
51-
<Button size="sm">
52-
<ViewIcon />
53-
</Button>
54-
</div>
55-
)
56-
}) : (<></>)}
50+
{data.data ? (
51+
data.data.quotes.map((it, index) => {
52+
return (
53+
<div key={index} className="flex gap-1 items-center text-sm">
54+
<span>{it}</span>
55+
<Button size="sm">
56+
<ViewIcon />
57+
</Button>
58+
</div>
59+
)
60+
})
61+
) : (
62+
<></>
63+
)}
5764
</div>
5865
</>
5966
)
@@ -62,13 +69,7 @@ function QuoteListPending() {
6269
function DevSection() {
6370
const [devMode] = useLocalStorage("devMode", false)
6471

65-
return (
66-
<>
67-
{devMode && (
68-
<QuoteListPendingRaw />
69-
)}
70-
</>
71-
)
72+
return <>{devMode && <QuoteListPendingRaw />}</>
7273
}
7374

7475
function PageBody() {

src/utils/local-storage.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
21
export function setItem(key: string, value: unknown) {
32
try {
4-
window.localStorage.setItem(key, JSON.stringify(value));
3+
window.localStorage.setItem(key, JSON.stringify(value))
54
} catch (err) {
6-
console.error(err);
5+
console.error(err)
76
}
87
}
98

109
export function getItem<T>(key: string): T | undefined {
1110
try {
12-
const data = window.localStorage.getItem(key);
13-
return data ? (JSON.parse(data) as T) : undefined;
11+
const data = window.localStorage.getItem(key)
12+
return data ? (JSON.parse(data) as T) : undefined
1413
} catch (err) {
15-
console.error(err);
14+
console.error(err)
1615
}
1716
}
1817

1918
export function removeItem(key: string) {
2019
try {
21-
window.localStorage.removeItem(key);
20+
window.localStorage.removeItem(key)
2221
} catch (err) {
23-
console.error(err);
22+
console.error(err)
2423
}
2524
}

0 commit comments

Comments
 (0)