mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
feat(branche): add crud branches.
This commit is contained in:
77
src/containers/Alerts/Branches/BranchDeleteAlert.js
Normal file
77
src/containers/Alerts/Branches/BranchDeleteAlert.js
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import intl from 'react-intl-universal';
|
||||||
|
import { FormattedMessage as T, FormattedHTMLMessage } from 'components';
|
||||||
|
import { Intent, Alert } from '@blueprintjs/core';
|
||||||
|
import { AppToaster } from 'components';
|
||||||
|
|
||||||
|
import { useDeleteBranch } from 'hooks/query';
|
||||||
|
|
||||||
|
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
|
||||||
|
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||||
|
|
||||||
|
import { compose } from 'utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Branch delete alert.
|
||||||
|
*/
|
||||||
|
function BranchDeleteAlert({
|
||||||
|
name,
|
||||||
|
|
||||||
|
// #withAlertStoreConnect
|
||||||
|
isOpen,
|
||||||
|
payload: { branchId },
|
||||||
|
|
||||||
|
// #withAlertActions
|
||||||
|
closeAlert,
|
||||||
|
}) {
|
||||||
|
|
||||||
|
const { mutateAsync: deleteBranch, isLoading } = useDeleteBranch();
|
||||||
|
|
||||||
|
// Handle cancel delete alert.
|
||||||
|
const handleCancelDelete = () => {
|
||||||
|
closeAlert(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle confirm delete branch.
|
||||||
|
const handleConfirmDeleteBranch = () => {
|
||||||
|
deleteBranch(branchId)
|
||||||
|
.then(() => {
|
||||||
|
AppToaster.show({
|
||||||
|
message: intl.get('branch.alert.delete_message'),
|
||||||
|
intent: Intent.SUCCESS,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(
|
||||||
|
({
|
||||||
|
response: {
|
||||||
|
data: { errors },
|
||||||
|
},
|
||||||
|
}) => {},
|
||||||
|
)
|
||||||
|
.finally(() => {
|
||||||
|
closeAlert(name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Alert
|
||||||
|
cancelButtonText={<T id={'cancel'} />}
|
||||||
|
confirmButtonText={<T id={'delete'} />}
|
||||||
|
icon="trash"
|
||||||
|
intent={Intent.DANGER}
|
||||||
|
isOpen={isOpen}
|
||||||
|
onCancel={handleCancelDelete}
|
||||||
|
onConfirm={handleConfirmDeleteBranch}
|
||||||
|
loading={isLoading}
|
||||||
|
>
|
||||||
|
<p>
|
||||||
|
<FormattedHTMLMessage id={'branch.once_delete_this_branch'} />
|
||||||
|
</p>
|
||||||
|
</Alert>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default compose(
|
||||||
|
withAlertStoreConnect(),
|
||||||
|
withAlertActions,
|
||||||
|
)(BranchDeleteAlert);
|
||||||
@@ -21,7 +21,8 @@ import CreditNotesAlerts from '../Sales/CreditNotes/CreditNotesAlerts';
|
|||||||
import VendorCreditNotesAlerts from '../Purchases/CreditNotes/VendorCreditNotesAlerts';
|
import VendorCreditNotesAlerts from '../Purchases/CreditNotes/VendorCreditNotesAlerts';
|
||||||
import TransactionsLockingAlerts from '../TransactionsLocking/TransactionsLockingAlerts';
|
import TransactionsLockingAlerts from '../TransactionsLocking/TransactionsLockingAlerts';
|
||||||
import WarehousesAlerts from '../Preferences/Warehouses/WarehousesAlerts';
|
import WarehousesAlerts from '../Preferences/Warehouses/WarehousesAlerts';
|
||||||
import WarehousesTransfersAlerts from '../WarehouseTransfers/WarehousesTransfersAlerts'
|
import WarehousesTransfersAlerts from '../WarehouseTransfers/WarehousesTransfersAlerts';
|
||||||
|
import BranchesAlerts from '../Preferences/Branches/BranchesAlerts';
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
...AccountsAlerts,
|
...AccountsAlerts,
|
||||||
@@ -47,5 +48,6 @@ export default [
|
|||||||
...VendorCreditNotesAlerts,
|
...VendorCreditNotesAlerts,
|
||||||
...TransactionsLockingAlerts,
|
...TransactionsLockingAlerts,
|
||||||
...WarehousesAlerts,
|
...WarehousesAlerts,
|
||||||
...WarehousesTransfersAlerts
|
...WarehousesTransfersAlerts,
|
||||||
|
...BranchesAlerts,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import intl from 'react-intl-universal';
|
||||||
|
|
||||||
import { Formik } from 'formik';
|
import { Formik } from 'formik';
|
||||||
|
import { Intent } from '@blueprintjs/core';
|
||||||
|
|
||||||
import { AppToaster } from 'components';
|
import { AppToaster } from 'components';
|
||||||
import { CreateBranchFormSchema } from './BranchForm.schema';
|
import { CreateBranchFormSchema } from './BranchForm.schema';
|
||||||
@@ -8,30 +11,67 @@ import BranchFormContent from './BranchFormContent';
|
|||||||
import { useBranchFormContext } from './BranchFormProvider';
|
import { useBranchFormContext } from './BranchFormProvider';
|
||||||
|
|
||||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||||
import { compose } from 'utils';
|
import { compose, transformToForm } from 'utils';
|
||||||
|
|
||||||
const defaultInitialValues = {
|
const defaultInitialValues = {
|
||||||
branch_name: '',
|
name: '',
|
||||||
branch_address_1: '',
|
code: '',
|
||||||
branch_address_2: '',
|
address: '',
|
||||||
phone_number: '',
|
phone_number: '',
|
||||||
email: '',
|
email: '',
|
||||||
website: '',
|
website: '',
|
||||||
branch_address_city: '',
|
city: '',
|
||||||
branch_address_country: '',
|
country: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
function BranchForm({
|
function BranchForm({
|
||||||
// #withDialogActions
|
// #withDialogActions
|
||||||
closeDialog,
|
closeDialog,
|
||||||
}) {
|
}) {
|
||||||
|
const {
|
||||||
|
dialogName,
|
||||||
|
branch,
|
||||||
|
branchId,
|
||||||
|
createBranchMutate,
|
||||||
|
editBranchMutate,
|
||||||
|
} = useBranchFormContext();
|
||||||
|
|
||||||
// Initial form values.
|
// Initial form values.
|
||||||
const initialValues = {
|
const initialValues = {
|
||||||
...defaultInitialValues,
|
...defaultInitialValues,
|
||||||
|
...transformToForm(branch, defaultInitialValues),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handles the form submit.
|
// Handles the form submit.
|
||||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {};
|
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
||||||
|
const form = { ...values };
|
||||||
|
|
||||||
|
// Handle request response success.
|
||||||
|
const onSuccess = (response) => {
|
||||||
|
AppToaster.show({
|
||||||
|
message: intl.get('branch.dialog.success_message'),
|
||||||
|
intent: Intent.SUCCESS,
|
||||||
|
});
|
||||||
|
closeDialog(dialogName);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle request response errors.
|
||||||
|
const onError = ({
|
||||||
|
response: {
|
||||||
|
data: { errors },
|
||||||
|
},
|
||||||
|
}) => {
|
||||||
|
if (errors) {
|
||||||
|
}
|
||||||
|
setSubmitting(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (branchId) {
|
||||||
|
editBranchMutate([branchId, form]).then(onSuccess).catch(onError);
|
||||||
|
} else {
|
||||||
|
createBranchMutate(form).then(onSuccess).catch(onError);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
|
import { DATATYPES_LENGTH } from 'common/dataTypes';
|
||||||
|
|
||||||
const Schema = Yup.object().shape({
|
const Schema = Yup.object().shape({
|
||||||
branch_name: Yup.string().required().label(intl.get('branch_name')),
|
name: Yup.string().required().label(intl.get('branch_name')),
|
||||||
branch_address_1: Yup.string().trim(),
|
code: Yup.string().trim().min(0).max(DATATYPES_LENGTH.STRING),
|
||||||
branch_address_2: Yup.string().trim(),
|
address: Yup.string().trim(),
|
||||||
branch_address_city: Yup.string().trim(),
|
city: Yup.string().trim(),
|
||||||
branch_address_country: Yup.string().trim(),
|
country: Yup.string().trim(),
|
||||||
website: Yup.string().url().nullable(),
|
website: Yup.string().url().nullable(),
|
||||||
phone_number: Yup.number(),
|
phone_number: Yup.number(),
|
||||||
email: Yup.string().email().nullable(),
|
email: Yup.string().email().nullable(),
|
||||||
|
|||||||
@@ -11,9 +11,10 @@ import BranchForm from './BranchForm';
|
|||||||
export default function BranchFormDialogContent({
|
export default function BranchFormDialogContent({
|
||||||
// #ownProps
|
// #ownProps
|
||||||
dialogName,
|
dialogName,
|
||||||
|
branchId,
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<BranchFormProvider dialogName={dialogName}>
|
<BranchFormProvider branchId={branchId} dialogName={dialogName}>
|
||||||
<BranchForm />
|
<BranchForm />
|
||||||
</BranchFormProvider>
|
</BranchFormProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ function BranchFormFields() {
|
|||||||
return (
|
return (
|
||||||
<div className={Classes.DIALOG_BODY}>
|
<div className={Classes.DIALOG_BODY}>
|
||||||
{/*------------ Branch Name -----------*/}
|
{/*------------ Branch Name -----------*/}
|
||||||
<FastField name={'branch_name'}>
|
<FastField name={'name'}>
|
||||||
{({ form, field, meta: { error, touched } }) => (
|
{({ form, field, meta: { error, touched } }) => (
|
||||||
<FormGroup
|
<FormGroup
|
||||||
label={<T id={'branch.dialog.label.branch_name'} />}
|
label={<T id={'branch.dialog.label.branch_name'} />}
|
||||||
@@ -34,15 +34,29 @@ function BranchFormFields() {
|
|||||||
</FormGroup>
|
</FormGroup>
|
||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
|
{/*------------ Branch Code -----------*/}
|
||||||
|
<FastField name={'code'}>
|
||||||
|
{({ form, field, meta: { error, touched } }) => (
|
||||||
|
<FormGroup
|
||||||
|
label={<T id={'branch.dialog.label.branch_code'} />}
|
||||||
|
intent={inputIntent({ error, touched })}
|
||||||
|
inline={true}
|
||||||
|
helperText={<ErrorMessage name="code" />}
|
||||||
|
className={'form-group--branch_name'}
|
||||||
|
>
|
||||||
|
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||||
|
</FormGroup>
|
||||||
|
)}
|
||||||
|
</FastField>
|
||||||
|
|
||||||
{/*------------ Branch Address 1 -----------*/}
|
{/*------------ Branch Address -----------*/}
|
||||||
<FastField name={'branch_address_1'}>
|
<FastField name={'address'}>
|
||||||
{({ form, field, meta: { error, touched } }) => (
|
{({ form, field, meta: { error, touched } }) => (
|
||||||
<FormGroup
|
<FormGroup
|
||||||
label={intl.get('branch.dialog.label.branch_address')}
|
label={intl.get('branch.dialog.label.branch_address')}
|
||||||
intent={inputIntent({ error, touched })}
|
intent={inputIntent({ error, touched })}
|
||||||
inline={true}
|
inline={true}
|
||||||
helperText={<ErrorMessage name="branch_address_1" />}
|
helperText={<ErrorMessage name="address" />}
|
||||||
className={'form-group--branch_address'}
|
className={'form-group--branch_address'}
|
||||||
>
|
>
|
||||||
<InputGroup
|
<InputGroup
|
||||||
@@ -54,24 +68,6 @@ function BranchFormFields() {
|
|||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
<BranchAddressWrap>
|
<BranchAddressWrap>
|
||||||
{/*------------ Branch Address 2 -----------*/}
|
|
||||||
<FastField name={'branch_address_2'}>
|
|
||||||
{({ form, field, meta: { error, touched } }) => (
|
|
||||||
<FormGroup
|
|
||||||
intent={inputIntent({ error, touched })}
|
|
||||||
inline={true}
|
|
||||||
helperText={<ErrorMessage name="branch_address_2" />}
|
|
||||||
className={'form-group--branch_address'}
|
|
||||||
>
|
|
||||||
<InputGroup
|
|
||||||
intent={inputIntent({ error, touched })}
|
|
||||||
placeholder={intl.get('branch.dialog.label.address_2')}
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormGroup>
|
|
||||||
)}
|
|
||||||
</FastField>
|
|
||||||
|
|
||||||
{/*------------ Branch Address City & Country-----------*/}
|
{/*------------ Branch Address City & Country-----------*/}
|
||||||
<FormGroup
|
<FormGroup
|
||||||
inline={true}
|
inline={true}
|
||||||
@@ -79,7 +75,7 @@ function BranchFormFields() {
|
|||||||
helperText={<ErrorMessage name="branch_address_2" />}
|
helperText={<ErrorMessage name="branch_address_2" />}
|
||||||
>
|
>
|
||||||
<ControlGroup>
|
<ControlGroup>
|
||||||
<FastField name={'branch_address_city'}>
|
<FastField name={'city'}>
|
||||||
{({ field, meta: { error, touched } }) => (
|
{({ field, meta: { error, touched } }) => (
|
||||||
<InputGroup
|
<InputGroup
|
||||||
intent={inputIntent({ error, touched })}
|
intent={inputIntent({ error, touched })}
|
||||||
@@ -89,7 +85,7 @@ function BranchFormFields() {
|
|||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
|
|
||||||
<FastField name={'branch_address_country'}>
|
<FastField name={'country'}>
|
||||||
{({ field, meta: { error, touched } }) => (
|
{({ field, meta: { error, touched } }) => (
|
||||||
<InputGroup
|
<InputGroup
|
||||||
intent={inputIntent({ error, touched })}
|
intent={inputIntent({ error, touched })}
|
||||||
@@ -112,7 +108,7 @@ function BranchFormFields() {
|
|||||||
helperText={<ErrorMessage name="phone_number" />}
|
helperText={<ErrorMessage name="phone_number" />}
|
||||||
className={'form-group--phone_number'}
|
className={'form-group--phone_number'}
|
||||||
>
|
>
|
||||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
<InputGroup placeholder={'https://'} {...field} />
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
|
|||||||
@@ -1,21 +1,33 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { DialogContent } from 'components';
|
import { DialogContent } from 'components';
|
||||||
// import {} from 'hooks/query';
|
import { useCreateBranch, useEditBranch, useBranch } from 'hooks/query';
|
||||||
|
|
||||||
const BranchFormContext = React.createContext();
|
const BranchFormContext = React.createContext();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Branch form dialog provider.
|
* Branch form dialog provider.
|
||||||
*/
|
*/
|
||||||
function BranchFormProvider({ dialogName, ...props }) {
|
function BranchFormProvider({ dialogName, branchId, ...props }) {
|
||||||
|
// Create and edit warehouse mutations.
|
||||||
|
const { mutateAsync: createBranchMutate } = useCreateBranch();
|
||||||
|
const { mutateAsync: editBranchMutate } = useEditBranch();
|
||||||
|
|
||||||
|
// Handle fetch branch detail.
|
||||||
|
const { data: branch, isLoading: isBranchLoading } = useBranch(branchId, {
|
||||||
|
enabled: !!branchId,
|
||||||
|
});
|
||||||
|
|
||||||
// State provider.
|
// State provider.
|
||||||
const provider = {
|
const provider = {
|
||||||
dialogName,
|
dialogName,
|
||||||
|
branch,
|
||||||
|
branchId,
|
||||||
|
createBranchMutate,
|
||||||
|
editBranchMutate,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DialogContent
|
<DialogContent isLoading={isBranchLoading}>
|
||||||
// isLoading={}
|
|
||||||
>
|
|
||||||
<BranchFormContext.Provider value={provider} {...props} />
|
<BranchFormContext.Provider value={provider} {...props} />
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -12,18 +12,28 @@ const BranchFormDialogContent = React.lazy(() =>
|
|||||||
/**
|
/**
|
||||||
* Branch form form dialog.
|
* Branch form form dialog.
|
||||||
*/
|
*/
|
||||||
function BranchFormDialog({ dialogName, payload = {}, isOpen }) {
|
function BranchFormDialog({
|
||||||
|
dialogName,
|
||||||
|
payload: { branchId, action },
|
||||||
|
isOpen,
|
||||||
|
}) {
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
name={dialogName}
|
name={dialogName}
|
||||||
title={<T id={'branch.dialog.label'} />}
|
title={
|
||||||
|
action === 'edit' ? (
|
||||||
|
<T id={'branch.dialog.label_edit_branch'} />
|
||||||
|
) : (
|
||||||
|
<T id={'branch.dialog.label_new_branch'} />
|
||||||
|
)
|
||||||
|
}
|
||||||
isOpen={isOpen}
|
isOpen={isOpen}
|
||||||
canEscapeJeyClose={true}
|
canEscapeJeyClose={true}
|
||||||
autoFocus={true}
|
autoFocus={true}
|
||||||
className={'dialog--branch-form'}
|
className={'dialog--branch-form'}
|
||||||
>
|
>
|
||||||
<DialogSuspense>
|
<DialogSuspense>
|
||||||
<BranchFormDialogContent dialogName={dialogName} />
|
<BranchFormDialogContent dialogName={dialogName} branchId={branchId} />
|
||||||
</DialogSuspense>
|
</DialogSuspense>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ function BranchesActions({
|
|||||||
onClick={handleClickNewBranche}
|
onClick={handleClickNewBranche}
|
||||||
intent={Intent.PRIMARY}
|
intent={Intent.PRIMARY}
|
||||||
>
|
>
|
||||||
<T id={'branches.label.new_branche'} />
|
<T id={'branches.label.new_branch'} />
|
||||||
</Button>
|
</Button>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
);
|
);
|
||||||
|
|||||||
7
src/containers/Preferences/Branches/BranchesAlerts.js
Normal file
7
src/containers/Preferences/Branches/BranchesAlerts.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
const BranchDeleteAlert = React.lazy(() =>
|
||||||
|
import('../../Alerts/Branches/BranchDeleteAlert'),
|
||||||
|
);
|
||||||
|
|
||||||
|
export default [{ name: 'branch-delete', component: BranchDeleteAlert }];
|
||||||
@@ -4,8 +4,11 @@ import styled from 'styled-components';
|
|||||||
import { DataTable } from 'components';
|
import { DataTable } from 'components';
|
||||||
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
||||||
import { useBranchesTableColumns, ActionsMenu } from './components';
|
import { useBranchesTableColumns, ActionsMenu } from './components';
|
||||||
|
import { useBranchesContext } from './BranchesProvider';
|
||||||
|
|
||||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||||
|
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||||
|
|
||||||
import { compose } from 'utils';
|
import { compose } from 'utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -14,23 +17,42 @@ import { compose } from 'utils';
|
|||||||
function BranchesDataTable({
|
function BranchesDataTable({
|
||||||
// #withDialogAction
|
// #withDialogAction
|
||||||
openDialog,
|
openDialog,
|
||||||
|
|
||||||
|
// #withAlertActions
|
||||||
|
openAlert,
|
||||||
}) {
|
}) {
|
||||||
// Table columns.
|
// Table columns.
|
||||||
const columns = useBranchesTableColumns();
|
const columns = useBranchesTableColumns();
|
||||||
|
|
||||||
|
const { branches, isBranchesLoading, isBranchesFetching } =
|
||||||
|
useBranchesContext();
|
||||||
|
|
||||||
|
const handleEditBranch = ({ id }) => {
|
||||||
|
openDialog('branch-form', { branchId: id, action: 'edit' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteBranch = ({ id }) => {
|
||||||
|
openAlert('branch-delete', { branchId: id });
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BranchesTable
|
<BranchesTable
|
||||||
columns={columns}
|
columns={columns}
|
||||||
data={[]}
|
data={branches}
|
||||||
// loading={}
|
loading={isBranchesLoading}
|
||||||
// progressBarLoading={}
|
headerLoading={isBranchesLoading}
|
||||||
|
progressBarLoading={isBranchesFetching}
|
||||||
TableLoadingRenderer={TableSkeletonRows}
|
TableLoadingRenderer={TableSkeletonRows}
|
||||||
|
noInitialFetch={true}
|
||||||
ContextMenu={ActionsMenu}
|
ContextMenu={ActionsMenu}
|
||||||
payload={{}}
|
payload={{
|
||||||
|
onEdit: handleEditBranch,
|
||||||
|
onDelete: handleDeleteBranch,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default compose(withDialogActions)(BranchesDataTable);
|
export default compose(withDialogActions, withAlertActions)(BranchesDataTable);
|
||||||
|
|
||||||
const BranchesTable = styled(DataTable)``;
|
const BranchesTable = styled(DataTable)``;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import styled from 'styled-components';
|
|||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
import { Card } from 'components';
|
import { Card } from 'components';
|
||||||
|
import { useBranches } from 'hooks/query';
|
||||||
|
import PreferencesPageLoader from '../PreferencesPageLoader';
|
||||||
|
|
||||||
const BranchesContext = React.createContext();
|
const BranchesContext = React.createContext();
|
||||||
|
|
||||||
@@ -10,8 +12,19 @@ const BranchesContext = React.createContext();
|
|||||||
* Branches data provider.
|
* Branches data provider.
|
||||||
*/
|
*/
|
||||||
function BranchesProvider({ ...props }) {
|
function BranchesProvider({ ...props }) {
|
||||||
|
// Fetches the branches list.
|
||||||
|
const {
|
||||||
|
isLoading: isBranchesLoading,
|
||||||
|
isFetching: isBranchesFetching,
|
||||||
|
data: branches,
|
||||||
|
} = useBranches();
|
||||||
|
|
||||||
// Provider state.
|
// Provider state.
|
||||||
const provider = {};
|
const provider = {
|
||||||
|
branches,
|
||||||
|
isBranchesLoading,
|
||||||
|
isBranchesFetching,
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import {
|
import { Intent, Menu, MenuDivider, MenuItem } from '@blueprintjs/core';
|
||||||
Intent,
|
|
||||||
Button,
|
|
||||||
Popover,
|
|
||||||
Menu,
|
|
||||||
MenuDivider,
|
|
||||||
Tag,
|
|
||||||
MenuItem,
|
|
||||||
Position,
|
|
||||||
} from '@blueprintjs/core';
|
|
||||||
|
|
||||||
import { safeCallback } from 'utils';
|
import { safeCallback } from 'utils';
|
||||||
import { Icon } from 'components';
|
import { Icon } from 'components';
|
||||||
@@ -47,14 +38,23 @@ export function useBranchesTableColumns() {
|
|||||||
return React.useMemo(
|
return React.useMemo(
|
||||||
() => [
|
() => [
|
||||||
{
|
{
|
||||||
id: 'branch_name',
|
id: 'name',
|
||||||
Header: intl.get('branches.column.branch_name'),
|
Header: intl.get('branches.column.branch_name'),
|
||||||
accessor: 'branch_name',
|
accessor: 'name',
|
||||||
className: 'branch_name',
|
className: 'name',
|
||||||
width: '120',
|
width: '120',
|
||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
textOverview: true,
|
textOverview: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'code',
|
||||||
|
Header: intl.get('branches.column.code'),
|
||||||
|
accessor: 'code',
|
||||||
|
className: 'code',
|
||||||
|
width: '100',
|
||||||
|
disableSortBy: true,
|
||||||
|
textOverview: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Header: intl.get('branches.column.address'),
|
Header: intl.get('branches.column.address'),
|
||||||
accessor: 'address',
|
accessor: 'address',
|
||||||
|
|||||||
99
src/hooks/query/branches.js
Normal file
99
src/hooks/query/branches.js
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
import { useQueryClient, useMutation } from 'react-query';
|
||||||
|
import { useRequestQuery } from '../useQueryRequest';
|
||||||
|
import useApiRequest from '../useRequest';
|
||||||
|
import t from './types';
|
||||||
|
|
||||||
|
// Common invalidate queries.
|
||||||
|
const commonInvalidateQueries = (queryClient) => {
|
||||||
|
// Invalidate warehouses.
|
||||||
|
queryClient.invalidateQueries(t.BRANCHES);
|
||||||
|
queryClient.invalidateQueries(t.BRANCH);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new branch.
|
||||||
|
*/
|
||||||
|
export function useCreateBranch(props) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const apiRequest = useApiRequest();
|
||||||
|
|
||||||
|
return useMutation((values) => apiRequest.post('branches', values), {
|
||||||
|
onSuccess: (res, values) => {
|
||||||
|
// Common invalidate queries.
|
||||||
|
commonInvalidateQueries(queryClient);
|
||||||
|
},
|
||||||
|
...props,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edits the given branch.
|
||||||
|
*/
|
||||||
|
export function useEditBranch(props) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const apiRequest = useApiRequest();
|
||||||
|
|
||||||
|
return useMutation(
|
||||||
|
([id, values]) => apiRequest.post(`branches/${id}`, values),
|
||||||
|
{
|
||||||
|
onSuccess: (res, [id, values]) => {
|
||||||
|
// Invalidate specific branch.
|
||||||
|
queryClient.invalidateQueries([t.BRANCH, id]);
|
||||||
|
|
||||||
|
// Common invalidate queries.
|
||||||
|
commonInvalidateQueries(queryClient);
|
||||||
|
},
|
||||||
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes the given branch.
|
||||||
|
*/
|
||||||
|
export function useDeleteBranch(props) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const apiRequest = useApiRequest();
|
||||||
|
|
||||||
|
return useMutation((id) => apiRequest.delete(`branches/${id}`), {
|
||||||
|
onSuccess: (res, id) => {
|
||||||
|
// Invalidate specific branch.
|
||||||
|
queryClient.invalidateQueries([t.BRANCH, id]);
|
||||||
|
|
||||||
|
// Common invalidate queries.
|
||||||
|
commonInvalidateQueries(queryClient);
|
||||||
|
},
|
||||||
|
...props,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve Branches list.
|
||||||
|
*/
|
||||||
|
export function useBranches(query, props) {
|
||||||
|
return useRequestQuery(
|
||||||
|
[t.BRANCHES, query],
|
||||||
|
{ method: 'get', url: 'branches', params: query },
|
||||||
|
{
|
||||||
|
select: (res) => res.data.branches,
|
||||||
|
defaultData: [],
|
||||||
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the branch details.
|
||||||
|
* @param {number}
|
||||||
|
*/
|
||||||
|
export function useBranch(id, props, requestProps) {
|
||||||
|
return useRequestQuery(
|
||||||
|
[t.BRANCH, id],
|
||||||
|
{ method: 'get', url: `branches/${id}`, ...requestProps },
|
||||||
|
{
|
||||||
|
select: (res) => res.data.branch,
|
||||||
|
defaultData: {},
|
||||||
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -34,3 +34,4 @@ export * from './creditNote';
|
|||||||
export * from './vendorCredit';
|
export * from './vendorCredit';
|
||||||
export * from './transactionsLocking';
|
export * from './transactionsLocking';
|
||||||
export * from './warehouses'
|
export * from './warehouses'
|
||||||
|
export * from './branches';
|
||||||
|
|||||||
@@ -202,6 +202,11 @@ const WAREHOUSE_TRANSFERS = {
|
|||||||
WAREHOUSE_TRANSFERS: 'WAREHOUSE_TRANSFERS',
|
WAREHOUSE_TRANSFERS: 'WAREHOUSE_TRANSFERS',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const BRANCHES = {
|
||||||
|
BRANCHES: 'BRANCHES',
|
||||||
|
BRANCH: 'BRANCH',
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
...ACCOUNTS,
|
...ACCOUNTS,
|
||||||
...BILLS,
|
...BILLS,
|
||||||
@@ -231,4 +236,5 @@ export default {
|
|||||||
...TARNSACTIONS_LOCKING,
|
...TARNSACTIONS_LOCKING,
|
||||||
...WAREHOUSES,
|
...WAREHOUSES,
|
||||||
...WAREHOUSE_TRANSFERS,
|
...WAREHOUSE_TRANSFERS,
|
||||||
|
...BRANCHES,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1826,20 +1826,25 @@
|
|||||||
"credit_note_preview.dialog.title": "Credit Note PDF Preview",
|
"credit_note_preview.dialog.title": "Credit Note PDF Preview",
|
||||||
"payment_receive_preview.dialog.title": "Payment Receive PDF Preview",
|
"payment_receive_preview.dialog.title": "Payment Receive PDF Preview",
|
||||||
"branches.label": "Branches",
|
"branches.label": "Branches",
|
||||||
"branches.label.new_branche": "New Branch",
|
"branches.label.new_branch": "New Branch",
|
||||||
"branches.action.edit_branch": "Edit Branch",
|
"branches.action.edit_branch": "Edit Branch",
|
||||||
"branches.action.delete_branch": "Edit Branch",
|
"branches.action.delete_branch": "Delete Branch",
|
||||||
"branches.column.branch_name": "Branch name",
|
"branches.column.branch_name": "Branch name",
|
||||||
"branches.column.address": "Address",
|
"branches.column.address": "Address",
|
||||||
"branches.column.phone_number": "Phone number",
|
"branches.column.phone_number": "Phone number",
|
||||||
"branch.dialog.label": "New Branch",
|
"branches.column.code": "Code",
|
||||||
|
"branch.dialog.label_new_branch": "New Branch",
|
||||||
|
"branch.dialog.label_edit_branch": "New Branch",
|
||||||
"branch.dialog.label.branch_name": "Branch Name",
|
"branch.dialog.label.branch_name": "Branch Name",
|
||||||
|
"branch.dialog.label.branch_code": "Code",
|
||||||
"branch.dialog.label.branch_address": "Branch Address",
|
"branch.dialog.label.branch_address": "Branch Address",
|
||||||
"branch.dialog.label.address_1": "Address 1",
|
"branch.dialog.label.address_1": "Address",
|
||||||
"branch.dialog.label.address_2": "Address 2",
|
|
||||||
"branch.dialog.label.city": "City",
|
"branch.dialog.label.city": "City",
|
||||||
"branch.dialog.label.country": "Country",
|
"branch.dialog.label.country": "Country",
|
||||||
"branch.dialog.label.phone_number": "Phone Number",
|
"branch.dialog.label.phone_number": "Phone Number",
|
||||||
"branch.dialog.label.email": "Email",
|
"branch.dialog.label.email": "Email",
|
||||||
"branch.dialog.label.website": "Website"
|
"branch.dialog.label.website": "Website",
|
||||||
|
"branch.dialog.success_message": "The branch has been created successfully.",
|
||||||
|
"branch.alert.delete_message":"The branch has been deleted successfully",
|
||||||
|
"branch.once_delete_this_branch":"Once you delete this branch, you won't be able to restore it later. Are you sure you want to delete this branch?"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user