index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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}}
  339. owner {_id login nick
  340. avatar {url}
  341. }
  342. answers{
  343. _id
  344. }
  345. answerTo{_id}
  346. }
  347. likes{ _id
  348. owner{_id}
  349. }
  350. owner {_id login nick
  351. avatar {url}
  352. }
  353. }
  354. }
  355. `,
  356. {
  357. post: JSON.stringify([{ _id }]),
  358. },
  359. ),
  360. )
  361. export const actionFindLikes = (_id) =>
  362. actionPromise(
  363. 'onePostLikes',
  364. gql(
  365. `query OneFind($post:String){
  366. PostFindOne(query:$post){
  367. likes{
  368. _id
  369. owner{
  370. _id login avatar {url}
  371. }
  372. }
  373. }
  374. }`,
  375. {
  376. post: JSON.stringify([{ _id }]),
  377. },
  378. ),
  379. )
  380. export const actionAllFollowers = (_id) =>
  381. actionPromise(
  382. 'allFollowers',
  383. gql(
  384. `query AllFollowers($userId:String){
  385. UserFindOne(query:$userId)
  386. {
  387. _id
  388. followers{_id login}
  389. }
  390. }`,
  391. {
  392. userId: JSON.stringify([{ _id }]),
  393. },
  394. ),
  395. )
  396. export const actionAllFollowing = (_id) =>
  397. actionPromise(
  398. 'allFollowing',
  399. gql(
  400. `query AllFollowing($userId:String){
  401. UserFindOne(query:$userId)
  402. {_id
  403. following{_id login}
  404. }
  405. }`,
  406. {
  407. userId: JSON.stringify([{ _id }]),
  408. },
  409. ),
  410. )
  411. export const actionAddComment = (postId, text) =>
  412. actionPromise(
  413. "addComment",
  414. gql(
  415. `mutation AddComment($comment:CommentInput){
  416. CommentUpsert(comment:$comment)
  417. {
  418. _id
  419. text
  420. createdAt
  421. }
  422. }`,
  423. {
  424. comment: {
  425. post: {
  426. _id: postId,
  427. },
  428. text
  429. },
  430. },
  431. ),
  432. )
  433. export const actionGetCommentsOnePost = (postId) =>
  434. actionPromise('commentsOnePost',
  435. gql(`query commentFindPost ($id:String!){
  436. PostFindOne(query:$id){
  437. comments {
  438. _id text createdAt
  439. owner{
  440. _id nick login
  441. avatar{
  442. _id url
  443. }
  444. }
  445. likes{_id}
  446. }
  447. }
  448. }`, { id: JSON.stringify([{ _id: postId }]) }))
  449. export const actionAddSubComment = (commentId, newResult) =>
  450. actionPromise(
  451. 'addSubComment',
  452. gql(
  453. `mutation AddComment($comment:CommentInput){
  454. CommentUpsert(comment:$comment)
  455. {
  456. _id
  457. text
  458. createdAt
  459. }
  460. }`,
  461. {
  462. comment: {
  463. answerTo: {
  464. _id: commentId,
  465. },
  466. text: newResult,
  467. },
  468. },
  469. ),
  470. )
  471. // export const actionAddFullComment = (postId,comment) => async(dispatch) => {
  472. // let addComment = await dispatch(actionAddComment(postId,comment));
  473. // if(addComment){
  474. // await dispatch(actionOnePost(postId));
  475. // }
  476. // }
  477. // export const actionAddFullComment = (postId, comment) => async (
  478. // dispatch,
  479. // getState,
  480. // ) => {
  481. // await dispatch(actionAddComment(postId, comment))
  482. // const {
  483. // promise: {
  484. // addComment: { status },
  485. // },
  486. // } = getState()
  487. // if (status === 'FULFILLED') {
  488. // await dispatch(actionOnePost(postId))
  489. // }
  490. // // await dispatch(actionOnePost(postId));
  491. // }
  492. // export const actionAddlike = _id =>
  493. // actionPromise("addLike", gql(`mutation AddLike($like:LikeInput){
  494. // LikeUpsert(like:$like){
  495. // _id
  496. // }
  497. // }`,{
  498. // like:{
  499. // "post":{
  500. // "_id": _id
  501. // }
  502. // }
  503. // }))
  504. export const actionAddLike = (postId) =>
  505. actionPromise(
  506. 'addLike',
  507. gql(
  508. `mutation AddLike($like:LikeInput){
  509. LikeUpsert(like:$like)
  510. {
  511. _id
  512. }
  513. }`,
  514. {
  515. like: {
  516. post: {
  517. _id: postId,
  518. },
  519. },
  520. },
  521. ),
  522. )
  523. export const actionGetFindLiked = (_id) =>
  524. actionPromise(
  525. 'findLiked',
  526. gql(
  527. ` query LikeFindPost($id:String!) {
  528. LikeFind(query:$id){
  529. owner { _id nick login
  530. avatar{_id url}
  531. }
  532. }
  533. } `,
  534. {
  535. id: JSON.stringify([{ 'post._id': _id }]),
  536. },
  537. ),
  538. )
  539. // export const actionDeleteFullLike = (likeId) => async(dispatch,getState) => {
  540. // let unLike = await dispatch(actionDeleteLike(likeId));
  541. // if(unLike){
  542. // await dispatch(actionOnePost(unLike?.post?._id));
  543. // }
  544. // }
  545. // export const actionDeleteFullLike = (likeId, postId) => async (
  546. // dispatch,
  547. // getState,
  548. // ) => {
  549. // await dispatch(actionDeleteLike(likeId, postId))
  550. // const {
  551. // promise: {
  552. // deleteLike: { status },
  553. // },
  554. // } = getState()
  555. // if (status === 'FULFILLED') {
  556. // await dispatch(actionOnePost(postId))
  557. // }
  558. // // await dispatch(actionOnePost(postId));
  559. // }
  560. export const actionDeleteLike = (likeId, postId) =>
  561. actionPromise(
  562. 'deleteLike',
  563. gql(
  564. `mutation DeleteLike($like:LikeInput){
  565. LikeDelete(like: $like)
  566. {
  567. _id
  568. }
  569. }`,
  570. {
  571. like: {
  572. _id: likeId,
  573. post: {
  574. _id: postId,
  575. },
  576. },
  577. },
  578. ),
  579. )
  580. // export const actionSetAvatar = (file, myId) => async (dispatch) => {
  581. // const avatar = await dispatch(actionAvatar(file, myId))
  582. // if (avatar) {
  583. // await dispatch(actionFullProfilePageUser(myId))
  584. // await dispatch(actionFullProfilePage(myId))
  585. // await dispatch(actionClearPromise('setAvatar'))
  586. // await dispatch(actionClearPromise('uploadFile'))
  587. // }
  588. // }
  589. export const actionPostsFeed = (myFollowing, skip) =>
  590. actionPromise(
  591. 'postsFeed',
  592. gql(
  593. `query PostsFeed($ownerId:String){
  594. PostFind(query:$ownerId){
  595. owner{_id login avatar{url}}
  596. images{_id url} title text
  597. _id likesCount
  598. likes{
  599. _id
  600. owner{
  601. _id login avatar {_id url}
  602. }
  603. }
  604. comments{
  605. _id, createdAt, text owner{_id login avatar{_id url}}
  606. answers{
  607. _id, createdAt, text owner{_id login avatar{_id url}}
  608. }
  609. }
  610. }
  611. }`,
  612. {
  613. ownerId: JSON.stringify([
  614. {
  615. ___owner: {
  616. $in: myFollowing,
  617. },
  618. },
  619. {
  620. sort: [{ _id: -1 }],
  621. skip: [skip || 0],
  622. limit: [10],
  623. },
  624. ]),
  625. },
  626. ),
  627. )
  628. export const actionFullAllGetPosts = () => ({
  629. type:"FEED_POSTS"
  630. })
  631. export const actionFullExplorePosts = () => ({
  632. type:"EXPLORE_POSTS"
  633. })
  634. export const actionPostsFeedCount = (myFollowing) =>
  635. actionPromise(
  636. 'postsFeedCount',
  637. gql(
  638. ` query CountAllPostsFeed($_id:String!){
  639. PostCount(query:$_id)
  640. }`,
  641. {
  642. _id: JSON.stringify([
  643. {
  644. ___owner: {
  645. $in: myFollowing,
  646. },
  647. },
  648. ]),
  649. },
  650. ),
  651. )
  652. export const actionExplorePosts = (skip) =>
  653. actionPromise(
  654. 'explorePosts',
  655. gql(
  656. ` query PostsFeed($_id:String){
  657. PostFind(query:$_id){
  658. owner{_id login avatar{url}}
  659. images{_id url} title text
  660. _id likesCount
  661. likes{
  662. _id
  663. owner{
  664. _id login avatar {_id url}
  665. }
  666. }
  667. comments{
  668. _id, createdAt, text owner{_id login avatar{_id url}}
  669. answers{
  670. _id, createdAt, text owner{_id login avatar{_id url}}
  671. }
  672. }
  673. }
  674. }`,
  675. {
  676. _id: JSON.stringify([
  677. {},
  678. {
  679. sort: [{ _id: -1 }],
  680. skip: [skip || 0],
  681. limit: [12],
  682. },
  683. ]),
  684. },
  685. ),
  686. )
  687. export const actionExplorePostsCount = () =>
  688. actionPromise(
  689. 'explorePostsCount',
  690. gql(
  691. ` query CountAllPosts($_id:String!){
  692. PostCount(query:$_id)
  693. }`,
  694. {
  695. _id: JSON.stringify([{}]),
  696. },
  697. ),
  698. )
  699. export const actionSearchUser = (userName) =>
  700. actionPromise(
  701. 'searchUser',
  702. gql(
  703. `
  704. query gf($query: String){
  705. UserFind(query: $query){
  706. _id, login avatar{url}
  707. }
  708. }`,
  709. {
  710. query: JSON.stringify([
  711. {
  712. $or: [{ login: `/${userName}/` }], //регулярки пишутся в строках
  713. },
  714. {
  715. sort: [{ login: 1 }],
  716. }, //сортируем по title алфавитно
  717. ]),
  718. },
  719. ),
  720. )
  721. export const actionUserUpsert = (user, _id) =>
  722. actionPromise(
  723. 'userUpsert',
  724. gql(
  725. `mutation UserUpsert($user:UserInput){
  726. UserUpsert(user:$user){
  727. _id login nick avatar{_id}
  728. }
  729. }`,
  730. {
  731. user: {
  732. _id,
  733. login: user?.login,
  734. nick : user?.nick
  735. },
  736. },
  737. ),
  738. )
  739. // export con
  740. export const actionAboutUser = (_id) =>
  741. actionPromise(
  742. 'aboutUser',
  743. gql(
  744. `query AboutMe($userId:String){
  745. UserFindOne(query:$userId)
  746. {
  747. _id createdAt login nick avatar{_id url}
  748. followers{_id login nick avatar{_id url}}
  749. following{_id login nick avatar{_id url}}
  750. }
  751. }`,
  752. {
  753. userId: JSON.stringify([{ _id }]),
  754. },
  755. ),
  756. )
  757. export const actionGetFollowing = (_id) =>
  758. actionPromise(
  759. 'getFollowing',
  760. gql(
  761. `query GetFollowing($userId:String){
  762. UserFindOne(query:$userId)
  763. {
  764. following{_id login nick avatar{_id url}}
  765. }
  766. }`,
  767. {
  768. userId: JSON.stringify([{ _id }]),
  769. },
  770. ),
  771. )
  772. export const actionGetFollowers = (userId) =>
  773. actionPromise(
  774. 'getFollowers',
  775. gql(
  776. `query GetFollowers($userId:String){
  777. UserFindOne(query:$userId)
  778. {
  779. _id
  780. followers{_id login nick avatar{_id url}}
  781. }
  782. }`,
  783. {
  784. userId: JSON.stringify([{ _id:userId }]),
  785. },
  786. ),
  787. )
  788. export const actionAllPostsUser = (userId, skip) =>
  789. actionPromise(
  790. 'allPosts',
  791. gql(
  792. `query allPosts($userId:String!){
  793. PostFind(query:$userId){
  794. owner{_id} _id title text images{_id url}
  795. }
  796. }`,
  797. {
  798. userId: JSON.stringify([
  799. { ___owner: userId },
  800. {
  801. sort: [{ _id: -1 }],
  802. skip: [skip || 0],
  803. limit: [12],
  804. },
  805. ]),
  806. },
  807. ),
  808. )
  809. export const actionChangeSubscribe = (oldFollowing) =>
  810. actionPromise(
  811. 'changeSubscribe',
  812. gql(
  813. `mutation changeSubscribe($user:UserInput) {
  814. UserUpsert(user: $user) {
  815. _id
  816. }
  817. }
  818. `,
  819. {
  820. user:
  821. oldFollowing
  822. ,
  823. },
  824. ),
  825. )
  826. export const actionAddFullLikeForFeed = (postId) => async (
  827. dispatch,
  828. getState,
  829. ) => {
  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. await dispatch(actionFullAllGetPosts())
  839. }
  840. // await dispatch(actionOnePost(postId));
  841. }
  842. export const actionDeleteFullLikeForFeed = (likeId, postId) => async (
  843. dispatch,
  844. getState,
  845. ) => {
  846. await dispatch(actionDeleteLike(likeId, postId))
  847. const {
  848. promise: {
  849. deleteLike: { status },
  850. },
  851. } = getState()
  852. if (status === 'FULFILLED') {
  853. await dispatch(actionOnePost(postId))
  854. await dispatch(actionFullAllGetPosts())
  855. }
  856. // await dispatch(actionOnePost(postId));
  857. }
  858. export const actionFindSubComment = (findId) =>
  859. actionPromise(
  860. 'subComments',
  861. gql(
  862. `query commentFindOne ($id:String!){
  863. CommentFindOne(query:$id){
  864. _id text answers {
  865. _id text
  866. post {_id }
  867. answers { _id}
  868. createdAt
  869. likes { _id owner
  870. {_id avatar{_id url} login nick } }
  871. owner {
  872. _id login nick
  873. avatar { _id url }
  874. }
  875. }
  876. }
  877. }`,
  878. {
  879. id: JSON.stringify([
  880. {
  881. _id: findId,
  882. },
  883. ]),
  884. },
  885. ),
  886. )