import { useEffect, useState } from 'react' import Grid from '@material-ui/core/Grid' import Divider from '@material-ui/core/Divider' import Typography from '@material-ui/core/Typography' import { connect } from 'react-redux' import { selectUserState } from '../../../redux/user/selector' import { selectAuthState } from '../../../redux/auth/selector' import { User } from '../../../interfaces/User' import { Button } from '@material-ui/core' import UserItem from './UserItem' import { Relationship } from '../../../interfaces/Relationship' import { Dispatch, bindActionCreators } from 'redux' import { getUserRelationship } from '../../../redux/user/service' import NextLink from 'next/link' import './style.scss' const mapStateToProps = (state: any) => { return { userState: selectUserState(state), authState: selectAuthState(state) } } const mapDispatchToProps = (dispatch: Dispatch) => ({ getUserRelationship: bindActionCreators(getUserRelationship, dispatch) }) interface Props { userState: any authState: any getUserRelationship: typeof getUserRelationship } const MyFriends = (props: Props) => { const { userState, authState, getUserRelationship } = props const initialState = { userId: undefined, updateNeeded: false } const [state, setState] = useState(initialState) const relationship = userState.get('relationship') as Relationship const friends = relationship.friend const requested = relationship.requested const friendsCount = friends?.length + requested?.length const loadUserRelationship = (userId: string, forceUpdate: boolean) => { if (userId && (forceUpdate || (userId !== state.userId && userId && userId !== '')) ) { getUserRelationship(userId) setState({ ...state, userId }) } } useEffect(() => { const user = authState.get('user') as User loadUserRelationship(user.id, false) }, []) useEffect(() => { const user = authState.get('user') as User const updateNeeded = userState.get('updateNeeded') loadUserRelationship(user.id, updateNeeded) loadUserRelationship(user.id, false) }, [authState, userState]) return (
Friends
{friends && friends.length > 0 && friends.map((friend) => { return })} {requested && requested.length > 0 && requested.map((friend) => { return })} {friendsCount === 0 && ( There is no friends. Please add the friends. )}
) } const MyFriendsWrapper = (props: any) => export default connect(mapStateToProps, mapDispatchToProps)(MyFriendsWrapper)