import Styles from './survey-list-styles.scss' ; import { Header , Footer , Error } from '@/presentation/components' ; import { SurveyListItem , surveyListState } from '@/presentation/pages/survey-list/components' ; import { useErrorHandler } from '@/presentation/hooks' ; import { LoadSurveyList } from '@/domain/usecases' ; import { useRecoilState , useResetRecoilState } from 'recoil' ; import React , { useEffect } from 'react' ; type Props = { loadSurveyList : LoadSurveyList ; } ; const SurveyList = ( { loadSurveyList } ) => { const resetSurveyListState = useResetRecoilState ( surveyListState ) ; const handleError = useErrorHandler ( ( error ) => { setState ( old => ( { ... old , error : error . message } ) ) ; } ) ; const [ state , setState ] = useRecoilState ( surveyListState ) ; const reload = ( ) : void => setState ( old => ( { surveys : [ ] , error : '' , reload : ! old . reload } ) ) ; useEffect ( ( ) => resetSurveyListState ( ) , [ ] ) ; useEffect ( ( ) => { loadSurveyList . loadAll ( ) . then ( surveys => setState ( old => ( { ... old , surveys } ) ) ) . catch ( handleError ) ; } , [ state . reload ] ) ; return ( < div className = { Styles . surveyListWrap } > < Header /> < div className = { Styles . contentWrap } > < h2 > Enquetes < /h2> { state . error ? < Error error = { state . error } reload = { reload } / > : < SurveyListItem surveys = { state . surveys } />} < /div> < Footer / > < /div>); } ; export default SurveyList ;