3forges / poc-preact-rtk-flowbite

0 stars 1 forks source link

questions about projectSlice, reducers & store #5

Open BorisTherin opened 8 months ago

BorisTherin commented 8 months ago

as we want to go into buisness with our redux store after this one POC, we need to have a look for our best options & practices.

const fetchUserData = createAsyncThunk( "user/fetchUserData", async (userId, { extra }) => { //const { apiClient } = extra; const response = await axios.get(/users/${userId}); return response.data; } );

export default fetchUserData;

BorisTherin commented 8 months ago

i found about that { apiClient } some pretty interresting things.

image

RTK Query data fetching API has been provided for this purpose & sounds really easier to code

take a look @

BorisTherin commented 8 months ago

!IMPORTANT

Read or leave redux !

mutable immutability isn't a jok' & u really want immer to rule this !

BorisTherin commented 8 months ago

aussi pour console.log correctement un proxy array avec la methode redux

image (source: https://redux-toolkit.js.org/rtk-query/usage/examples )

Jean-Baptiste-Lasselle commented 8 months ago

About the state,the store, and the createSlice

const pestoInitialState: GetProjectsListRequestState = {
  projectList: [],
  status: "completed",
}

/// au dessus, c'est l'objetquo'n créée pour y coller les pesto projects que l'on récupère de l'API

export const pestoProjectSlice = createSlice({
  name: "pestoProject",
  initialState: {
    ...pestoInitialState,
    voudrais: {
      je: "faire",
      truc:{
        de: {
          ouf: "wouaaa"
        }
      }
    }
  },
  reducers: {
    /* EMPTY */
    // setItem: addItem
  },
  extraReducers: (builder) => {
    /**
     * Create Pesto Project Reducer 
     */
    builder
      .addCase(createPestoProjectAsync.pending, (state) => {
        state.status = "pending"
        console.log(" PESTO REDUCER [createPestoProjectAsync] pending...")
        console.log(` PESTO REDUCER [createPestoProjectAsync] [${state.voudrais.je + ' /// ' + state.voudrais.truc.de.ouf }]`)
        console.log(`${state.voudrais.je}`)
        console.log(`${state.voudrais.truc.de.ouf}`)
Jean-Baptiste-Lasselle commented 8 months ago

oh là, là j'ai trouvé un truc à essayer pour la mise à jour du store: https://redux-toolkit.js.org/rtk-query/usage/migrating-to-rtk-query#slice