index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. export const actionAuthLogin = (token) => ({ type: 'AUTH_LOGIN', token })
  2. export const actionAuthLogout = () => ({ type: 'AUTH_LOGOUT' })
  3. export const getGQL = (url) => (query, variables) =>
  4. fetch(url, {
  5. method: 'POST',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. ...(localStorage.authToken
  9. ? { Authorization: 'Bearer ' + localStorage.authToken }
  10. : {}),
  11. },
  12. body: JSON.stringify({ query, variables }),
  13. })
  14. .then((res) => res.json())
  15. .then((data) => {
  16. if (data.data) {
  17. return Object.values(data.data)[0]
  18. } else {
  19. throw new Error(JSON.stringify(data.errors))
  20. }
  21. })
  22. export const backendURL = 'http://hipstagram.asmer.fs.a-level.com.ua'
  23. export const gql = getGQL(backendURL + '/graphql')
  24. export const actionPending = (name) => ({ type: 'PROMISE', name, status: 'PENDING' })
  25. export const actionFulfilled = (name, payload) => ({
  26. type: 'PROMISE',
  27. name,
  28. status: 'FULFILLED',
  29. payload,
  30. })
  31. export const actionRejected = (name, error) => ({
  32. type: 'PROMISE',
  33. name,
  34. status: 'REJECTED',
  35. error,
  36. })
  37. export const actionPromise = (name, promise) => async (dispatch) => {
  38. dispatch(actionPending(name))
  39. try {
  40. let payload = await promise
  41. dispatch(actionFulfilled(name, payload))
  42. return payload
  43. } catch (error) {
  44. dispatch(actionRejected(name, error))
  45. }
  46. }
  47. export const actionFullLogin = (login, password) => async (dispatch) => {
  48. let token = await dispatch(
  49. actionPromise(
  50. 'auth',
  51. gql(
  52. ` query login($login:String!, $password:String!){
  53. login(login:$login, password:$password)} `,
  54. { login, password },
  55. ),
  56. ),
  57. )
  58. if (token) {
  59. dispatch(actionAuthLogin(token))
  60. }
  61. }
  62. export const actionRegister = (login, password) =>
  63. actionPromise(
  64. 'register',
  65. gql(
  66. `mutation register($login: String!, $password: String!) {
  67. UserUpsert(user: {login: $login, password: $password, nick: $login}) {
  68. _id login
  69. }
  70. }`,
  71. { login: login, password: password },
  72. ),
  73. )
  74. export const actionFullRegister = (login, password) => async (dispatch) => {
  75. let tokenCheck = await dispatch(actionRegister(login, password))
  76. if (tokenCheck?.login === login) {
  77. dispatch(actionFullLogin(login, password))
  78. }
  79. }
  80. // export const actionAllAboutMe = (_id) =>
  81. // actionPromise(
  82. // 'aboutMe',
  83. // gql(
  84. // `query AboutMe($userId:String){
  85. // UserFindOne(query:$userId)
  86. // {
  87. // _id createdAt login nick avatar{_id url}
  88. // likesCount followers{_id login nick} following{_id login nick}
  89. // }
  90. // }`,
  91. // { userId: JSON.stringify([{ _id }]) },
  92. // ),
  93. // )
  94. // const actionAboutMe = (id) =>
  95. // actionPromise('aboutMe',gql(`query AboutMe($userId:String){
  96. // UserFindOne(query:$userId)
  97. // {
  98. // login nick avatar{_id url} createdAt
  99. // }
  100. // }`,{userId: JSON.stringify([{_id:id}])}))