How to fetch data with typicode json server with React Redux

Viewed 238

I have a JSON file under my src folder named items.json:

[
  {
    "tags": [
      "Trees"
    ],
    "price": 10.99,
    "name": "Handcrafted Trees Mug",
    "description": "enim corporis voluptatibus laudantium possimus alias dolorem voluptatem similique aut aliquam voluptatem voluptatem omnis id consequatur",
    "slug": "Handcrafted-Trees-Mug",
    "added": 1485723766805,
    "manufacturer": "OHara-Group",
    "itemType": "mug"
  },
  {
    "tags": [
      "Beach",
      "Ocean",
      "Water"
    ],
    "price": 19.99,
    "name": "Rustic Beach Mug",
    "description": "totam at veritatis eligendi assumenda ex quia praesentium quibusdam ducimus",
    "slug": "Rustic-Beach-Mug",
    "added": 1481573896833,
    "manufacturer": "Sipes-Inc",
    "itemType": "mug"
  }, ...
]

So I have to fetch this data using typicode json server but I have never used it so I couldnt understand how to do it. https://github.com/typicode/json-server

I have action ready for this request but I would be really glad, if you give me some hints to use it. And here is my action:

import Axios from 'axios';
import {
  PRODUCT_LIST_FAIL,
  PRODUCT_LIST_REQUEST,
  PRODUCT_LIST_SUCCESS,
} from '../constants/productConstants';

export const listProducts = () => async (dispatch) => {
  dispatch({
    type: PRODUCT_LIST_REQUEST,
  });
  try {
    const { data } = await Axios.get('');
    dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
  } catch (error) {
    dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
  }
};

So how should I get the data?

I tried to run this command json-server --watch items.json but I got this error:

json-server : File C:\Users\Lenovo\AppData\Roaming\npm\json-server.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

0 Answers
Related