Tweets Module Clean Up:
In this step, spilt /twittme-web/src/tweets/components.js into multiple files.
move “ActionBtn” out:
- create a /twittme-web/src/tweets/buttons.js
- cut ActionBtn() from /twittme-web/src/tweets/components.js to here
- add import modules ```javascript import React from “react”;
import { apiTweetAction } from “./lookup”;
export function ActionBtn(props) { const { tweet, action, didPerformAction } = props; const likes = tweet.likes ? tweet.likes : 0;
const className = props.className? props.className: "btn btn-primary btn-sm";const actionDisplay = action.display ? action.display : "Action"; //ensure that we have an actionconst handleActionBackendEvent = (response, status) => {console.log(response, status);if ((status === 200 || status === 201) && didPerformAction) {didPerformAction(response, status);}};const handleClick = (event) => {event.preventDefault();apiTweetAction(tweet.id, action.type, handleActionBackendEvent);};const display =action.type === "like" ? `${likes} ${actionDisplay}` : actionDisplay;return (<button className={className} onClick={handleClick}>{display}</button>);
}
- import ActionBtn() in /twittme-web/src/tweets/components.js```javascript//...import {apiTweetCreate,apiTweetList,//apiTweetAction} from "./lookup";import {ActionBtn} from "./buttons" //new//...
- import and export ActionBtn() in /twittme-web/src/tweets/index.js ```javascript import { //ActionBtn, Tweet, TweetsList, TweetsComponent, } from “./components”;
import { ActionBtn } from “./buttons”; //new
export { ActionBtn, Tweet, TweetsList, TweetsComponent };
<a name="t4g5x"></a>#### move list and detail outside:- create /twittme-web/src/tweets/list.js and /twittme-web/src/tweets/detail.js to send out more functions- detail.js- **cut** ParentTweet() and Tweet() from /twittme-web/src/tweets/components.js to here- import required modules```javascriptimport React, { useState } from "react";import { ActionBtn } from "./buttons";export function ParentTweet(props) {const { tweet } = props;return tweet.parent ? (<div className="row"><div className="col-11 mx-auto p-3 border rounded"><p className="mb-0 text-muted small">Retweet</p><Tweet hideActions className={" "} tweet={tweet.parent} /></div></div>) : null;}export function Tweet(props) {const { tweet, didRetweet, hideActions } = props;const [actionTweet, setActionTweet] = useState(props.tweet ? props.tweet : null);const className = props.className? props.className: "col-10 mx-auto col-md-6";const handlePerformAction = (newActionTweet, status) => {if (status === 200) {setActionTweet(newActionTweet);} else if (status === 201) {if (didRetweet) {didRetweet(newActionTweet);}}};return (<div className={className}><div><p>{tweet.id} - {tweet.content}</p><ParentTweet tweet={tweet} /></div>{actionTweet && hideActions !== true && (<div className="btn btn-group"><ActionBtntweet={actionTweet}didPerformAction={handlePerformAction}action={{ type: "like", display: "Likes" }}/><ActionBtntweet={actionTweet}didPerformAction={handlePerformAction}action={{ type: "unlike", display: "Unlike" }}/><ActionBtntweet={actionTweet}didPerformAction={handlePerformAction}action={{ type: "retweet", display: "Retweet" }}/></div>)}</div>);}
- list.js
- cut TweetsList() from /twittme-web/src/tweets/components.js to here
- import required modules ```javascript import React, { useEffect, useState } from “react”;
import { apiTweetList } from “./lookup”;
import { Tweet } from “./detail”;
export function TweetsList(props) { //console.log(props.username) const [tweetsInit, setTweetsInit] = useState([]); const [tweets, setTweets] = useState([]);
const [tweetsDidSet, setTweetsDidSet] = useState(false);
useEffect(() => { const final = […props.newTweets].concat(tweetsInit); //[…content] means a new list with the content if (final.length !== tweets.length) { setTweets(final); } }, [props.newTweets, tweets, tweetsInit]);
useEffect(() => { if (tweetsDidSet === false) { const handleTweetListLookup = (response, status) => { if (status === 200) { setTweetsInit(response); setTweetsDidSet(true); } else { alert(“There was an error”); } }; apiTweetList(props.username, handleTweetListLookup); } }, [tweetsInit, tweetsDidSet, setTweetsDidSet, props.username]);
//new const handleDidRetweet = (newTweet) => { const updateTweetsInit = […tweetsInit]; //grabbing tweetsInit list updateTweetsInit.unshift(newTweet); //add newTweet to the beginning of updateTweetsInit setTweetsInit(updateTweetsInit); //update status const updateFinalTweets = […tweets]; //grabbing tweets list updateFinalTweets.unshift(tweets); //add tweets to the beginning of updateFinalTweets setTweets(updateFinalTweets); //update status };
return tweets.map((item, index) => {
return (
- import and export functions in /twittme-web/src/tweets/index.js```javascriptimport {//Tweet,//TweetsList,TweetsComponent,} from "./components";import { ActionBtn } from "./buttons";import {Tweet} from "./detail"; //newimport {TweetsList} from "./list"; //newexport { ActionBtn, Tweet, TweetsList, TweetsComponent };
- and remove some imports in /twittme-web/src/tweets/components.js ```javascript import React, { //useEffect, useState, } from “react”;
import { apiTweetCreate, //apiTweetList } from “./lookup”;
//import { ActionBtn } from “./buttons”;
import { TweetsList } from “./list”;
<a name="J1hjn"></a>####<a name="42o4W"></a>#### move create outside:- in /twittme-web/src/tweets/components.js- split TweetsComponent() into TweetsComponent() and TweetCreate()```javascriptimport React, { useState } from "react";import { apiTweetCreate } from "./lookup";import { TweetsList } from "./list";function TweetCreate(props) {const textAreaRef = React.createRef();const { didTweet } = props;//const [newTweets, setNewTweets] = useState([]);//const canTweet = props.canTweet === "false" ? false : true;const handleBackendUpdate = (response, status) => {//let tempNewTweets = [...newTweets];if (status === 201) {//tempNewTweets.unshift(response);//setNewTweets(tempNewTweets);//didTweet(tempNewTweets);didTweet(response);} else {console.log(response);alert("An error occured please try again");}};const handleSubmit = (event) => {event.preventDefault();const newVal = textAreaRef.current.value;console.log("new value", newVal);apiTweetCreate(newVal, handleBackendUpdate);textAreaRef.current.value = ""; //after the submission, clear the textarea};return (//<div className={"col-12 mb-3"}><div className={props.className}><form onSubmit={handleSubmit}><textarearef={textAreaRef}required={true}className="form-control"name="tweet"></textarea><button type="submit" className="btn btn-primary my-3">Tweet</button></form></div>);}export function TweetsComponent(props) {//const textAreaRef = React.createRef();const [newTweets, setNewTweets] = useState([]);//passed canTweet is a string, not a booleanconst canTweet = props.canTweet === "false" ? false : true;//const handleBackendUpdate = (response, status) => {const handleNewTweet = (newTweet) => {let tempNewTweets = [...newTweets];tempNewTweets.unshift(newTweet);setNewTweets(tempNewTweets);/*if (status === 201) {tempNewTweets.unshift(response);setNewTweets(tempNewTweets);} else {console.log(response);alert("An error occured please try again");}*/};/*const handleSubmit = (event) => {event.preventDefault();const newVal = textAreaRef.current.value;console.log("new value", newVal);apiTweetCreate(newVal, handleBackendUpdate);textAreaRef.current.value = ""; //after the submission, clear the textarea};*/return (<div className={props.className}>{canTweet === true && (<TweetCreate didTweet={handleNewTweet} className="col-12 mb-3" />)}<TweetsList newTweets={newTweets} {...props} /></div>);/*return (<div className={props.className}>{canTweet === true && (<div className="col-12 mb-3"><form onSubmit={handleSubmit}><textarearef={textAreaRef}required={true}className="form-control"name="tweet"></textarea><button type="submit" className="btn btn-primary my-3">Tweet</button></form></div>)}<TweetsList newTweets={newTweets} {...props} /></div>);*/}
- create /twittme-web/src/tweets/create.js
- cut TweetCreate() from /twittme-web/src/tweets/components.js
- import necessary modules ```javascript import React from “react”;
import { apiTweetCreate } from “./lookup”;
//function TweetCreate(props) { export function TweetCreate(props) { const textAreaRef = React.createRef();
const { didTweet } = props;
const handleBackendUpdate = (response, status) => { if (status === 201) { didTweet(response); } else { console.log(response); alert(“An error occured please try again”); } };
const handleSubmit = (event) => { event.preventDefault(); const newVal = textAreaRef.current.value;
console.log("new value", newVal);apiTweetCreate(newVal, handleBackendUpdate);textAreaRef.current.value = ""; //after the submission, clear the textarea
};
return (
