Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
13 kB
2
Indexable
import React from 'react';
import { withPage, useAppState } from '@d-lift/core';
import BodySection from '@/Layout/BodySection';
import RightSection from '@/Layout/RightSection';
import QuestionResponse from './Components/QuestionResponse';
import RespondingState from './Components/RespondingState';
import { Page } from '@d-lift/uxcomponents';
import './state-overview.scss';

const StateOverview = () => {
    const [collectionData, setCollectionData] = useAppState('collectionData', undefined);
    const [showAnswer, setShowAnswer] = useAppState('showAnswer', undefined);
    const [showTable, setShowTable] = useAppState('showTable', true);
    const [selectedState, setSelectedState] = useAppState('selectedState', undefined);
    const [selectedStateDesc, setSelectedStateDesc] = useAppState('selectedStateDesc', '');

    const renderAnswers = (state) => {
        setSelectedState(state);
        setShowAnswer(true);
        setShowTable(false);
        setTimeout(() => {
            setShowTable(true);
        }, 100);
    };

    return (
        <Page ref-table-list="STATE">
            <div className="d-flex ux-rfi-mobile-layout">
                <BodySection>
                    <QuestionResponse
                        renderAnswers={renderAnswers}
                        showAnswer={showAnswer}
                        showTable={showTable}
                        selectedState={selectedState}
                        collectionData={collectionData}
                        setCollectionData={setCollectionData}
                        selectedStateDesc={selectedStateDesc} // Ensure this prop is passed
                    />
                </BodySection>
                <RightSection>
                    <RespondingState
                        renderAnswers={renderAnswers}
                        collectionData={collectionData}
                        setSelectedStateDesc={setSelectedStateDesc} // Ensure this prop is passed
                    />
                </RightSection>
            </div>
        </Page>
    );
};

export default withPage(
    {
        Description: 'State Overview Page',
        ContentManager: true,
        LayoutStyle: 'rfi-dashboard',
        TemplateOptions: { cardWorkFlow: true },
    },
    StateOverview
);







import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { Lift, useAppState, Util } from '@d-lift/core';
import catalogService from '@/Services/CatalogService';
import { Header, Para, DataTable } from '@d-lift/uxcomponents';
import ConstantKeys from '@/Constants/ConstantKeys';
import RFIUtil from '@/Util/RFIUtil';
import _ from 'lodash';

