index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. import { ConsoleSqlOutlined } from '@ant-design/icons'
  2. import {
  3. actionFullProfilePageUser,
  4. actionFullProfilePage,
  5. } from '../redux/saga'
  6. import { actionFeedTypeCount } from '../redux/reducers/feed/feedReducer'
  7. import { actionFeedType } from '../redux/reducers/feed/feedReducer'
  8. import { actionExploreTypeCount } from '../redux/reducers/explore/exploreReducer'
  9. import { actionClearExplorePosts } from '../redux/reducers/explore/exploreReducer'
  10. import { actionExploreType } from '../redux/reducers/explore/exploreReducer'
  11. import { actionClearFeedPosts } from '../redux/reducers/feed/feedReducer'
  12. import {actionProfilePageDataType} from '../redux/reducers/myData/myProfileReducer'
  13. import { history } from '../helpers'
  14. import{promiseWorker} from '../redux/saga'
  15. import { all, put, takeEvery, takeLatest, takeLeading, select,call } from 'redux-saga/effects'; //
  16. export const actionAuthLogin = (token) => ({ type: 'AUTH_LOGIN', token })
  17. export const actionAuthLogout = () => ({ type: 'AUTH_LOGOUT' })
  18. export const getGQL = (url) => (query, variables) =>
  19. fetch(url, {
  20. method: 'POST',
  21. headers: {
  22. 'Content-Type': 'application/json',
  23. ...(localStorage.authToken
  24. ? { Authorization: 'Bearer ' + localStorage.authToken }
  25. : {}),
  26. },
  27. body: JSON.stringify({ query, variables }),
  28. })
  29. .then((res) => res.json())
  30. .then((data) => {
  31. if (data.data) {
  32. return Object.values(data.data)[0]
  33. } else {
  34. throw new Error(JSON.stringify(data.errors))
  35. }
  36. })
  37. export const gql = getGQL('/graphql')
  38. export const getGQLAnon = (url) => (query, variables) =>
  39. fetch(url, {
  40. method: 'POST',
  41. headers: {
  42. 'Content-Type': 'application/json'
  43. },
  44. body: JSON.stringify({ query, variables }),
  45. })
  46. .then((res) => res.json())
  47. .then((data) => {
  48. if (data.data) {
  49. return Object.values(data.data)[0]
  50. } else {
  51. throw new Error(JSON.stringify(data.errors))
  52. }
  53. })
  54. export const gqlAnon = getGQLAnon('/graphql')
  55. export const actionPending = (name) => ({
  56. type: 'PROMISE',
  57. name,
  58. status: 'PENDING',
  59. })
  60. export const actionFulfilled = (name, payload) => ({
  61. type: 'PROMISE',
  62. name,
  63. status: 'FULFILLED',
  64. payload,
  65. })
  66. export const actionRejected = (name, error) => ({
  67. type: 'PROMISE',
  68. name,
  69. status: 'REJECTED',
  70. error,
  71. })
  72. // export const actionPromise = (name, promise) => async (dispatch) => {
  73. // dispatch(actionPending(name))
  74. // try {
  75. // let payload = await promise
  76. // dispatch(actionFulfilled(name, payload))
  77. // return payload
  78. // } catch (error) {
  79. // dispatch(actionRejected(name, error))
  80. // }
  81. // }
  82. const actionPromise = (name, promise) =>
  83. ({ type: 'PROMISE_START', name, promise })
  84. export const actionAboutMe = (_id) =>
  85. actionPromise(
  86. 'aboutMe',
  87. gql(
  88. `query AboutMe($userId:String){
  89. UserFindOne(query:$userId)
  90. {
  91. _id createdAt login nick avatar{_id url}
  92. followers{_id login nick avatar{_id url}}
  93. following{_id login nick avatar{_id url}}
  94. }
  95. }`,
  96. {
  97. userId: JSON.stringify([{ _id }]),
  98. },
  99. ),
  100. )
  101. export const actionFullLogin = (login, password) => //упрощенный action для саги
  102. ({type: 'FULL_LOGIN', login, password})
  103. export const actionLogin = (login, password) => { //функция, которая возвращает результат actionPromise
  104. return actionPromise(
  105. "auth",
  106. gql(
  107. `query log($login:String, $password:String) {
  108. login(login:$login, password:$password)
  109. }`,
  110. { login, password }
  111. )
  112. );
  113. };
  114. // export const actionFullLogin = (login, password) => async (dispatch) => {
  115. // let token = await dispatch(
  116. // actionPromise(
  117. // 'auth',
  118. // gql(
  119. // ` query login($login:String!, $password:String!){
  120. // login(login:$login, password:$password)} `,
  121. // { login, password },
  122. // ),
  123. // ),
  124. // )
  125. // if (token) {
  126. // await dispatch(actionAuthLogin(token))
  127. // }
  128. // }
  129. export const actionRegister = (login, password) =>
  130. actionPromise(
  131. 'register',
  132. gql(
  133. `mutation register($login: String!, $password: String!) {
  134. createUser (login: $login, password: $password) {
  135. _id login
  136. }
  137. }`,
  138. { login: login, password: password },
  139. ),
  140. )
  141. export const actionChangePassword = (login, password, newPassword) =>
  142. actionPromise(
  143. 'newPassword',
  144. gqlAnon(
  145. `mutation changePassword($login: String!, $password: String!, $newPassword: String!) {
  146. changePassword (login: $login, password: $password, newPassword: $newPassword) {
  147. _id login
  148. }
  149. }`,
  150. { login, password, newPassword },
  151. ),
  152. )
  153. export const actionFullRegister = (login, password) => async (dispatch) => {
  154. let tokenCheck = await dispatch(actionRegister(login, password))
  155. if (tokenCheck?.login === login) {
  156. await dispatch(actionFullLogin(login, password))
  157. history.push('/feed')
  158. }
  159. }
  160. export const uploadFile = (file) => {
  161. const myForm = new FormData()
  162. myForm.append('photo', file)
  163. return fetch('/upload', {
  164. method: 'POST',
  165. headers: localStorage.authToken
  166. ? { Authorization: 'Bearer ' + localStorage.authToken }
  167. : {},
  168. body: myForm,
  169. }).then((result) => result.json())
  170. }
  171. export const uploadFileType = {
  172. name: 'photo',
  173. action: `/upload`,
  174. headers: localStorage.authToken
  175. ? { Authorization: 'Bearer ' + localStorage.authToken }
  176. : {},
  177. }
  178. export const actionUploadFile = (file) =>
  179. actionPromise('uploadFile', uploadFile(file))
  180. export const actionClearPromise = (name) => (dispatch) => {
  181. return dispatch(actionClearPromiseForName(name))
  182. }
  183. export const actionClearPromiseForName = (name) => ({
  184. type: 'PROMISE_CLEAR',
  185. name,
  186. })
  187. export const actionAllClearPromise = () => ({
  188. type: 'PROMISE_All_CLEAR',
  189. })
  190. export const actionUploadFiles = (files) =>
  191. actionPromise(
  192. 'uploadFiles',
  193. Promise.all(files.map((file) => uploadFile(file))),
  194. )
  195. export const actionAvatar = (imageId, myId) =>
  196. actionPromise(
  197. 'setAvatar',
  198. gql(
  199. `mutation setAvatar($imageId:ID, $userId:String){
  200. UserUpsert(user:{_id: $userId, avatar: {_id: $imageId}}){
  201. _id, avatar{
  202. _id
  203. }
  204. }
  205. }`,
  206. { imageId, userId: myId },
  207. ),
  208. )
  209. export const actionPostUpsert = (post, _id) => async (dispatch) => {
  210. await dispatch(
  211. actionPromise(
  212. 'postUpsert',
  213. gql(
  214. `
  215. mutation PostUpsert($post:PostInput){
  216. PostUpsert(post:$post){
  217. _id title text images{_id url}
  218. }
  219. }`,
  220. {
  221. post: {
  222. ...post,
  223. _id: _id,
  224. images: post.images.map(({ _id }) => ({ _id })),
  225. },
  226. },
  227. ),
  228. ),
  229. )
  230. }
  231. export const actionAllPosts = (userId) =>
  232. actionPromise(
  233. 'allPostsMe',
  234. gql(
  235. `query allPosts($userId:String!){
  236. PostFind(query:$userId){
  237. owner{_id} _id title text images{_id url}
  238. }
  239. }`,
  240. {
  241. userId: JSON.stringify([
  242. { ___owner: userId },
  243. {
  244. sort: [{ _id: -1 }],
  245. skip: [0],
  246. limit: [300],
  247. },
  248. ]),
  249. },
  250. ),
  251. )
  252. export const actionPostsCount = (_id) =>
  253. actionPromise(
  254. 'countAllPostsUser',
  255. gql(
  256. ` query CountAllPostsUser($_id:String!){
  257. PostCount(query:$_id)
  258. }`,
  259. { _id: JSON.stringify([{ ___owner: { $in: [_id] } }]) },
  260. ),
  261. )
  262. // export const actionAllPostsFeed = () =>
  263. // actionPromise(
  264. // 'postsAllFeed',
  265. // gql(
  266. // ` query allPosts($_id:String){
  267. // PostFind(query:$_id){
  268. // owner{_id login avatar{_id url}}
  269. // _id title text images{_id url}
  270. // likes{
  271. // _id
  272. // owner{
  273. // _id login avatar {_id url}
  274. // }
  275. // }
  276. // comments{
  277. // _id, createdAt, text owner{_id login avatar{_id url}}
  278. // answers{
  279. // _id, createdAt, text owner{_id login avatar{_id url}}
  280. // }
  281. // }
  282. // }
  283. // }`,
  284. // {
  285. // _id: JSON.stringify([
  286. // {},
  287. // {
  288. // sort: [{ _id: -1 }],
  289. // skip: [0],
  290. // limit: [10],
  291. // },
  292. // ]),
  293. // },
  294. // ),
  295. // )
  296. export const actionOnePost = (_id) =>
  297. actionPromise(
  298. 'onePost',
  299. gql(
  300. `query OneFind($post:String){
  301. PostFindOne(query:$post){
  302. _id title text images{_id url}
  303. owner{_id login avatar{_id url}}
  304. createdAt
  305. comments{
  306. _id, createdAt, text owner{_id login avatar{_id url}}
  307. answers{
  308. _id, createdAt, text owner{_id login avatar{_id url}}
  309. }
  310. owner{_id login avatar{_id url}}}
  311. likes{
  312. _id
  313. owner{
  314. _id login avatar {_id url}
  315. }
  316. }}}
  317. `,
  318. {
  319. post: JSON.stringify([{ _id }]),
  320. },
  321. ),
  322. )
  323. export const actionFindLikes = (_id) =>
  324. actionPromise(
  325. 'onePostLikes',
  326. gql(
  327. `query OneFind($post:String){
  328. PostFindOne(query:$post){
  329. likes{
  330. _id
  331. owner{
  332. _id login avatar {url}
  333. }
  334. }
  335. }
  336. }`,
  337. {
  338. post: JSON.stringify([{ _id }]),
  339. },
  340. ),
  341. )
  342. export const actionAllFollowers = (_id) =>
  343. actionPromise(
  344. 'allFollowers',
  345. gql(
  346. `query AllFollowers($userId:String){
  347. UserFindOne(query:$userId)
  348. {
  349. followers{_id login}
  350. }
  351. }`,
  352. {
  353. userId: JSON.stringify([{ _id }]),
  354. },
  355. ),
  356. )
  357. export const actionAllFollowing = (_id) =>
  358. actionPromise(
  359. 'allFollowing',
  360. gql(
  361. `query AllFollowing($userId:String){
  362. UserFindOne(query:$userId)
  363. {
  364. following{_id login}
  365. }
  366. }`,
  367. {
  368. userId: JSON.stringify([{ _id }]),
  369. },
  370. ),
  371. )
  372. export const actionAddComment = (postId, text) =>
  373. actionPromise(
  374. "addComment",
  375. gql(
  376. `mutation AddComment($comment:CommentInput){
  377. CommentUpsert(comment:$comment)
  378. {
  379. _id
  380. text
  381. createdAt
  382. }
  383. }`,
  384. {
  385. comment: {
  386. post: {
  387. _id: postId,
  388. },
  389. text
  390. },
  391. },
  392. ),
  393. )
  394. export const actionGetCommentsOnePost = (postId) =>
  395. actionPromise('commentsOnePost', gql(`query commentFindPost ($id:String!){
  396. PostFindOne(query:$id){
  397. comments {
  398. _id text createdAt
  399. owner{
  400. _id nick login
  401. avatar{
  402. _id url
  403. }
  404. }
  405. likes{_id}
  406. }
  407. }
  408. }`, { id: JSON.stringify([{ _id: postId }]) }))
  409. export const actionAddSubComment = (commentId, comment) =>
  410. actionPromise(
  411. 'addSubComment',
  412. gql(
  413. `mutation AddComment($comment:CommentInput){
  414. CommentUpsert(comment:$comment)
  415. {
  416. _id
  417. text
  418. createdAt
  419. }
  420. }`,
  421. {
  422. comment: {
  423. answerTo: {
  424. _id: commentId,
  425. },
  426. text: comment,
  427. },
  428. },
  429. ),
  430. )
  431. // export const actionAddFullComment = (postId,comment) => async(dispatch) => {
  432. // let addComment = await dispatch(actionAddComment(postId,comment));
  433. // if(addComment){
  434. // await dispatch(actionOnePost(postId));
  435. // }
  436. // }
  437. // export const actionAddFullComment = (postId, comment) => async (
  438. // dispatch,
  439. // getState,
  440. // ) => {
  441. // await dispatch(actionAddComment(postId, comment))
  442. // const {
  443. // promise: {
  444. // addComment: { status },
  445. // },
  446. // } = getState()
  447. // if (status === 'FULFILLED') {
  448. // await dispatch(actionOnePost(postId))
  449. // }
  450. // // await dispatch(actionOnePost(postId));
  451. // }
  452. export const actionAddSubFullComment = (postId, commentId, comment) => async (
  453. dispatch,
  454. getState,
  455. ) => {
  456. await dispatch(actionAddSubComment(commentId, comment))
  457. const {
  458. promise: {
  459. addSubComment: { status },
  460. },
  461. } = getState()
  462. if (status === 'FULFILLED') {
  463. await dispatch(actionOnePost(postId))
  464. }
  465. // await dispatch(actionOnePost(postId));
  466. }
  467. // export const actionAddlike = _id =>
  468. // actionPromise("addLike", gql(`mutation AddLike($like:LikeInput){
  469. // LikeUpsert(like:$like){
  470. // _id
  471. // }
  472. // }`,{
  473. // like:{
  474. // "post":{
  475. // "_id": _id
  476. // }
  477. // }
  478. // }))
  479. export const actionAddLike = (postId) =>
  480. actionPromise(
  481. 'addLike',
  482. gql(
  483. `mutation AddLike($like:LikeInput){
  484. LikeUpsert(like:$like)
  485. {
  486. _id
  487. }
  488. }`,
  489. {
  490. like: {
  491. post: {
  492. _id: postId,
  493. },
  494. },
  495. },
  496. ),
  497. )
  498. export const actionGetFindLiked = (_id) =>
  499. actionPromise(
  500. 'findLiked',
  501. gql(
  502. ` query LikeFindPost($id:String!) {
  503. LikeFind(query:$id){
  504. owner { _id nick login
  505. avatar{_id url}
  506. }
  507. }
  508. } `,
  509. {
  510. id: JSON.stringify([{ 'post._id': _id }]),
  511. },
  512. ),
  513. )
  514. // export const actionDeleteFullLike = (likeId) => async(dispatch,getState) => {
  515. // let unLike = await dispatch(actionDeleteLike(likeId));
  516. // if(unLike){
  517. // await dispatch(actionOnePost(unLike?.post?._id));
  518. // }
  519. // }
  520. // export const actionDeleteFullLike = (likeId, postId) => async (
  521. // dispatch,
  522. // getState,
  523. // ) => {
  524. // await dispatch(actionDeleteLike(likeId, postId))
  525. // const {
  526. // promise: {
  527. // deleteLike: { status },
  528. // },
  529. // } = getState()
  530. // if (status === 'FULFILLED') {
  531. // await dispatch(actionOnePost(postId))
  532. // }
  533. // // await dispatch(actionOnePost(postId));
  534. // }
  535. export const actionDeleteLike = (likeId, postId) =>
  536. actionPromise(
  537. 'deleteLike',
  538. gql(
  539. `mutation DeleteLike($like:LikeInput){
  540. LikeDelete(like: $like)
  541. {
  542. _id
  543. }
  544. }`,
  545. {
  546. like: {
  547. _id: likeId,
  548. post: {
  549. _id: postId,
  550. },
  551. },
  552. },
  553. ),
  554. )
  555. export const actionSetAvatar = (file, myId) => async (dispatch) => {
  556. const avatar = await dispatch(actionAvatar(file, myId))
  557. if (avatar) {
  558. await dispatch(actionFullProfilePageUser(myId))
  559. await dispatch(actionFullProfilePage(myId))
  560. await dispatch(actionClearPromise('setAvatar'))
  561. await dispatch(actionClearPromise('uploadFile'))
  562. }
  563. }
  564. export const actionPostsFeed = (myFollowing, skip) =>
  565. actionPromise(
  566. 'postsFeed',
  567. gql(
  568. `query PostsFeed($ownerId:String){
  569. PostFind(query:$ownerId){
  570. owner{_id login avatar{url}}
  571. images{_id url} title text
  572. _id likesCount
  573. likes{
  574. _id
  575. owner{
  576. _id login avatar {_id url}
  577. }
  578. }
  579. comments{
  580. _id, createdAt, text owner{_id login avatar{_id url}}
  581. answers{
  582. _id, createdAt, text owner{_id login avatar{_id url}}
  583. }
  584. }
  585. }
  586. }`,
  587. {
  588. ownerId: JSON.stringify([
  589. {
  590. ___owner: {
  591. $in: myFollowing,
  592. },
  593. },
  594. {
  595. sort: [{ _id: -1 }],
  596. skip: [skip || 0],
  597. limit: [10],
  598. },
  599. ]),
  600. },
  601. ),
  602. )
  603. export const actionFullAllGetPosts = () => ({
  604. type:"FEED_POSTS"
  605. })
  606. export const actionFullExplorePosts = () => ({
  607. type:"EXPLORE_POSTS"
  608. })
  609. export const actionPostsFeedCount = (myFollowing) =>
  610. actionPromise(
  611. 'postsFeedCount',
  612. gql(
  613. ` query CountAllPostsFeed($_id:String!){
  614. PostCount(query:$_id)
  615. }`,
  616. {
  617. _id: JSON.stringify([
  618. {
  619. ___owner: {
  620. $in: myFollowing,
  621. },
  622. },
  623. ]),
  624. },
  625. ),
  626. )
  627. export const actionExplorePosts = (skip) =>
  628. actionPromise(
  629. 'explorePosts',
  630. gql(
  631. ` query PostsFeed($_id:String){
  632. PostFind(query:$_id){
  633. owner{_id login avatar{url}}
  634. images{_id url} title text
  635. _id likesCount
  636. likes{
  637. _id
  638. owner{
  639. _id login avatar {_id url}
  640. }
  641. }
  642. comments{
  643. _id, createdAt, text owner{_id login avatar{_id url}}
  644. answers{
  645. _id, createdAt, text owner{_id login avatar{_id url}}
  646. }
  647. }
  648. }
  649. }`,
  650. {
  651. _id: JSON.stringify([
  652. {},
  653. {
  654. sort: [{ _id: -1 }],
  655. skip: [skip || 0],
  656. limit: [12],
  657. },
  658. ]),
  659. },
  660. ),
  661. )
  662. export const actionExplorePostsCount = () =>
  663. actionPromise(
  664. 'explorePostsCount',
  665. gql(
  666. ` query CountAllPosts($_id:String!){
  667. PostCount(query:$_id)
  668. }`,
  669. {
  670. _id: JSON.stringify([{}]),
  671. },
  672. ),
  673. )
  674. export const actionSearchUser = (userName) =>
  675. actionPromise(
  676. 'searchUser',
  677. gql(
  678. `
  679. query gf($query: String){
  680. UserFind(query: $query){
  681. _id, login avatar{url}
  682. }
  683. }`,
  684. {
  685. query: JSON.stringify([
  686. {
  687. $or: [{ login: `/${userName}/` }], //регулярки пишутся в строках
  688. },
  689. {
  690. sort: [{ login: 1 }],
  691. }, //сортируем по title алфавитно
  692. ]),
  693. },
  694. ),
  695. )
  696. export const actionUserUpsert = (user, myId) =>
  697. actionPromise(
  698. 'userUpsert',
  699. gql(
  700. `mutation UserUpsert($user:UserInput){
  701. UserUpsert(user:$user){
  702. _id login nick avatar{_id}
  703. }
  704. }`,
  705. {
  706. user: {
  707. _id: myId,
  708. login: user?.login,
  709. nick : user?.nick
  710. },
  711. },
  712. ),
  713. )
  714. // export con
  715. export const actionAboutUser = (_id) =>
  716. actionPromise(
  717. 'aboutUser',
  718. gql(
  719. `query AboutMe($userId:String){
  720. UserFindOne(query:$userId)
  721. {
  722. _id createdAt login nick avatar{_id url}
  723. followers{_id login nick avatar{_id url}}
  724. following{_id login nick avatar{_id url}}
  725. }
  726. }`,
  727. {
  728. userId: JSON.stringify([{ _id }]),
  729. },
  730. ),
  731. )
  732. export const actionAllPostsUser = (userId, skip) =>
  733. actionPromise(
  734. 'allPosts',
  735. gql(
  736. `query allPosts($userId:String!){
  737. PostFind(query:$userId){
  738. owner{_id} _id title text images{_id url}
  739. }
  740. }`,
  741. {
  742. userId: JSON.stringify([
  743. { ___owner: userId },
  744. {
  745. sort: [{ _id: -1 }],
  746. skip: [skip || 0],
  747. limit: [10],
  748. },
  749. ]),
  750. },
  751. ),
  752. )
  753. export const actionSubscribe = (my_Id, followId, oldFollowing) =>
  754. actionPromise(
  755. 'subscribe',
  756. gql(
  757. `mutation subscribe($user:UserInput) {
  758. UserUpsert(user: $user) {
  759. _id following{_id login}
  760. followers{
  761. _id login
  762. }
  763. }
  764. }
  765. `,
  766. {
  767. user: {
  768. _id: my_Id,
  769. following: [...(oldFollowing || []), { _id: followId }],
  770. },
  771. },
  772. ),
  773. )
  774. export const actionUnSubscribe = (my_Id, oldFollowing) =>
  775. actionPromise(
  776. 'unSubscribe',
  777. gql(
  778. `mutation unSubscribe($user:UserInput) {
  779. UserUpsert(user: $user) {
  780. _id following{_id login}
  781. followers{
  782. _id login
  783. }
  784. }
  785. }
  786. `,
  787. {
  788. user: {
  789. _id: my_Id,
  790. following: oldFollowing || [],
  791. },
  792. },
  793. ),
  794. )
  795. export const actionFullSubscribe = (my_Id, followId) => async (
  796. dispatch,
  797. getState,
  798. ) => {
  799. const oldFollowing = (
  800. getState().promise.aboutMe?.payload?.following || []
  801. ).map(({ _id }) => ({ _id }))
  802. let followingId = await dispatch(
  803. actionSubscribe(my_Id, followId, oldFollowing),
  804. )
  805. if (followingId) {
  806. Promise.all([
  807. await dispatch(actionFullProfilePageUser(followId)),
  808. await dispatch(actionFullProfilePage(my_Id)),
  809. ])
  810. await dispatch(actionClearFeedPosts())
  811. }
  812. }
  813. export const actionFullUnSubscribe = (my_Id, followId) => async (
  814. dispatch,
  815. getState,
  816. ) => {
  817. const oldFollowing = (getState().promise.aboutMe?.payload?.following || [])
  818. .filter((item) => item._id !== followId)
  819. .map(({ _id }) => ({ _id }))
  820. if (oldFollowing) {
  821. await dispatch(actionUnSubscribe(my_Id, oldFollowing))
  822. Promise.all([
  823. await dispatch(actionFullProfilePageUser(followId)),
  824. await dispatch(actionFullProfilePage(my_Id)),
  825. ])
  826. await dispatch(actionClearFeedPosts())
  827. }
  828. }
  829. export const actionAddFullLike = (postId) => async (dispatch, getState) => {
  830. await dispatch(actionAddLike(postId))
  831. const {
  832. promise: {
  833. addLike: { status },
  834. },
  835. } = getState()
  836. if (status === 'FULFILLED') {
  837. await dispatch(actionOnePost(postId))
  838. }
  839. // await dispatch(actionOnePost(postId));
  840. }
  841. export const actionAddFullLikeForFeed = (postId) => async (
  842. dispatch,
  843. getState,
  844. ) => {
  845. await dispatch(actionAddLike(postId))
  846. const {
  847. promise: {
  848. addLike: { status },
  849. },
  850. } = getState()
  851. if (status === 'FULFILLED') {
  852. await dispatch(actionOnePost(postId))
  853. await dispatch(actionFullAllGetPosts())
  854. }
  855. // await dispatch(actionOnePost(postId));
  856. }
  857. export const actionDeleteFullLikeForFeed = (likeId, postId) => async (
  858. dispatch,
  859. getState,
  860. ) => {
  861. await dispatch(actionDeleteLike(likeId, postId))
  862. const {
  863. promise: {
  864. deleteLike: { status },
  865. },
  866. } = getState()
  867. if (status === 'FULFILLED') {
  868. await dispatch(actionOnePost(postId))
  869. await dispatch(actionFullAllGetPosts())
  870. }
  871. // await dispatch(actionOnePost(postId));
  872. }
  873. export const actionUserUpdate = (user, myId) => async (dispatch, getState) => {
  874. await dispatch(actionUserUpsert(user, myId))
  875. const {
  876. promise: {
  877. userUpsert: { status },
  878. },
  879. } = getState()
  880. if (status === 'FULFILLED') {
  881. await dispatch(actionFullProfilePage(myId))
  882. await dispatch(actionFullProfilePageUser(myId))
  883. }
  884. }
  885. export const actionFindSubComment = (findId) =>
  886. actionPromise(
  887. 'subComments',
  888. gql(
  889. `query commentFindOne ($id:String!){
  890. CommentFindOne(query:$id){
  891. _id text answers {
  892. _id text
  893. post {_id }
  894. answers { _id}
  895. createdAt
  896. likes { _id owner
  897. {_id avatar{_id url} login nick } }
  898. owner {
  899. _id login nick
  900. avatar { _id url }
  901. }
  902. }
  903. }
  904. }`,
  905. {
  906. id: JSON.stringify([
  907. {
  908. _id: findId,
  909. },
  910. ]),
  911. },
  912. ),
  913. )