mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
chrone: sperate client and server to different repos.
This commit is contained in:
76
src/containers/Items/ItemsListProvider.js
Normal file
76
src/containers/Items/ItemsListProvider.js
Normal file
@@ -0,0 +1,76 @@
|
||||
import React, { createContext } from 'react';
|
||||
import { isEmpty } from 'lodash';
|
||||
import {
|
||||
getFieldsFromResourceMeta,
|
||||
transformTableQueryToParams,
|
||||
isTableEmptyStatus,
|
||||
} from 'utils';
|
||||
import { transformItemsTableState } from './utils';
|
||||
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
import { useResourceViews, useResourceMeta, useItems } from 'hooks/query';
|
||||
|
||||
const ItemsContext = createContext();
|
||||
|
||||
/**
|
||||
* Items list provider.
|
||||
*/
|
||||
function ItemsListProvider({ tableState, tableStateChanged, ...props }) {
|
||||
const tableQuery = transformItemsTableState(tableState);
|
||||
|
||||
// Fetch accounts resource views and fields.
|
||||
const { data: itemsViews, isLoading: isViewsLoading } =
|
||||
useResourceViews('items');
|
||||
|
||||
// Fetch the accounts resource fields.
|
||||
const {
|
||||
data: resourceMeta,
|
||||
isLoading: isResourceLoading,
|
||||
isFetching: isResourceFetching,
|
||||
} = useResourceMeta('items');
|
||||
|
||||
// Handle fetching the items table based on the given query.
|
||||
const {
|
||||
data: { items, pagination, filterMeta },
|
||||
isFetching: isItemsFetching,
|
||||
isLoading: isItemsLoading,
|
||||
} = useItems(
|
||||
{
|
||||
...transformTableQueryToParams(tableQuery),
|
||||
},
|
||||
{ keepPreviousData: true },
|
||||
);
|
||||
|
||||
// Detarmines the datatable empty status.
|
||||
const isEmptyStatus =
|
||||
!tableStateChanged && !isItemsLoading && isEmpty(items);
|
||||
|
||||
const state = {
|
||||
itemsViews,
|
||||
items,
|
||||
pagination,
|
||||
|
||||
fields: getFieldsFromResourceMeta(resourceMeta.fields),
|
||||
|
||||
isViewsLoading,
|
||||
isItemsLoading,
|
||||
isItemsFetching: isItemsFetching,
|
||||
isResourceLoading,
|
||||
isResourceFetching,
|
||||
|
||||
isEmptyStatus,
|
||||
};
|
||||
|
||||
return (
|
||||
<DashboardInsider
|
||||
loading={isItemsLoading || isResourceLoading}
|
||||
name={'items-list'}
|
||||
>
|
||||
<ItemsContext.Provider value={state} {...props} />
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
const useItemsListContext = () => React.useContext(ItemsContext);
|
||||
|
||||
export { ItemsListProvider, useItemsListContext };
|
||||
Reference in New Issue
Block a user