-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/history integration #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
040805b
e9314a1
92308d4
ecb2ebe
f4451b4
419b76e
d41b801
bf60971
7f33f73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import { HistoryDynamic } from '@/features/history/components/history'; | ||
| import { mockHistoryData } from '@/testing/mocks/history'; | ||
| import { fetchHistory } from '@/utils/supabase/fetch-history'; | ||
|
|
||
| export default function HistoryPage() { | ||
| return <HistoryDynamic historyData={mockHistoryData} />; | ||
| export default async function HistoryPage() { | ||
| const data = await fetchHistory(); | ||
|
|
||
| return <HistoryDynamic historyData={data} />; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getUrlFromParams, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@/features/rest-client/utils/get-parameters'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { generateRouteUrl } from '@/utils/route-generator'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { insertHistory } from '@/utils/supabase/history-insert'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type State = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -176,30 +177,40 @@ export const useRestClientStore = create<RestClientStore>((set, get) => ({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: bodyWithVariables, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!result.ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(`HTTP ${result.status.toString()}: ${result.statusText}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const data: unknown = await result.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const proxyResponse = parse(ProxyResponseSchema, data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const duration = performance.now() - startTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const duration = +(performance.now() - startTime).toFixed(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const responseSize = new TextEncoder().encode(proxyResponse.body).length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dataForSave = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status: proxyResponse.status, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers: proxyResponse.headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: proxyResponse.body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timestamp, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| duration, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requestSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responseSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { error } = await insertHistory({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...dataForSave, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers: JSON.stringify(requestHeaders), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: crypto.randomUUID(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error: proxyResponse.error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (proxyResponse.error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(proxyResponse.error); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!result.ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(`HTTP ${result.status.toString()}: ${result.statusText}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status: proxyResponse.status, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| statusText: getReasonPhrase(proxyResponse.status), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers: proxyResponse.headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: proxyResponse.body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timestamp, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| duration, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requestSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responseSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response: { ...dataForSave, statusText: getReasonPhrase(proxyResponse.status), error: error?.message }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isLoading: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
204
to
215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing an error when
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,8 @@ | ||||||
| export const formateTimestamp = (locale: string, timestamp: string) => | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the function name
Suggested change
|
||||||
| new Intl.DateTimeFormat(locale, { | ||||||
| day: '2-digit', | ||||||
| month: '2-digit', | ||||||
| year: 'numeric', | ||||||
| hour: '2-digit', | ||||||
| minute: '2-digit', | ||||||
| }).format(new Date(timestamp)); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { PostgrestError } from '@supabase/supabase-js'; | ||
|
|
||
| import type { HistoryInsertData } from '@/features/history/types/history-data'; | ||
|
|
||
| import { createClient } from '@/utils/supabase/server'; | ||
|
|
||
| type FetchHistoryResponse = { | ||
| data: HistoryInsertData[] | null; | ||
| error: PostgrestError | null; | ||
| }; | ||
|
|
||
| export const fetchHistory = async () => { | ||
| const supabase = await createClient(); | ||
|
|
||
| const { data, error }: FetchHistoryResponse = await supabase | ||
| .from('history') | ||
| .select('*') | ||
| .order('timestamp', { ascending: false }); | ||
|
|
||
| if (error) { | ||
| console.error('Error fetching data:', error); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return data; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { HistoryInsertData } from '@/features/history/types/history-data'; | ||
|
|
||
| import { createClient } from '@/utils/supabase/client'; | ||
|
|
||
| export const supabase = createClient(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file creates and exports a singleton |
||
|
|
||
| export const insertHistory = async (data: HistoryInsertData) => await supabase.from('history').insert(data).single(); | ||
Uh oh!
There was an error while loading. Please reload this page.