mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
chrone: sperate client and server to different repos.
This commit is contained in:
41
src/store/organizations/organizations.actions.js
Normal file
41
src/store/organizations/organizations.actions.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const setOrganizations = (organizations) => {
|
||||
return {
|
||||
type: t.ORGANIZATIONS_LIST_SET,
|
||||
payload: {
|
||||
organizations,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const fetchOrganizations = () => (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.get('organization/all')
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.ORGANIZATIONS_LIST_SET,
|
||||
payload: {
|
||||
organizations: response.data.organizations,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
export const setOrganizationSetupCompleted =
|
||||
(congrats) => (dispatch, getState) => {
|
||||
const tenantId = getState().authentication.tenantId;
|
||||
|
||||
dispatch({
|
||||
type: t.SET_ORGANIZATION_CONGRATS,
|
||||
payload: {
|
||||
tenantId,
|
||||
congrats,
|
||||
},
|
||||
});
|
||||
};
|
||||
39
src/store/organizations/organizations.reducers.js
Normal file
39
src/store/organizations/organizations.reducers.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { omit } from 'lodash';
|
||||
import t from 'store/types';
|
||||
|
||||
const initialState = {
|
||||
data: {},
|
||||
byOrganizationId: {},
|
||||
};
|
||||
|
||||
const reducer = createReducer(initialState, {
|
||||
|
||||
[t.ORGANIZATIONS_LIST_SET]: (state, action) => {
|
||||
const { organizations } = action.payload;
|
||||
const _data = {};
|
||||
const _dataByOrganizationId = {};
|
||||
|
||||
organizations.forEach((organization) => {
|
||||
_data[organization.id] = {
|
||||
...state.data[organization.id],
|
||||
...organization.metadata,
|
||||
...omit(organization, ['metadata']),
|
||||
};
|
||||
_dataByOrganizationId[organization.organization_id] = organization.id;
|
||||
});
|
||||
state.data = _data;
|
||||
state.byOrganizationId = _dataByOrganizationId;
|
||||
},
|
||||
|
||||
[t.SET_ORGANIZATION_CONGRATS]: (state, action) => {
|
||||
const { tenantId, congrats } = action.payload;
|
||||
|
||||
state.data[tenantId] = {
|
||||
...(state.data[tenantId] || {}),
|
||||
is_congrats: !!congrats,
|
||||
};
|
||||
}
|
||||
})
|
||||
|
||||
export default reducer;
|
||||
39
src/store/organizations/organizations.selectors.js
Normal file
39
src/store/organizations/organizations.selectors.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
|
||||
const organizationSelector = (state, props) => {
|
||||
const tenantId = state.organizations.byOrganizationId[props.organizationId];
|
||||
return state.organizations.data[tenantId];
|
||||
};
|
||||
|
||||
export const getOrganizationByIdFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => organization);
|
||||
|
||||
export const isOrganizationSeededFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => {
|
||||
return !!organization?.seeded_at;
|
||||
});
|
||||
|
||||
export const isOrganizationBuiltFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => {
|
||||
return !!organization?.initialized_at;
|
||||
});
|
||||
|
||||
export const isOrganizationReadyFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => {
|
||||
return organization?.is_ready;
|
||||
});
|
||||
|
||||
export const isOrganizationSubscribedFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => {
|
||||
return organization?.subscriptions?.length > 0;
|
||||
});
|
||||
|
||||
export const isOrganizationCongratsFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => {
|
||||
return !!organization?.is_congrats;
|
||||
});
|
||||
|
||||
export const isOrganizationBuildRunningFactory = () =>
|
||||
createSelector(organizationSelector, (organization) => {
|
||||
return !!organization?.is_build_running;
|
||||
});
|
||||
7
src/store/organizations/organizations.types.js
Normal file
7
src/store/organizations/organizations.types.js
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
export default {
|
||||
ORGANIZATION_SET: 'ORGANIZATION_SET',
|
||||
ORGANIZATIONS_LIST_SET: 'ORGANIZATIONS_LIST_SET',
|
||||
SET_ORGANIZATION_CONGRATS: 'SET_ORGANIZATION_CONGRATS',
|
||||
ORGANIZATION_MUTATE_BASE_CURRENCY_ABILITIES: 'ORGANIZATION_MUTATE_BASE_CURRENCY_ABILITIES'
|
||||
};
|
||||
33
src/store/organizations/withSetupWizard.js
Normal file
33
src/store/organizations/withSetupWizard.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
export default (mapState) => {
|
||||
const mapStateToProps = (state, props) => {
|
||||
const {
|
||||
isOrganizationSetupCompleted,
|
||||
isOrganizationReady,
|
||||
isSubscriptionActive,
|
||||
isOrganizationBuildRunning
|
||||
} = props;
|
||||
|
||||
const condits = {
|
||||
isCongratsStep: isOrganizationSetupCompleted,
|
||||
isSubscriptionStep: !isSubscriptionActive,
|
||||
isInitializingStep: isOrganizationBuildRunning,
|
||||
isOrganizationStep: !isOrganizationReady && !isOrganizationBuildRunning,
|
||||
};
|
||||
const scenarios = [
|
||||
{ condition: condits.isSubscriptionStep, step: 'subscription' },
|
||||
{ condition: condits.isOrganizationStep, step: 'organization' },
|
||||
{ condition: condits.isInitializingStep, step: 'initializing' },
|
||||
{ condition: condits.isCongratsStep, step: 'congrats' },
|
||||
];
|
||||
const setupStep = scenarios.find((scenario) => scenario.condition);
|
||||
const mapped = {
|
||||
...condits,
|
||||
setupStepId: setupStep?.step,
|
||||
setupStepIndex: scenarios.indexOf(setupStep) + 1,
|
||||
};
|
||||
return mapState ? mapState(mapped, state, props) : mapped;
|
||||
};
|
||||
return connect(mapStateToProps);
|
||||
};
|
||||
Reference in New Issue
Block a user