index.js 23 KB

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