I want to create a cart that contain list of products that user added and save it in localStorage. if we have this code for RTK query is that possible to define that cart inside the createApi() ? if not, how can I use it in another slice?
export const productApi = createApi({
reducerPath: "productApi",
baseQuery: fetchBaseQuery({ baseUrl: "http://127.0.0.1:8000/api/" }),
tagTypes: ["Products"],
endpoints: (builder) => ({
SingleProduct: builder.query<IProduct, number>({
query: (id) => `product/${id}/`,
providesTags: (result, error, id) => [{ type: "Products", id }],
}),
}),
});
if not possible to add cart action directly to createApi how can I extract the SingleProduct and use it in this slice?
const cartSlice = createSlice({
name: "cart",
initialState,
reducers: {
addToCart: (state, action: PayloadAction<{ id: number, qty: number }>) => {
const data = // what should i put here?
state.push(data);
localStorage.setItem("cartItems", JSON.stringify(state));
},
},
});