diff options
author | Ivan Chen <ivan@tagg.id> | 2021-05-13 15:09:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-13 15:09:58 -0400 |
commit | 0fb9405c949b2dc92e0e834f684a1f938892f230 (patch) | |
tree | 5814ecfb0829e3d518ba27df8b21156cb6c3e627 /src/components/messages/MessageButton.tsx | |
parent | 2c51e35b1a6160bfedea6a3d38d0c4de2736ebdc (diff) | |
parent | 9ff2d1e32f7eb7406e139a493b1dbf4422fd1a4c (diff) |
Merge pull request #420 from shravyaramesh/tma803-friend-accept-notif
[TMA-803] Modify friend request accepted notification
Diffstat (limited to 'src/components/messages/MessageButton.tsx')
-rw-r--r-- | src/components/messages/MessageButton.tsx | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/components/messages/MessageButton.tsx b/src/components/messages/MessageButton.tsx new file mode 100644 index 00000000..5ac42c4c --- /dev/null +++ b/src/components/messages/MessageButton.tsx @@ -0,0 +1,73 @@ +import React from 'react'; +import {Fragment, useContext} from 'react'; +import {useStore} from 'react-redux'; +import {ChatContext} from '../../App'; +import {RootState} from '../../store/rootReducer'; +import {FriendshipStatusType} from '../../types'; +import {createChannel} from '../../utils'; +import {Alert, StyleProp, TextStyle, ViewStyle} from 'react-native'; +import {BasicButton} from '../common'; +import {useNavigation} from '@react-navigation/native'; +import {ERROR_UNABLE_CONNECT_CHAT} from '../../constants/strings'; + +interface MessageButtonProps { + userXId: string; + isBlocked: boolean; + friendship_status: FriendshipStatusType; + friendship_requester_id?: string; + solid?: boolean; + externalStyles?: Record<string, StyleProp<ViewStyle | TextStyle>>; +} + +const MessageButton: React.FC<MessageButtonProps> = ({ + userXId, + isBlocked, + friendship_status, + friendship_requester_id, + solid, + externalStyles, +}) => { + const navigation = useNavigation(); + const {chatClient, setChannel} = useContext(ChatContext); + + const state: RootState = useStore().getState(); + const loggedInUserId = state.user.user.userId; + + const canMessage = () => { + if ( + userXId && + !isBlocked && + (friendship_status === 'no_record' || + friendship_status === 'friends' || + (friendship_status === 'requested' && + friendship_requester_id === loggedInUserId)) + ) { + return true; + } else { + return false; + } + }; + + const onPressMessage = async () => { + if (chatClient.user && userXId) { + const channel = await createChannel(loggedInUserId, userXId, chatClient); + setChannel(channel); + navigation.navigate('Chat'); + } else { + Alert.alert(ERROR_UNABLE_CONNECT_CHAT); + } + }; + + return canMessage() ? ( + <BasicButton + title={'Message'} + onPress={onPressMessage} + externalStyles={externalStyles} + solid={solid ? solid : false} + /> + ) : ( + <Fragment /> + ); +}; + +export default MessageButton; |