mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
refactoring: migrating to react-query to manage service-side state.
This commit is contained in:
45
client/src/containers/ItemsCategories/ItemCategoriesList.js
Normal file
45
client/src/containers/ItemsCategories/ItemCategoriesList.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import React, { useEffect } from 'react';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { useIntl } from 'react-intl';
|
||||
|
||||
import DashboardPageContent from 'components/Dashboard/DashboardPageContent';
|
||||
|
||||
import ItemsCategoriesAlerts from './ItemsCategoriesAlerts';
|
||||
import ItemsCategoryActionsBar from './ItemsCategoryActionsBar';
|
||||
import { ItemsCategoriesProvider } from './ItemsCategoriesProvider';
|
||||
import ItemCategoriesViewPage from './ItemCategoriesViewPage';
|
||||
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import { compose } from 'utils';
|
||||
|
||||
/**
|
||||
* Item categories list.
|
||||
*/
|
||||
const ItemCategoryList = ({
|
||||
// #withDashboardActions
|
||||
changePageTitle,
|
||||
}) => {
|
||||
const { id } = useParams();
|
||||
const { formatMessage } = useIntl();
|
||||
|
||||
useEffect(() => {
|
||||
id
|
||||
? changePageTitle(formatMessage({ id: 'edit_category_details' }))
|
||||
: changePageTitle(formatMessage({ id: 'category_list' }));
|
||||
}, [id, changePageTitle, formatMessage]);
|
||||
|
||||
return (
|
||||
<ItemsCategoriesProvider query={{}}>
|
||||
<ItemsCategoryActionsBar />
|
||||
|
||||
<DashboardPageContent>
|
||||
<ItemCategoriesViewPage />
|
||||
</DashboardPageContent>
|
||||
<ItemsCategoriesAlerts />
|
||||
</ItemsCategoriesProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export default compose(
|
||||
withDashboardActions,
|
||||
)(ItemCategoryList);
|
||||
75
client/src/containers/ItemsCategories/ItemCategoriesTable.js
Normal file
75
client/src/containers/ItemsCategories/ItemCategoriesTable.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import React, { useMemo } from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
import classNames from 'classnames';
|
||||
|
||||
import { TableActionsCell } from './components';
|
||||
import DataTable from 'components/DataTable';
|
||||
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
||||
|
||||
import { useItemsCategoriesContext } from './ItemsCategoriesProvider';
|
||||
import { CLASSES } from 'common/classes';
|
||||
|
||||
/**
|
||||
* Items categories table.
|
||||
*/
|
||||
export default function ItemsCategoryTable({
|
||||
// #ownProps
|
||||
tableProps,
|
||||
}) {
|
||||
const { formatMessage } = useIntl();
|
||||
const {
|
||||
isItemsCategoriesFetching,
|
||||
itemsCategories,
|
||||
} = useItemsCategoriesContext();
|
||||
|
||||
// Table columns.
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
id: 'name',
|
||||
Header: formatMessage({ id: 'category_name' }),
|
||||
accessor: 'name',
|
||||
width: 220,
|
||||
},
|
||||
{
|
||||
id: 'description',
|
||||
Header: formatMessage({ id: 'description' }),
|
||||
accessor: 'description',
|
||||
className: 'description',
|
||||
width: 220,
|
||||
},
|
||||
{
|
||||
id: 'count',
|
||||
Header: formatMessage({ id: 'count' }),
|
||||
accessor: 'count',
|
||||
className: 'count',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
Header: '',
|
||||
Cell: TableActionsCell,
|
||||
className: 'actions',
|
||||
width: 50,
|
||||
},
|
||||
],
|
||||
[formatMessage],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={classNames(CLASSES.DASHBOARD_DATATABLE)}>
|
||||
<DataTable
|
||||
noInitialFetch={true}
|
||||
columns={columns}
|
||||
data={itemsCategories}
|
||||
loading={isItemsCategoriesFetching}
|
||||
manualSortBy={true}
|
||||
expandable={true}
|
||||
sticky={true}
|
||||
selectionColumn={true}
|
||||
TableLoadingRenderer={TableSkeletonRows}
|
||||
{...tableProps}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import React from 'react';
|
||||
import ItemCategoriesDataTable from './ItemCategoriesTable';
|
||||
|
||||
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
|
||||
import { compose } from 'utils';
|
||||
|
||||
/**
|
||||
* Items categories view page.
|
||||
*/
|
||||
function ItemsCategoriesViewPage({
|
||||
// #withAlertsActions
|
||||
openAlert,
|
||||
|
||||
// #withDialogActions
|
||||
openDialog,
|
||||
}) {
|
||||
// Handle selected rows change.
|
||||
const handleSelectedRowsChange = (selectedRows) => {};
|
||||
|
||||
// Handle delete Item.
|
||||
const handleDeleteCategory = ({ id }) => {
|
||||
openAlert('item-category-delete', { itemCategoryId: id });
|
||||
};
|
||||
|
||||
// Handle Edit item category.
|
||||
const handleEditCategory = (category) => {
|
||||
openDialog('item-category-form', { action: 'edit', id: category.id });
|
||||
};
|
||||
|
||||
return (
|
||||
<ItemCategoriesDataTable
|
||||
tableProps={{
|
||||
payload: {
|
||||
onDeleteCategory: handleDeleteCategory,
|
||||
onEditCategory: handleEditCategory,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withDialogActions,
|
||||
withAlertActions,
|
||||
)(ItemsCategoriesViewPage);
|
||||
@@ -0,0 +1,12 @@
|
||||
import React from 'react';
|
||||
import ItemCategoryDeleteAlert from 'containers/Alerts/Items/ItemCategoryDeleteAlert';
|
||||
// import ItemCategoryBulkDeleteAlert from 'containers/Alerts/Items/ItemCategoryBulkDeleteAlert';
|
||||
|
||||
export default function ItemsCategoriesAlerts() {
|
||||
return (
|
||||
<div class="items-categories-alerts">
|
||||
<ItemCategoryDeleteAlert name={'item-category-delete'} />
|
||||
{/* <ItemCategoryBulkDeleteAlert name={'item-categories-bulk-delete'} /> */}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import React, { createContext } from 'react';
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
import { useItemsCategories } from 'hooks/query';
|
||||
|
||||
const ItemsCategoriesContext = createContext();
|
||||
|
||||
/**
|
||||
* Items categories provider.
|
||||
*/
|
||||
function ItemsCategoriesProvider({ query, ...props }) {
|
||||
const {
|
||||
data: { itemsCategories, pagination },
|
||||
isFetching: isItemsCategoriesFetching,
|
||||
} = useItemsCategories();
|
||||
|
||||
const state = {
|
||||
isItemsCategoriesFetching,
|
||||
itemsCategories,
|
||||
pagination,
|
||||
};
|
||||
|
||||
return (
|
||||
<DashboardInsider name={'item-category-list'}>
|
||||
<ItemsCategoriesContext.Provider value={state} {...props} />
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
const useItemsCategoriesContext = () =>
|
||||
React.useContext(ItemsCategoriesContext);
|
||||
|
||||
export {
|
||||
ItemsCategoriesProvider,
|
||||
useItemsCategoriesContext,
|
||||
};
|
||||
110
client/src/containers/ItemsCategories/ItemsCategoryActionsBar.js
Normal file
110
client/src/containers/ItemsCategories/ItemsCategoryActionsBar.js
Normal file
@@ -0,0 +1,110 @@
|
||||
import React from 'react';
|
||||
import {
|
||||
NavbarGroup,
|
||||
NavbarDivider,
|
||||
Button,
|
||||
Classes,
|
||||
Intent,
|
||||
Popover,
|
||||
Position,
|
||||
PopoverInteractionKind,
|
||||
} from '@blueprintjs/core';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
import classNames from 'classnames';
|
||||
import { If, Icon } from 'components';
|
||||
|
||||
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
|
||||
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import withItemCategories from './withItemCategories';
|
||||
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||
|
||||
import { compose } from 'utils';
|
||||
|
||||
/**
|
||||
* Items categories actions bar.
|
||||
*/
|
||||
function ItemsCategoryActionsBar({
|
||||
// #withItemCategories
|
||||
itemCategoriesSelectedRows,
|
||||
|
||||
// #withDialog
|
||||
openDialog,
|
||||
|
||||
// #withAlertActions
|
||||
openAlert,
|
||||
}) {
|
||||
const onClickNewCategory = () => {
|
||||
openDialog('item-category-form', {});
|
||||
};
|
||||
|
||||
// Handle the items categories bulk delete.
|
||||
const handelBulkDelete = () => {
|
||||
openAlert('item-categories-bulk-delete', {
|
||||
itemCategoriesIds: itemCategoriesSelectedRows,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<DashboardActionsBar>
|
||||
<NavbarGroup>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon="plus" />}
|
||||
text={<T id={'new_category'} />}
|
||||
onClick={onClickNewCategory}
|
||||
/>
|
||||
<NavbarDivider />
|
||||
|
||||
<Popover
|
||||
minimal={true}
|
||||
// content={filterDropdown}
|
||||
interactionKind={PopoverInteractionKind.CLICK}
|
||||
position={Position.BOTTOM_LEFT}
|
||||
canOutsideClickClose={true}
|
||||
>
|
||||
<Button
|
||||
className={classNames(Classes.MINIMAL, 'button--filter')}
|
||||
text={
|
||||
true ? (
|
||||
<T id={'filter'} />
|
||||
) : (
|
||||
`${0} filters applied`
|
||||
)
|
||||
}
|
||||
icon={<Icon icon="filter-16" iconSize={16} />}
|
||||
/>
|
||||
</Popover>
|
||||
|
||||
<If condition={itemCategoriesSelectedRows.length}>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon="trash-16" iconSize={16} />}
|
||||
text={<T id={'delete'} />}
|
||||
intent={Intent.DANGER}
|
||||
onClick={handelBulkDelete}
|
||||
/>
|
||||
</If>
|
||||
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon="file-import-16" iconSize={16} />}
|
||||
text={<T id={'import'} />}
|
||||
/>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon="file-export-16" iconSize={16} />}
|
||||
text={<T id={'export'} />}
|
||||
/>
|
||||
</NavbarGroup>
|
||||
</DashboardActionsBar>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withDialogActions,
|
||||
withItemCategories(({ itemCategoriesSelectedRows }) => ({
|
||||
itemCategoriesSelectedRows,
|
||||
})),
|
||||
withAlertActions,
|
||||
)(ItemsCategoryActionsBar);
|
||||
54
client/src/containers/ItemsCategories/components.js
Normal file
54
client/src/containers/ItemsCategories/components.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import React from 'react';
|
||||
import {
|
||||
Menu,
|
||||
Popover,
|
||||
Button,
|
||||
Position,
|
||||
MenuItem,
|
||||
MenuDivider,
|
||||
Intent,
|
||||
} from '@blueprintjs/core';
|
||||
import { useIntl } from 'react-intl';
|
||||
import { Icon } from 'components';
|
||||
import { safeCallback } from 'utils';
|
||||
|
||||
/**
|
||||
* Row actions menu list.
|
||||
*/
|
||||
export function ActionMenuList({
|
||||
row: { original },
|
||||
payload: { onEditCategory, onDeleteCategory },
|
||||
}) {
|
||||
const { formatMessage } = useIntl();
|
||||
|
||||
return (
|
||||
<Menu>
|
||||
<MenuItem
|
||||
icon={<Icon icon="pen-18" />}
|
||||
text={formatMessage({ id: 'edit_category' })}
|
||||
onClick={safeCallback(onEditCategory, original)}
|
||||
/>
|
||||
<MenuDivider />
|
||||
<MenuItem
|
||||
text={formatMessage({ id: 'delete_category' })}
|
||||
intent={Intent.DANGER}
|
||||
onClick={safeCallback(onDeleteCategory, original)}
|
||||
icon={<Icon icon="trash-16" iconSize={16} />}
|
||||
/>
|
||||
</Menu>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Table actions cell.
|
||||
*/
|
||||
export function TableActionsCell(props) {
|
||||
return (
|
||||
<Popover
|
||||
content={<ActionMenuList {...props} />}
|
||||
position={Position.RIGHT_TOP}
|
||||
>
|
||||
<Button icon={<Icon icon="more-h-16" iconSize={16} />} />
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
19
client/src/containers/ItemsCategories/withItemCategories.js
Normal file
19
client/src/containers/ItemsCategories/withItemCategories.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import { connect } from 'react-redux';
|
||||
import { getItemsCategoriesListFactory } from 'store/itemCategories/ItemsCategories.selectors';
|
||||
import { getResourceViews } from 'store/customViews/customViews.selectors';
|
||||
|
||||
export default (mapState) => {
|
||||
const getItemsCategoriesList = getItemsCategoriesListFactory();
|
||||
|
||||
const mapStateToProps = (state, props) => {
|
||||
const mapped = {
|
||||
categoriesList: getItemsCategoriesList(state, props),
|
||||
itemCategoriesViews: getResourceViews(state, props, 'items_categories'),
|
||||
categoriesTableLoading: state.itemCategories.loading,
|
||||
itemCategoriesSelectedRows: state.itemCategories.selectedRows,
|
||||
};
|
||||
return mapState ? mapState(mapped, state, props) : mapState;
|
||||
};
|
||||
|
||||
return connect(mapStateToProps);
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
import { connect } from 'react-redux';
|
||||
import {
|
||||
fetchItemCategories,
|
||||
submitItemCategory,
|
||||
deleteItemCategory,
|
||||
editItemCategory,
|
||||
deleteBulkItemCategories,
|
||||
} from 'store/itemCategories/itemsCategory.actions';
|
||||
import t from 'store/types';
|
||||
|
||||
export const mapDispatchToProps = (dispatch) => ({
|
||||
requestSubmitItemCategory: (form) => dispatch(submitItemCategory({ form })),
|
||||
requestFetchItemCategories: (query) =>
|
||||
dispatch(fetchItemCategories({ query })),
|
||||
requestDeleteItemCategory: (id) => dispatch(deleteItemCategory(id)),
|
||||
requestEditItemCategory: (id, form) => dispatch(editItemCategory(id, form)),
|
||||
requestDeleteBulkItemCategories: (ids) =>
|
||||
dispatch(deleteBulkItemCategories({ ids })),
|
||||
|
||||
changeItemCategoriesView: (id) =>
|
||||
dispatch({
|
||||
type: t.ITEM_CATEGORIES_SET_CURRENT_VIEW,
|
||||
currentViewId: parseInt(id, 10),
|
||||
}),
|
||||
|
||||
addItemCategoriesTableQueries: (queries) =>
|
||||
dispatch({
|
||||
type: t.ITEM_CATEGORIES_TABLE_QUERIES_ADD,
|
||||
queries,
|
||||
}),
|
||||
setSelectedRowsCategories: (selectedRows) =>
|
||||
dispatch({
|
||||
type: t.ITEM_CATEGORY_SELECTED_ROW_SET,
|
||||
payload: { selectedRows },
|
||||
}),
|
||||
});
|
||||
|
||||
export default connect(null, mapDispatchToProps);
|
||||
@@ -0,0 +1,13 @@
|
||||
import { connect } from 'react-redux';
|
||||
import { getItemCategoryByIdFactory } from 'store/itemCategories/ItemsCategories.selectors';
|
||||
|
||||
export default () => {
|
||||
const getCategoryId = getItemCategoryByIdFactory();
|
||||
|
||||
const mapStateToProps = (state, props) => {
|
||||
return {
|
||||
itemCategoryDetail: getCategoryId(state, props),
|
||||
};
|
||||
};
|
||||
return connect(mapStateToProps);
|
||||
};
|
||||
Reference in New Issue
Block a user