feat: register pages routes guards.

feat: retrieve all organizations details to authenticated user.
feat: redux organization reducers and actions.
This commit is contained in:
Ahmed Bouhuolia
2020-10-11 00:08:51 +02:00
parent 8622320eef
commit 507690fedf
22 changed files with 348 additions and 66 deletions

View File

@@ -5,6 +5,7 @@ const initialState = {
token: '',
organization: '',
user: '',
tenant: {},
locale: '',
errors: [],
};
@@ -15,6 +16,7 @@ export default createReducer(initialState, {
state.token = token;
state.user = user;
state.organization = tenant.organization_id;
state.tenant = tenant;
},
[t.LOGIN_FAILURE]: (state, action) => {
@@ -36,3 +38,9 @@ export const isAuthenticated = (state) => !!state.authentication.token;
export const hasErrorType = (state, errorType) => {
return state.authentication.errors.find((e) => e.type === errorType);
};
export const isTenantSeeded = (state) => !!state.tenant.seeded_at;
export const isTenantBuilt = (state) => !!state.tenant.initialized_at;
export const isTenantHasSubscription = () => false;
export const isTenantSubscriptionExpired = () => false;

View File

@@ -0,0 +1,16 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const fetchOrganizations = () => {
return (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); });
});
};

View File

@@ -0,0 +1,25 @@
import { createReducer } from '@reduxjs/toolkit';
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] = organization;
_dataByOrganizationId[organization.organization_id] = organization.id;
});
state.data = _data;
state.byOrganizationId = _dataByOrganizationId;
},
})
export default reducer;

View File

@@ -0,0 +1,18 @@
import { createSelector } from '@reduxjs/toolkit';
const oragnizationByTenantIdSelector = (state, props) => state.organizations[props.tenantId];
const organizationByIdSelector = (state, props) => state.organizations.byOrganizationId[props.organizationId];
export const getOrganizationByOrgIdFactory = () => createSelector(
organizationByIdSelector,
(organization) => {
return organization;
},
);
export const getOrganizationByTenantIdFactory = () => createSelector(
oragnizationByTenantIdSelector,
(organization) => {
return organization;
}
)

View File

@@ -0,0 +1,5 @@
export default {
ORGANIZATIONS_LIST_SET: 'ORGANIZATIONS_LIST_SET',
};

View File

@@ -25,9 +25,11 @@ import bills from './Bills/bills.reducer';
import vendors from './vendors/vendors.reducer';
import paymentReceives from './PaymentReceive/paymentReceive.reducer';
import paymentMades from './PaymentMades/paymentMade.reducer';
import organizations from './organizations/organizations.reducers';
export default combineReducers({
authentication,
organizations,
dashboard,
users,
accounts,

View File

@@ -24,6 +24,7 @@ import bills from './Bills/bills.type';
import vendors from './vendors/vendors.types';
import paymentReceives from './PaymentReceive/paymentReceive.type';
import paymentMades from './PaymentMades/paymentMade.type';
import organizations from './organizations/organizations.types';
export default {
...authentication,
@@ -52,4 +53,5 @@ export default {
...bills,
...paymentReceives,
...paymentMades,
...organizations,
};