|
@@ -1,5 +1,5 @@
|
|
|
import { createApi } from '@reduxjs/toolkit/query/react'
|
|
|
-import { createSlice } from "@reduxjs/toolkit";
|
|
|
+import { createSlice, current } from "@reduxjs/toolkit";
|
|
|
import { gql } from 'graphql-request'
|
|
|
import { graphqlRequestBaseQuery } from '@rtk-query/graphql-request-base-query'; //npm install
|
|
|
|
|
@@ -27,7 +27,11 @@ export const api = createApi({
|
|
|
document: gql`
|
|
|
query GetCategories{
|
|
|
CategoryFind(query: "[{\\"parent\\": null}]") {
|
|
|
- _id name
|
|
|
+ _id name goods {
|
|
|
+ _id name
|
|
|
+ }, subCategories {
|
|
|
+ name
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
`}),
|
|
@@ -89,6 +93,89 @@ export const api = createApi({
|
|
|
variables: { q: JSON.stringify([{ _id }]) }
|
|
|
}),
|
|
|
}),
|
|
|
+ getUserById: builder.query({
|
|
|
+ query: (_id) => ({
|
|
|
+ document: gql`
|
|
|
+ query GetUser($q: String) {
|
|
|
+ UserFindOne(query: $q) {
|
|
|
+ _id
|
|
|
+ login
|
|
|
+ nick
|
|
|
+ avatar { url }
|
|
|
+ acl
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ variables: { q: JSON.stringify([{ _id }]) }
|
|
|
+ }),
|
|
|
+ providesTags: (result, error, id) => ([{ type: 'User', id }])
|
|
|
+ }),
|
|
|
+ getOwnerOrder: builder.query({
|
|
|
+ query: () => ({
|
|
|
+ document: gql`
|
|
|
+ query orders($q: String) {
|
|
|
+ OrderFind(query: $q) {
|
|
|
+ _id, total, createdAt, owner{
|
|
|
+ _id, login
|
|
|
+ }, orderGoods{
|
|
|
+ price, count, good{
|
|
|
+ name, _id, images {
|
|
|
+ url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ variables: { q: JSON.stringify([{}]) }
|
|
|
+ })
|
|
|
+ }),
|
|
|
+ getOrderGood: builder.query({
|
|
|
+ query: () => ({
|
|
|
+ document: gql`
|
|
|
+ query orders($q: String) {
|
|
|
+ OrderGoodFind(query: $q) {
|
|
|
+ _id, createdAt, price, total, count, goodName, good {
|
|
|
+ _id, name
|
|
|
+ }, order {
|
|
|
+ _id, total, orderGoods {
|
|
|
+ count, good {
|
|
|
+ name
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, owner {
|
|
|
+ _id login
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ variables: { q: JSON.stringify([{}]) }
|
|
|
+ })
|
|
|
+ }),
|
|
|
+ getGoods: builder.query({
|
|
|
+ query: () => ({
|
|
|
+ document: gql`
|
|
|
+ query GetGoods($q: String) {
|
|
|
+ GoodFind(query: $q) {
|
|
|
+ _id, name, description, price, categories {
|
|
|
+ _id
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ variables: { q: JSON.stringify([{}]) }
|
|
|
+ })
|
|
|
+ }),
|
|
|
+ getUsers: builder.query({
|
|
|
+ query: () => ({
|
|
|
+ document: gql`
|
|
|
+ query GetUsers($q: String) {
|
|
|
+ UserFind(query: $q) {
|
|
|
+ _id, login, createdAt
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ variables: { q: JSON.stringify([{}]) }
|
|
|
+ })
|
|
|
+ }),
|
|
|
login: builder.mutation({
|
|
|
query: ({ login, password }) => ({
|
|
|
document: gql`
|
|
@@ -102,7 +189,7 @@ export const api = createApi({
|
|
|
register: builder.mutation({
|
|
|
query: ({ login, password }) => ({
|
|
|
document: gql`
|
|
|
- mutation registration($login:String, $password: String){
|
|
|
+ mutation registration($login:String, $password: String) {
|
|
|
UserUpsert(user: {login:$login, password: $password}){
|
|
|
_id login createdAt
|
|
|
}
|
|
@@ -110,27 +197,49 @@ export const api = createApi({
|
|
|
variables: { login, password }
|
|
|
})
|
|
|
}),
|
|
|
- getOwnerOrder: builder.query({
|
|
|
- query: () => ({
|
|
|
+ createOrder: builder.mutation({
|
|
|
+ query: (orderGoods) => ({
|
|
|
document: gql`
|
|
|
- query orders($q: String) {
|
|
|
- OrderFind(query: $q) {
|
|
|
- _id, total, createdAt, owner{
|
|
|
- _id, login
|
|
|
- }, orderGoods{
|
|
|
- price, count, good{
|
|
|
- name
|
|
|
- }
|
|
|
+ mutation ordering($orderGoods: OrderInput) {
|
|
|
+ OrderUpsert(order: $orderGoods) {
|
|
|
+ _id, total, orderGoods {
|
|
|
+ good {
|
|
|
+ name, _id
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- `,
|
|
|
- variables: { q: JSON.stringify([{}]) }
|
|
|
+ }`,
|
|
|
+ variables: { orderGoods: { orderGoods } }
|
|
|
+ })
|
|
|
+ }),
|
|
|
+ createCategory: builder.mutation({
|
|
|
+ query: (category) => ({
|
|
|
+ document: gql`
|
|
|
+ mutation createCategory($category: CategoryInput) {
|
|
|
+ CategoryUpsert(category: $category) {
|
|
|
+ _id
|
|
|
+ }
|
|
|
+ }`,
|
|
|
+ variables: { category }
|
|
|
})
|
|
|
- })
|
|
|
+ }),
|
|
|
+
|
|
|
}),
|
|
|
});
|
|
|
|
|
|
+export const createCategory = category =>
|
|
|
+async dispatch => {
|
|
|
+ const payload = await dispatch(api.endpoints.createCategory.initiate(category));
|
|
|
+ console.log('category created', payload);
|
|
|
+}
|
|
|
+
|
|
|
+export const actionOrder = orderGoods =>
|
|
|
+ async dispatch => {
|
|
|
+ await dispatch(api.endpoints.createOrder.initiate(orderGoods));
|
|
|
+ console.log('order was created');
|
|
|
+ dispatch(cartSlice.actions.clearCart());
|
|
|
+ }
|
|
|
+
|
|
|
function jwtDecode(token) {
|
|
|
try {
|
|
|
const tokenArr = token.split(".");
|
|
@@ -192,6 +301,11 @@ const initialState = {
|
|
|
goodsCount: 0
|
|
|
}
|
|
|
|
|
|
+function calculateTotalAmounts(state, currentState) {
|
|
|
+ state.totalAmount = currentState.goods.reduce((totalAmount, item) => totalAmount + item.good.price * item.count, 0);
|
|
|
+ state.goodsCount = currentState.goods.reduce((goodsCount, item) => goodsCount + item.count, 0);
|
|
|
+}
|
|
|
+
|
|
|
export const cartSlice = createSlice({
|
|
|
name: 'cart',
|
|
|
initialState,
|
|
@@ -203,8 +317,7 @@ export const cartSlice = createSlice({
|
|
|
else {
|
|
|
state.goods[itemIndex].count += +action.payload.count;
|
|
|
}
|
|
|
- state.totalAmount += action.payload.count * action.payload.good.price;
|
|
|
- state.goodsCount += action.payload.count;
|
|
|
+ calculateTotalAmounts(state, current(state));
|
|
|
},
|
|
|
decreaseGoodCount(state, action) {
|
|
|
const itemIndex = getItemIndex(state, action.payload.good._id);
|
|
@@ -214,19 +327,16 @@ export const cartSlice = createSlice({
|
|
|
else {
|
|
|
state.goods = state.goods.filter(item => item.good._id !== action.payload.good._id);
|
|
|
}
|
|
|
- state.goodsCount -= 1;
|
|
|
- state.totalAmount -= action.payload.good.price;
|
|
|
+ calculateTotalAmounts(state, current(state));
|
|
|
},
|
|
|
setGoodCount(state, action) {
|
|
|
const itemIndex = getItemIndex(state, action.payload.good._id);
|
|
|
state.goods[itemIndex].count = action.payload?.count;
|
|
|
- //state.totalAmount += action.payload.count * action.payload.good.price;
|
|
|
- //задать для totalAmount и goodsCount
|
|
|
+ calculateTotalAmounts(state, current(state));
|
|
|
},
|
|
|
deleteGood(state, action) {
|
|
|
state.goods = state.goods.filter(item => item.good._id !== action.payload.good._id);
|
|
|
- state.totalAmount -= action.payload.count * action.payload.good.price;
|
|
|
- state.goodsCount -= action.payload.count;
|
|
|
+ calculateTotalAmounts(state, current(state));
|
|
|
},
|
|
|
clearCart() {
|
|
|
return initialState;
|
|
@@ -236,4 +346,5 @@ export const cartSlice = createSlice({
|
|
|
},
|
|
|
});
|
|
|
export const { addGood, deleteGood, decreaseGoodCount, setGoodCount, clearCart } = cartSlice.actions;
|
|
|
-export const { useGetCategoriesQuery, useGetCategoryByIdQuery, useGetGoodByIdQuery, useLoginMutation, useGetOwnerOrderQuery, useRegisterMutation } = api;
|
|
|
+export const { useGetCategoriesQuery, useGetCategoryByIdQuery, useGetGoodByIdQuery, useLoginMutation, useGetOwnerOrderQuery,
|
|
|
+ useRegisterMutation, useGetUserByIdQuery, useCreateCategoryMutation, useGetGoodsQuery, useGetUsersQuery, useGetOrderGoodQuery} = api;
|