From fcb624c1a9b6024d03b57aa29522837d9e15f58d Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Fri, 14 Oct 2022 11:53:30 +0500 Subject: [PATCH] migrated all instance of userActivity call to react-query --- .../container/postDropdownContainer.tsx | 45 ++++++++++++++----- .../postView/view/postDisplayView.tsx | 10 +++-- .../quickReplyModalContent.tsx | 14 ++++-- src/providers/ecency/ecency.types.ts | 7 ++- src/providers/hive/dhive.js | 6 +-- src/providers/queries/pointQueries.ts | 8 +++- .../editor/container/editorContainer.tsx | 42 ++++++++++++----- .../login/container/loginContainer.tsx | 22 ++++++--- 8 files changed, 112 insertions(+), 42 deletions(-) diff --git a/src/components/postDropdown/container/postDropdownContainer.tsx b/src/components/postDropdown/container/postDropdownContainer.tsx index 798d342b8..c2f854558 100644 --- a/src/components/postDropdown/container/postDropdownContainer.tsx +++ b/src/components/postDropdown/container/postDropdownContainer.tsx @@ -23,6 +23,9 @@ import PostDropdownView from '../view/postDropdownView'; import { OptionsModal } from '../../atoms'; import { updateCurrentAccount } from '../../../redux/actions/accountAction'; import showLoginAlert from '../../../utils/showLoginAlert'; +import { useUserActivityMutation } from '../../../providers/queries/pointQueries'; +import { generateRndStr } from '../../../utils/editor'; +import { EPointActivityIds } from '../../../providers/ecency/ecency.types'; /* * Props Name Description Value @@ -79,11 +82,11 @@ class PostDropdownContainer extends PureComponent { const _canUpdateCommunityPin = subscribedCommunities.data && !!content && content.community ? subscribedCommunities.data.reduce((role, subscription) => { - if (content.community === subscription[0]) { - return ['owner', 'admin', 'mod'].includes(subscription[2]); - } - return role; - }, false) + if (content.community === subscription[0]) { + return ['owner', 'admin', 'mod'].includes(subscription[2]); + } + return role; + }, false) : false; const _isPinnedInCommunity = !!content && content.stats?.is_pinned; @@ -287,7 +290,7 @@ class PostDropdownContainer extends PureComponent { buttons: [ { text: intl.formatMessage({ id: 'alert.cancel' }), - onPress: () => {}, + onPress: () => { }, }, { text: intl.formatMessage({ id: 'alert.confirm' }), @@ -326,7 +329,16 @@ class PostDropdownContainer extends PureComponent { }; _reblog = () => { - const { content, currentAccount, dispatch, intl, isLoggedIn, pinCode, navigation } = this + const { + content, + currentAccount, + dispatch, + intl, + isLoggedIn, + pinCode, + navigation, + userActivityMutation + } = this .props as any; if (!isLoggedIn) { showLoginAlert({ navigation, intl }); @@ -334,8 +346,13 @@ class PostDropdownContainer extends PureComponent { } if (isLoggedIn) { reblog(currentAccount, pinCode, content.author, get(content, 'permlink', '')) - .then(() => { - //TODO: track user activity points ty=130 + .then((response) => { + //track user activity points ty=130 + userActivityMutation.mutate({ + pointsTy:EPointActivityIds.REBLOG, + transactionId:response.id + }) + dispatch( toastNotification( intl.formatMessage({ @@ -367,7 +384,7 @@ class PostDropdownContainer extends PureComponent { }; _updatePinnedPost = async ({ unpinPost }: { unpinPost: boolean } = { unpinPost: false }) => { - const { content, currentAccount, pinCode, dispatch, intl, isLoggedIn } = this.props; + const { content, currentAccount, pinCode, dispatch, intl } = this.props; const params = { ...currentAccount.about.profile, @@ -499,4 +516,10 @@ const mapStateToProps = (state) => ({ subscribedCommunities: state.communities.subscribedCommunities, }); -export default withNavigation(connect(mapStateToProps)(injectIntl(PostDropdownContainer))); +const mapQueriesToProps = () => ({ + userActivityMutation: useUserActivityMutation() +}) + +export default withNavigation(connect(mapStateToProps)(injectIntl((props) => ( + ))) +); diff --git a/src/components/postView/view/postDisplayView.tsx b/src/components/postView/view/postDisplayView.tsx index 964aa3037..dfec42fde 100644 --- a/src/components/postView/view/postDisplayView.tsx +++ b/src/components/postView/view/postDisplayView.tsx @@ -5,7 +5,6 @@ import get from 'lodash/get'; // Providers import { useSafeAreaInsets } from 'react-native-safe-area-context'; -import { userActivity } from '../../../providers/ecency/ePoint'; // Utils import { getTimeFromNow } from '../../../utils/time'; @@ -25,6 +24,8 @@ import getWindowDimensions from '../../../utils/getWindowDimensions'; import { useAppDispatch } from '../../../hooks'; import { showReplyModal } from '../../../redux/actions/uiAction'; import postTypes from '../../../constants/postTypes'; +import { useUserActivityMutation } from '../../../providers/queries/pointQueries'; +import { EPointActivityIds } from '../../../providers/ecency/ecency.types'; const HEIGHT = getWindowDimensions().height; const WIDTH = getWindowDimensions().width; @@ -49,6 +50,7 @@ const PostDisplayView = ({ }) => { const dispatch = useAppDispatch(); const insets = useSafeAreaInsets(); + const userActivityMutation = useUserActivityMutation(); const commentsRef = useRef(); const scrollRef = useRef(); @@ -66,8 +68,10 @@ const PostDisplayView = ({ // Component Life Cycles useEffect(() => { if (isLoggedIn && get(currentAccount, 'name') && !isNewPost) { - //TODO: track user activity with react-query - userActivity(10); + //track user activity for view post + userActivityMutation.mutate({ + pointsTy:EPointActivityIds.VIEW_POST + }) } }, []); diff --git a/src/components/quickReplyModal/quickReplyModalContent.tsx b/src/components/quickReplyModal/quickReplyModalContent.tsx index bf0bed806..ff72c740c 100644 --- a/src/components/quickReplyModal/quickReplyModalContent.tsx +++ b/src/components/quickReplyModal/quickReplyModalContent.tsx @@ -5,7 +5,7 @@ import { View, Text, Alert, TouchableOpacity, Keyboard, Platform } from 'react-n import { useIntl } from 'react-intl'; import { IconButton, MainButton, TextButton, TextInput, UserAvatar } from '..'; import { useSelector, useDispatch } from 'react-redux'; -import { delay, generateReplyPermlink } from '../../utils/editor'; +import { delay, generateReplyPermlink, generateRndStr } from '../../utils/editor'; import { postComment } from '../../providers/hive/dhive'; import { toastNotification } from '../../redux/actions/uiAction'; import { @@ -14,13 +14,15 @@ import { updateDraftCache, } from '../../redux/actions/cacheActions'; import { default as ROUTES } from '../../constants/routeNames'; -import { get, debounce } from 'lodash'; +import { get } from 'lodash'; import { navigate } from '../../navigation/service'; import { postBodySummary } from '@ecency/render-helper'; import { Draft } from '../../redux/reducers/cacheReducer'; import { RootState } from '../../redux/store/store'; import { useImperativeHandle } from 'react'; import { forwardRef } from 'react'; +import { EPointActivityIds } from '../../providers/ecency/ecency.types'; +import { useUserActivityMutation } from '../../providers/queries/pointQueries'; export interface QuickReplyModalContentProps { selectedPost?: any; @@ -34,6 +36,7 @@ export const QuickReplyModalContent = forwardRef(({ }: QuickReplyModalContentProps, ref) => { const intl = useIntl(); const dispatch = useDispatch(); + const userActivityMutation = useUserActivityMutation(); const inputRef = useRef(null); @@ -148,8 +151,11 @@ export const QuickReplyModalContent = forwardRef(({ commentValue, parentTags, ) - .then(() => { - + .then((response) => { + userActivityMutation.mutate({ + pointsTy:EPointActivityIds.COMMENT, + transactionId:response.id + }) setIsSending(false); setCommentValue(''); diff --git a/src/providers/ecency/ecency.types.ts b/src/providers/ecency/ecency.types.ts index 86a97e545..cd9888f37 100644 --- a/src/providers/ecency/ecency.types.ts +++ b/src/providers/ecency/ecency.types.ts @@ -85,5 +85,10 @@ export enum NotificationFilters { } export enum EPointActivityIds { - VOTE = 120 + VIEW_POST = 10, + LOGIN = 20, + POST = 100, + COMMENT = 110, + VOTE = 120, + REBLOG = 130, } diff --git a/src/providers/hive/dhive.js b/src/providers/hive/dhive.js index 14375b328..5e8d7b440 100644 --- a/src/providers/hive/dhive.js +++ b/src/providers/hive/dhive.js @@ -1415,7 +1415,7 @@ export const postContent = ( const { id } = resp; if (!isEdit) { //TODO: remove userActivity - userActivity(t, id); + // userActivity(t, id); } return resp; }) @@ -1464,7 +1464,7 @@ export const postComment = ( const { id } = resp; if (!isEdit) { //TODO: remove userActivity - userActivity(t, id); + // userActivity(t, id); } return resp; }) @@ -1593,7 +1593,7 @@ const _postContent = async ( export const reblog = (account, pinCode, author, permlink) => _reblog(account, pinCode, author, permlink).then((resp) => { //TODO: remove userActivity - userActivity(130, resp.id); + // userActivity(130, resp.id); return resp; }); diff --git a/src/providers/queries/pointQueries.ts b/src/providers/queries/pointQueries.ts index 2739a9741..7ef572175 100644 --- a/src/providers/queries/pointQueries.ts +++ b/src/providers/queries/pointQueries.ts @@ -5,8 +5,8 @@ import { userActivity } from "../ecency/ePoint" interface UserActivityVars { - localId: string; pointsTy: EPointActivityIds; + localId?: string; //use for redux storage blockNum?: string; transactionId?: string; } @@ -25,10 +25,16 @@ export const useUserActivityMutation = () => { onSuccess: (data, vars) => { console.log("successfully logged activity", data, vars) //remove entry from redux + if(vars.localId){ + console.log("must remove from redux") + } }, onError: (error, vars) => { console.log("failed to log activity", error, vars) //add entry in redux + if(!vars.localId){ + console.log("must add to from redux") + } } }) } \ No newline at end of file diff --git a/src/screens/editor/container/editorContainer.tsx b/src/screens/editor/container/editorContainer.tsx index 482c99f99..0afa60fb6 100644 --- a/src/screens/editor/container/editorContainer.tsx +++ b/src/screens/editor/container/editorContainer.tsx @@ -8,7 +8,7 @@ import { isArray } from 'lodash'; // Services and Actions import { Buffer } from 'buffer'; -import { QueryClient, useQueryClient } from '@tanstack/react-query'; +import { useQueryClient } from '@tanstack/react-query'; import { addDraft, updateDraft, getDrafts, addSchedule } from '../../../providers/ecency/ecency'; import { toastNotification, setRcOffer } from '../../../redux/actions/uiAction'; import { @@ -46,6 +46,8 @@ import { } from '../../../redux/actions/cacheActions'; import QUERIES from '../../../providers/queries/queryKeys'; import bugsnapInstance from '../../../config/bugsnag'; +import { useUserActivityMutation } from '../../../providers/queries/pointQueries'; +import { EPointActivityIds } from '../../../providers/ecency/ecency.types'; /* * Props Name Description Value @@ -53,7 +55,8 @@ import bugsnapInstance from '../../../config/bugsnag'; * */ -class EditorContainer extends Component { + +class EditorContainer extends Component { _isMounted = false; _updatedDraftFields = null; _appState = AppState.currentState; @@ -546,6 +549,7 @@ class EditorContainer extends Component { intl, navigation, pinCode, + userActivityMutation // isDefaultFooter, } = this.props; const { rewardType, isPostSending, thumbUrl, draftId, shouldReblog } = this.state; @@ -615,13 +619,21 @@ class EditorContainer extends Component { ) .then((response) => { console.log(response); - //TODO: track user activity ty=110 for comment, ty=100 for blog + // track user activity for points + userActivityMutation.mutate({ + pointsTy:EPointActivityIds.POST, + transactionId:response.id + }) //reblog if flag is active if (shouldReblog) { reblog(currentAccount, pinCode, author, permlink) .then((resp) => { - //TODO: track user activity ty=130 + //track user activity for points on reblog + userActivityMutation.mutate({ + pointsTy:EPointActivityIds.REBLOG, + transactionId:resp.id + }) console.log('Successfully reblogged post', resp); }) .catch((err) => { @@ -667,7 +679,7 @@ class EditorContainer extends Component { }; _submitReply = async (fields) => { - const { currentAccount, pinCode, dispatch } = this.props; + const { currentAccount, pinCode, dispatch, userActivityMutation } = this.props; const { isPostSending } = this.state; if (isPostSending) { @@ -695,8 +707,13 @@ class EditorContainer extends Component { fields.body, parentTags, ) - .then(() => { - //TODO: track user activity ty = 110 + .then((response) => { + //record user activity for points + userActivityMutation.mutate({ + pointsTy:EPointActivityIds.COMMENT, + transactionId:response.id + }) + AsyncStorage.setItem('temp-reply', ''); this._handleSubmitSuccess(); @@ -1132,10 +1149,11 @@ const mapStateToProps = (state) => ({ drafts: state.cache.drafts, }); +const mapQueriesToProps = () => ({ + queryClient:useQueryClient(), + userActivityMutation:useUserActivityMutation() +}) + export default connect(mapStateToProps)( - injectIntl( - //NOTE: remove extra integration step once compoent converted to functional component - //TOOD: inject add and update draft mutation hooks as well - (props) => , - ), + injectIntl((props) => ), ); diff --git a/src/screens/login/container/loginContainer.tsx b/src/screens/login/container/loginContainer.tsx index 6c4d1ae56..535f479f6 100644 --- a/src/screens/login/container/loginContainer.tsx +++ b/src/screens/login/container/loginContainer.tsx @@ -8,7 +8,7 @@ import messaging from '@react-native-firebase/messaging'; // Services and Actions import { login, loginWithSC2 } from '../../../providers/hive/auth'; import { lookupAccounts } from '../../../providers/hive/dhive'; -import { userActivity } from '../../../providers/ecency/ePoint'; + import { failedAccount, addOtherAccount, @@ -33,6 +33,8 @@ import persistAccountGenerator from '../../../utils/persistAccountGenerator'; import { fetchSubscribedCommunities } from '../../../redux/actions/communitiesAction'; import { showActionModal } from '../../../redux/actions/uiAction'; import { UserAvatar } from '../../../components'; +import { useUserActivityMutation } from '../../../providers/queries/pointQueries'; +import { EPointActivityIds } from '../../../providers/ecency/ecency.types'; /* * Props Name Description Value @@ -149,7 +151,7 @@ class LoginContainer extends PureComponent { }; _handleOnPressLogin = (username, password) => { - const { dispatch, intl, isPinCodeOpen, navigation } = this.props; + const { dispatch, intl, isPinCodeOpen, navigation, userActivityMutation } = this.props; this.setState({ isLoading: true }); @@ -165,8 +167,8 @@ class LoginContainer extends PureComponent { dispatch(setInitPosts([])); dispatch(setFeedPosts([])); - //TODO: track user activity with react query - userActivity(20); + //track user activity for login + userActivityMutation.mutate({ pointsTy:EPointActivityIds.LOGIN }) setExistUser(true); this._setPushToken(result.name); const encryptedPin = encryptKey(Config.DEFAULT_PIN, Config.PIN_KEY); @@ -191,8 +193,8 @@ class LoginContainer extends PureComponent { const errorDescription = err?.response?.data?.error_description ? err?.response?.data?.error_description : intl.formatMessage({ - id: err.message, - }); + id: err.message, + }); Alert.alert( intl.formatMessage({ id: 'login.login_failed', @@ -288,4 +290,10 @@ const mapStateToProps = (state) => ({ isPinCodeOpen: state.application.isPinCodeOpen, }); -export default injectIntl(connect(mapStateToProps)(LoginContainer)); +const mapQueriesToProps = () => ({ + userActivityMutation: useUserActivityMutation() +}) + +export default connect(mapStateToProps)(injectIntl((props) => ( + +)));