/* eslint-disable eqeqeq */ import Axios from "axios"; import moment from "moment/moment"; import React from "react"; import {connect} from "react-redux"; import {toast} from "react-toastify"; import TabView from "../../../components/tab-view/TabView"; import {IRepoCompany} from "../../../reducers/RepoCompanyReducer"; import {IExternalUser} from "../../../reducers/SettingsReducer"; import {HistorySource, ITemenosHistory} from "../../../services/service-types/AccountHistoryTypes"; import {IRepoAccount} from "../../../services/service-types/RepoAccountTypes"; import Configuration from "../../../shared/configuration/Configuration"; import DateConversions from "../../../shared/conversions/DateConversions"; import AsyncState from "../../../shared/enumerations/AsyncState"; import AxiosMessageFormatting from "../../../shared/formatting/AxiosMessageFormatting"; import UrlFormatting from "../../../shared/formatting/UrlFormatting"; import ToastDefaults from "../../../shared/notifications/ToastDefaults"; import IAsyncStateContainer from "../../../shared/types/IAsyncStateContainer"; import {IAppState} from "../../../store/Store"; import AccountActivity from "../../account-activity/AccountActivity"; import AuctionTab from "../../auction-tab/AuctionTab"; import RefundStatusTab from "../../refund-status-tab/RefundStatusTab"; import RepoHistoryTab from "../../repo-history-tab/RepoHistoryTab"; import RepoInfoTab from "../../repo-info-tab/RepoInfoTab"; import RepoStatusChangeHistoryTable from "../../repo-status-change-history-table/RepoStatusChangeHistoryTable"; import TransactionHistoryTable, {ITransactionHistory} from "../../transaction-history-table/TransactionHistoryTable"; import {getExternalUserMap, getRepoTypeMap} from "./../../../selectors/SettingsSelector"; interface IAccountPageTabViewProps { selectedAccount?: IRepoAccount; } interface IPropsFromState { readonly repoCompanies: IAsyncStateContainer; readonly externalUserMap: IExternalUser[]; readonly repoTypeMap: string[]; readonly transactionTypeCodes: string[]; readonly transactionTypeCodeQueryPrefix: string; readonly transactionHistoryParameterDateFormat: string; readonly transactionHistoryStartDateOffset: number; } const mapStateToProps = (state: IAppState): IPropsFromState => { return { repoCompanies: state.repoCompanyState.repoCompanies, externalUserMap: getExternalUserMap(state), repoTypeMap: getRepoTypeMap(state), transactionTypeCodes: state.settingsState.transactionTypeCodes, transactionTypeCodeQueryPrefix: state.settingsState.transactionTypeCodeQueryPrefix, transactionHistoryParameterDateFormat: state.settingsState.transactionHistoryParameterDateFormat, transactionHistoryStartDateOffset: state.settingsState.transactionHistoryStartDateOffset }; }; interface IAccountPageState { caseHistories: IAsyncStateContainer; accountHistories: IAsyncStateContainer; transactionHistories: IAsyncStateContainer; } type Props = IAccountPageTabViewProps & IPropsFromState; class AccountPageTabView extends React.Component { constructor(props: Props) { super(props); this.state = { caseHistories: {state: AsyncState.Inactive}, accountHistories: {state: AsyncState.Inactive}, transactionHistories: {state: AsyncState.Inactive}, }; this.updateCaseHistory = this.updateCaseHistory.bind(this); this.updateAccountHistory = this.updateAccountHistory.bind(this); this.updateTransactionHistory = this.updateTransactionHistory.bind(this); this.updateHistories = this.updateHistories.bind(this); } componentDidUpdate(prevProps: Props) { // Update letters if the Selected Account has updated if (this.props.selectedAccount !== prevProps.selectedAccount) { this.updateHistories(); } } async updateCaseHistory() { if (!this.props.selectedAccount) { return; } if ( this.state.caseHistories.state === AsyncState.Inactive || this.state.caseHistories.state === AsyncState.Failure ) { this.setState({ caseHistories: { state: AsyncState.Loading, }, }); if (this.props.selectedAccount.referenceCaseId) { const caseHistories = await this.getCaseHistory(this.props.selectedAccount.referenceCaseId); this.setState({ caseHistories: caseHistories, }); } else { this.setState({ caseHistories: { state: AsyncState.Failure } }); } } } async updateAccountHistory() { if (!this.props.selectedAccount) { return; } if ( this.state.accountHistories.state === AsyncState.Inactive || this.state.accountHistories.state === AsyncState.Failure ) { this.setState({ accountHistories: { state: AsyncState.Loading, }, }); if (this.props.selectedAccount.referenceId) { const accountHistories = await this.getAccountHistory(this.props.selectedAccount.referenceId); this.setState({ accountHistories: accountHistories, }); } else { this.setState({ accountHistories: { state: AsyncState.Failure } }); } } } async updateTransactionHistory() { if (!this.props.selectedAccount) { return; } if ( this.state.transactionHistories.state === AsyncState.Inactive || this.state.transactionHistories.state === AsyncState.Failure ) { this.setState({ transactionHistories: { state: AsyncState.Loading, }, }); const transactionHistories = await this.getTransactionHistory(this.props.selectedAccount.accountNo); this.setState({ transactionHistories: transactionHistories, }); } } async getCaseHistory(caseId: string): Promise> { const parameterizedUrl = UrlFormatting.ReplaceURLParams( Configuration.PrefixCollectionsUrl(Configuration.Get.urls.collections.case_history), {key: "caseId", value: caseId}, ); try { const result = await Axios.get(parameterizedUrl); const historyResult = result.data as ITemenosHistory[]; historyResult.map((x) => { x.Created = DateConversions.utcToMoment(x.Created); x.Source = HistorySource.Case; return x; }); return { data: historyResult, state: AsyncState.Success, }; } catch (err) { toast.error(AxiosMessageFormatting.FormatError(err), ToastDefaults.baseParams); window.console.error(err); return { state: AsyncState.Failure, }; } } async getAccountHistory(accountNo: string): Promise> { const parameterizedUrl = UrlFormatting.ReplaceURLParams( Configuration.PrefixCollectionsUrl(Configuration.Get.urls.collections.account_history), {key: "accountNo", value: accountNo}, ); try { const result = await Axios.get(parameterizedUrl); const historyResult = result.data as ITemenosHistory[]; historyResult.map((x) => { x.Created = DateConversions.utcToMoment(x.Created); x.Source = HistorySource.Account; return x; }); return { data: historyResult, state: AsyncState.Success, }; } catch (err) { toast.error(AxiosMessageFormatting.FormatError(err), ToastDefaults.baseParams); window.console.error(err); return { state: AsyncState.Failure, }; } } async getTransactionHistory(accountNo: string): Promise> { const parameterizedUrl = this.getTransactionHistoriesURL(accountNo); if (!parameterizedUrl) { return { state: AsyncState.Failure, }; } try { const result = await Axios.get(parameterizedUrl); if (result.data.length < 1) { return result.data; } const transactionHistoryResult = result.data as ITransactionHistory[]; transactionHistoryResult.map((x) => { x.postingDate = DateConversions.utcToMoment(x.postingDate); x.effectiveDate = DateConversions.utcToMoment(x.effectiveDate); return x; }); return { data: transactionHistoryResult, state: AsyncState.Success, }; } catch (err) { toast.error(AxiosMessageFormatting.FormatError(err), ToastDefaults.baseParams); window.console.error(err); return { state: AsyncState.Failure, }; } } getTransactionHistoriesURL(accountNo: string): string | undefined { return UrlFormatting.ReplaceURLParams( Configuration.PrefixCoreUrl(Configuration.Get.urls.core.loan_transactions), {key: "loanNo", value: accountNo}, {key: "startDate", value: moment().add(this.props.transactionHistoryStartDateOffset, "d").format(this.props.transactionHistoryParameterDateFormat)}, {key: "endDate", value: moment().format(this.props.transactionHistoryParameterDateFormat)} ) + UrlFormatting.FormatArrayValuesIntoQuery(this.props.transactionTypeCodes, this.props.transactionTypeCodeQueryPrefix); } updateHistories() { this.updateCaseHistory(); this.updateAccountHistory(); } render() { if (!this.props.selectedAccount) { return (
), }, ]} />
); } else { return (
, tabOpenCallback: this.updateHistories, }, { header: "Repo Info", element: , }, { header: "Auction Info", element: , tabOpenCallback: this.updateHistories, }, { header: "Repo History", element: , tabOpenCallback: this.updateHistories }, { header: "Transaction History", element: , tabOpenCallback: this.updateTransactionHistory }, { header: "Repo Status Changes", element: , }, { header: "Refund Status", element: } ]} />
); } } } export const _AccountPageTabView = AccountPageTabView; export default connect(mapStateToProps, null)(AccountPageTabView);