const QuestionResponse = ({
    collectionData,
    setCollectionData,
    renderAnswers,
    showAnswer,
    showTable,
    selectedState,
    selectedStateDesc, // Make sure to include this prop
}) => {
    const [expandedCard, setExpandedCard] = useAppState('expandedCard', null);
    const [selectedAnswers, setSelectedAnswers] = useAppState('selectedAnswers', '');
    const [filteredStates, setFilteredStates] = useAppState('filteredStates', []);

    useEffect(() => {
        getRfiCollection();
    }, []);

    const refTableContent = RFIUtil.getRefTableDataByCacheName(
        ConstantKeys.REF_TABLE.REF_TABLES_CACHE,
    );

    const getRfiCollection = async () => {
        try {
            Lift.spinner.show();

            let response = await catalogService.fetchRfiCollection({
                requestBody: { catalogId: 551 },
            });

            processCollectionData(response);
            fetchStateList(response);
        } catch (error) {
            Lift.Application.Notification.error(
                ConstantKeys.NOTIFICATION.MSG.ERR.FETCH_RFI_COLLECTIONS_ERR,
            );
        } finally {
            Lift.spinner.hide();
        }
    };

    const processCollectionData = (responseData) => {
        const formattedCollectionData = [];
        if (!_.isEmpty(responseData) && !_.isEmpty(responseData.rfiDetails)) {
            responseData.rfiDetails.forEach((data) => {
                let table = [];
                table = data.questionsList?.map((question) => ({
                    id: question?.id,
                    question: question?.question,
                    answers: question?.answers,
                }));

                formattedCollectionData.push(table);
            });
        }

        setCollectionData({
            ...responseData,
            ...{ formattedCollectionData: formattedCollectionData },
        });
    };

    const fetchStateList = (responseData) => {
        const distinctStatesValues = [
            ...new Set(responseData.rfiDetails.map((item) => item.state)),
        ];

        if (refTableContent) {
            const distinctStatesList = refTableContent.STATE?.filter((state) =>
                distinctStatesValues?.includes(state.CODE),
            );

            if (distinctStatesValues?.includes(ConstantKeys.REF_VALUE.ALL)) {
                distinctStatesList.push({
                    CODE: ConstantKeys.REF_VALUE.ALL,
                    DESCRIPTION: ConstantKeys.CONTEXT_KEYS.ALL_LOWERCASE,
                });
            }

            setFilteredStates(
                distinctStatesList.sort((a, b) => a.DESCRIPTION.localeCompare(b.DESCRIPTION)),
            );
        }
    };

    const renderResponse = (colData, rowData, index) => {
        return rowData.answers?.map((answer, idx) =>
            showAnswer !== true ? (
                <a className="ux-rfi-state-link" onClick={() => renderAnswers(answer.state)}>
                    {answer.state}
                    {idx < rowData.answers.length - 1 && ', '}
                </a>
            ) : answer.state === selectedState ? (
                <div>{answer.answer}</div>
            ) : (
                <></>
            ),
        );
    };

    const toggleCard = (index) => {
        setExpandedCard(expandedCard === index ? null : index);
    };

    const renderProgramCategoryValues = (values, refTableName) => {
        if (values) {
            return values
                .split(',')
                .map((value) => Util.getRefTableDescriptionByCode(refTableName, value))
                .join(', ');
        }
    };

    return (
        <>
            {collectionData ? (
                <>
                    <div className="ux-rfi-green-bg">
                        <Header
                            className="pt-1 ux-rfi-white-header"
                            size="1"
                            showIf={selectedStateDesc}> {/* Use selectedStateDesc here */}
                            {selectedStateDesc}
                        </Header>
                        <Header header-size="4" className="pt-1 ux-rfi-white-header">
                            {collectionData.title}
                        </Header>
                        <Para className="ux-rfi-white-font" showIf={collectionData.description}>
                            {collectionData.description}
                        </Para>
                    </div>
                    {collectionData.rfiDetails.map((rfiDetail, index) => (
                        <div key={index} className="ux-rfi-light-green-border mt-3">
                            <div>
                                <div className="main-component">
                                    <div onClick={() => toggleCard(index)} className="p-1">
                                        <Header
                                            header-size="4"
                                            preIconClass={`${
                                                expandedCard == index
                                                    ? 'fa fa-angle-up'
                                                    : 'fa fa-angle-down'
                                            }`}>
                                            {rfiDetail.title}
                                        </Header>
                                    </div>

                                    {expandedCard === index ? (
                                        <div className="col-9 pb-3">
                                            <Para
                                                labelKey="offering"
                                                className="ux-rfi-label-normal font-weight-bold"></Para>
                                            {' ' +
                                                Util.getRefTableDescriptionByCode(
                                                    ConstantKeys.REF_TABLE_NAMES.OFFERING,
                                                    rfiDetail.offering,
                                                )}
                                            <br />
                                            <Para
                                                labelKey="program"
                                                className="ux-rfi-label-normal font-weight-bold"></Para>
                                            {' ' +
                                                renderProgramCategoryValues(
                                                    rfiDetail.programs,
                                                    ConstantKeys.REF_TABLE_NAMES.PROGRAM,
                                                )}
                                            <br />
                                            <Para
                                                labelKey="category"
                                                className="ux-rfi-label-normal font-weight-bold"></Para>
                                            {' ' +
                                                renderProgramCategoryValues(
                                                    rfiDetail.category,
                                                    ConstantKeys.REF_TABLE_NAMES.CATEGORY,
                                                )}
                                        </div>
                                    ) : null}

                                    <DataTable
                                        showIf={showTable}
                                        col-data-keys="question,response"
                                        col-default-headers="Question,Response"
                                        customContent={{
                                            Response: renderResponse,
                                        }}
                                        datacollection={
                                            'collectionData.formattedCollectionData[' + index + ']'
                                        }
                                        keyField="id"
                                        hover="false"
                                        bordered="true"
                                        striped="false"
                                        emptymsg-key="no_records_found"
                                        className="va-contacts-table"
                                    />
                                </div>
                            </div>
                        </div>
                    ))}
                </>
            ) : null}
        </>
    );
};

QuestionResponse.propTypes = {
    renderAnswers: PropTypes.func.isRequired,
    collectionData: PropTypes.object.isRequired,
    setCollectionData: PropTypes.func.isRequired,
    showAnswer: PropTypes.bool.isRequired,
    showTable: PropTypes.bool.isRequired,
    selectedState: PropTypes.string,
    selectedStateDesc: PropTypes.string, // Add this prop type
};

export default QuestionResponse;










import React from 'react';
import PropTypes from 'prop-types';
import { useAppState } from '@d-lift/core';

const RespondingState = ({ renderAnswers, collectionData, setSelectedStateDesc }) => {
    const [filteredStates] = useAppState('filteredStates', []);

    const handleStateClick = (state) => {
        const stateDesc = filteredStates.find((s) => s.CODE === state)?.DESCRIPTION;
        setSelectedStateDesc(stateDesc);
        renderAnswers(state);
    };

    return (
        <div>
            <h2>States</h2>
            <ul>
                {filteredStates.map((state) => (
                    <li key={state.CODE} onClick={() => handleStateClick(state.CODE)}>
                        {state.DESCRIPTION}
                    </li>
                ))}
            </ul>
        </div>
    );
};

RespondingState.propTypes = {
    renderAnswers: PropTypes.func.isRequired,
    collectionData: PropTypes.object.isRequired,
    setSelectedStateDesc: PropTypes.func.isRequired, // Add this prop type
};

export default RespondingState;


Leave a Comment