{"size": 1305, "ext": "js", "max_stars_repo_path": "client/store/orders.js", "max_stars_repo_name": "Snivy-Speedsters/Grace-Shopper", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/store/orders.js", "max_issues_repo_name": "Snivy-Speedsters/Grace-Shopper", "max_issues_repo_issues_event_min_datetime": "2022-03-01T21:33:43.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T19:58:38.000Z", "max_forks_repo_path": "client/store/orders.js", "max_forks_repo_name": "Snivy-Speedsters/Grace-Shopper", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 26.6326530612, "max_line_length": 98, "alphanum_fraction": 0.6704980843}
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

const TOKEN = "token"

export const fetchOrders = createAsyncThunk(
  '/users/orders/fetch',
  async () => {
    const token = window.localStorage.getItem(TOKEN)
    const { data } = await axios.get("/api/users/orders", { headers: { authorization: token }});
    return data
	}
)

export const fetchAllOrders = createAsyncThunk(
  '/admin/orders',
  async () => {
    const token = window.localStorage.getItem(TOKEN)
    const { data } = await axios.get("/api/admin/orders", { headers: { authorization: token }});
    return data
	}
)

export const completeOrder = createAsyncThunk(
  '/admin/order/complete',
  async (orderId, { dispatch }) => {
    const token = window.localStorage.getItem(TOKEN)
    await axios.put(`/api/admin/order/${orderId}/complete`, { headers: { authorization: token }});
    dispatch(fetchAllOrders())
	}
)


export const ordersSlice = createSlice({
  name: 'orders',
  initialState: [],
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase (fetchOrders.fulfilled, (state, action) => {
      return action.payload
    }),
    builder.addCase (fetchAllOrders.fulfilled, (state, action) => {
      return action.payload
    })
  }
})

export default ordersSlice.reducer
