Fix RolesContainer, refactor NestsContainer

This commit is contained in:
Matthew Penner 2021-01-03 16:25:32 -07:00
parent ce40194147
commit b1d30c1bde
8 changed files with 74 additions and 107 deletions

View file

@ -1,5 +1,7 @@
import http from '@/api/http';
import http, { getPaginationSet, PaginatedResult } from '@/api/http';
import { rawDataToNest } from '@/api/transformers';
import { createContext, useContext } from 'react';
import useSWR from 'swr';
export interface Nest {
id: number;
@ -11,10 +13,22 @@ export interface Nest {
updatedAt: Date;
}
export default (): Promise<Nest[]> => {
return new Promise((resolve, reject) => {
http.get('/api/application/nests')
.then(({ data }) => resolve((data.data || []).map(rawDataToNest)))
.catch(reject);
interface ctx {
page: number;
setPage: (value: number | ((s: number) => number)) => void;
}
export const Context = createContext<ctx>({ page: 1, setPage: () => 1 });
export default () => {
const { page } = useContext(Context);
return useSWR<PaginatedResult<Nest>>([ 'nests', page ], async () => {
const { data } = await http.get('/api/application/nests', { params: { page } });
return ({
items: (data.data || []).map(rawDataToNest),
pagination: getPaginationSet(data.meta.pagination),
});
});
};