Skip to content

Commit

Permalink
Background color slice using redux toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbergqvist committed Oct 9, 2024
1 parent b1a30e0 commit 2e9b6d2
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 64 deletions.
10 changes: 0 additions & 10 deletions lab3/src/actions/backgroundActions.js

This file was deleted.

5 changes: 1 addition & 4 deletions lab3/src/components/App/App.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import React from "react";
import Container from "../Container/Container";
import { Provider } from "react-redux";
import configureStore from "../../store";
import store from "../../store";

import "./App.css";

const preloadedState = window.__PRELOADED_STATE__;
const store = configureStore(preloadedState);

const App = () => (
<Provider store={store}>
<Container />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from "react";
import { useDispatch } from "react-redux";
import { setBackgroundColor } from "../../actions/backgroundActions";
import colors from "./colors";
import { updatedBackgroundColor } from "../../features/background/backgroundSlice";
import "./BackgroundColorPicker.css";

const BackgroundColorPicker = () => {
const dispatch = useDispatch();

const handleOnChange = (e) => {
dispatch(setBackgroundColor(e.target.value));
dispatch(updatedBackgroundColor(e.target.value));
};

return (
Expand Down
3 changes: 2 additions & 1 deletion lab3/src/components/Container/Container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import BackgroundColorPicker from "../BackgroundColorPicker/BackgroundColorPicke
import List from "../List/List";
import { useDispatch, useSelector } from "react-redux";
import { getAllUsers } from "../../actions/userActions";
import { selectBackgroundColor } from "../../features/background/backgroundSlice";

import "./Container.css";

const Container = () => {
const dispatch = useDispatch();
const backgroundColor = useSelector((state) => state.background.bgColor);
const backgroundColor = useSelector(selectBackgroundColor);

// TODO: Task 5 - Get all users upon mounting the component
useEffect(() => {
Expand Down
22 changes: 22 additions & 0 deletions lab3/src/features/background/backgroundSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createSlice } from "@reduxjs/toolkit";

const initialState = {
// TODO: Task 2
bgColor: "gold",
};

const backgroundSlice = createSlice({
name: "background",
initialState,
reducers: {
updatedBackgroundColor(state, action) {
state.bgColor = action.payload;
},
},
});

export const { updatedBackgroundColor } = backgroundSlice.actions;

export const selectBackgroundColor = (state) => state.background.bgColor;

export default backgroundSlice.reducer;
19 changes: 0 additions & 19 deletions lab3/src/reducers/backgroundReducer.js

This file was deleted.

10 changes: 0 additions & 10 deletions lab3/src/reducers/reducers.js

This file was deleted.

30 changes: 12 additions & 18 deletions lab3/src/store.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@

import { applyMiddleware, createStore } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import logger from "redux-logger";
import thunk from "redux-thunk";
import rootReducer from "./reducers/reducers";

const configureStore = (preloadedState) => {

const middleware = [
thunk,
logger
];

return createStore(rootReducer, preloadedState, composeWithDevTools(applyMiddleware(...middleware)));
};

export default configureStore;
import { configureStore } from "@reduxjs/toolkit";
import backgroundReducer from "./features/background/backgroundSlice";
import userReducer from "./reducers/userReducer";

const store = configureStore({
reducer: {
background: backgroundReducer,
users: userReducer,
},
});

export default store;
67 changes: 67 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@reduxjs/toolkit": "^2.2.8"
}
}

0 comments on commit 2e9b6d2

Please sign in to comment.