|
@@ -0,0 +1,85 @@
|
|
|
|
+const getGQL = url =>
|
|
|
|
+ (query, variables={}) => fetch(url, {
|
|
|
|
+ method: 'POST',
|
|
|
|
+ headers: {
|
|
|
|
+ //Accept: "application/json",
|
|
|
|
+ "Content-Type": "application/json",
|
|
|
|
+ ...(localStorage.authToken ? {Authorization: "Bearer " + localStorage.authToken } : {})
|
|
|
|
+ },
|
|
|
|
+ body: JSON.stringify({query, variables})
|
|
|
|
+ }).then(res => res.json())
|
|
|
|
+
|
|
|
|
+ let gql = getGQL("/graphql")
|
|
|
|
+
|
|
|
|
+export const actionPending = name => ({type: "PROMISE" ,status:"PENDING", name})
|
|
|
|
+export const actionResolved = (name,payload) => ({type: "PROMISE" ,status:"RESOLVED", name,payload})
|
|
|
|
+export const actionRejected = (name,error) => ({type: "PROMISE" ,status:"REJECTED", name,error})
|
|
|
|
+
|
|
|
|
+export const actionPromise = (name, promise) =>
|
|
|
|
+ async dispatch => {
|
|
|
|
+ dispatch(actionPending(name))
|
|
|
|
+ try {
|
|
|
|
+ let payload = await promise
|
|
|
|
+ dispatch(actionResolved(name , payload))
|
|
|
|
+ return payload
|
|
|
|
+ }
|
|
|
|
+ catch(error){
|
|
|
|
+ dispatch(actionRejected(name , error))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let log = async(login , password) => {
|
|
|
|
+ let query = ` query log($l:String!,$p:String!) {
|
|
|
|
+ login(login:$l,password:$p)
|
|
|
|
+ }`
|
|
|
|
+let qVariables = {
|
|
|
|
+ "l": login,
|
|
|
|
+ "p": password
|
|
|
|
+}
|
|
|
|
+let result = await gql(query,qVariables)
|
|
|
|
+return result
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+let reg = async(login,password) => {
|
|
|
|
+ let query = `mutation reg($l:String! , $p:String!) {
|
|
|
|
+ createUser(login:$l,password:$p){
|
|
|
|
+ _id login
|
|
|
|
+}
|
|
|
|
+}`
|
|
|
|
+ let qVariables = {
|
|
|
|
+ "l": login,
|
|
|
|
+ "p": password
|
|
|
|
+ }
|
|
|
|
+ let result = await gql(query,qVariables)
|
|
|
|
+ return result
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+export const actionAuthLogin = token => ({type:'LOGIN', token})
|
|
|
|
+export const actionAuthLogout = () => ({type:'LOGOUT'})
|
|
|
|
+export const actionFullLogin = (login , password) => async dispatch => {
|
|
|
|
+ let result = await dispatch(actionPromise("login",log(login,password)))
|
|
|
|
+ if (result?.data?.login !== null){
|
|
|
|
+ dispatch(actionAuthLogin(result.data.login))
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ alert ('такого пользователя не существует')
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export const actionRegister = (login,password) => async dispatch => {
|
|
|
|
+ return await dispatch (actionPromise('register' , reg(login,password)))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+export const actionFullRegister = (login,password) => async dispatch => {
|
|
|
|
+ let result = await dispatch (actionRegister(login,password))
|
|
|
|
+ if (result?.errors === undefined) {
|
|
|
|
+ await dispatch (actionFullLogin(login,password))
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ alert("Такой пользователь уже есть")
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|