-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add shared models and reusable hooks for table state management…
… and request params preparation
- Loading branch information
Showing
18 changed files
with
1,149 additions
and
841 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,152 +1,18 @@ | ||
import { rest } from 'msw'; | ||
import { parse, stringify } from 'qs'; | ||
import { stringify } from 'qs'; | ||
import axios from 'axios'; | ||
|
||
import { DataGridRequestParams } from './types'; | ||
export type ExampleType = { | ||
date: string; | ||
id: string; | ||
email: string; | ||
amount: string; | ||
status: 'success' | 'error'; | ||
firstName: string; | ||
lastName: string; | ||
}; | ||
export const exampleData: ExampleType[] = [ | ||
{ | ||
date: '2023-08-25', | ||
id: '1001', | ||
email: '[email protected]', | ||
amount: '50.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-24', | ||
id: '1002', | ||
email: '[email protected]', | ||
amount: '75.00', | ||
status: 'error', | ||
}, | ||
{ | ||
date: '2023-08-23', | ||
id: '1003', | ||
email: '[email protected]', | ||
amount: '100.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-22', | ||
id: '1004', | ||
email: '[email protected]', | ||
amount: '30.00', | ||
status: 'error', | ||
}, | ||
{ | ||
date: '2023-08-21', | ||
id: '1005', | ||
email: '[email protected]', | ||
amount: '60.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-20', | ||
id: '1006', | ||
email: '[email protected]', | ||
amount: '90.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-19', | ||
id: '1007', | ||
email: '[email protected]', | ||
amount: '120.00', | ||
status: 'error', | ||
}, | ||
{ | ||
date: '2023-08-18', | ||
id: '1008', | ||
email: '[email protected]', | ||
amount: '25.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-17', | ||
id: '1009', | ||
email: '[email protected]', | ||
amount: '70.00', | ||
status: 'error', | ||
}, | ||
{ | ||
date: '2023-08-16', | ||
id: '1010', | ||
email: '[email protected]', | ||
amount: '110.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-15', | ||
id: '1011', | ||
email: '[email protected]', | ||
amount: '40.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-14', | ||
id: '1012', | ||
email: '[email protected]', | ||
amount: '80.00', | ||
status: 'error', | ||
}, | ||
{ | ||
date: '2023-08-13', | ||
id: '1013', | ||
email: '[email protected]', | ||
amount: '95.00', | ||
status: 'success', | ||
}, | ||
{ | ||
date: '2023-08-12', | ||
id: '1014', | ||
email: '[email protected]', | ||
amount: '55.00', | ||
status: 'error', | ||
}, | ||
{ | ||
date: '2023-08-11', | ||
id: '1015', | ||
email: '[email protected]', | ||
amount: '65.00', | ||
status: 'success', | ||
}, | ||
]; | ||
const mockedApiBaseUrl = 'https://table-api.com/api'; | ||
export const getDataMockHandler = rest.get(`${mockedApiBaseUrl}`, (req, res, ctx) => { | ||
const params = parse(req.url.searchParams.toString()); | ||
const { page, size } = params; | ||
const total = exampleData.length; | ||
const sizeNumber = Number(size); | ||
const pageNumber = Number(page); | ||
const totalPages = Math.ceil(total / sizeNumber); | ||
const pages = exampleData.reduce<ExampleType[][]>((accumulator, value, index) => { | ||
const entryIndex = Math.floor(index / sizeNumber); | ||
const page = accumulator[entryIndex] || (accumulator[entryIndex] = []); | ||
page.push(value); | ||
return accumulator; | ||
}, []); | ||
return res( | ||
ctx.json({ | ||
page: pages[pageNumber], | ||
meta: { | ||
page: pageNumber, | ||
size: sizeNumber, | ||
total, | ||
totalPages, | ||
}, | ||
}), | ||
); | ||
}); | ||
interface GetDataMockArguments { | ||
page: number; | ||
size: number; | ||
} | ||
export const getDataMock = async ({ page, size }: GetDataMockArguments) => { | ||
const queryParams = stringify({ page, size }, { addQueryPrefix: true }); | ||
const { data: response } = await axios.get(`${mockedApiBaseUrl}${queryParams}`); | ||
|
||
const apiUrl = 'http://localhost:1337/api/example/users'; | ||
|
||
export const getDataMock = async (params: DataGridRequestParams) => { | ||
const queryParams = stringify(params, { addQueryPrefix: true }); | ||
const { data: response } = await axios.get(`${apiUrl}${queryParams}`); | ||
return response; | ||
}; |
Oops, something went wrong.