|
@@ -1,7 +1,28 @@
|
|
|
import { GraphQLClient } from 'graphql-request';
|
|
|
|
|
|
//const gql = new GraphQLClient('/graphql', {headers: {Authorization: 'Bearer ' + ... }})
|
|
|
-const gql = new GraphQLClient('/graphql')
|
|
|
+
|
|
|
+const getGQL = (url='http://shop-roles.asmer.fs.a-level.com.ua/graphql',
|
|
|
+ getHeaders=(()=>localStorage.authToken ? {Authorization: `Bearer ${localStorage.authToken}`} :
|
|
|
+ {})) =>
|
|
|
+(query='', variables={}) =>
|
|
|
+ fetch(url, {
|
|
|
+ method: 'POST',
|
|
|
+ headers:{
|
|
|
+ 'Accept': 'application/json',
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ ...getHeaders()
|
|
|
+ },
|
|
|
+ body: JSON.stringify({query, variables})
|
|
|
+ }).then(res => res.json()).then(data => {
|
|
|
+ if (data.errors){
|
|
|
+ throw new Error(JSON.stringify(data.errors, null, 4))
|
|
|
+ }
|
|
|
+ const firstKey = Object.keys(data.data)[0]
|
|
|
+ return firstKey ? data.data[firstKey] : data.data
|
|
|
+ })
|
|
|
+
|
|
|
+const gql = getGQL()
|
|
|
|
|
|
export const actionSearch = text => ({type: 'SEARCH', text})
|
|
|
export const actionSearchResult = payload => ({type: 'SEARCH_RESULT', payload})
|
|
@@ -26,17 +47,19 @@ export const actionPromise = (name, promise) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export const actionPosts = () => {
|
|
|
- return actionPromise('posts', gql.request(`query psts{
|
|
|
- getPosts{
|
|
|
- id, title, text
|
|
|
- }
|
|
|
- }`))
|
|
|
+export const actionOrders = () => {
|
|
|
+ return actionPromise('orders', gql( `query orders($allQuery:String){
|
|
|
+ OrderFind(query:$allQuery){
|
|
|
+ _id total orderGoods{
|
|
|
+ total count good{ name }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`, {allQuery: JSON.stringify([{}])}))
|
|
|
}
|
|
|
|
|
|
|
|
|
export const actionCategories = () => {
|
|
|
- return actionPromise('categories', gql.request(
|
|
|
+ return actionPromise('categories', gql(
|
|
|
`query {
|
|
|
CategoryFind(query:"[{\\"parent\\":null}]"){
|
|
|
_id name
|
|
@@ -47,7 +70,7 @@ export const actionCategories = () => {
|
|
|
|
|
|
|
|
|
export const actionCategory = (_id) => {
|
|
|
- return actionPromise('category', gql.request(
|
|
|
+ return actionPromise('category', gql(
|
|
|
`query cat($query:String){
|
|
|
CategoryFindOne(query:$query){
|
|
|
_id name goods{
|
|
@@ -61,7 +84,7 @@ export const actionCategory = (_id) => {
|
|
|
export const actionLogin = (login,password) =>
|
|
|
async dispatch => {
|
|
|
console.log(login, password)
|
|
|
- let result = await dispatch(actionPromise('login', gql.request(`query login($login: String!, $password: String!){
|
|
|
+ let result = await dispatch(actionPromise('login', gql(`query login($login: String!, $password: String!){
|
|
|
login(username: $login, password: $password)
|
|
|
}`, {login, password})))
|
|
|
if (result){
|