index.js 24 KB

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