import React from 'react' ; import { useSelector , useStore } from 'react-redux' ; import { follow , unfollow } from '@app/redux/actions/follow' ; import { getUserInfo } from '@app/redux/reducers/user' ; interface Props { posts ? : any ; user ? : any ; topic ? : any ; children ? : any ; } export default function ( { posts , user , topic , children } ) { let target = posts || user || topic ; const me = useSelector ( ( state ) => getUserInfo ( state ) ) ; const store = useStore ( ) ; const _follow = ( args ) => follow ( args ) ( store . dispatch , store . getState ) ; const _unfollow = ( args ) => unfollow ( args ) ( store . dispatch , store . getState ) ; if ( me && posts && posts . user_id && posts . user_id . _id == me . _id || me && user && user . _id == me . _id ) { return '' ; } const stopPropagation = function ( e ) { e . stopPropagation ( ) ; } ; const handleFollow = function ( e ) { e . stopPropagation ( ) ; let args = { } ; if ( posts ) args . posts_id = posts . _id ; if ( user ) args . user_id = user . _id ; if ( topic ) args . topic_id = topic . _id ; _follow ( { args } ) . then ( function ( [ err , res ] ) { if ( err ) { $ . toast ( { text : err , position : 'top-center' , showHideTransition : 'slide' , icon : 'info' , loader : false , allowToastClose : false } ) ; } } ) ; } ; const handleUnfollow = function ( e ) { e . stopPropagation ( ) ; let args = { } ; if ( posts ) args . posts_id = posts . _id ; if ( user ) args . user_id = user . _id ; if ( topic ) args . topic_id = topic . _id ; _unfollow ( { args } ) . then ( function ( [ err , res ] ) { if ( err ) { $ . toast ( { text : err , position : 'top-center' , showHideTransition : 'slide' , icon : 'error' , loader : false , allowToastClose : false } ) ; } } ) ; } ; if ( ! me ) { return ( < span data - toggle = "modal" data - target = "#sign" onClick = { stopPropagation } > { children } < /span>); } else if ( target . follow ) { return ( < span onClick = { handleUnfollow } > { children } < /span>); } else { return ( < span onClick = { handleFollow } > { children } < /span>); } }