213 lines
5.0 KiB
JavaScript
213 lines
5.0 KiB
JavaScript
import { ORGANIZATION_ACTIONS } from '../actions/organizationActions';
|
|
|
|
const initialState = {
|
|
// Organizations list
|
|
organizations: [],
|
|
totalOrganizations: 0,
|
|
currentPage: 1,
|
|
totalPages: 1,
|
|
pageSize: 10,
|
|
hasPrevious: false,
|
|
hasNext: false,
|
|
|
|
// Current organization (for edit/view)
|
|
currentOrganization: null,
|
|
|
|
// Search results
|
|
searchResults: [],
|
|
searchQuery: '',
|
|
|
|
// Loading states
|
|
loading: false,
|
|
organizationLoading: false,
|
|
searchLoading: false,
|
|
|
|
// Error states
|
|
error: null,
|
|
organizationError: null,
|
|
searchError: null,
|
|
|
|
// Operation states
|
|
creating: false,
|
|
updating: false,
|
|
deleting: false,
|
|
};
|
|
|
|
const organizationReducer = (state = initialState, action) => {
|
|
switch (action.type) {
|
|
// Fetch Organizations
|
|
case ORGANIZATION_ACTIONS.FETCH_ORGANIZATIONS_REQUEST:
|
|
return {
|
|
...state,
|
|
loading: true,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.FETCH_ORGANIZATIONS_SUCCESS: {
|
|
const { organizations, total_count, page, total_pages, limit } = action.payload.data;
|
|
|
|
return {
|
|
...state,
|
|
loading: false,
|
|
organizations: organizations,
|
|
totalOrganizations: total_count || organizations.length,
|
|
currentPage: page || 1,
|
|
totalPages: total_pages || 1,
|
|
pageSize: limit || 10,
|
|
hasPrevious: false,
|
|
hasNext: false,
|
|
error: null,
|
|
};
|
|
}
|
|
|
|
case ORGANIZATION_ACTIONS.FETCH_ORGANIZATIONS_FAILURE:
|
|
return {
|
|
...state,
|
|
loading: false,
|
|
error: action.payload,
|
|
};
|
|
|
|
// Fetch Single Organization
|
|
case ORGANIZATION_ACTIONS.FETCH_ORGANIZATION_REQUEST:
|
|
return {
|
|
...state,
|
|
organizationLoading: true,
|
|
organizationError: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.FETCH_ORGANIZATION_SUCCESS:
|
|
return {
|
|
...state,
|
|
organizationLoading: false,
|
|
currentOrganization: action.payload.data,
|
|
organizationError: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.FETCH_ORGANIZATION_FAILURE:
|
|
return {
|
|
...state,
|
|
organizationLoading: false,
|
|
organizationError: action.payload,
|
|
};
|
|
|
|
// Create Organization
|
|
case ORGANIZATION_ACTIONS.CREATE_ORGANIZATION_REQUEST:
|
|
return {
|
|
...state,
|
|
creating: true,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.CREATE_ORGANIZATION_SUCCESS:
|
|
return {
|
|
...state,
|
|
creating: false,
|
|
organizations: [action.payload.data, ...state.organizations],
|
|
totalOrganizations: state.totalOrganizations + 1,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.CREATE_ORGANIZATION_FAILURE:
|
|
return {
|
|
...state,
|
|
creating: false,
|
|
error: action.payload,
|
|
};
|
|
|
|
// Update Organization
|
|
case ORGANIZATION_ACTIONS.UPDATE_ORGANIZATION_REQUEST:
|
|
return {
|
|
...state,
|
|
updating: true,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.UPDATE_ORGANIZATION_SUCCESS:
|
|
return {
|
|
...state,
|
|
updating: false,
|
|
organizations: state.organizations.map(org =>
|
|
org.id === action.payload.data.id ? action.payload.data : org
|
|
),
|
|
currentOrganization: action.payload.data,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.UPDATE_ORGANIZATION_FAILURE:
|
|
return {
|
|
...state,
|
|
updating: false,
|
|
error: action.payload,
|
|
};
|
|
|
|
// Delete Organization
|
|
case ORGANIZATION_ACTIONS.DELETE_ORGANIZATION_REQUEST:
|
|
return {
|
|
...state,
|
|
deleting: true,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.DELETE_ORGANIZATION_SUCCESS:
|
|
return {
|
|
...state,
|
|
deleting: false,
|
|
organizations: state.organizations.filter(org => org.id !== action.payload),
|
|
totalOrganizations: state.totalOrganizations - 1,
|
|
error: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.DELETE_ORGANIZATION_FAILURE:
|
|
return {
|
|
...state,
|
|
deleting: false,
|
|
error: action.payload,
|
|
};
|
|
|
|
// Search Organizations
|
|
case ORGANIZATION_ACTIONS.SEARCH_ORGANIZATIONS_REQUEST:
|
|
return {
|
|
...state,
|
|
searchLoading: true,
|
|
searchError: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.SEARCH_ORGANIZATIONS_SUCCESS:
|
|
return {
|
|
...state,
|
|
searchLoading: false,
|
|
searchResults: action.payload.data || action.payload,
|
|
searchQuery: action.payload.query || '',
|
|
searchError: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.SEARCH_ORGANIZATIONS_FAILURE:
|
|
return {
|
|
...state,
|
|
searchLoading: false,
|
|
searchError: action.payload,
|
|
};
|
|
|
|
// Clear States
|
|
case ORGANIZATION_ACTIONS.CLEAR_ORGANIZATION_ERROR:
|
|
return {
|
|
...state,
|
|
error: null,
|
|
organizationError: null,
|
|
searchError: null,
|
|
};
|
|
|
|
case ORGANIZATION_ACTIONS.CLEAR_CURRENT_ORGANIZATION:
|
|
return {
|
|
...state,
|
|
currentOrganization: null,
|
|
organizationError: null,
|
|
};
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export default organizationReducer;
|