From 78374850f9b96e239c15db9bb37e565c734e4458 Mon Sep 17 00:00:00 2001 From: Jesper Wrang Date: Thu, 11 Jan 2018 00:20:01 +0100 Subject: [PATCH] Started on Reddits API, token fetching is done --- js/utils.js | 1 - src/js/components/StatusBox.js | 0 src/js/components/Thread.js | 21 ++++++++++++++--- src/js/index.js | 5 ++++ src/js/reddit/clientID.js | 3 +++ src/js/reddit/index.js | 42 ++++++++++++++++++++++++++++++++++ src/js/utils.js | 3 +++ 7 files changed, 71 insertions(+), 4 deletions(-) delete mode 100644 js/utils.js create mode 100644 src/js/components/StatusBox.js create mode 100644 src/js/reddit/clientID.js create mode 100644 src/js/reddit/index.js diff --git a/js/utils.js b/js/utils.js deleted file mode 100644 index 2f259b7..0000000 --- a/js/utils.js +++ /dev/null @@ -1 +0,0 @@ -s \ No newline at end of file diff --git a/src/js/components/StatusBox.js b/src/js/components/StatusBox.js new file mode 100644 index 0000000..e69de29 diff --git a/src/js/components/Thread.js b/src/js/components/Thread.js index 78e173b..e07e590 100644 --- a/src/js/components/Thread.js +++ b/src/js/components/Thread.js @@ -2,11 +2,27 @@ import React from 'react' import {prettyScore} from 'utils' export default class Thread extends React.Component { + constructor() { + super() + + this.state = { + url: '', + thumbnailWidth: 70, + thumbnailHeight: 70, + thumbnail: '', + position: 0, + score: 0 + + } + } + + + render () { const url = this.props.url.replace('reddit.com', 'removeddit.com') let thumbnail - let thumbnailWidth = this.props.thread.thumbnail_width ? this.props.thread.thumbnail_width * 0.5 : 70 - let thumbnailHeight = this.props.thread.thumbnail_height ? this.props.thread.thumbnail_height * 0.5 : 70 + const thumbnailWidth = this.props.thread.thumbnail_width ? this.props.thread.thumbnail_width * 0.5 : 70 + const thumbnailHeight = this.props.thread.thumbnail_height ? this.props.thread.thumbnail_height * 0.5 : 70 if(['self', 'default', 'image', 'nsfw'].includes(this.props.thread.thumbnail)) { thumbnail = @@ -17,7 +33,6 @@ export default class Thread extends React.Component { ) } - return (
diff --git a/src/js/index.js b/src/js/index.js index b17cce4..a9b9911 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -8,3 +8,8 @@ ReactDOM.render( , document.getElementById("app") ) + + +// import Reddit from './reddit' + +// Reddit.comments(1,2).then(() => console.log('lololololololol')) diff --git a/src/js/reddit/clientID.js b/src/js/reddit/clientID.js new file mode 100644 index 0000000..d586a81 --- /dev/null +++ b/src/js/reddit/clientID.js @@ -0,0 +1,3 @@ +// Change this to your own client ID: https://www.reddit.com/prefs/apps +// The app NEEDS TO BE an installed app and NOT a web apps +export default 'YSidAws9twqCZg' \ No newline at end of file diff --git a/src/js/reddit/index.js b/src/js/reddit/index.js new file mode 100644 index 0000000..89fa2a5 --- /dev/null +++ b/src/js/reddit/index.js @@ -0,0 +1,42 @@ +import clientID from './clientID' +import {json} from 'utils' + +// Reddit API +const init = { + headers: { + 'Authorization': '' + } +} + +let hasToken = false + +const tokenInit = { + headers: { + 'Authorization': 'Basic ' + btoa(clientID + ':'), + 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' + }, + method: 'POST', + body: 'grant_type='+encodeURIComponent('https://oauth.reddit.com/grants/installed_client')+'&device_id=DO_NOT_TRACK_THIS_DEVICE' +} + +const fetchToken = () => { + if(hasToken) { + return Promise.resolve() + } + + return fetch('https://www.reddit.com/api/v1/access_token', tokenInit) + .then(json) + .then(json => { + init.headers.Authorization = 'bearer ' + json.access_token; + hasToken = true + }) +} + +export default { + comments(subreddit, threadID) { + return fetchToken() + .then(() => console.log('hi')) + } + + +} \ No newline at end of file diff --git a/src/js/utils.js b/src/js/utils.js index 0c00d3d..6d56856 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -2,6 +2,9 @@ import SnuOwnd from 'libraries/snuownd.js' const markdown = SnuOwnd.getParser() +// JSON parsing for fetch +export const json = x => x.json() + // Parse comments export const parse = text => { return markdown.render(text)