index.js 22 KB

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