import { ChangeEvent, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { PATHS } from '@/shared/constants/paths'; import { useNavigate } from '@/shared/lib/hooks/use-navigate'; import { useNoticeListMutation } from '@/entities/support/api/use-notice-list-mutation'; import { DEFAULT_PAGE_PARAM } from '@/entities/common/model/constant'; import { DetailData, InformCl, NoticeItem, NoticeListParams, NoticeListResponse, SearchCl } from '@/entities/support/model/types'; import { SupportNoticeItem } from '@/entities/support/ui/notice-item'; import { DefaultRequestPagination, HeaderType } from '@/entities/common/model/types'; import { useSetHeaderTitle, useSetHeaderType, useSetFooterMode, useSetOnBack } from '@/widgets/sub-layout/use-sub-layout'; import useIntersectionObserver from '@/widgets/intersection-observer'; import { NoticeDetail } from '@/entities/support/ui/detail/notice-detail'; import { useParams } from 'react-router'; import { showAlert } from '@/widgets/show-alert'; import { snackBar } from '@/shared/lib'; import { useDetailOnStore } from '@/shared/model/store'; export const NoticeListPage = () => { const { navigate } = useNavigate(); const { seq } = useParams(); const { t } = useTranslation(); const [onActionIntersect, setOnActionIntersect] = useState(false); const [pageParam, setPageParam] = useState(DEFAULT_PAGE_PARAM); const [informCl, setInformCl] = useState(''); const [searchKeyword, setSearchKeyword] = useState(''); const [resultList, setResultList] = useState>([]); const { detailOn, setDetailOn } = useDetailOnStore(); const [detailSeq, setDetailSeq] = useState(); useSetHeaderTitle(t('support.notice.title')); useSetHeaderType(HeaderType.LeftArrow); useSetFooterMode(false); useSetOnBack(() => { navigate(PATHS.home); }); const { mutateAsync: noticeList } = useNoticeListMutation(); const onIntersect: IntersectionObserverCallback = (entries: Array) => { entries.forEach((entry: IntersectionObserverEntry) => { if(entry.isIntersecting){ if(onActionIntersect && !!pageParam.cursor){ setOnActionIntersect(false); callList('page'); } } }); }; const { setTarget } = useIntersectionObserver({ threshold: 1, onIntersect }); const callList = (type?: string) => { setOnActionIntersect(false); let listParams: NoticeListParams = { informCl: informCl, searchCl: (!!searchKeyword)? SearchCl.HEAD: null, searchKeyword: searchKeyword, page: pageParam }; if(type !== 'page' && listParams.page){ listParams.page.cursor = null; } noticeList(listParams).then((rs: NoticeListResponse) => { if(type === 'page'){ setResultList([ ...resultList, ...rs.content ]); } else{ setResultList(rs.content); } if(rs.hasNext && rs.nextCursor !== pageParam.cursor && rs.content.length === DEFAULT_PAGE_PARAM.size ){ setPageParam({ ...pageParam, ...{ cursor: rs.nextCursor } }); } else{ setPageParam({ ...pageParam, ...{ cursor: null } }); } setOnActionIntersect( !!rs.hasNext && rs.nextCursor !== pageParam.cursor && rs.content.length === DEFAULT_PAGE_PARAM.size ); }).catch((e: any) => { if(e.response?.data?.error?.message){ snackBar(e.response?.data?.error?.message); return; } }); }; const onClickToAction = () => { // Remove focus from active element if (document.activeElement instanceof HTMLElement) { document.activeElement.blur(); } callList(); }; const setDetailData = (detailData: DetailData) => { setDetailOn(detailData.detailOn); if(detailData?.seq){ setDetailSeq(detailData?.seq); } }; const getNoticeList = () => { let rs = []; for(let i=0;i ); } } return rs; }; useEffect(() => { callList(); }, [informCl]); useEffect(() => { if(seq){ setDetailOn(true); setDetailSeq(parseInt(seq)); } }, [seq]); return ( <>
) => setSearchKeyword(e.target.value) } onKeyDown={ (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.nativeEvent.isComposing) { onClickToAction(); } }} />
{ getNoticeList() }
); };