diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/comments.tsx | 96 | ||||
-rw-r--r-- | src/utils/moments.ts | 34 | ||||
-rw-r--r-- | src/utils/users.ts | 50 |
3 files changed, 173 insertions, 7 deletions
diff --git a/src/utils/comments.tsx b/src/utils/comments.tsx new file mode 100644 index 00000000..0d551682 --- /dev/null +++ b/src/utils/comments.tsx @@ -0,0 +1,96 @@ +import React from 'react'; +import {StyleProp, Text, TextStyle} from 'react-native'; +import { + isMentionPartType, + parseValue, + Part, + PartType, +} from 'react-native-controlled-mentions'; +import TaggTypeahead from '../components/common/TaggTypeahead'; +import {TAGG_LIGHT_BLUE} from '../constants'; +import {UserType} from '../types'; + +/** + * Part renderer + * + * https://github.com/dabakovich/react-native-controlled-mentions#rendering-mentioninputs-value + */ +const renderPart = ( + part: Part, + index: number, + handlePress: (user: UserType) => void, +) => { + // Just plain text + if (!part.partType) { + return <Text key={index}>{part.text}</Text>; + } + + // Mention type part + if (isMentionPartType(part.partType)) { + return ( + <Text + key={`${index}-${part.data?.trigger}`} + style={part.partType.textStyle} + onPress={() => { + if (part.data) { + handlePress({ + userId: part.data.id, + username: part.data.name, + }); + } + }}> + {part.text} + </Text> + ); + } + + // Other styled part types + return ( + <Text key={`${index}-pattern`} style={part.partType.textStyle}> + {part.text} + </Text> + ); +}; + +interface RenderProps { + value: string; + styles: StyleProp<TextStyle>; + partTypes: PartType[]; + onPress: (user: UserType) => void; +} + +/** + * Value renderer. Parsing value to parts array and then mapping the array using 'renderPart' + * + * https://github.com/dabakovich/react-native-controlled-mentions#rendering-mentioninputs-value + */ +export const renderTextWithMentions: React.FC<RenderProps> = ({ + value, + styles, + partTypes, + onPress, +}) => { + const {parts} = parseValue(value, partTypes); + return ( + <Text style={styles}> + {parts.map((part, index) => renderPart(part, index, onPress))} + </Text> + ); +}; + +export const mentionPartTypes: (style: 'blue' | 'white') => PartType[] = ( + style, +) => { + return [ + { + trigger: '@', + renderSuggestions: (props) => <TaggTypeahead {...props} />, + allowedSpacesCount: 0, + isInsertSpaceAfterMention: true, + textStyle: + style === 'blue' + ? {color: TAGG_LIGHT_BLUE} + : {color: 'white', fontWeight: '800'}, + }, + ]; +}; diff --git a/src/utils/moments.ts b/src/utils/moments.ts index 87f062af..90d69519 100644 --- a/src/utils/moments.ts +++ b/src/utils/moments.ts @@ -37,3 +37,37 @@ export const getTimePosted = (date_time: string) => { } return time; }; + +export const getTimeInShorthand = (date_time: string) => { + const datePosted = moment(date_time); + const now = moment(); + var time = date_time; + var difference = now.diff(datePosted, 's'); + + // Creating elapsedTime string to display to user + // 0 to less than 1 minute + if (difference < 60) { + time = difference + 's'; + } + // 1 minute to less than 1 hour + else if (difference >= 60 && difference < 60 * 60) { + difference = now.diff(datePosted, 'm'); + time = difference + 'm'; + } + // 1 hour to less than 1 day + else if (difference >= 60 * 60 && difference < 24 * 60 * 60) { + difference = now.diff(datePosted, 'h'); + time = difference + 'h'; + } + // Any number of days + else if (difference >= 24 * 60 * 60 && difference < 24 * 60 * 60 * 7) { + difference = now.diff(datePosted, 'd'); + time = difference + 'd'; + } + // More than 7 days + else if (difference >= 24 * 60 * 60 * 7) { + difference = now.diff(datePosted, 'w'); + time = difference + 'w'; + } + return time; +}; diff --git a/src/utils/users.ts b/src/utils/users.ts index 7148eb79..334cb3c0 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -1,6 +1,6 @@ import AsyncStorage from '@react-native-community/async-storage'; import {INTEGRATED_SOCIAL_LIST} from '../constants'; -import {isUserBlocked, loadSocialPosts} from '../services'; +import {isUserBlocked, loadSocialPosts, removeBadgesService} from '../services'; import { loadAllSocials, loadBlockedList, @@ -10,6 +10,7 @@ import { loadUserMoments, loadUserNotifications, logout, + updateUserBadges, } from '../store/actions'; import {NO_SOCIAL_ACCOUNTS} from '../store/initialStates'; import {loadUserMomentCategories} from './../store/actions/momentCategories'; @@ -17,10 +18,11 @@ import {loadUserX} from './../store/actions/userX'; import {AppDispatch} from './../store/configureStore'; import {RootState} from './../store/rootReducer'; import { - ProfilePreviewType, ProfileInfoType, + ProfilePreviewType, ScreenType, UserType, + UniversityBadge, } from './../types/types'; const loadData = async (dispatch: AppDispatch, user: UserType) => { @@ -168,11 +170,6 @@ export const checkIfUserIsBlocked = async ( return await isUserBlocked(userId, loggedInUser.userId, token); }; -export const defaultUserProfile = () => { - const defaultImage = require('../assets/images/avatar-placeholder.png'); - return defaultImage; -}; - /** * Used to determine whether the logged-in user is able to view userX's private * information or not. @@ -204,3 +201,42 @@ export const canViewProfile = ( } return false; }; + +/* Function to call remove badge service, + * remove selected badge from list passed in and + * dispatch thunk action to update store + */ +export const removeUserBadge = async ( + badges: UniversityBadge[], + badgeName: string, + userId: string, + dispatch: AppDispatch, +) => { + const success = await removeBadgesService([badgeName], userId); + if (success === true) { + badges = badges.filter((badge) => badge.name !== badgeName); + dispatch(updateUserBadges(badges)); + } +}; + +export const navigateToProfile = async ( + state: RootState, + dispatch: any, + navigation: any, + screenType: ScreenType, + user: UserType, +) => { + const loggedInUserId = state.user.user.userId; + const {userId, username} = user; + if (!userXInStore(state, screenType, userId)) { + await fetchUserX( + dispatch, + {userId: userId, username: username}, + screenType, + ); + } + navigation.push('Profile', { + userXId: userId === loggedInUserId ? undefined : userId, + screenType, + }); +}; |