Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Step 1:

Navigate to the features/stepSlice folder.

There are 3 seperate files:

state.js

This page provides the initialState value.

api.js

This page provides API action logic.

export const getCrops = createAsyncThunk(
  "steps/getCrops",
  async ({ regionId }, thunkAPI) => {
    const res = await fetch(
      `https://developapi.covercrop-selector.org/v2/crops/?regions=${regionId}&context=seed_calc`
    ).then((data) => data.json());
    return res;
  }
);

index.js

This contains the stepSlice object that creates a slice using the createSlice() function.

Parameters:

  • name: “steps”

  • initialState: (imported from state.js page)

  • reducers: we add both the action and reducer effect here.

    • Example: updateAllSteps

  • extraReducers: we list our reducer states here with three types: pending, fulfilled, rejected.

    • Pending:

      [getCrops.pending]: (state) => {
            state.loading = false;
          },
    • Fulfilled:

      [getCrops.fulfilled]: (state, { payload }) => {
            state.loading = false;
            state.value.crops = payload.data;
          },
    • Rejected:

      [getCrops.rejected]: (state) => {
            state.loading = false;
            state.error = true;
          },

  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.