ca-mfe-page-scoring-ui
Version:
This is a react shell for the SPGI Platform MFE implementation
128 lines (121 loc) • 5.6 kB
text/typescript
import { createSlice } from '@reduxjs/toolkit';
import {
formatDate,
getFormattedDate
} from '@utils/common';
import { dateFormat, financialsType } from '@root/constants';
const initialState = {
selectedValues: {
periodType: {},
currency: {},
asOfDate: getFormattedDate,
prevAsOfDate: getFormattedDate,
financialStatement: {},
financialType: '',
propCurrency: null,
selectPeriod: {},
dataSource:{},
useFinancialStmtDateConsent: false,
error: null,
isLoading: true,
scoreDate: getFormattedDate
},
financialStatementList: [],
selectPeriodList: [],
newSelectPeriodList: [],
companyInfoValidation: true,
validateCompanyInfo: false
};
const getValidScoreDate = (currentStatementDate, asOfDate) => new Date(currentStatementDate) >= new Date() ?
formatDate(asOfDate, dateFormat) : formatDate(currentStatementDate, dateFormat);
export const companyDataSettingsSlice = createSlice({
name: 'companyDataSettings',
initialState,
reducers: {
fetchInit: state => {
state.selectedValues.isLoading = true;
},
updateData: (state, action) => {
if (action.payload.label === 'asOfDate') {
const updatedDate = formatDate(action.payload.value, dateFormat);
if(action.payload.prevDate){
state.selectedValues.prevAsOfDate = action.payload.prevDate;
}
state.selectedValues.asOfDate = updatedDate;
if(!state.selectedValues.useFinancialStmtDateConsent){
state.selectedValues.scoreDate = updatedDate;
}
} else if (action.payload.fieldType === 'periodType') {
state.selectedValues.periodType = action.payload;
} else if (action.payload.fieldType === 'currency') {
state.selectedValues.currency = action.payload;
} else if (action.payload.fieldType === 'financialStatement') {
state.selectedValues.useFinancialStmtDateConsent &&
(state.selectedValues.scoreDate = getValidScoreDate(action.payload.periodDate, state.selectedValues.asOfDate ));
state.selectedValues.financialStatement = action.payload;
} else if (action.payload.fieldType === 'dataSource') {
state.selectedValues.dataSource = action.payload;
} else if (action.payload.fieldType === 'useFinancialStmtDateConsent') {
state.selectedValues.useFinancialStmtDateConsent = action.payload.value;
} else if (action.payload.fieldType === 'financialType') {
state.selectedValues.financialType = action.payload.value;
} else if (action.payload.fieldType === 'selectPeriod') {
state.selectedValues.useFinancialStmtDateConsent &&
(state.selectedValues.scoreDate = getValidScoreDate(action.payload.periodDate, state.selectedValues.asOfDate ));
state.selectedValues.selectPeriod = action.payload;
} else if (action.payload.fieldType === 'propCurrency') {
state.selectedValues.propCurrency = action.payload.value;
}
state.selectedValues.isLoading = false;
},
setFinancialStatementList: (state, action) => {
state.financialStatementList = action.payload;
},
setSelectPeriodList: (state, action) => {
state.selectPeriodList = action.payload;
},
setNewSelectPeriodList: (state, action) => {
state.newSelectPeriodList = action.payload;
},
setScoreDate: (state, action) => {
const asOfDate = state.selectedValues.asOfDate;
if (action.payload.checked) {
state.selectedValues.scoreDate =
state.selectedValues.financialType === financialsType.WithFinancials ?
getValidScoreDate(state.selectedValues.financialStatement.periodDate, asOfDate) :
getValidScoreDate(state.selectedValues.selectPeriod.periodDate, asOfDate);
}
else {
state.selectedValues.scoreDate = formatDate(asOfDate, dateFormat);
}
},
resetCompanyDataSettings: (state, action) => {
state.selectedValues.asOfDate = formatDate(action.payload?.asOfDate || '', dateFormat);
state.selectedValues.prevAsOfDate = initialState.selectedValues.prevAsOfDate;
state.selectedValues.currency = initialState.selectedValues.currency;
state.selectedValues.propCurrency= initialState.selectedValues.propCurrency;
state.selectedValues.financialStatement = initialState.selectedValues.financialStatement;
state.selectedValues.financialType = action.payload?.financialType
? action.payload.financialType : initialState.selectedValues.financialType;
state.selectedValues.selectPeriod = action.payload?.selectPeriod ?
action.payload.selectPeriod : initialState.selectedValues.selectPeriod;
state.financialStatementList = initialState.financialStatementList;
state.selectPeriodList = initialState.selectPeriodList;
},
onDataFailure: (state, action) => {
state.selectedValues.error = action.payload;
state.selectedValues.isLoading = false;
},
isCompanyInfoValid: (state, action) => {
state.companyInfoValidation = action.payload;
},
trigCompanyInfoValidation: (state, action) => {
state.validateCompanyInfo = action.payload;
}
}
});
export const { updateData, onDataFailure, setSelectPeriodList,
fetchInit, setFinancialStatementList, resetCompanyDataSettings,
setScoreDate, isCompanyInfoValid, trigCompanyInfoValidation,
setNewSelectPeriodList } = companyDataSettingsSlice.actions;
export default companyDataSettingsSlice.reducer;