import React, { useEffect, useState } from 'react'; import { Text, View, TouchableOpacity, Image, TextInput, ScrollView, Dimensions, KeyboardAvoidingView, Platform } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { StackNavigationProp } from '@react-navigation/stack'; import { useSelector } from 'react-redux'; import ImagePicker from 'react-native-image-crop-picker'; import I18n from 'i18n-js'; import { IApplicationState } from '../../../definitions'; import { useTheme, withTheme } from '../../../theme'; import { themes } from '../../../lib/constants'; import { getIcon, handleSendMessage } from '../helpers'; import BoardDropdownModal from './BoardDropdown'; import styles from './styles'; import ReadyToPost from './ReadyToPost'; import IconOrAvatar from '../../../containers/RoomItem/IconOrAvatar'; import { useAppSelector } from '../../../lib/hooks'; import { getUidDirectMessage } from '../../../lib/methods/helpers'; import { getUserSelector } from '../../../selectors/login'; const hitSlop = { top: 10, right: 10, bottom: 10, left: 10 }; type ScreenProps = { route: any; }; const NewPostView: React.FC = ({ route }) => { const navigation = useNavigation>(); const isMasterDetail = useSelector((state: IApplicationState) => state.app.isMasterDetail); const { displayMode, showAvatar } = useSelector((state: IApplicationState) => state.sortPreferences); const server = useSelector((state: IApplicationState) => state.share.server.server || state.server.server); const user = useSelector((state: IApplicationState) => getUserSelector(state)); // const { theme } = useTheme(); const theme = 'light'; const [title, setTitle] = useState(''); const [image, setImage] = useState(null); const [description, setDescription] = useState(''); const [showBoardsModal, setShowBoardsModal] = useState(false); const [selectedBoard, setSelectedBoard] = useState(route.params?.selectedBoard ?? undefined); const [showReadyToPost, setShowReadyToPost] = useState(false); const [boards, setBoards] = useState(route.params?.boards ?? []); const id = getUidDirectMessage(selectedBoard); const userStatus = useAppSelector(state => state.activeUsers[id || '']?.status); useEffect(() => { navigation.setOptions({ title: 'Create a post', headerStyle: { shadowColor: 'transparent' } }); if (!isMasterDetail) { navigation.setOptions({ headerLeft: () => ( navigation.goBack()} hitSlop={hitSlop}> ), headerRight: () => null }); } }); const onImagePicker = () => { const options = { cropping: true, compressImageQuality: 0.8, freeStyleCropEnabled: true, cropperAvoidEmptySpaceAroundImage: false, cropperChooseText: I18n.t('Choose'), cropperCancelText: I18n.t('Cancel'), includeBase64: true }; ImagePicker.openPicker(options).then(image => { setImage({ ...image, data: `data:image/jpeg;base64,${image.data}` }); }); }; const showBanner = () => { if (image) { const imageAspectRatio = image?.width / image?.height; const width = Dimensions.get('window').width - 40; const height = width / imageAspectRatio; return ( ); } }; const getSelectedBoardIcon = () => { if (!selectedBoard) { return ; } const status = selectedBoard?.t === 'l' ? selectedBoard?.visitor?.status || selectedBoard?.v?.status : userStatus; return ( ); }; const isButtonDisabled = () => { if (title.trim() == '' && description.trim() == '') { return true; } return false; }; return ( Select Board setShowBoardsModal(true)} hitSlop={hitSlop}> {getSelectedBoardIcon()} {selectedBoard?.title ? selectedBoard.title : 'Select'} Post Title setTitle(e)} value={title} /> {showBanner()} Description setDescription(e)} multiline value={description} maxLength={4000} /> setShowReadyToPost(true)} disabled={isButtonDisabled()} > Publish setShowBoardsModal(false)} data={boards} onSelect={board => { setSelectedBoard(board); setShowBoardsModal(false); }} /> setShowReadyToPost(false)} onPost={() => { const { rid } = selectedBoard; const hasAttachment = image ? true : false; let finalDescrition = description; if (title) { finalDescrition = `${title} \n\n ${description}`; } let fileInfo = {}; if (hasAttachment) { fileInfo = { name: image.filename, description: finalDescrition, size: image.size, type: image.mime, path: image.path, store: 'Uploads' }; } handleSendMessage({ message: finalDescrition, rid, callBack: () => { setShowReadyToPost(false); navigation.goBack(); }, hasAttachment, fileInfo, server, user: { id: user.id, token: user.token } }); }} /> ); }; export default withTheme(NewPostView);