From 593ab2d7d4ef972594131b125d9320c618e54691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Wed, 17 May 2023 21:26:50 -0600 Subject: Redux used to add ToDos WE MADE IT!! I don't know how, but it is functional. :'3 --- src/features/todo/ToDo.jsx | 233 +++++++++++++++++++++++++++++++++++++++++++ src/features/todo/reducer.js | 32 ++++++ 2 files changed, 265 insertions(+) create mode 100644 src/features/todo/ToDo.jsx create mode 100644 src/features/todo/reducer.js (limited to 'src/features/todo') diff --git a/src/features/todo/ToDo.jsx b/src/features/todo/ToDo.jsx new file mode 100644 index 0000000..c956ba1 --- /dev/null +++ b/src/features/todo/ToDo.jsx @@ -0,0 +1,233 @@ +import React, { useState } from "react"; +import { useSelector, useDispatch } from "react-redux"; +import { add_todo, select_todos } from "./reducer"; + +export function Reducer() { + const my_todos = useSelector(select_todos); + const dispatch = useDispatch(); + const [new_text, set_new_text] = useState(""); + const [new_done, set_new_done] = useState(false); + const [new_priority, set_new_priority] = useState("Low"); + + function handle_exit_modal() { + // https://stackoverflow.com/questions/27826381/clearing-form-input-fields-in-bootstrap + $("form").get(0).reset(); // Reset form + + set_new_text(""); + set_new_done(false); + set_new_priority("Low"); + + console.log("CLEANED."); + } + + return ( + <> +
+ +
+ + +
+ + + + + + + + + + + + {my_todos.map((item) => ( + + + + + + + + ))} + +
#NamePriorityDue DateActions
+
+ +
+
{item.text}{item.priority}{item.due_date} +
+ + +
+
+
+ + ); +} diff --git a/src/features/todo/reducer.js b/src/features/todo/reducer.js new file mode 100644 index 0000000..29422e9 --- /dev/null +++ b/src/features/todo/reducer.js @@ -0,0 +1,32 @@ +import { createSlice } from "@reduxjs/toolkit"; + +export const todo_slice = createSlice({ + name: "todo_list", + initialState: { + todos: [], + last_id: 0, + }, + + reducers: { + add_todo: (state, action) => { + state.todos = [ + ...state.todos, + { + id: ++state.last_id, + text: action.payload.text, + due_date: action.payload.due_date, + done: action.payload.done, + priority: action.payload.priority, + creation_date: action.payload.creation_date, + }, + ]; + console.log(state.todos); + }, + }, +}); + +export const { add_todo } = todo_slice.actions; + +export const select_todos = (state) => state.todo_list.todos; + +export default todo_slice.reducer; -- cgit v1.2.3