/
ExportLibrary.js
349 lines (332 loc) · 10.5 KB
/
ExportLibrary.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import React from 'react';
import { View, Text, TextInput, FlatList, ActivityIndicator, TouchableOpacity } from 'react-native';
import FastImage from 'react-native-fast-image';
import Icon from 'react-native-vector-icons/Ionicons';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import * as colors from 'kitsu/constants/colors';
import { Kitsu, setToken } from 'kitsu/config/api';
import { queued, success, failed, pending } from 'kitsu/assets/img/sidebar_icons/';
import myanimelist from 'kitsu/assets/img/myanimelist.png';
import defaultAvatar from 'kitsu/assets/img/default_avatar.png';
import { navigationOptions, SidebarButton, SidebarTitle, ItemSeparator } from 'kitsu/screens/Sidebar/common/';
import { styles } from './styles';
const keyExtractor = (item, index) => index.toString();
const ExportItem = ({ canonicalTitle, posterImage, syncStatus }) => {
let icon = null;
let details = '';
switch (syncStatus) {
case 'pending':
icon = pending;
details = 'Currently Updating MAL Entry';
break;
case 'success':
icon = success;
details = 'Successfully Created MAL Entry';
break;
case 'error':
icon = failed;
details = 'Failed to Update MAL Entry';
break;
default:
icon = failed;
details = 'Failed to Update MAL Entry';
break;
}
return (
<View style={[styles.item, { paddingHorizontal: 12 }]}>
<FastImage
style={{ width: 30, height: 30 }}
source={(posterImage && { uri: posterImage.small || posterImage.large }) || defaultAvatar}
cache="web"
/>
<View style={{ flex: 1 }}>
<View style={{ marginHorizontal: 12, justifyContent: 'center' }}>
<Text
numberOfLines={1}
ellipsizeMode={'tail'}
style={{ fontWeight: '600', fontFamily: 'OpenSans', fontSize: 12 }}
>
{canonicalTitle}
</Text>
<Text style={styles.hintText}>
{details}
</Text>
</View>
</View>
<View>
<FastImage source={icon} style={[styles.itemImage, { right: -2 }]} cache="web" />
</View>
</View>
);
};
class ExportLibrary extends React.Component {
static navigationOptions = ({ navigation }) => navigationOptions(navigation, 'MyAnimeList Sync');
state = {
loading: true,
authenticating: false,
hasAccount: false,
linkedAccount: {},
libraryEntryLogs: [],
totalEntryLogs: null,
pageIndex: 1,
pageLimit: 10,
loadingMore: false,
username: '',
password: '',
}
componentDidMount() {
this.fetchLinkedAccounts();
}
onSyncButtonPressed = async () => {
const { accessToken, currentUser } = this.props;
const { username, password } = this.state;
const { id } = currentUser;
setToken(accessToken);
this.setState({ authenticating: true });
try {
const linkedAccount = await Kitsu.create('linkedAccounts', {
externalUserId: username,
token: password,
kind: 'my-anime-list',
syncTo: true,
user: {
id,
},
});
this.setState({
linkedAccount,
authenticating: false,
hasAccount: true,
username: '',
password: '',
});
} catch (e) {
this.setState({
error: e,
authenticating: false,
});
}
}
onDisconnectButtonPressed = async () => {
const { accessToken } = this.props;
const { linkedAccount } = this.state;
setToken(accessToken);
this.setState({ loading: true });
try {
await Kitsu.destroy('linkedAccounts', linkedAccount.id);
this.setState({
hasAccount: false,
loading: false,
});
} catch (e) {
this.setState({
error: e,
loading: false,
});
}
}
fetchLinkedAccounts = async () => {
const { accessToken, currentUser } = this.props;
const { id } = currentUser;
setToken(accessToken);
try {
const linkedAccounts = await Kitsu.findAll('linkedAccounts', {
user: { id },
filter: { kind: 'my-anime-list' },
});
const hasAccount = typeof linkedAccounts[0] !== 'undefined';
// show form if user has no linked accounts. else, load the data and fetch
// entry logs.
this.setState({
linkedAccount: linkedAccounts[0],
loading: hasAccount,
hasAccount,
}, () => {
if (hasAccount) {
this.fetchLibraryEntryLogs();
}
});
} catch (e) {
this.setState({
error: e,
loading: false,
hasAccount: false,
});
}
}
fetchLibraryEntryLogs = async () => {
const { accessToken } = this.props;
const { linkedAccount, pageLimit } = this.state;
setToken(accessToken);
// console.log(accessToken, linkedAccount);
try {
const libraryEntryLogs = await Kitsu.findAll('libraryEntryLogs', {
filter: { linked_account_id: linkedAccount.id },
fields: { media: 'canonicalTitle,titles,posterImage,slug' },
page: { limit: pageLimit },
sort: '-created_at',
include: 'media',
});
this.setState({
libraryEntryLogs,
totalEntryLogs: libraryEntryLogs.meta.count,
loading: false,
pageIndex: 1,
hasAccount: true,
});
} catch (e) {
this.setState({
error: e,
loading: false,
hasAccount: false,
});
}
}
loadMoreEntryLogs = async () => {
const { loadingMore, pageLimit, pageIndex, totalEntryLogs, linkedAccount } = this.state;
const hasMore = totalEntryLogs / pageLimit > pageIndex;
if (!loadingMore && hasMore) {
const { accessToken, currentUser } = this.props;
const { id } = currentUser;
setToken(accessToken);
this.setState({ loadingMore: true });
try {
const libraryEntryLogs = await Kitsu.findAll('libraryEntryLogs', {
filter: { linked_account_id: linkedAccount.id },
fields: { media: 'canonicalTitle,titles,posterImage,slug' },
page: {
limit: pageLimit,
offset: pageLimit * pageIndex,
},
sort: '-created_at',
include: 'media',
});
this.setState({
libraryEntryLogs: this.state.libraryEntryLogs.concat(libraryEntryLogs),
loading: false,
loadingMore: false,
pageIndex: pageIndex + 1,
hasAccount: true,
});
} catch (e) {
this.setState({
error: e,
loading: false,
loadingMore: false,
hasAccount: false,
});
}
}
}
renderSetupScreen = () => {
const { username, password, authenticating } = this.state;
return (
<View style={styles.containerStyle}>
<View
style={styles.card}
>
<View style={{ padding: 8 }}>
<View style={{ alignItems: 'center' }}>
<FastImage
source={myanimelist}
style={styles.cardLogo}
cache="web"
/>
</View>
<Text
style={styles.cardText}
>
Enter your username below to connect your MAL account to your Kitsu account. All future updates will be synced.
</Text>
</View>
<ItemSeparator />
<View style={styles.inputWrapper}>
<TextInput
style={styles.input}
value={username}
onChangeText={t => this.setState({ username: t })}
placeholder={'Your MyAnimeList Username'}
underlineColorAndroid={'transparent'}
autoCapitalize={'none'}
keyboardAppearance={'dark'}
/>
</View>
<ItemSeparator />
<View style={styles.inputWrapper}>
<TextInput
style={styles.input}
value={password}
onChangeText={t => this.setState({ password: t })}
placeholder={'Your MyAnimeList Password'}
secureTextEntry
underlineColorAndroid={'transparent'}
autoCapitalize={'none'}
keyboardAppearance={'dark'}
/>
</View>
</View>
<SidebarButton
style={{ marginTop: 0, paddingHorizontal: 12 }}
disabled={username.length === 0}
onPress={this.onSyncButtonPressed}
title={`Connect MAL Account`}
loading={authenticating}
/>
</View>
);
}
renderItemSeparatorComponent() {
return <ItemSeparator />;
}
renderLoadingIndicator() {
return (
<View style={[styles.containerStyle, { justifyContent: 'center', alignItems: 'center' }]}>
<ActivityIndicator size={'large'} />
</View>
);
}
render() {
const { loading, linkedAccount, libraryEntryLogs, hasAccount } = this.state;
if (loading) return this.renderLoadingIndicator();
if (!hasAccount) return this.renderSetupScreen();
return (
<View style={styles.containerStyle}>
<View style={[styles.card, { flexDirection: 'row', padding: 8, alignItems: 'center', justifyContent: 'space-between' }]}>
<View style={{ flexDirection: 'row', alignItems: 'center' }} >
<FastImage source={pending} style={{ width: 30, height: 30 }} cache="web" />
<Text style={{ marginLeft: 8, fontFamily: 'OpenSans', fontWeight: '500' }}>{linkedAccount.externalUserId}</Text>
</View>
<TouchableOpacity onPress={this.onDisconnectButtonPressed} style={{}}>
<FastImage source={failed} style={{ width: 16, height: 16 }} cache="web" />
</TouchableOpacity>
</View>
<SidebarTitle title={'Entries'} />
<FlatList
data={libraryEntryLogs}
keyExtractor={keyExtractor}
renderItem={({ item }) => {
const { canonicalTitle, posterImage } = item.media;
return (
<ExportItem
canonicalTitle={canonicalTitle}
posterImage={posterImage}
syncStatus={item.syncStatus}
/>
);
}}
ItemSeparatorComponent={this.renderItemSeparatorComponent}
removeClippedSubviews={false}
onEndReached={this.loadMoreEntryLogs}
onEndReachedThreshold={0.3}
// ListEmptyComponent={<Text> No Library exports </Text>}
/>
</View>
);
}
}
const mapStateToProps = ({ auth, user }) => ({
accessToken: auth.tokens.access_token,
currentUser: user.currentUser,
});
export default connect(mapStateToProps, {})(ExportLibrary);