This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App component | |
// this component renders the entire app inside the Provider and Router | |
const App = (): JSX.Element => { | |
const classes = useStyles() | |
return ( | |
<Provider store={store}> | |
<Router> | |
<div className={classes.app}> | |
{/* nav bar */} | |
<Navbar /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cors from "cors" | |
import express from "express" | |
import { todoRouter } from "./routes/todo" | |
// create a new express app | |
// this will be used to add all routes and request handlers | |
const app = express() | |
// allow express to accept application/json requests | |
app.use(express.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* make a request to mark a to-do item as incomplete | |
*/ | |
const uncheckTodo = (todo: TodoItem): void => { | |
if (!todo._id) { | |
return | |
} | |
dispatch(uncompleteTodo(todo._id)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const uncompleteTodo = (guid: string) => async (dispatch: Dispatch) => { | |
dispatch({ type: "TODO_UNCOMPLETED_STARTED" }) | |
const response = await fetch(`${API_ENDPOINT}/todo/${guid}/uncomplete`, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
}).then(r => r.json()) | |
if (!!response && !!response?.todo) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* make a request to add a todo item | |
*/ | |
const addTodo = (): void => { | |
dispatch(createTodo({ | |
label: `Todo ${[...completeTodos, ...incompleteTodos].length}`, | |
})) | |
} | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface TodoUncompletedStarted { type: "TODO_UNCOMPLETED_STARTED" } | |
export interface TodoUncompleted { | |
type: "TODO_UNCOMPLETED" | |
payload: { | |
todo: TodoItem | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dispatch = useDispatch() | |
// complete and incomplete todos | |
// these are fetched from redux | |
const completeTodos = useSelector(selectCompleteTodos) | |
const incompleteTodos = useSelector(selectIncompleteTodos) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{/* complete items */} | |
{completeTodos.length > 0 && ( | |
<> | |
<div className={classes.header}> | |
<Typography component="h6" variant="h4">Completed Items</Typography> | |
</div> | |
{completeTodos.map(todo => ( | |
<p key={todo._id}>{todo.label}</p> | |
))} | |
</> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{/* complete items */} | |
{completeTodos.length > 0 && ( | |
<> | |
<div className={classes.header}> | |
<Typography component="h6" variant="h4">Completed Items</Typography> | |
</div> | |
{completeTodos.map(todo => ( | |
<div key={todo._id} className={classes.todoItem}> | |
<IconButton color="inherit" onClick={() => uncheckTodo(todo)}> | |
<ClearIcon /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reducer | |
const reducer = (state: TodoState = defaultState, action: TodoAction): TodoState => { | |
switch (action.type) { | |
... | |
case "TODO_UNCOMPLETED_STARTED": { | |
return { ...state, isUncompletingTodo: true } | |
} | |
case "TODO_UNCOMPLETED": { |
OlderNewer