Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
5.0 kB
3
Indexable
Never
import React, { useEffect } from 'react';
import { withPage, useAppState, AppContext } from '@d-lift/core';
import ConstantKeys from '@/Constants/ConstantKeys';
import SearchRFIForm from './Component/SearchRFIForm';
import SearchRFIFilters from './Component/SearchRFIFilters';
import { Page } from '@d-lift/uxcomponents';
import RightSection from '@/Layout/RightSection';
import BodySection from '@/Layout/BodySection';
import './search.scss';
import RFIUtil from '@/Util/RFIUtil';

const Search = () => {
    const defaultSearchRFI = {
        search: '',
        category: [...ConstantKeys.DEFAULT_OPTION.CATEGORY],
        programs: [...ConstantKeys.DEFAULT_OPTION.PROGRAM],
        state: '',
        neededBy: '',
        offering: '',
    };

    const [searchRFI, setSearchRFI] = useAppState('searchRFI', defaultSearchRFI);

    const updateSearchRFI = (newState) => {
        setSearchRFI(newState);
    };

    const [categoryList, setCategoryList] = useAppState('categoryList', []);
    const [programList, setProgramList] = useAppState('programList', []);
    const [filteredCategory, setFilteredCategory] = useAppState('filteredCategory', []);
    const [filteredPrograms, setFilteredPrograms] = useAppState('filteredPrograms', []);
    const [refDataLoaded, setRefDataLoaded] = useAppState('refDataLoaded', false);
    const [filteredStates, setFilteredStates] = useAppState('filteredStates', []);
    const [filteredOfferings, setFilteredOfferings] = useAppState('filteredOfferings', []);

    const refTableContent = RFIUtil.fetchRefTableContentFromSession(
        ConstantKeys.REF_TABLE.CREATE_RFI_REF_TABLES,
    );
    useEffect(() => {
        setFilteredStates(RFIUtil.getAuthorizedStateList());
        RFIUtil.isRefDataLoaded(ConstantKeys.REF_TABLE.CREATE_RFI_REF_TABLES, setRefDataLoaded);

        if (refDataLoaded && refTableContent) {
            const programs = refTableContent.PROGRAM;
            setProgramList(programs);

            const categories = refTableContent.CATEGORY;
            setCategoryList(categories);
            filterProgramAndCategory({ target: { value: ConstantKeys.DEFAULT_OPTION.OFFERING } });
            filterAuthOfferings();
        }
    }, [refDataLoaded]);

    const filterAuthOfferings = () => {
        let filteredOfferingData = [];
        const privilegeData = AppContext.security.getProfile().profile;

        // Extract all unique offering values from the privilegeData
        const uniqueOfferings = [...new Set(privilegeData.flatMap((detail) => detail.offering))];

        // Filter offerings based on the unique offerings
        filteredOfferingData = refTableContent?.OFFERING?.filter((offering) =>
            uniqueOfferings?.includes(offering?.CODE),
        );

        // Update the offering list if filtered data is available
        if (filteredOfferingData?.length > 0) {
            setFilteredOfferings(filteredOfferingData);
        }
    };

    const filterProgramAndCategory = (event) => {
        const selectedOffering = event.target.value
            ? event.target.value
            : ConstantKeys.DEFAULT_OPTION.OFFERING;

        const filteredProg = RFIUtil.getTableDataForOffering(
            ConstantKeys.CONTEXT_KEYS.Program,
            selectedOffering,
        );
        setFilteredPrograms(filteredProg);

        const filteredCategories = RFIUtil.getTableDataForOffering(
            ConstantKeys.CONTEXT_KEYS.Category,
            selectedOffering,
        );
        setFilteredCategory(filteredCategories);

        updateSearchRFI({
            ...searchRFI,
            category: [...ConstantKeys.DEFAULT_OPTION.CATEGORY],
            programs: [...ConstantKeys.DEFAULT_OPTION.PROGRAM],
        });
    };

    return (
        <Page ref-table-list="PROGRAM, CATEGORY, OFFERING, STATE,DEADLINE">
            {refDataLoaded ? (
                <div className="d-flex ux-rfi-mobile-layout">
                    <BodySection>
                        <SearchRFIForm
                            searchRFI={searchRFI}
                            updateSearchRFI={updateSearchRFI}
                            filteredCategory={filteredCategory}
                            filteredPrograms={filteredPrograms}></SearchRFIForm>
                    </BodySection>

                    <RightSection>
                        <SearchRFIFilters
                            filterProgramAndCategory={filterProgramAndCategory}
                            filteredStates={filteredStates}
                            filteredOfferings={filteredOfferings}></SearchRFIFilters>
                    </RightSection>
                </div>
            ) : (
                <></>
            )}
        </Page>
    );
};

export default withPage(
    {
        Description: 'Search RFI Page',
        ContentManager: true,
        LayoutStyle: 'rfi-dashboard',
    },
    Search,
);
Leave a Comment