diff --git a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js index b3060da94..6b3b55b49 100644 --- a/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js +++ b/native/community-settings/tag-farcaster-channel/tag-farcaster-channel.react.js @@ -1,177 +1,223 @@ // @flow import { useActionSheet } from '@expo/react-native-action-sheet'; import invariant from 'invariant'; import * as React from 'react'; import { View, Text, TouchableOpacity, Platform } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; +import { + createOrUpdateFarcasterChannelTagActionTypes, + useCreateOrUpdateFarcasterChannelTag, +} from 'lib/actions/community-actions.js'; import { NeynarClientContext } from 'lib/components/neynar-client-provider.react.js'; import type { FarcasterChannel } from 'lib/types/farcaster-types.js'; import { useCurrentUserFID } from 'lib/utils/farcaster-utils.js'; +import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import type { TagFarcasterChannelNavigationProp } from './tag-farcaster-channel-navigator.react.js'; +import RegistrationButton from '../../account/registration/registration-button.react.js'; import SWMansionIcon from '../../components/swmansion-icon.react.js'; import { type NavigationRoute } from '../../navigation/route-names.js'; import { useSelector } from '../../redux/redux-utils.js'; import { useStyles, useColors } from '../../themes/colors.js'; export type TagFarcasterChannelParams = { +communityID: string, }; type Props = { +navigation: TagFarcasterChannelNavigationProp<'TagFarcasterChannel'>, +route: NavigationRoute<'TagFarcasterChannel'>, }; -// eslint-disable-next-line no-unused-vars function TagFarcasterChannel(props: Props): React.Node { + const { route } = props; + + const { communityID } = route.params; + const styles = useStyles(unboundStyles); const colors = useColors(); const fid = useCurrentUserFID(); invariant(fid, 'FID should be set'); const [selectedChannel, setSelectedChannel] = React.useState(null); const [channelOptions, setChannelOptions] = React.useState< $ReadOnlyArray, >([]); const neynarClientContext = React.useContext(NeynarClientContext); invariant(neynarClientContext, 'NeynarClientContext is missing'); const { client } = neynarClientContext; React.useEffect(() => { void (async () => { const channels = await client.fetchFollowedFarcasterChannels(fid); setChannelOptions(channels); })(); }, [client, fid]); const activeTheme = useSelector(state => state.globalThemeInfo.activeTheme); const { showActionSheetWithOptions } = useActionSheet(); const insets = useSafeAreaInsets(); const onOptionSelected = React.useCallback( (selectedIndex: ?number) => { if ( selectedIndex === undefined || selectedIndex === null || selectedIndex === channelOptions.length ) { return; } setSelectedChannel(channelOptions[selectedIndex]); }, [channelOptions], ); const onPressSelectChannel = React.useCallback(() => { const channelNames = channelOptions.map(channel => channel.name); const options = Platform.OS === 'ios' ? [...channelNames, 'Cancel'] : channelNames; const cancelButtonIndex = Platform.OS === 'ios' ? options.length - 1 : -1; const containerStyle = { paddingBottom: insets.bottom, }; showActionSheetWithOptions( { options, cancelButtonIndex, containerStyle, userInterfaceStyle: activeTheme ?? 'dark', }, onOptionSelected, ); }, [ activeTheme, channelOptions, insets.bottom, onOptionSelected, showActionSheetWithOptions, ]); + const dispatchActionPromise = useDispatchActionPromise(); + + const createOrUpdateFarcasterChannelTag = + useCreateOrUpdateFarcasterChannelTag(); + + const createCreateOrUpdateActionPromise = React.useCallback(async () => { + if (!selectedChannel) { + return undefined; + } + + try { + return await createOrUpdateFarcasterChannelTag({ + commCommunityID: communityID, + farcasterChannelID: selectedChannel.id, + }); + } catch (e) { + console.log('error', e); // TODO: Improve error handling + throw e; + } + }, [communityID, createOrUpdateFarcasterChannelTag, selectedChannel]); + + const onPressTag = React.useCallback(() => { + void dispatchActionPromise( + createOrUpdateFarcasterChannelTagActionTypes, + createCreateOrUpdateActionPromise(), + ); + }, [createCreateOrUpdateActionPromise, dispatchActionPromise]); + const channelSelectionStyles = React.useMemo( () => [styles.sectionContainer, styles.touchableSectionContainer], [styles.sectionContainer, styles.touchableSectionContainer], ); const channelSelectionTextContent = selectedChannel?.name ? selectedChannel.name : 'No Farcaster channel tagged'; + const buttonVariant = selectedChannel ? 'enabled' : 'disabled'; + const tagFarcasterChannel = React.useMemo( () => ( Tag a Farcaster channel so followers can find your Comm community! FARCASTER CHANNEL {channelSelectionTextContent} + ), [ styles.sectionContainer, styles.sectionText, styles.sectionHeaderText, channelSelectionStyles, onPressSelectChannel, channelSelectionTextContent, colors.panelForegroundSecondaryLabel, + onPressTag, + buttonVariant, ], ); return tagFarcasterChannel; } const unboundStyles = { sectionContainer: { backgroundColor: 'panelForeground', marginBottom: 24, padding: 16, }, sectionText: { color: 'panelForegroundLabel', fontSize: 14, }, sectionHeaderText: { fontSize: 14, fontWeight: '400', lineHeight: 20, color: 'panelForegroundLabel', paddingHorizontal: 16, paddingBottom: 4, }, touchableSectionContainer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, }; export default TagFarcasterChannel;