-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { | ||
SetStateAction, | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import { useNavigation, useRoute } from '@react-navigation/core'; | ||
import { ITextSelection, useLoadData } from '@src/hooks'; | ||
import instance from '@src/Instance'; | ||
import { updateCommentContent, useCommentContent } from '@src/state'; | ||
import { | ||
NativeSyntheticEvent, | ||
TextInput, | ||
TextInputSelectionChangeEventData, | ||
} from 'react-native'; | ||
import { NativeStackNavigationProp } from '@react-navigation/native-stack'; | ||
import HeaderButton from '@components/Common/Button/HeaderButton'; | ||
|
||
interface UseEditReplyScreen { | ||
text: string; | ||
setText: React.Dispatch<SetStateAction<string>>; | ||
selection: ITextSelection; | ||
onSelectionChange: ( | ||
e: NativeSyntheticEvent<TextInputSelectionChangeEventData>, | ||
) => void; | ||
isLoading: boolean; | ||
inputRef: React.MutableRefObject<TextInput | undefined>; | ||
} | ||
|
||
export const useEditReplyScreen = (): UseEditReplyScreen => { | ||
const navigation = useNavigation<NativeStackNavigationProp<any>>(); | ||
const { commentId } = useRoute<any>().params; | ||
|
||
const commentContent = useCommentContent(commentId); | ||
|
||
const [text, setText] = useState(commentContent ?? ''); | ||
const [selection, setSelection] = useState<ITextSelection>({ | ||
start: 0, | ||
end: 0, | ||
}); | ||
|
||
const { isLoading, refresh: submit } = useLoadData(); | ||
|
||
const inputRef = useRef<TextInput>(); | ||
|
||
const onSubmitPress = (): void => { | ||
submit(async () => { | ||
await instance.editComment(commentId, text); | ||
|
||
updateCommentContent(commentId, text); | ||
|
||
navigation.pop(); | ||
}); | ||
}; | ||
|
||
const onSelectionChange = useCallback( | ||
(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => { | ||
setSelection({ | ||
start: e.nativeEvent.selection.start, | ||
end: e.nativeEvent.selection.end, | ||
}); | ||
}, | ||
[setSelection], | ||
); | ||
|
||
useEffect(() => { | ||
navigation.setOptions({ | ||
headerLeft: () => ( | ||
<HeaderButton | ||
title="Back" | ||
onPress={() => { | ||
navigation.pop(); | ||
}} | ||
/> | ||
), | ||
}); | ||
|
||
inputRef.current?.setNativeProps({ | ||
text, | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
navigation.setOptions({ | ||
headerRight: () => ( | ||
<HeaderButton title="Submit" onPress={onSubmitPress} /> | ||
), | ||
}); | ||
}, [text]); | ||
|
||
return { | ||
text, | ||
setText, | ||
onSelectionChange, | ||
selection, | ||
isLoading, | ||
inputRef, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useCallback, useRef } from 'react'; | ||
import { INavigationProps } from '@src/types'; | ||
import { ScrollView, YStack } from 'tamagui'; | ||
import AppToast from '@components/Common/Toast/AppToast'; | ||
import LoadingOverlay from '@components/Common/Loading/LoadingOverlay'; | ||
import TextInput from '@components/Common/Form/TextInput'; | ||
import KeyboardAccessoryView from '@components/Common/Keyboard/KeyboardAccesoryView'; | ||
import { useEditReplyScreen } from '@components/Reply/hooks/useEditReplyScreen'; | ||
import { ScrollView as RNScrollView } from 'react-native'; | ||
|
||
export default function EditReplyScreen({ | ||
navigation, | ||
route, | ||
}: INavigationProps): React.JSX.Element { | ||
const editReplyScreen = useEditReplyScreen(); | ||
|
||
const viewRef = useRef<RNScrollView>(); | ||
|
||
const onLayout = useCallback(() => { | ||
viewRef.current?.scrollToEnd({ animated: false }); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{/* @ts-expect-error - this is valid */} | ||
<ScrollView automaticallyAdjustKeyboardInsets={true} ref={viewRef}> | ||
<AppToast translate={100} /> | ||
<LoadingOverlay visible={editReplyScreen.isLoading} /> | ||
<YStack space="$2" mb="$2"> | ||
<TextInput | ||
inputAccessoryViewID="accessory" | ||
onSelectionChange={editReplyScreen.onSelectionChange} | ||
onChangeText={editReplyScreen.setText} | ||
fontSize={16} | ||
ref={editReplyScreen.inputRef} | ||
autoFocus={true} | ||
multiline={true} | ||
scrollEnabled={false} | ||
onLayout={onLayout} | ||
placeholder="What do you want to say?" | ||
/> | ||
</YStack> | ||
</ScrollView> | ||
<KeyboardAccessoryView | ||
text={editReplyScreen.text} | ||
setText={editReplyScreen.setText} | ||
selection={editReplyScreen.selection} | ||
inputRef={editReplyScreen.inputRef} | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const getParentId = (path: string): number => { | ||
const pathArr = path.split('.'); | ||
|
||
return Number(pathArr[pathArr.length - 2]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './buildCommentChains'; | ||
export * from './createCommentReplyComponents'; | ||
export * from './getParentId'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters