From 278706b8c600ec5edca47f56d3805b241f49069d Mon Sep 17 00:00:00 2001 From: bendtherules Date: Mon, 30 Mar 2020 13:10:14 +0530 Subject: [PATCH] Add basic Board component --- src/App.js | 71 ++++++----------------------- src/app/store.js | 18 ++++---- src/features/Board/Board.js | 30 ++++++++++++ src/features/Board/Board.module.css | 0 src/features/Board/boardSlice.js | 62 +++++++++++++++++++++++++ src/utils/generateID.js | 6 +++ 6 files changed, 121 insertions(+), 66 deletions(-) create mode 100644 src/features/Board/Board.js create mode 100644 src/features/Board/Board.module.css create mode 100644 src/features/Board/boardSlice.js create mode 100644 src/utils/generateID.js diff --git a/src/App.js b/src/App.js index 7825dd2..a80ed5d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,58 +1,13 @@ -import React from 'react'; -import logo from './logo.svg'; -import { Counter } from './features/counter/Counter'; -import './App.css'; - -function App() { - return ( -
-
- logo - -

- Edit src/App.js and save to reload. -

- - Learn - - React - - , - - Redux - - , - - Redux Toolkit - - , and - - React Redux - - -
-
- ); -} - -export default App; +import React from "react"; +import { Board } from "./features/Board/Board"; +import "./App.css"; + +function App() { + return ( +
+ +
+ ); +} + +export default App; diff --git a/src/app/store.js b/src/app/store.js index b867c96..d7aabe6 100644 --- a/src/app/store.js +++ b/src/app/store.js @@ -1,8 +1,10 @@ -import { configureStore } from '@reduxjs/toolkit'; -import counterReducer from '../features/counter/counterSlice'; - -export default configureStore({ - reducer: { - counter: counterReducer, - }, -}); +import { configureStore } from "@reduxjs/toolkit"; +import counterReducer from "../features/counter/counterSlice"; +import boardReducer from "../features/Board/boardSlice"; + +export default configureStore({ + reducer: { + counter: counterReducer, + board: boardReducer + } +}); diff --git a/src/features/Board/Board.js b/src/features/Board/Board.js new file mode 100644 index 0000000..051da59 --- /dev/null +++ b/src/features/Board/Board.js @@ -0,0 +1,30 @@ +import React, { useState } from "react"; +import { useSelector, useDispatch } from "react-redux"; +import { createList, selectBoard } from "./boardSlice"; + +import styles from "./Board.module.css"; + +export function Board() { + const boardData = useSelector(selectBoard); + const dispatch = useDispatch(); + + return ( +
+
{boardData.name}
+
+ {boardData.lists.map(listData => ( +
+
{listData.name}
+
+ {listData.cards.map(cardData => ( +
+ {cardData.text} +
+ ))} +
+
+ ))} +
+
+ ); +} diff --git a/src/features/Board/Board.module.css b/src/features/Board/Board.module.css new file mode 100644 index 0000000..e69de29 diff --git a/src/features/Board/boardSlice.js b/src/features/Board/boardSlice.js new file mode 100644 index 0000000..b399aa3 --- /dev/null +++ b/src/features/Board/boardSlice.js @@ -0,0 +1,62 @@ +import { createSlice } from "@reduxjs/toolkit"; +import generateID from "../../utils/generateID"; + +export const slice = createSlice({ + name: "board", + initialState: { + // TODO: hardcoded for now, change when renaming facility is available + name: "Kubric UI", + lists: [ + // Elements look like - + // { + // id: '1', + // name: "List 1", + // cards: [ + // { + // id: '2', + // text: "card 1" + // }, + // ] + // } + + { + id: `List #${generateID()}`, + name: "Backlog", + cards: [ + { + id: `Card #${generateID()}`, + text: + "UI: Modify display listing of ads to like the vimeo videos sections" + }, + { + id: `Card #${generateID()}`, + text: "Creative insights view" + } + ] + } + ] + }, + reducers: { + // NOTE: Modifying state directly here is FINE and intended. + // Because this uses redux tolkit, which uses immer internally + // https://redux.js.org/redux-toolkit/overview#whats-included + createList: (state, action) => { + const { name } = action.payload; + + state.lists.push({ + id: `List #${generateID()}`, + name, + cards: [] + }); + } + } +}); + +export const { createList } = slice.actions; + +// The function below is called a selector and allows us to select a value from +// the state. Selectors can also be defined inline where they're used instead of +// in the slice file. For example: `useSelector((state) => state.counter.value)` +export const selectBoard = state => state.board; + +export default slice.reducer; diff --git a/src/utils/generateID.js b/src/utils/generateID.js new file mode 100644 index 0000000..b24b9e9 --- /dev/null +++ b/src/utils/generateID.js @@ -0,0 +1,6 @@ +// Quick and dirty ID generation. +let lastID = 0; + +export default function generateID() { + return lastID++; +}