Skip to content

Commit d395147

Browse files
committed
Dispatch postAdded when "Save Post" is clicked
1 parent 173c0a3 commit d395147

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/features/posts/AddPostForm.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react'
2+
import { nanoid } from '@reduxjs/toolkit'
3+
24
import { useAppDispatch } from '@/app/hooks'
35

6+
import { type Post, postAdded } from './postsSlice'
7+
48
// TS types for the input fields
59
// See: https://epicreact.dev/how-to-type-a-react-form-on-submit-handler/
610
interface AddPostFormFields extends HTMLFormControlsCollection {
@@ -12,6 +16,8 @@ interface AddPostFormElements extends HTMLFormElement {
1216
}
1317

1418
export const AddPostForm = () => {
19+
const dispatch = useAppDispatch()
20+
1521
const handleSubmit = (e: React.FormEvent<AddPostFormElements>) => {
1622
// Prevent server submission
1723
e.preventDefault()
@@ -20,7 +26,12 @@ export const AddPostForm = () => {
2026
const title = elements.postTitle.value
2127
const content = elements.postContent.value
2228

23-
console.log('Values: ', { title, content })
29+
const newPost: Post = {
30+
id: nanoid(),
31+
title,
32+
content,
33+
}
34+
dispatch(postAdded(newPost))
2435

2536
e.currentTarget.reset()
2637
}

0 commit comments

Comments
 (0)