From 6c1f8967e85e699f98b66b5c6fb5c6d9534a12b7 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Tue, 12 Nov 2019 17:59:40 +0700 Subject: [PATCH] periodical fetching updates --- .../containers/CommentWrapper/index.tsx | 1 + .../containers/CommentWrapper/styles.scss | 1 + src/constants/api.ts | 2 ++ src/index.tsx | 3 ++ src/redux/auth/api.ts | 9 ++++++ src/redux/auth/reducer.ts | 4 +++ src/redux/auth/sagas.ts | 31 +++++++++++++++++-- src/redux/auth/types.ts | 4 +++ 8 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/components/containers/CommentWrapper/index.tsx b/src/components/containers/CommentWrapper/index.tsx index 8ebe6e13..1a7de34c 100644 --- a/src/components/containers/CommentWrapper/index.tsx +++ b/src/components/containers/CommentWrapper/index.tsx @@ -35,6 +35,7 @@ const CommentWrapper: FC = ({
window.postMessage({ type: 'username', username: user.username }, '*')} />
~{path(['username'], user)}
diff --git a/src/components/containers/CommentWrapper/styles.scss b/src/components/containers/CommentWrapper/styles.scss index 4a8c3e24..5fa9046e 100644 --- a/src/components/containers/CommentWrapper/styles.scss +++ b/src/components/containers/CommentWrapper/styles.scss @@ -56,6 +56,7 @@ background-size: cover; flex: 0 0 $comment_height; will-change: transform; + cursor: pointer; @include tablet { height: 32px; diff --git a/src/constants/api.ts b/src/constants/api.ts index 58722380..2b86a82c 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -9,6 +9,8 @@ export const API = { PROFILE: (username: string) => `/user/${username}/profile`, MESSAGES: (username: string) => `/user/${username}/messages`, MESSAGE_SEND: (username: string) => `/user/${username}/messages`, + GET_UPDATES: '/user/updates', + UPLOAD: (target, type) => `/upload/${target}/${type}`, }, NODE: { diff --git a/src/index.tsx b/src/index.tsx index 1e571735..3352faf8 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -19,6 +19,7 @@ render( ); /* + - backend: exclude node covers on import - profile modal - profile editing @@ -29,6 +30,8 @@ render( - password restore - signup? - text post can also has songs http://vault48.org/post5052 +- fulltext search: https://github.com/typeorm/typeorm/issues/3191 +- zoom: https://manuelstofer.github.io/pinchzoom/ - notifications ? - better node brief update diff --git a/src/redux/auth/api.ts b/src/redux/auth/api.ts index 70134863..bf6650f6 100644 --- a/src/redux/auth/api.ts +++ b/src/redux/auth/api.ts @@ -50,3 +50,12 @@ export const apiAuthSendMessage = ({ .post(API.USER.MESSAGE_SEND(username), { message }, configWithToken(access)) .then(resultMiddleware) .catch(errorMiddleware); + +export const apiAuthGetUpdates = ({ + access, + exclude_dialogs, +}): Promise> => + api + .get(API.USER.GET_UPDATES, configWithToken(access, { params: { exclude_dialogs } })) + .then(resultMiddleware) + .catch(errorMiddleware); diff --git a/src/redux/auth/reducer.ts b/src/redux/auth/reducer.ts index e83ac2b5..6759bb77 100644 --- a/src/redux/auth/reducer.ts +++ b/src/redux/auth/reducer.ts @@ -11,6 +11,10 @@ const INITIAL_STATE: IAuthState = { token: null, user: { ...EMPTY_USER }, + updates: { + messages: [], + }, + login: { error: null, is_loading: false, diff --git a/src/redux/auth/sagas.ts b/src/redux/auth/sagas.ts index e085647d..7287c796 100644 --- a/src/redux/auth/sagas.ts +++ b/src/redux/auth/sagas.ts @@ -1,5 +1,5 @@ import { call, put, takeLatest, select, delay } from 'redux-saga/effects'; -import { AUTH_USER_ACTIONS, EMPTY_USER, USER_ERRORS } from '~/redux/auth/constants'; +import { AUTH_USER_ACTIONS, EMPTY_USER, USER_ERRORS, USER_ROLES } from '~/redux/auth/constants'; import { authSetToken, userSetLoginError, @@ -17,11 +17,12 @@ import { apiAuthGetUserProfile, apiAuthGetUserMessages, apiAuthSendMessage, + apiAuthGetUpdates, } from '~/redux/auth/api'; import { modalSetShown, modalShowDialog } from '~/redux/modal/actions'; -import { selectToken, selectAuthProfile } from './selectors'; +import { selectToken, selectAuthProfile, selectAuthUser } from './selectors'; import { IResultWithStatus } from '../types'; -import { IUser } from './types'; +import { IUser, IAuthState } from './types'; import { REHYDRATE, RehydrateAction } from 'redux-persist'; import { selectModal } from '../modal/selectors'; import { IModalState } from '../modal/reducer'; @@ -189,6 +190,29 @@ function* sendMessage({ message, onSuccess }: ReturnType onSuccess(); } +function* getUpdates() { + const user = yield select(selectAuthUser); + + if (!user || !user.is_user || user.role === USER_ROLES.GUEST || !user.id) return; + + const modal: IModalState = yield select(selectModal); + const profile: IAuthState['profile'] = yield select(selectAuthProfile); + + const exclude_dialogs = + modal.is_shown && modal.dialog === DIALOGS.PROFILE && profile.user.id ? profile.user.id : null; + + const { error, data } = yield call(reqWrapper, apiAuthGetUpdates, { exclude_dialogs }); + + if (error || !data) return; +} + +function* startPollingSaga() { + while (true) { + yield call(getUpdates); + yield delay(30000); + } +} + function* authSaga() { yield takeLatest(REHYDRATE, checkUserSaga); yield takeLatest(AUTH_USER_ACTIONS.LOGOUT, logoutSaga); @@ -197,6 +221,7 @@ function* authSaga() { yield takeLatest(AUTH_USER_ACTIONS.OPEN_PROFILE, openProfile); yield takeLatest(AUTH_USER_ACTIONS.GET_MESSAGES, getMessages); yield takeLatest(AUTH_USER_ACTIONS.SEND_MESSAGE, sendMessage); + yield takeLatest(REHYDRATE, startPollingSaga); } export default authSaga; diff --git a/src/redux/auth/types.ts b/src/redux/auth/types.ts index b5445451..6999c6ef 100644 --- a/src/redux/auth/types.ts +++ b/src/redux/auth/types.ts @@ -25,6 +25,10 @@ export type IAuthState = Readonly<{ user: IUser; token: string; + updates: { + messages: IMessage[]; + }; + login: { error: string; is_loading: boolean;