simplified saving reply draft on modal close

This commit is contained in:
Nouman Tahir 2022-07-18 18:13:48 +05:00
parent 14467b2c9d
commit 123c2f25e7
2 changed files with 22 additions and 18 deletions

View File

@ -1,7 +1,7 @@
import React, { useEffect, useState, useCallback } from 'react'; import React, { useEffect, useState, useCallback } from 'react';
import EStyleSheet from 'react-native-extended-stylesheet'; import EStyleSheet from 'react-native-extended-stylesheet';
import styles from './quickReplyModalStyles'; import styles from './quickReplyModalStyles';
import { View, Text, Alert, TouchableOpacity, Keyboard, Platform, KeyboardAvoidingView } from 'react-native'; import { View, Text, Alert, TouchableOpacity, Keyboard, Platform } from 'react-native';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { IconButton, MainButton, TextButton, TextInput, UserAvatar } from '..'; import { IconButton, MainButton, TextButton, TextInput, UserAvatar } from '..';
import { useSelector, useDispatch } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
@ -19,20 +19,19 @@ import { navigate } from '../../navigation/service';
import { postBodySummary } from '@ecency/render-helper'; import { postBodySummary } from '@ecency/render-helper';
import { Draft } from '../../redux/reducers/cacheReducer'; import { Draft } from '../../redux/reducers/cacheReducer';
import { RootState } from '../../redux/store/store'; import { RootState } from '../../redux/store/store';
import { useImperativeHandle } from 'react';
import { forwardRef } from 'react';
export interface QuickReplyModalContentProps { export interface QuickReplyModalContentProps {
fetchPost?: any;
selectedPost?: any; selectedPost?: any;
inputRef?: any;
handleCloseRef?: any; handleCloseRef?: any;
onClose:()=>void; onClose: () => void;
} }
export const QuickReplyModalContent = ({ export const QuickReplyModalContent = forwardRef(({
selectedPost, selectedPost,
handleCloseRef,
onClose, onClose,
}: QuickReplyModalContentProps) => { }: QuickReplyModalContentProps, ref) => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useDispatch(); const dispatch = useDispatch();
const currentAccount = useSelector((state: RootState) => state.account.currentAccount); const currentAccount = useSelector((state: RootState) => state.account.currentAccount);
@ -49,26 +48,26 @@ export const QuickReplyModalContent = ({
let parentPermlink = selectedPost ? selectedPost.permlink : ''; let parentPermlink = selectedPost ? selectedPost.permlink : '';
let draftId = `${currentAccount.name}/${parentAuthor}/${parentPermlink}`; //different draftId for each user acount let draftId = `${currentAccount.name}/${parentAuthor}/${parentPermlink}`; //different draftId for each user acount
useImperativeHandle(ref, () => ({
useEffect(() => { handleSheetClose() {
handleCloseRef.current = handleSheetClose; _addQuickCommentIntoCache();
}, [commentValue]); },
}));
// load quick comment value from cache // load quick comment value from cache
useEffect(() => { useEffect(() => {
if (drafts.has(draftId) && currentAccount.name === drafts.get(draftId).author) { if (drafts.has(draftId) && currentAccount.name === drafts.get(draftId).author) {
const quickComment: Draft = drafts.get(draftId); const quickComment: Draft = drafts.get(draftId);
//TODO: use native method to update comment value instead
setCommentValue(quickComment.body); setCommentValue(quickComment.body);
} else { } else {
//TODO: use native method to update comment value instead
setCommentValue(''); setCommentValue('');
} }
}, [selectedPost]); }, [selectedPost]);
// handlers
const handleSheetClose = () => {
_addQuickCommentIntoCache();
};
// add quick comment value into cache // add quick comment value into cache
const _addQuickCommentIntoCache = (value = commentValue) => { const _addQuickCommentIntoCache = (value = commentValue) => {
@ -144,6 +143,7 @@ export const QuickReplyModalContent = ({
stateTimer = setTimeout(() => { stateTimer = setTimeout(() => {
setIsSending(false); setIsSending(false);
onClose(); onClose();
//TODO: use native method to update comment value instead
setCommentValue(''); setCommentValue('');
dispatch( dispatch(
toastNotification( toastNotification(
@ -214,6 +214,7 @@ export const QuickReplyModalContent = ({
const _deboucedCacheUpdate = useCallback(debounce(_addQuickCommentIntoCache, 500), []) const _deboucedCacheUpdate = useCallback(debounce(_addQuickCommentIntoCache, 500), [])
const _onChangeText = (value) => { const _onChangeText = (value) => {
//TODO: use native method to update comment value instead
setCommentValue(value); setCommentValue(value);
_deboucedCacheUpdate(value) _deboucedCacheUpdate(value)
} }
@ -301,4 +302,4 @@ export const QuickReplyModalContent = ({
) )
return _renderContent return _renderContent
}; });

View File

@ -10,10 +10,13 @@ const QuickReplyModal = () => {
const replyModalVisible = useAppSelector((state) => state.ui.replyModalVisible); const replyModalVisible = useAppSelector((state) => state.ui.replyModalVisible);
const replyModalPost = useAppSelector(state => state.ui.replyModalPost) const replyModalPost = useAppSelector(state => state.ui.replyModalPost)
const handleCloseRef = useRef(null); const modalContentRef = useRef(null);
const _onClose = () => { const _onClose = () => {
if(modalContentRef.current){
modalContentRef.current.handleSheetClose();
}
dispatch(hideReplyModal()); dispatch(hideReplyModal());
} }
@ -24,9 +27,9 @@ const QuickReplyModal = () => {
onClose={_onClose} onClose={_onClose}
> >
<QuickReplyModalContent <QuickReplyModalContent
ref={modalContentRef}
selectedPost={replyModalPost} selectedPost={replyModalPost}
onClose={_onClose} onClose={_onClose}
handleCloseRef={handleCloseRef}
/> />
</InputSupportModal> </InputSupportModal>