Skip to content

Data Structure to factory pattern and entity pattern #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add factory to generate the model of tweets and remove duplicate tweets
  • Loading branch information
FelipeBarrosCruz committed May 5, 2017
commit a41d472ff3510dc7ff2d6aa4eba686b2f3aa2ce1
15 changes: 15 additions & 0 deletions 5-redux-react/src/js/factories/tweetFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Tweet from "../entities/tweetEntity"
import _ from "lodash"

export default class TweetFactory {
constructor (data) {
this.tweets = []
if (Array.isArray(data)) {
this.tweets = _.uniqBy(data, 'id').map(value => new Tweet(value))
}
}

getAll () {
return this.tweets
}
}
4 changes: 2 additions & 2 deletions 5-redux-react/src/js/reducers/tweetsReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Tweet from "../entities/tweetEntity"
import Tweets from "../factories/TweetFactory"

export default function reducer(state={
tweets: [],
Expand All @@ -19,7 +19,7 @@ export default function reducer(state={
...state,
fetching: false,
fetched: true,
tweets: action.payload.map(value => new Tweet(value)),
tweets: new Tweets(action.payload).getAll(),
}
}
case "ADD_TWEET": {
Expand Down