From 0570f1fa41bc9d41303ade2cf54592a4670cb87b Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 26 Jan 2021 18:09:32 -0500 Subject: renamed services to have consistent naming and added common service --- src/services/CommonService.ts | 22 ++++ src/services/ExploreService.ts | 59 ++++++++++ src/services/ExploreServices.ts | 60 ----------- src/services/MomentService.ts | 209 ++++++++++++++++++++++++++++++++++++ src/services/MomentServices.ts | 209 ------------------------------------ src/services/UserFriendsService.ts | 149 +++++++++++++++++++++++++ src/services/UserFriendsServices.ts | 149 ------------------------- src/services/index.ts | 7 +- 8 files changed, 443 insertions(+), 421 deletions(-) create mode 100644 src/services/CommonService.ts create mode 100644 src/services/ExploreService.ts delete mode 100644 src/services/ExploreServices.ts create mode 100644 src/services/MomentService.ts delete mode 100644 src/services/MomentServices.ts create mode 100644 src/services/UserFriendsService.ts delete mode 100644 src/services/UserFriendsServices.ts (limited to 'src') diff --git a/src/services/CommonService.ts b/src/services/CommonService.ts new file mode 100644 index 00000000..4f9fb47a --- /dev/null +++ b/src/services/CommonService.ts @@ -0,0 +1,22 @@ +import RNFetchBlob from 'rn-fetch-blob'; + +export const loadImageFromURL = async (url: string) => { + try { + if (!url) { + return undefined; + } + const response = await RNFetchBlob.config({ + fileCache: true, + appendExt: 'jpg', + }).fetch('GET', url); + const status = response.info().status; + if (status === 200) { + return response.path(); + } else { + return undefined; + } + } catch (error) { + console.log(error); + return undefined; + } +}; diff --git a/src/services/ExploreService.ts b/src/services/ExploreService.ts new file mode 100644 index 00000000..980258be --- /dev/null +++ b/src/services/ExploreService.ts @@ -0,0 +1,59 @@ +import AsyncStorage from '@react-native-community/async-storage'; +import {ALL_USERS_ENDPOINT, DISCOVER_ENDPOINT} from '../constants'; +import {EMPTY_EXPLORE_SECTIONS} from '../store/initialStates'; +import {ExploreSectionType, ProfilePreviewType} from '../types'; + +export const getAllTaggUsers = async (token: string) => { + try { + const response = await fetch(ALL_USERS_ENDPOINT, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const response_data = await response.json(); + return response_data; + } else { + console.log( + 'Something went wrong! 😭', + 'Not able to retrieve tagg users list', + ); + } + } catch (error) { + console.log( + 'Something went wrong! 😭', + 'Not able to retrieve tagg users list', + error, + ); + } +}; + +export const getAllExploreSections = async () => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(DISCOVER_ENDPOINT, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status !== 200) { + return EMPTY_EXPLORE_SECTIONS; + } + const data = await response.json(); + const exploreSections: Record = { + 'New to Tagg': data.categories.new_to_tagg, + 'People You May Know': data.categories.people_you_may_know, + 'Trending on Tagg': data.categories.trending_on_tagg, + "Brown '21": data.categories.brown_21, + "Brown '22": data.categories.brown_22, + "Brown '23": data.categories.brown_23, + }; + + return exploreSections; + } catch (error) { + console.log('Unable to fetch explore data'); + } +}; diff --git a/src/services/ExploreServices.ts b/src/services/ExploreServices.ts deleted file mode 100644 index ca4f1b69..00000000 --- a/src/services/ExploreServices.ts +++ /dev/null @@ -1,60 +0,0 @@ -import AsyncStorage from '@react-native-community/async-storage'; -import {getDeviceToken} from 'react-native-device-info'; -import {ALL_USERS_ENDPOINT, DISCOVER_ENDPOINT} from '../constants'; -import {EMPTY_EXPLORE_SECTIONS} from '../store/initialStates'; -import {ExploreSectionType, ProfilePreviewType} from '../types'; - -export const getAllTaggUsers = async (token: string) => { - try { - const response = await fetch(ALL_USERS_ENDPOINT, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - const response_data = await response.json(); - return response_data; - } else { - console.log( - 'Something went wrong! 😭', - 'Not able to retrieve tagg users list', - ); - } - } catch (error) { - console.log( - 'Something went wrong! 😭', - 'Not able to retrieve tagg users list', - error, - ); - } -}; - -export const getAllExploreSections = async () => { - try { - const token = await AsyncStorage.getItem('token'); - const response = await fetch(DISCOVER_ENDPOINT, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - if (response.status !== 200) { - return EMPTY_EXPLORE_SECTIONS; - } - const data = await response.json(); - const exploreSections: Record = { - 'New to Tagg': data.categories.new_to_tagg, - 'People You May Know': data.categories.people_you_may_know, - 'Trending on Tagg': data.categories.trending_on_tagg, - "Brown '21": data.categories.brown_21, - "Brown '22": data.categories.brown_22, - "Brown '23": data.categories.brown_23, - }; - - return exploreSections; - } catch (error) { - console.log('Unable to fetch explore data'); - } -}; diff --git a/src/services/MomentService.ts b/src/services/MomentService.ts new file mode 100644 index 00000000..7bad6d4c --- /dev/null +++ b/src/services/MomentService.ts @@ -0,0 +1,209 @@ +import AsyncStorage from '@react-native-community/async-storage'; +import {Alert} from 'react-native'; +import RNFetchBlob from 'rn-fetch-blob'; +import { + COMMENTS_ENDPOINT, + MOMENTS_ENDPOINT, + MOMENT_THUMBNAIL_ENDPOINT, +} from '../constants'; +import {ERROR_FAILED_TO_COMMENT} from '../constants/strings'; +import {MomentType} from '../types'; +import {checkImageUploadStatus} from '../utils'; + +//Get all comments for a moment +export const getMomentComments = async ( + momentId: string, + callback: Function, +) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(COMMENTS_ENDPOINT + '?moment_id=' + momentId, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const comments = await response.json(); + callback(comments); + } else { + console.log('Could not load comments'); + } + } catch (error) { + console.log('Could not load comments', error); + } +}; + +export const postMomentComment = async ( + commenter: string, + comment: string, + momentId: string, +) => { + try { + const token = await AsyncStorage.getItem('token'); + const request = new FormData(); + request.append('moment_id', momentId); + request.append('commenter', commenter); + request.append('comment', comment); + const response = await fetch(COMMENTS_ENDPOINT, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: request, + }); + if (response.status !== 200) { + throw 'server error'; + } + return await response.json(); + } catch (error) { + Alert.alert(ERROR_FAILED_TO_COMMENT); + return undefined; + } +}; + +//Get count of comments for a moment +export const getMomentCommentsCount = async ( + momentId: string, + callback: Function, +) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(COMMENTS_ENDPOINT + `${momentId}/`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const response_data = await response.json(); + callback(response_data.count); + } else { + console.log( + 'Something went wrong! 😭', + 'Not able to retrieve comments count', + ); + } + } catch (error) { + console.log( + 'Something went wrong! 😭', + 'Not able to retrieve comments count', + error, + ); + } +}; + +export const postMoment: ( + fileName: string, + uri: string, + caption: string, + category: string, + userId: string, +) => Promise = async ( + fileName, + uri, + caption, + category, + userId, +) => { + try { + const request = new FormData(); + //Manipulating filename to end with .jpg instead of .heic + if (fileName.endsWith('.heic') || fileName.endsWith('.HEIC')) { + fileName = fileName.split('.')[0] + '.jpg'; + } + request.append('image', { + uri: uri, + name: fileName, + type: 'image/jpg', + }); + request.append('moment', category); + request.append('user_id', userId); + request.append('captions', JSON.stringify({image: caption})); + const token = await AsyncStorage.getItem('token'); + let response = await fetch(MOMENTS_ENDPOINT, { + method: 'POST', + headers: { + 'Content-Type': 'multipart/form-data', + Authorization: 'Token ' + token, + }, + body: request, + }); + let statusCode = response.status; + let data = await response.json(); + if (statusCode === 200 && checkImageUploadStatus(data.moments)) { + return data.profile_completion_stage; + } + } catch (err) { + console.log(err); + } + return undefined; +}; + +export const loadMoments: ( + userId: string, + token: string, +) => Promise = async (userId, token) => { + let moments: MomentType[] = []; + try { + const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (status === 200) { + const data = await response.json(); + moments = data; + } else { + console.log('Could not load moments!'); + return []; + } + } catch (err) { + console.log(err); + return []; + } + return moments; +}; + +export const deleteMoment = async (momentId: string) => { + try { + const token = await AsyncStorage.getItem('token'); + + const response = await fetch(MOMENTS_ENDPOINT + `${momentId}/`, { + method: 'DELETE', + headers: { + Authorization: 'Token ' + token, + }, + }); + return response.status === 200; + } catch (error) { + console.log(error); + console.log('Unable to delete moment', momentId); + return false; + } +}; + +export const loadMomentThumbnail = async (momentId: string) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await RNFetchBlob.config({ + fileCache: true, + appendExt: 'jpg', + }).fetch('GET', MOMENT_THUMBNAIL_ENDPOINT + `${momentId}/`, { + Authorization: 'Token ' + token, + }); + const status = response.info().status; + if (status === 200) { + return response.path(); + } else { + return undefined; + } + } catch (error) { + console.log(error); + return undefined; + } +}; diff --git a/src/services/MomentServices.ts b/src/services/MomentServices.ts deleted file mode 100644 index 7bad6d4c..00000000 --- a/src/services/MomentServices.ts +++ /dev/null @@ -1,209 +0,0 @@ -import AsyncStorage from '@react-native-community/async-storage'; -import {Alert} from 'react-native'; -import RNFetchBlob from 'rn-fetch-blob'; -import { - COMMENTS_ENDPOINT, - MOMENTS_ENDPOINT, - MOMENT_THUMBNAIL_ENDPOINT, -} from '../constants'; -import {ERROR_FAILED_TO_COMMENT} from '../constants/strings'; -import {MomentType} from '../types'; -import {checkImageUploadStatus} from '../utils'; - -//Get all comments for a moment -export const getMomentComments = async ( - momentId: string, - callback: Function, -) => { - try { - const token = await AsyncStorage.getItem('token'); - const response = await fetch(COMMENTS_ENDPOINT + '?moment_id=' + momentId, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - const comments = await response.json(); - callback(comments); - } else { - console.log('Could not load comments'); - } - } catch (error) { - console.log('Could not load comments', error); - } -}; - -export const postMomentComment = async ( - commenter: string, - comment: string, - momentId: string, -) => { - try { - const token = await AsyncStorage.getItem('token'); - const request = new FormData(); - request.append('moment_id', momentId); - request.append('commenter', commenter); - request.append('comment', comment); - const response = await fetch(COMMENTS_ENDPOINT, { - method: 'POST', - headers: { - Authorization: 'Token ' + token, - }, - body: request, - }); - if (response.status !== 200) { - throw 'server error'; - } - return await response.json(); - } catch (error) { - Alert.alert(ERROR_FAILED_TO_COMMENT); - return undefined; - } -}; - -//Get count of comments for a moment -export const getMomentCommentsCount = async ( - momentId: string, - callback: Function, -) => { - try { - const token = await AsyncStorage.getItem('token'); - const response = await fetch(COMMENTS_ENDPOINT + `${momentId}/`, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - const response_data = await response.json(); - callback(response_data.count); - } else { - console.log( - 'Something went wrong! 😭', - 'Not able to retrieve comments count', - ); - } - } catch (error) { - console.log( - 'Something went wrong! 😭', - 'Not able to retrieve comments count', - error, - ); - } -}; - -export const postMoment: ( - fileName: string, - uri: string, - caption: string, - category: string, - userId: string, -) => Promise = async ( - fileName, - uri, - caption, - category, - userId, -) => { - try { - const request = new FormData(); - //Manipulating filename to end with .jpg instead of .heic - if (fileName.endsWith('.heic') || fileName.endsWith('.HEIC')) { - fileName = fileName.split('.')[0] + '.jpg'; - } - request.append('image', { - uri: uri, - name: fileName, - type: 'image/jpg', - }); - request.append('moment', category); - request.append('user_id', userId); - request.append('captions', JSON.stringify({image: caption})); - const token = await AsyncStorage.getItem('token'); - let response = await fetch(MOMENTS_ENDPOINT, { - method: 'POST', - headers: { - 'Content-Type': 'multipart/form-data', - Authorization: 'Token ' + token, - }, - body: request, - }); - let statusCode = response.status; - let data = await response.json(); - if (statusCode === 200 && checkImageUploadStatus(data.moments)) { - return data.profile_completion_stage; - } - } catch (err) { - console.log(err); - } - return undefined; -}; - -export const loadMoments: ( - userId: string, - token: string, -) => Promise = async (userId, token) => { - let moments: MomentType[] = []; - try { - const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (status === 200) { - const data = await response.json(); - moments = data; - } else { - console.log('Could not load moments!'); - return []; - } - } catch (err) { - console.log(err); - return []; - } - return moments; -}; - -export const deleteMoment = async (momentId: string) => { - try { - const token = await AsyncStorage.getItem('token'); - - const response = await fetch(MOMENTS_ENDPOINT + `${momentId}/`, { - method: 'DELETE', - headers: { - Authorization: 'Token ' + token, - }, - }); - return response.status === 200; - } catch (error) { - console.log(error); - console.log('Unable to delete moment', momentId); - return false; - } -}; - -export const loadMomentThumbnail = async (momentId: string) => { - try { - const token = await AsyncStorage.getItem('token'); - const response = await RNFetchBlob.config({ - fileCache: true, - appendExt: 'jpg', - }).fetch('GET', MOMENT_THUMBNAIL_ENDPOINT + `${momentId}/`, { - Authorization: 'Token ' + token, - }); - const status = response.info().status; - if (status === 200) { - return response.path(); - } else { - return undefined; - } - } catch (error) { - console.log(error); - return undefined; - } -}; diff --git a/src/services/UserFriendsService.ts b/src/services/UserFriendsService.ts new file mode 100644 index 00000000..f2e15824 --- /dev/null +++ b/src/services/UserFriendsService.ts @@ -0,0 +1,149 @@ +//Abstracted common friends api calls out here + +import {Alert} from 'react-native'; +import {FriendshipStatusType} from 'src/types'; +import {FRIENDS_ENDPOINT} from '../constants'; +import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings'; + +export const loadFriends = async (userId: string, token: string) => { + try { + const response = await fetch(FRIENDS_ENDPOINT + `?user_id=${userId}`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status === 200) { + const body = await response.json(); + return body; + } else { + throw new Error(await response.json()); + } + } catch (error) { + console.log(error); + } +}; + +export const friendOrUnfriendUser = async ( + user: string, + friend: string, + token: string, + friendship_status: FriendshipStatusType, +) => { + try { + let body; + let method = ''; + let endpoint = FRIENDS_ENDPOINT; + + switch (friendship_status) { + case 'no_record': + method = 'POST'; + body = JSON.stringify({ + requested: friend, + }); + break; + case 'requested': + method = 'DELETE'; + endpoint += `${friend}/`; + body = JSON.stringify({ + reason: 'cancelled', + }); + break; + case 'friends': + method = 'DELETE'; + endpoint += `${friend}/`; + body = JSON.stringify({ + reason: 'unfriended', + }); + } + + const response = await fetch(endpoint, { + method: method, + headers: { + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }, + body: body, + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + return true; + } else { + console.log(await response.json()); + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } + } catch (error) { + console.log(error); + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } +}; + +export const declineFriendRequestService = async ( + user_id: string, + token: string | null, +) => { + try { + const response = await fetch(FRIENDS_ENDPOINT + `${user_id}/`, { + method: 'DELETE', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + reason: 'declined', + }), + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + return true; + } else { + console.log(await response.json()); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } + } catch (error) { + console.log(error); + Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); + return false; + } +}; + +export const acceptFriendRequestService = async ( + requester_id: string, + token: string | null, +) => { + try { + const response = await fetch(FRIENDS_ENDPOINT + `${requester_id}/`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }, + }); + const status = response.status; + if (Math.floor(status / 100) === 2) { + return true; + } else { + console.log(await response.json()); + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } + } catch (error) { + console.log(error); + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } +}; diff --git a/src/services/UserFriendsServices.ts b/src/services/UserFriendsServices.ts deleted file mode 100644 index f2e15824..00000000 --- a/src/services/UserFriendsServices.ts +++ /dev/null @@ -1,149 +0,0 @@ -//Abstracted common friends api calls out here - -import {Alert} from 'react-native'; -import {FriendshipStatusType} from 'src/types'; -import {FRIENDS_ENDPOINT} from '../constants'; -import {ERROR_SOMETHING_WENT_WRONG_REFRESH} from '../constants/strings'; - -export const loadFriends = async (userId: string, token: string) => { - try { - const response = await fetch(FRIENDS_ENDPOINT + `?user_id=${userId}`, { - method: 'GET', - headers: { - Authorization: 'Token ' + token, - }, - }); - if (response.status === 200) { - const body = await response.json(); - return body; - } else { - throw new Error(await response.json()); - } - } catch (error) { - console.log(error); - } -}; - -export const friendOrUnfriendUser = async ( - user: string, - friend: string, - token: string, - friendship_status: FriendshipStatusType, -) => { - try { - let body; - let method = ''; - let endpoint = FRIENDS_ENDPOINT; - - switch (friendship_status) { - case 'no_record': - method = 'POST'; - body = JSON.stringify({ - requested: friend, - }); - break; - case 'requested': - method = 'DELETE'; - endpoint += `${friend}/`; - body = JSON.stringify({ - reason: 'cancelled', - }); - break; - case 'friends': - method = 'DELETE'; - endpoint += `${friend}/`; - body = JSON.stringify({ - reason: 'unfriended', - }); - } - - const response = await fetch(endpoint, { - method: method, - headers: { - 'Content-Type': 'application/json', - Authorization: 'Token ' + token, - }, - body: body, - }); - const status = response.status; - if (Math.floor(status / 100) === 2) { - return true; - } else { - console.log(await response.json()); - Alert.alert( - 'Something went wrong! 😭', - "Would you believe me if I told you that I don't know what happened?", - ); - return false; - } - } catch (error) { - console.log(error); - Alert.alert( - 'Something went wrong! 😭', - "Would you believe me if I told you that I don't know what happened?", - ); - return false; - } -}; - -export const declineFriendRequestService = async ( - user_id: string, - token: string | null, -) => { - try { - const response = await fetch(FRIENDS_ENDPOINT + `${user_id}/`, { - method: 'DELETE', - headers: { - Authorization: 'Token ' + token, - }, - body: JSON.stringify({ - reason: 'declined', - }), - }); - const status = response.status; - if (Math.floor(status / 100) === 2) { - return true; - } else { - console.log(await response.json()); - Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); - return false; - } - } catch (error) { - console.log(error); - Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH); - return false; - } -}; - -export const acceptFriendRequestService = async ( - requester_id: string, - token: string | null, -) => { - try { - const response = await fetch(FRIENDS_ENDPOINT + `${requester_id}/`, { - method: 'PATCH', - headers: { - 'Content-Type': 'application/json', - Authorization: 'Token ' + token, - }, - }); - const status = response.status; - if (Math.floor(status / 100) === 2) { - return true; - } else { - console.log(await response.json()); - Alert.alert( - 'Something went wrong! 😭', - "Would you believe me if I told you that I don't know what happened?", - ); - return false; - } - } catch (error) { - console.log(error); - Alert.alert( - 'Something went wrong! 😭', - "Would you believe me if I told you that I don't know what happened?", - ); - return false; - } -}; diff --git a/src/services/index.ts b/src/services/index.ts index 56cefddd..9289cfb1 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,11 +1,12 @@ export * from './UserProfileService'; export * from './SocialLinkingService'; -export * from './MomentServices'; -export * from './ExploreServices'; -export * from './UserFriendsServices'; +export * from './MomentService'; +export * from './ExploreService'; +export * from './UserFriendsService'; export * from './ReportingService'; export * from './BlockUserService'; export * from './MomentCategoryService'; export * from './NotificationService'; export * from './FCMService'; export * from './WaitlistUserService'; +export * from './CommonService'; -- cgit v1.2.3-70-g09d2 From 02722456c7314d8d39ac314c2360f6f4f20e327f Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 26 Jan 2021 18:09:42 -0500 Subject: added thumbnail types --- src/types/types.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/types/types.ts b/src/types/types.ts index d9d0b56b..9b9fb627 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -11,6 +11,7 @@ export interface ProfilePreviewType { username: string; first_name: string; last_name: string; + thumbnail_url: string; } export type FriendshipStatusType = 'friends' | 'requested' | 'no_record'; @@ -84,6 +85,7 @@ export interface MomentType { date_created: string; moment_category: string; path_hash: string; + thumbnail_url: string; } export interface CommentType { -- cgit v1.2.3-70-g09d2 From aaf670d8cd5a628f63068b3f12a262cf9576a615 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 26 Jan 2021 18:09:58 -0500 Subject: cleaned up dead code --- src/screens/search/SearchScreen.tsx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'src') diff --git a/src/screens/search/SearchScreen.tsx b/src/screens/search/SearchScreen.tsx index 059bd968..f0be7c9e 100644 --- a/src/screens/search/SearchScreen.tsx +++ b/src/screens/search/SearchScreen.tsx @@ -2,7 +2,6 @@ import AsyncStorage from '@react-native-community/async-storage'; import {useFocusEffect} from '@react-navigation/native'; import React, {useCallback, useEffect, useState} from 'react'; import { - Dimensions, Keyboard, RefreshControl, ScrollView, @@ -37,9 +36,7 @@ const NO_USER: UserType = { */ const SearchScreen: React.FC = () => { - const {recentSearches, explores} = useSelector( - (state: RootState) => state.taggUsers, - ); + const {recentSearches} = useSelector((state: RootState) => state.taggUsers); const [query, setQuery] = useState(''); const [results, setResults] = useState>([]); const [recents, setRecents] = useState>( @@ -47,7 +44,6 @@ const SearchScreen: React.FC = () => { ); const [searching, setSearching] = useState(false); const top = Animated.useValue(-SCREEN_HEIGHT); - const [user, setUser] = useState(NO_USER); const [refreshing, setRefreshing] = useState(false); const dispatch = useDispatch(); @@ -70,10 +66,6 @@ const SearchScreen: React.FC = () => { const loadResults = async (q: string) => { try { const token = await AsyncStorage.getItem('token'); - if (!token) { - setUser(NO_USER); - return; - } const response = await fetch(`${SEARCH_ENDPOINT}?query=${q}`, { method: 'GET', headers: { -- cgit v1.2.3-70-g09d2 From f7ed1dae99761eb4647d3b6cb68f009fd467ad8a Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 26 Jan 2021 18:10:12 -0500 Subject: thumbnail for explore --- src/components/search/ExploreSection.tsx | 15 +++++++++------ src/components/search/ExploreSectionUser.tsx | 18 ++++++------------ 2 files changed, 15 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/components/search/ExploreSection.tsx b/src/components/search/ExploreSection.tsx index 17079e77..025c8c3c 100644 --- a/src/components/search/ExploreSection.tsx +++ b/src/components/search/ExploreSection.tsx @@ -1,5 +1,5 @@ import React, {Fragment} from 'react'; -import {ScrollView, StyleSheet, Text, View} from 'react-native'; +import {FlatList, StyleSheet, Text, View} from 'react-native'; import {ProfilePreviewType} from '../../types'; import {normalize} from '../../utils'; import ExploreSectionUser from './ExploreSectionUser'; @@ -17,12 +17,15 @@ const ExploreSection: React.FC = ({title, users}) => { return users.length !== 0 ? ( {title} - - - {users.map((user) => ( + } + renderItem={({item: user}: {item: ProfilePreviewType}) => ( - ))} - + )} + showsHorizontalScrollIndicator={false} + horizontal + /> ) : ( diff --git a/src/components/search/ExploreSectionUser.tsx b/src/components/search/ExploreSectionUser.tsx index 68e077e3..b0cfe5c6 100644 --- a/src/components/search/ExploreSectionUser.tsx +++ b/src/components/search/ExploreSectionUser.tsx @@ -9,7 +9,7 @@ import { } from 'react-native'; import LinearGradient from 'react-native-linear-gradient'; import {useDispatch, useSelector, useStore} from 'react-redux'; -import {loadAvatar} from '../../services'; +import {loadImageFromURL} from '../../services'; import {RootState} from '../../store/rootReducer'; import {ProfilePreviewType, ScreenType} from '../../types'; import {fetchUserX, normalize, userXInStore} from '../../utils'; @@ -36,18 +36,13 @@ const ExploreSectionUser: React.FC = ({ const dispatch = useDispatch(); useEffect(() => { - let mounted = true; - const loadAvatarImage = async () => { - const response = await loadAvatar(id, true); - if (mounted) { + (async () => { + const response = await loadImageFromURL(user.thumbnail_url); + if (response) { setAvatar(response); } - }; - loadAvatarImage(); - return () => { - mounted = false; - }; - }, [user]); + })(); + }, []); const handlePress = async () => { if (!userXInStore(state, screenType, user.id)) { @@ -63,7 +58,6 @@ const ExploreSectionUser: React.FC = ({ screenType, }); }; - return ( Date: Tue, 26 Jan 2021 18:10:23 -0500 Subject: thumbnail for profile preview and moments --- src/components/moments/MomentTile.tsx | 3 +-- src/components/profile/ProfilePreview.tsx | 43 +++++++++++-------------------- 2 files changed, 16 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/components/moments/MomentTile.tsx b/src/components/moments/MomentTile.tsx index 16e91c82..69701192 100644 --- a/src/components/moments/MomentTile.tsx +++ b/src/components/moments/MomentTile.tsx @@ -15,7 +15,6 @@ const MomentTile: React.FC = ({ }) => { const navigation = useNavigation(); - const {path_hash} = moment; return ( { @@ -26,7 +25,7 @@ const MomentTile: React.FC = ({ }); }}> - + ); diff --git a/src/components/profile/ProfilePreview.tsx b/src/components/profile/ProfilePreview.tsx index 389ca367..000dd5c5 100644 --- a/src/components/profile/ProfilePreview.tsx +++ b/src/components/profile/ProfilePreview.tsx @@ -12,21 +12,11 @@ import { } from 'react-native'; import {useDispatch, useSelector, useStore} from 'react-redux'; import {ERROR_UNABLE_TO_VIEW_PROFILE} from '../../constants/strings'; -import {loadAvatar} from '../../services'; +import {loadImageFromURL} from '../../services'; import {RootState} from '../../store/rootreducer'; -import { - PreviewType, - ProfilePreviewType, - ScreenType, - UserType, -} from '../../types'; +import {PreviewType, ProfilePreviewType, ScreenType} from '../../types'; import {checkIfUserIsBlocked, fetchUserX, userXInStore} from '../../utils'; -const NO_USER: UserType = { - userId: '', - username: '', -}; - /** * This component returns user's profile picture friended by username as a touchable component. * What happens when someone clicks on this component is partly decided by the prop isComment. @@ -43,28 +33,24 @@ interface ProfilePreviewProps extends ViewProps { } const ProfilePreview: React.FC = ({ - profilePreview: {username, first_name, last_name, id}, + profilePreview: {username, first_name, last_name, id, thumbnail_url}, previewType, screenType, }) => { const navigation = useNavigation(); const {user: loggedInUser} = useSelector((state: RootState) => state.user); - const [avatarURI, setAvatarURI] = useState(null); - const [user, setUser] = useState(NO_USER); + const [avatar, setAvatar] = useState(null); const dispatch = useDispatch(); + useEffect(() => { - let mounted = true; - const loadAvatarImage = async () => { - const response = await loadAvatar(id, true); - if (mounted) { - setAvatarURI(response); + (async () => { + console.log(thumbnail_url); + const response = await loadImageFromURL(thumbnail_url); + if (response) { + setAvatar(response); } - }; - loadAvatarImage(); - return () => { - mounted = false; - }; - }, [id]); + })(); + }, []); /** * Adds a searched user to the recently searched cache if they're tapped on. @@ -80,6 +66,7 @@ const ProfilePreview: React.FC = ({ username, first_name, last_name, + thumbnail_url, }; try { @@ -211,8 +198,8 @@ const ProfilePreview: React.FC = ({ -- cgit v1.2.3-70-g09d2 From d70ade2e653ee93aca09d62ab7822b8d0f6dc2b5 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 26 Jan 2021 18:23:44 -0500 Subject: fixed no internet app crash --- src/components/profile/FriendsCount.tsx | 6 +++--- src/components/search/Explore.tsx | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/components/profile/FriendsCount.tsx b/src/components/profile/FriendsCount.tsx index 9647710e..851dbc3b 100644 --- a/src/components/profile/FriendsCount.tsx +++ b/src/components/profile/FriendsCount.tsx @@ -17,10 +17,10 @@ const FriendsCount: React.FC = ({ userXId, screenType, }) => { - const count = (userXId + const {friends} = userXId ? useSelector((state: RootState) => state.userX[screenType][userXId]) - : useSelector((state: RootState) => state.friends) - )?.friends.length; + : useSelector((state: RootState) => state.friends); + const count = friends ? friends.length : 0; const displayedCount: string = count < 5e3 diff --git a/src/components/search/Explore.tsx b/src/components/search/Explore.tsx index 4a71249b..2a3bc749 100644 --- a/src/components/search/Explore.tsx +++ b/src/components/search/Explore.tsx @@ -12,9 +12,10 @@ const Explore: React.FC = () => { return ( Search Profiles - {EXPLORE_SECTION_TITLES.map((title: ExploreSectionType) => ( - - ))} + {explores && + EXPLORE_SECTION_TITLES.map((title: ExploreSectionType) => ( + + ))} ); }; -- cgit v1.2.3-70-g09d2 From e897fdbbbe8442f05000645395753ff008a19bf4 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 26 Jan 2021 18:35:41 -0500 Subject: use thumbnails for notification screen --- src/components/notifications/Notification.tsx | 46 +++++++++++---------------- src/components/profile/ProfilePreview.tsx | 1 - 2 files changed, 18 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/components/notifications/Notification.tsx b/src/components/notifications/Notification.tsx index e6d16f82..e648b554 100644 --- a/src/components/notifications/Notification.tsx +++ b/src/components/notifications/Notification.tsx @@ -1,25 +1,19 @@ import {useNavigation} from '@react-navigation/native'; import React, {useEffect, useState} from 'react'; import {Image, StyleSheet, Text, View} from 'react-native'; -import {Button} from 'react-native-elements'; import {TouchableWithoutFeedback} from 'react-native-gesture-handler'; import {useDispatch, useStore} from 'react-redux'; +import {loadImageFromURL, loadMomentThumbnail} from '../../services'; import { + acceptFriendRequest, declineFriendRequest, loadUserNotifications, updateUserXFriends, } from '../../store/actions'; -import {acceptFriendRequest} from '../../store/actions'; -import {NotificationType, ProfilePreviewType, ScreenType, MomentType} from '../../types'; -import { - fetchUserX, - SCREEN_HEIGHT, - SCREEN_WIDTH, - userXInStore, -} from '../../utils'; +import {RootState} from '../../store/rootReducer'; +import {MomentType, NotificationType, ScreenType} from '../../types'; +import {fetchUserX, SCREEN_HEIGHT, userXInStore} from '../../utils'; import AcceptDeclineButtons from '../common/AcceptDeclineButtons'; -import {loadAvatar, loadMomentThumbnail} from '../../services'; - interface NotificationProps { item: NotificationType; @@ -30,7 +24,7 @@ interface NotificationProps { const Notification: React.FC = (props) => { const { item: { - actor: {id, username, first_name, last_name}, + actor: {id, username, first_name, last_name, thumbnail_url}, verbage, notification_type, notification_object, @@ -44,22 +38,18 @@ const Notification: React.FC = (props) => { const state: RootState = useStore().getState(); const dispatch = useDispatch(); - const [avatarURI, setAvatarURI] = useState(undefined); + const [avatar, setAvatar] = useState(undefined); const [momentURI, setMomentURI] = useState(undefined); const backgroundColor = unread ? '#DCF1F1' : 'rgba(0,0,0,0)'; + useEffect(() => { - let mounted = true; - const loadAvatarImage = async (user_id: string) => { - const response = await loadAvatar(user_id, true); - if (mounted) { - setAvatarURI(response); + (async () => { + const response = await loadImageFromURL(thumbnail_url); + if (response) { + setAvatar(response); } - }; - loadAvatarImage(id); - return () => { - mounted = false; - }; - }, [id]); + })(); + }, []); useEffect(() => { let mounted = true; @@ -137,8 +127,8 @@ const Notification: React.FC = (props) => { @@ -159,8 +149,8 @@ const Notification: React.FC = (props) => { )} {notification_type === 'CMT' && notification_object && ( - - )} + + )} ); diff --git a/src/components/profile/ProfilePreview.tsx b/src/components/profile/ProfilePreview.tsx index 000dd5c5..38defb8d 100644 --- a/src/components/profile/ProfilePreview.tsx +++ b/src/components/profile/ProfilePreview.tsx @@ -44,7 +44,6 @@ const ProfilePreview: React.FC = ({ useEffect(() => { (async () => { - console.log(thumbnail_url); const response = await loadImageFromURL(thumbnail_url); if (response) { setAvatar(response); -- cgit v1.2.3-70-g09d2 From 00ddb6fdec43635261ffa950731d9dd1ce70a815 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Wed, 27 Jan 2021 18:33:49 -0500 Subject: small backend change --- src/screens/profile/IndividualMoment.tsx | 2 +- src/types/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index 6b82b31c..ea0c8fb6 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -70,7 +70,7 @@ const IndividualMoment: React.FC = ({ style={styles.postContent} momentId={item.moment_id} caption={item.caption} - pathHash={item.path_hash} + pathHash={item.moment_url} dateTime={item.date_created} screenType={screenType} /> diff --git a/src/types/types.ts b/src/types/types.ts index 1a4595f6..1d800423 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -84,7 +84,7 @@ export interface MomentType { caption: string; date_created: string; moment_category: string; - path_hash: string; + moment_url: string; thumbnail_url: string; } -- cgit v1.2.3-70-g09d2 From 614fbcd8b0180b328019e6952ee1fc0de4643eaa Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Wed, 27 Jan 2021 18:43:57 -0500 Subject: fixed thumbnail for comments --- src/components/comments/CommentTile.tsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src') diff --git a/src/components/comments/CommentTile.tsx b/src/components/comments/CommentTile.tsx index 39605f2c..1247e89a 100644 --- a/src/components/comments/CommentTile.tsx +++ b/src/components/comments/CommentTile.tsx @@ -67,10 +67,7 @@ const CommentTile: React.FC = ({ ]}> Date: Wed, 27 Jan 2021 18:44:40 -0500 Subject: even better code --- src/components/comments/CommentTile.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src') diff --git a/src/components/comments/CommentTile.tsx b/src/components/comments/CommentTile.tsx index 1247e89a..237c65fc 100644 --- a/src/components/comments/CommentTile.tsx +++ b/src/components/comments/CommentTile.tsx @@ -66,9 +66,7 @@ const CommentTile: React.FC = ({ typeOfComment === 'Thread' ? styles.moreMarginWithThread : {}, ]}> -- cgit v1.2.3-70-g09d2