-
-
Save codecademydev/89d724f948a00e6410ed5621d6f46f77 to your computer and use it in GitHub Desktop.
Codecademy export
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 React, { useState } from 'react'; | |
import { useDispatch, useSelector } from 'react-redux'; | |
import { | |
createCommentIsPending, | |
postCommentForArticleId, | |
} from '../features/comments/commentsSlice'; | |
export default function CommentForm({ articleId }) { | |
const dispatch = useDispatch(); | |
const [comment, setComment] = useState(''); | |
// Declare isCreatePending here. | |
const handleSubmit = (e) => { | |
e.preventDefault(); | |
// dispatch your asynchronous action here! | |
console.log('comment', comment) | |
dispatch(postCommentForArticleId({articleId,comment})) | |
setComment(''); | |
}; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<label htmlFor='comment' className='label'> | |
Add Comment: | |
</label> | |
<div id='input-container'> | |
<input | |
id='comment' | |
value={comment} | |
onChange={(e) => setComment(e.currentTarget.value)} | |
type='text' | |
/> | |
<button | |
className='comment-button' | |
> | |
Submit | |
</button> | |
</div> | |
</form> | |
); | |
} |
Raw
CommentList.js
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
Show hidden characters
import React from 'react'; | |
import Comment from './Comment'; | |
export default function CommentList({ comments }) { | |
if (!comments) { | |
return null; | |
} | |
return ( | |
<ul className='comments-list'> | |
{comments.map((comment,key) => { | |
return <Comment key={key} comment={comment}/> | |
})} | |
</ul> | |
); | |
} |
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 React, { useEffect } from 'react'; | |
import { useDispatch, useSelector } from 'react-redux'; | |
import { | |
loadCommentsForArticleId, | |
selectComments, | |
isLoadingComments, | |
} from '../comments/commentsSlice'; | |
import { selectCurrentArticle } from '../currentArticle/currentArticleSlice'; | |
import CommentList from '../../components/CommentList'; | |
import CommentForm from '../../components/CommentForm'; | |
const Comments = () => { | |
const dispatch = useDispatch(); | |
const article = useSelector(selectCurrentArticle); | |
// Declare additional selected data here. | |
const comments = useSelector(selectComments); | |
const commentsAreLoading = useSelector(isLoadingComments); | |
// Dispatch loadCommentsForArticleId with useEffect here. | |
useEffect(()=> { | |
if(article) dispatch(loadCommentsForArticleId(article.id)) | |
},[dispatch,article]) | |
const commentsForArticleId = article ? comments[article.id] : []; | |
if (commentsAreLoading) return <div>Loading Comments</div>; | |
if (!article) return null; | |
return ( | |
<div className='comments-container'> | |
<h3 className='comments-title'>Comments</h3> | |
<CommentList comments={commentsForArticleId} /> | |
<CommentForm articleId={article.id} /> | |
</div> | |
); | |
}; | |
export default Comments; |
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 createAsyncThunk and createSlice here. | |
import {createAsyncThunk,createSlice} from '@reduxjs/toolkit'; | |
// Create loadCommentsForArticleId here. | |
export const loadCommentsForArticleId = createAsyncThunk( | |
'comments/loadCommentsForArticleId', | |
async (id) => { | |
const response = await fetch(`api/articles/${id}/comments`); | |
const json = await response.json(); | |
return json; | |
} | |
) | |
// Create postCommentForArticleId here. | |
export const postCommentForArticleId = createAsyncThunk( | |
'comments/postCommentForArticleId', | |
async ({articleId, comment}) => { | |
const requestBody = JSON.stringify({comment: comment}) | |
const response = await fetch(`api/articles/${articleId}/comments`) | |
const json = await response.json() | |
return json | |
} | |
) | |
export const commentsSlice = createSlice({ | |
name: 'comments', | |
initialState: { | |
// Add initial state properties here. | |
byArticleId: { | |
// 123: ['Great article', 'I disagree.'], | |
}, | |
isLoadingComments: false, | |
failedToLoadComments: false, | |
createCommentIsPending: false, | |
failedToCreateComment: false, | |
}, | |
// Add extraReducers here. | |
extraReducers: (builder) => { | |
// loadCommentsForArticleId | |
builder | |
.addCase(loadCommentsForArticleId.pending, (state) => { | |
state.isLoadingComments = true | |
state.failedToLoadComments = false | |
}) | |
.addCase(loadCommentsForArticleId.fulfilled, (state,action) => { | |
state.isLoadingComments = false | |
state.failedToLoadComments = false | |
state.byArticleId[action.payload.articleId] = action.payload.comments | |
}) | |
.addCase(loadCommentsForArticleId.rejected, (state) => { | |
state.isLoadingComments = false | |
state.failedToLoadComments = true | |
}) | |
// postCommentForArticleId | |
builder | |
.addCase(postCommentForArticleId.pending, (state) => { | |
state.createCommentIsPending = true | |
state.failedToCreateComment = false | |
}) | |
.addCase(postCommentForArticleId.fulfilled, (state, action) => { | |
state.createCommentIsPending = false | |
state.failedToCreateComment = false | |
state.byArticleId[action.payload.articleId].push(action.payload) | |
}) | |
.addCase(postCommentForArticleId.rejected, (state) => { | |
state.createCommentIsPending = false | |
state.failedToCreateComment = true | |
}) | |
} | |
}); | |
export const selectComments = (state) => state.comments.byArticleId; | |
export const isLoadingComments = (state) => state.comments.isLoadingComments; | |
export const createCommentIsPending = (state) => state.comments.createCommentIsPending; | |
export default commentsSlice.reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for this ! I was struggling in a few areas on this project