File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 11import React from 'react'
2+ import { nanoid } from '@reduxjs/toolkit'
3+
24import { 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/
610interface AddPostFormFields extends HTMLFormControlsCollection {
@@ -12,6 +16,8 @@ interface AddPostFormElements extends HTMLFormElement {
1216}
1317
1418export 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 }
You can’t perform that action at this time.
0 commit comments