Move a lot of logic of out components and into modules

This commit is contained in:
Jesper Wrang 2018-02-03 18:19:19 +01:00
parent 98aaeaa647
commit 1ab40a5b6a
7 changed files with 60 additions and 75 deletions

View file

@ -53,7 +53,7 @@ __)|_(_||||||(_|| ) |/\|(_|_) | \ |(_)| )|_
`':::_:' -- '' -'-' `':_::::'`
-->
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title>Removeddit</title>
<meta charset="UTF-8">

View file

@ -8,10 +8,10 @@ import {
import Header from 'components/Header'
import { About, Thread, Subreddit } from 'pages'
export default props => (
export default () => (
<BrowserRouter basename={__dirname}>
<React.Fragment>
<Header status={props.status} />
<Header />
<div className='main'>
<Switch>
<Route exact path='/' component={About} />

View file

@ -1,7 +1,7 @@
import { json, toBase10, toBase36, chunk, flatten } from 'utils'
const baseURL = 'https://elastic.pushshift.io'
const threadURL = `${baseURL}/rs/submissions/_search?source=`
const postURL = `${baseURL}/rs/submissions/_search?source=`
const commentURL = `${baseURL}/rc/comments/_search?source=`
const commentIDsURL = 'https://api.pushshift.io/reddit/submission/comment_ids/'
@ -11,7 +11,7 @@ export const getCommentIDs = threadID => (
.then(results => results.data)
)
export const getThread = threadID => {
export const getPost = threadID => {
const elasticQuery = {
query: {
term: {
@ -21,12 +21,12 @@ export const getThread = threadID => {
}
return (
fetch(threadURL + JSON.stringify(elasticQuery))
fetch(postURL + JSON.stringify(elasticQuery))
.then(json)
.then(jsonData => jsonData.hits.hits[0]._source)
.then(thread => {
thread.id = toBase36(thread.id)
return thread
.then(post => {
post.id = toBase36(post.id)
return post
})
)
}
@ -55,7 +55,7 @@ const fetchComments = commentIDs => {
comment._source.id = toBase36(comment._id)
if (!comment._source.parent_id) {
console.log('MISSING PARENT ID')
console.error('MISSING PARENT ID')
}
comment._source.parent_id = toBase36(comment._source.parent_id)

View file

@ -98,3 +98,8 @@ const handleMoreChildren = (threadID, commentID, results) => (
const debug = results => {
console.log(JSON.parse(JSON.stringify(results)))
}
// GET REMAINING COMMENTS
export const getRemainingComments = commentIDs => {
}

View file

@ -1,36 +1,17 @@
// import React from 'react'
// import ReactDOM from 'react-dom'
// import { Provider } from 'react-redux'
// import { store } from 'state'
// import App from 'App'
// // import { setStatusLoading } from 'state'
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import { store } from 'state'
import App from 'App'
// import { setStatusLoading } from 'state'
// import '../sass/main.sass'
import '../sass/main.sass'
// ReactDOM.render(
// <Provider store={store}>
// <App />
// </Provider>,
// document.getElementById('app')
// )
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app')
)
// store.dispatch(setStatusLoading('hi'))
import { getCommentIDs } from 'api/reddit'
import { getCommentIDs as getAllCommentIDs } from 'api/pushshift'
import { difference } from './utils/index'
Promise.all([
getAllCommentIDs('6z1hch'),
getCommentIDs('TwoXChromosomes', '6z1hch'),
])
.then(resp => console.log(resp[1].length, difference(resp[0], resp[1])))
// getCommentIDs('6z1hch')
// getThread('6z1hch').then(console.log)
// getComments(['dmt88s6', 'dmtqxj8', 'dmt1euz']).then(console.log)
// .then(ids => console.log(unique(ids)))

View file

@ -1,23 +1,24 @@
import React from 'react'
import ThreadHead from 'components/ThreadHead'
import Post from 'components/Post'
import CommentSection from 'components/CommentSection'
import {
getThread,
getPost,
getCommentIDs,
extractHead,
} from 'reddit'
} from 'api/reddit'
import {
getThread as getRemovedThread,
getPost as getRemovedPost,
getCommentIDs as getAllCommentIDs,
} from 'pushshift'
getComments as getRemovedComments,
} from 'api/pushshift'
import { isDeleted } from 'utils'
import { difference } from '../utils/index'
export default class Thread extends React.Component {
constructor(props) {
super(props)
this.state = {
thread: {},
post: {},
comments: [],
}
}
@ -25,44 +26,42 @@ export default class Thread extends React.Component {
componentDidMount() {
const { subreddit, threadID } = this.props.match.params
console.timeEnd('scripts loaded')
let pushshiftCommentIDs
Promise.all([
// OUTDATED
// Get thread from reddit
// getThread(subreddit, threadID)
// .then(extractHead)
// .then(thread => {
// this.setState({ thread })
// // Fetch the thread from pushshift if it was deleted/removed
// if (isDeleted(thread.selftext)) {
// getRemovedThread(threadID)
// .then(removedThread => {
// removedThread.removed = true
// this.setState({ thread: removedThread })
// })
// }
// })
// // Get comment ids from reddit
// .then(() => getCommentIDs(subreddit, threadID)),
getPost(subreddit, threadID)
.then(post => {
this.setState({ post })
// Fetch the thread from pushshift if it was deleted/removed
if (isDeleted(post.selftext)) {
// getRemovedPost(threadID)
// .then(removedPost => {
// removedPost.removed = true
// this.setState({ post: removedPost })
// })
}
})
// Get comment ids from reddit
.then(() => getCommentIDs(subreddit, threadID)),
// Get comment ids from pushshift
getAllCommentIDs(threadID)
.then(commentIDs => { pushshiftCommentIDs = commentIDs }),
getAllCommentIDs(threadID),
])
.then(() => {
// .then(console.log)
.then(results => {
const foundIDs = results[0]
const allIDs = results[1]
const removedIDs = difference(allIDs, foundIDs)
// Get removed comments from pushshift
return getRemovedComments(removedIDs)
})
}
render() {
return (
<div>
<ThreadHead {...this.state.thread} />
<CommentSection root={this.state.thread.id} comments={this.state.comments} />
<Post {...this.state.post} />
<CommentSection root={this.state.post.id} comments={this.state.comments} />
</div>
)
}