Skip to content

Commit d0af57b

Browse files
moved dispatch to components
1 parent f3b6523 commit d0af57b

File tree

6 files changed

+62
-61
lines changed

6 files changed

+62
-61
lines changed

src/App.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import Home from './pages/home/home';
44
import Switch from 'react-switch';
55
import { FaSun, FaMoon } from 'react-icons/fa';
66
import { useReducer, useState } from 'react';
7-
import { NoteType } from './components/note/note-type';
87
import { StateContext, StateType } from './context/state/state';
98
import { Notes } from './components/note/data';
9+
import { ADD_NOTE, DELETE_NOTE, SET_EDIT_MODE, SET_NOTE_FOR_EDIT, UPDATE_NOTE } from './actions';
1010

1111
function App() {
1212
const [theme, setTheme] = useState('light');
@@ -15,20 +15,20 @@ function App() {
1515
const [state, dispatch] = useReducer(
1616
(state: StateType, action: { type: string; payload: any }) => {
1717
switch (action.type) {
18-
case 'SET_EDIT_MODE':
18+
case SET_EDIT_MODE:
1919
return { ...state, editMode: action.payload };
20-
case 'SET_NOTE_FOR_EDIT':
20+
case SET_NOTE_FOR_EDIT:
2121
return { ...state, noteToBeEdited: action.payload };
22-
case 'ADD_NOTE':
22+
case ADD_NOTE:
2323
return { ...state, notes: [action.payload, ...state.notes] };
24-
case 'DELETE_NOTE':
24+
case DELETE_NOTE:
2525
const index = state.notes.findIndex(
2626
(note) => note.id === action.payload
2727
);
2828
let editedNotes = [...state.notes];
2929
editedNotes.splice(index, 1);
3030
return { ...state, notes: editedNotes };
31-
case 'UPDATE_NOTE':
31+
case UPDATE_NOTE:
3232
const indexUpdated = state.notes.findIndex(
3333
(note) => note.id === action.payload.id
3434
);
@@ -49,7 +49,6 @@ function App() {
4949
} else {
5050
setTheme('light');
5151
}
52-
console.log(checked, check);
5352
};
5453

5554
return (

src/actions/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const SET_EDIT_MODE = 'SET_EDIT_MODE';
2+
export const SET_NOTE_FOR_EDIT = 'SET_NOTE_FOR_EDIT';
3+
export const ADD_NOTE = 'ADD_NOTE';
4+
export const DELETE_NOTE = 'DELETE_NOTE';
5+
export const UPDATE_NOTE = 'UPDATE_NOTE';
6+
export type ALL_ACTIONS = 'SET_EDIT_MODE' | 'SET_NOTE_FOR_EDIT' | 'ADD_NOTE' | 'DELETE_NOTE' | 'UPDATE_NOTE'

src/components/add-note/add-note.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@ import { v4 as uuidv4 } from 'uuid';
55
import Card from '../card/card';
66
import { ThemeContext } from '../../context/theme/theme';
77
import { StateContext } from '../../context/state/state';
8+
import { ADD_NOTE, UPDATE_NOTE, SET_EDIT_MODE } from '../../actions';
89

9-
type AddNoteProps = {
10-
addNote: (note: NoteType) => void;
11-
updateNote: (updatedNote: NoteType) => void;
12-
};
10+
type AddNoteProps = {};
1311

1412
function AddNote(props: AddNoteProps) {
1513
const [text, setText] = useState('');
1614
const [priority, setPriority] = useState<Priority>('low');
1715
const theme = useContext(ThemeContext);
18-
const {state,dispatch} = useContext(StateContext);
19-
16+
const { state, dispatch } = useContext(StateContext);
17+
2018
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2119
setText(e.target.value);
2220
};
2321

24-
2522
const setNoteContent = (note: NoteType) => {
2623
setText(note.text);
2724
setPriority(note.priority);
@@ -37,16 +34,23 @@ function AddNote(props: AddNoteProps) {
3734
e.preventDefault();
3835
if (state.editMode) {
3936
state.noteToBeEdited &&
40-
props.updateNote({
41-
text,
42-
priority,
43-
id: state.noteToBeEdited.id,
37+
dispatch({
38+
type: UPDATE_NOTE,
39+
payload: {
40+
text,
41+
priority,
42+
id: state.noteToBeEdited.id,
43+
},
4444
});
45+
dispatch({ type: SET_EDIT_MODE, payload: false });
4546
} else {
46-
props.addNote({
47-
text,
48-
priority,
49-
id: uuidv4(),
47+
dispatch({
48+
type: ADD_NOTE,
49+
payload: {
50+
text,
51+
priority,
52+
id: uuidv4(),
53+
},
5054
});
5155
}
5256

@@ -59,7 +63,7 @@ function AddNote(props: AddNoteProps) {
5963
};
6064

6165
return (
62-
<Card bgColor={theme==='dark'? '#333':'#ddd'} height="2" padding="1">
66+
<Card bgColor={theme === 'dark' ? '#333' : '#ddd'} height="2" padding="1">
6367
<form className="add-note">
6468
<input type="text" onChange={handleChange} value={text} />
6569
<select onChange={handleSelect} value={priority}>

src/components/note/note.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
import './note.css';
22

3-
import { ColorLight, ColorDark, Priority } from './note-type';
3+
import { ColorLight, ColorDark, Priority, NoteType } from './note-type';
44
import Card from '../card/card';
55
import { FaTrash, FaEdit } from 'react-icons/fa';
66
import { useContext } from 'react';
77
import { ThemeContext } from '../../context/theme/theme';
8+
import { StateContext } from '../../context/state/state';
9+
import { DELETE_NOTE, SET_EDIT_MODE, SET_NOTE_FOR_EDIT } from '../../actions';
810

911
type NoteProps = {
1012
id: string;
1113
text: string;
1214
priority?: Priority;
13-
editNote: (id: string) => void;
14-
deleteNote: (id: string) => void;
15+
note: NoteType
1516
};
1617

1718
function Note(props: NoteProps) {
1819
const theme = useContext(ThemeContext);
20+
const {dispatch} = useContext(StateContext);
21+
22+
const editNote = (note:NoteType) => {
23+
dispatch({type:SET_EDIT_MODE,payload:true});
24+
dispatch({type:SET_NOTE_FOR_EDIT,payload:note});
25+
};
1926

2027
return (
2128
<Card
22-
bgColor={ theme ==='dark'? props.priority && ColorDark[props.priority]:
23-
props.priority && ColorLight[props.priority]}
29+
bgColor={
30+
theme === 'dark'
31+
? props.priority && ColorDark[props.priority]
32+
: props.priority && ColorLight[props.priority]
33+
}
2434
height="2"
2535
padding="1"
2636
>
2737
<>
2838
<div>{props.text}</div>
2939
<div className="right-corner">
30-
<FaEdit onClick={() => props.editNote(props.id)}></FaEdit>
31-
<FaTrash onClick={() => props.deleteNote(props.id)}></FaTrash>
40+
<FaEdit onClick={() => editNote(props.note)}></FaEdit>
41+
<FaTrash
42+
onClick={() => dispatch({ type: DELETE_NOTE, payload: props.id })}
43+
></FaTrash>
3244
</div>
3345
</>
3446
</Card>

src/context/state/state.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { createContext } from 'react';
2+
import { ALL_ACTIONS } from '../../actions';
23
import { NoteType } from '../../components/note/note-type';
34
export type StateType = {
45
notes: NoteType[];
56
editMode: boolean;
67
noteToBeEdited: NoteType | null;
78
};
9+
export type ActionType = {
10+
type: ALL_ACTIONS,
11+
payload: any
12+
}
13+
814
export const StateContext = createContext<{
915
state: StateType;
10-
dispatch: any
11-
}>({} as { state: StateType; dispatch:any });
16+
dispatch: (action:ActionType)=>void
17+
}>({} as { state: StateType; dispatch:(action:ActionType)=>void });

src/pages/home/home.tsx

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,25 @@ import './home.css';
22
import Note from '../../components/note/note';
33
import AddNote from '../../components/add-note/add-note';
44
import { useContext } from 'react';
5-
import { NoteType } from '../../components/note/note-type';
65
import { ThemeContext } from '../../context/theme/theme';
76
import { StateContext } from '../../context/state/state';
87

98
function Home() {
109
const theme = useContext(ThemeContext);
11-
const {state, dispatch} = useContext(StateContext);
10+
const {state} = useContext(StateContext);
1211

13-
const addNote = (note: NoteType) => {
14-
dispatch({type:'ADD_NOTE',payload:note})
15-
};
16-
17-
const updateNote = (updatedNote: NoteType) => {
18-
dispatch({type:'UPDATE_NOTE',payload:updatedNote});
19-
dispatch({type:'SET_EDIT_MODE',payload:false});
20-
};
21-
22-
const editNote = (id: string) => {
23-
console.log('edit', id);
24-
const note = state.notes.find((note) => note.id === id);
25-
dispatch({type:'SET_EDIT_MODE',payload:true});
26-
if (note) {
27-
dispatch({type:'SET_NOTE_FOR_EDIT',payload:note});
28-
}
29-
};
30-
31-
const deleteNote = (id: string) => {
32-
dispatch({type:'DELETE_NOTE',payload:id})
33-
};
3412
return (
3513
<div className={`home ${theme}`}>
3614
<h2>Notes App [{state.notes.length}]</h2>
37-
<AddNote
38-
addNote={addNote}
39-
updateNote={updateNote}
40-
></AddNote>
15+
<AddNote></AddNote>
4116
<div>
4217
{state.notes.map((note) => (
4318
<Note
4419
key={note.id}
4520
id={note.id}
4621
priority={note.priority}
4722
text={note.text}
48-
editNote={editNote}
49-
deleteNote={deleteNote}
23+
note={note}
5024
></Note>
5125
))}
5226
</div>

0 commit comments

Comments
 (0)