This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
186 lines (167 loc) · 5.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React, { Component } from 'react';
import 'whatwg-fetch';
import * as util from './util.js';
import 'babel/polyfill';
class App extends Component {
constructor(...args) {
super(...args);
this.state = {};
this.state.error = false;
this.state.userImageUrl = null;
this.state.loggedIn = false;
this.state.userDetails = {};
this.generateImage = this.generateImage.bind(this);
this.onError = this.onError.bind(this);
this.onLogin = this.onLogin.bind(this);
this.postToFacebook = this.postToFacebook.bind(this);
this.requestPublishPermissions = this.requestPublishPermissions.bind(this);
}
generateImage() {
this.state.userImageUrl = null;
this.state.error = false;
const profileImageUrl = encodeURIComponent(`https://graph.facebook.com/${this.state.userDetails.userID}/picture?width=800&height=800`);
util.mergeImages(`/proxy-image-app-np?url=${profileImageUrl}&rand=${Math.random()}`, 'flag.png')
.then((image) => {
this.state.userImageUrl = image;
this.setState(this.state);
})
.catch(this.onError);
}
componentWillMount() {
FB.init({
appId : '519129691585992',
xfbml : true,
version : 'v2.4'
});
}
onError(err) {
console.log(err);
this.state.error = err.message;
this.setState(this.state);
}
postToFacebook() {
this.requestPublishPermissions()
.then(() => {
util.postToFacebook({
accessToken: this.state.userDetails.accessToken,
image: util.dataURItoBlob(this.state.userImageUrl),
message: 'Get your pic with Nepalese flag from http://sundar-nepal.subash.me',
userID: this.state.userDetails.userID
}).then(() => {
alert('Posted Successfully');
}).catch(this.onError);
})
.catch(this.onError)
}
requestPublishPermissions() {
return new Promise((resolve, reject) => {
FB.login((response) => {
if (response.authResponse) {
this.state.userDetails = {
userID : response.authResponse.userID,
accessToken: response.authResponse.accessToken
};
resolve();
} else {
reject(new Error('Failed to request permission'));
}
}, { scope: 'publish_actions'});
});
}
onLogin(details) {
this.state.userDetails = details;
this.state.loggedIn = true;
this.generateImage();
this.setState(this.state);
}
render() {
const userImageUrl = this.state.userImageUrl;
const error = this.state.error;
const loggedIn = this.state.loggedIn;
const defaultImageUrl = 'default.jpg';
let imageBoxContent = null;
if(error) {
imageBoxContent = (
<div>
<div className="alert alert-danger">{error.length ? error : 'An error occurred.'}</div>
<Login onLogin={this.onLogin}/>
</div>
);
} else if(!loggedIn){
imageBoxContent = <Login onLogin={this.onLogin}/>;
} else if(loggedIn && !userImageUrl){
imageBoxContent = (
<div className="loading">Please Wait</div>
);
} else {
imageBoxContent = (
<div className="post">
<a className="btn btn-default" href={userImageUrl} download="profile-nepal.jpg">Download</a>
</div>
);
}
return (
<div className="container">
<ImageBox src={userImageUrl || defaultImageUrl}>{imageBoxContent}</ImageBox>
<div className="social">
<iframe src="https://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fsundar-nepal.subash.me&send=false&layout=button_count&width=120&show_faces=false&font&colorscheme=light&action=like&height=21" scrolling="no" frameBorder="0" style={{ border: 'none', overflow: 'hidden', width: 100, height: 21 }} allowTransparency="true"></iframe>
</div>
<div className="alert alert-info footer-info">
<ul>
<li>Made by, <a href="http://twitter.com/sbspk"><i className="fa fa-twitter"></i> @sbspk</a>, <a href="https://facebook.com/return.undefined"><i className="fa fa-facebook"></i> Subash Pathak</a></li>
<li>Tested only in latest version of Chrome and Firefox. </li>
<li>Code for this app is available on <a href="https://github.com/Subash/Sundar-Nepal" target="_blank">Github</a> </li>
</ul>
</div>
</div>
)
}
}
class ImageBox extends Component {
render() {
return (
<div className="ImageBox">
<div className="ImageBox__image">
<img src={this.props.src} width="500" height="500"/>
</div>
<div className="ImageBox__inner">{this.props.children}</div>
</div>
)
}
}
class Login extends Component {
login() {
FB.login((response) => {
if (response.authResponse) {
this.props.onLogin({
userID : response.authResponse.userID,
accessToken: response.authResponse.accessToken
});
} else {
console.log('Not Logged In');
}
});
}
checkLogin() {
FB.getLoginStatus( (response)=> {
if(response.status === 'connected') {
this.props.onLogin({
userID : response.authResponse.userID,
accessToken: response.authResponse.accessToken
});
} else {
this.login();
}
});
}
render() {
return (
<div>
<button onClick={this.checkLogin.bind(this)} className="btn btn-default">Create from Facebook</button>
{' '}
<a href="http://images.janaklabs.com" className="btn btn-default">Upload</a>
</div>
)
}
}
React.render(<App />, document.getElementById('app'));