From 9895bd4c52854767657f4d70dba4018cba2b8751 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Fri, 21 May 2021 20:04:12 -0400 Subject: Refactor code --- src/components/common/MomentTags.tsx | 79 ++++++++++++++++++++++++++++ src/components/common/index.ts | 1 + src/components/moments/MomentPostContent.tsx | 9 ++-- src/screens/profile/CaptionScreen.tsx | 53 ++++++------------- 4 files changed, 101 insertions(+), 41 deletions(-) create mode 100644 src/components/common/MomentTags.tsx (limited to 'src') diff --git a/src/components/common/MomentTags.tsx b/src/components/common/MomentTags.tsx new file mode 100644 index 00000000..f6f475bc --- /dev/null +++ b/src/components/common/MomentTags.tsx @@ -0,0 +1,79 @@ +import React, {MutableRefObject, useEffect, useState} from 'react'; +import {MomentTagType, ProfilePreviewType} from '../../types'; +import TaggDraggable from '../taggs/TaggDraggable'; +import Draggable from './Draggable'; + +interface MomentTagsProps { + editing: boolean; + tags: MomentTagType[]; + imageRef: MutableRefObject; + deleteFromList?: (user: ProfilePreviewType) => void; +} + +const MomentTags: React.FC = ({ + editing, + tags, + imageRef, + deleteFromList, +}) => { + const [offset, setOffset] = useState([0, 0]); + const [curStart, setCurStart] = useState([0, 0]); + const [imageDimensions, setImageDimensions] = useState([0, 0]); + + useEffect(() => { + imageRef.current.measure((fx, fy, width, height, px, py) => { + console.log(px, py); + console.log(width, height); + setOffset([px, py]); + setImageDimensions([width, height]); + }); + }, []); + + if (!tags) { + return null; + } + + return editing && deleteFromList ? ( + <> + {tags.map((tag) => ( + null}> + deleteFromList(tag.user)} + setStart={setCurStart} + /> + + ))} + + ) : ( + <> + {tags.map((tag) => ( + null}> + null} + setStart={setCurStart} + /> + + ))} + + ); +}; + +export default MomentTags; diff --git a/src/components/common/index.ts b/src/components/common/index.ts index 48abb8b8..692c9f8a 100644 --- a/src/components/common/index.ts +++ b/src/components/common/index.ts @@ -27,3 +27,4 @@ export {default as Avatar} from './Avatar'; export {default as TaggTypeahead} from './TaggTypeahead'; export {default as TaggUserRowCell} from './TaggUserRowCell'; export {default as LikeButton} from './LikeButton'; +export {default as MomentTags} from './MomentTags'; diff --git a/src/components/moments/MomentPostContent.tsx b/src/components/moments/MomentPostContent.tsx index 9a174e73..e702cb68 100644 --- a/src/components/moments/MomentPostContent.tsx +++ b/src/components/moments/MomentPostContent.tsx @@ -1,20 +1,20 @@ import {useNavigation} from '@react-navigation/native'; -import React, {useEffect, useState} from 'react'; +import React, {useEffect, useRef, useState} from 'react'; import {Image, StyleSheet, Text, View, ViewProps} from 'react-native'; import {useDispatch, useStore} from 'react-redux'; import {getCommentsCount, loadMomentTags} from '../../services'; -import {userMomentsFetched} from '../../store/reducers'; import {RootState} from '../../store/rootReducer'; import {MomentTagType, ScreenType, UserType} from '../../types'; import { getTimePosted, navigateToProfile, + normalize, SCREEN_HEIGHT, SCREEN_WIDTH, - normalize, } from '../../utils'; import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments'; import {CommentsCount} from '../comments'; +import {MomentTags} from '../common'; interface MomentPostContentProps extends ViewProps { screenType: ScreenType; @@ -37,6 +37,7 @@ const MomentPostContent: React.FC = ({ const state: RootState = useStore().getState(); const navigation = useNavigation(); const dispatch = useDispatch(); + const imageRef = useRef(null); useEffect(() => { const loadTags = async () => { @@ -60,10 +61,12 @@ const MomentPostContent: React.FC = ({ return ( + = ({route, navigation}) => { const [loading, setLoading] = useState(false); const imageRef = useRef(null); - // state variables used to position draggables - const [offset, setOffset] = useState([0, 0]); - const [curStart, setCurStart] = useState([0, 0]); - const [imageDimensions, setImageDimensions] = useState([0, 0]); - const [taggList, setTaggList] = useState([ { first_name: 'Ivan', @@ -94,16 +87,6 @@ const CaptionScreen: React.FC = ({route, navigation}) => { }); }; - /** - * need a handler to take care of creating a tagged user object, append that object to the taggList state variable. - */ - useEffect(() => { - imageRef.current.measure((fx, fy, width, height, px, py) => { - setOffset([px, py]); - setImageDimensions([width, height]); - }); - }, []); - const handleShare = async () => { setLoading(true); if (!image.filename) { @@ -165,25 +148,19 @@ const CaptionScreen: React.FC = ({route, navigation}) => { onChange={setCaption} partTypes={mentionPartTypes('blue')} /> - {taggList.map((user) => ( - null}> - - setTaggList(taggList.filter((u) => u.id !== user.id)) - } - setStart={setCurStart} - /> - - ))} + ({ + id: '', + x: 0, + y: 0, + user, + }))} + imageRef={imageRef} + deleteFromList={(user) => + setTaggList(taggList.filter((u) => u.id !== user.id)) + } + /> -- cgit v1.2.3-70-g09d2