<script>
import { createStore } from 'redux'; // Import the redux package
const initialState = {
counter: 0,
posts: [], // Initial state for storing fetched posts
};
// Reducer function to handle state updates
function reducer(state = initialState, action) {
switch (action.type) {
case 'INCREMENT_COUNTER':
return { ...state, counter: state.counter + 1 }; // Increment the counter value in the state
case 'DECREMENT_COUNTER':
return { ...state, counter: state.counter - 1 }; // Decrement the counter value in the state
case 'INCREMENT_COUNTER_BY_AMOUNT':
return { ...state, counter: state.counter + action.payload }; // Increment the counter value in the state by some amount
case 'DECREMENT_COUNTER_BY_AMOUNT':
return { ...state, counter: state.counter - action.payload }; // Decrement the counter value in the state by some amount
case 'RESET_COUNTER':
return { ...state, counter: 0 }; // Reset the counter value in the state to 0
case 'FETCH_POSTS_SUCCESS':
return { ...state, posts: action.payload }; // Update the posts array in the state with the fetched data
case 'RESET_POSTS':
return { ...state, posts: [] }; // Reset posts array
default:
return state; // Return the current state if the action is not recognized
}
}
// Store enhancer function to enhance the store capabilities
function storeEnhancer(createStoreApi) {
return function (reducer, initialState) {
const reduxStore = createStoreApi(reducer, initialState);
return {
...reduxStore,