Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Detect synced items when adding with release date
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgssa committed Jun 20, 2020
1 parent d88fd9a commit 772b763
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/class/history-sync/ActivityActionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ export default class ActivityActionCreators {
response
});
}

static addWithReleaseDate(value) {
ViewingActivityAppDispatcher.dispatch({
type: ActionTypes.ADD_WITH_RELEASE_DATE,
value,
});
}
}
9 changes: 9 additions & 0 deletions src/class/history-sync/ActivityStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let _activities = [];
let _isLoading = false;
let _isLoadingTraktData = true;
let _message = undefined;
let _addWithReleaseDate = false;

class ActivityStore extends EventEmitter {
emitChange() {
Expand Down Expand Up @@ -53,6 +54,10 @@ class ActivityStore extends EventEmitter {
activityToUpdate.trakt = activity.trakt;
activityToUpdate.add = activity.add;
}

addWithReleaseDate() {
return _addWithReleaseDate;
}
}

const activityStore = new ActivityStore();
Expand Down Expand Up @@ -117,6 +122,10 @@ activityStore.dispatchToken = ViewingActivityAppDispatcher.register((action) =>
_message = browser.i18n.getMessage(`syncFailed`);
activityStore.emitChange();
break;
case ActionTypes.ADD_WITH_RELEASE_DATE:
_addWithReleaseDate = action.value;
activityStore.emitChange();
break;
}
});
export default activityStore;
9 changes: 8 additions & 1 deletion src/class/history-sync/TraktWebAPIUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BrowserStorage from '../BrowserStorage';
import Request from '../Request';
import Search from '../Search';
import ActivityActionCreators from './ActivityActionCreators';
import ActivityStore from './ActivityStore';

const URL = `${Settings.apiUri}/sync/history`;

Expand All @@ -16,8 +17,9 @@ export default class TraktWebAPIUtils {
}
}

static addActivities(activities, addWithReleaseDate) {
static addActivities(activities) {
// noinspection JSIgnoredPromiseFromCall
const addWithReleaseDate = ActivityStore.addWithReleaseDate();
Request.send({
method: `POST`,
url: URL,
Expand Down Expand Up @@ -112,10 +114,15 @@ export default class TraktWebAPIUtils {
let alreadyOnTrakt = false;
let date;

const addWithReleaseDate = ActivityStore.addWithReleaseDate();
const historyEntries = JSON.parse(response).reverse();
for (let history of historyEntries) {
if (history && history.watched_at) {
date = moment(history.watched_at);
if (addWithReleaseDate && history.watched_at.endsWith('00.000Z')) {
alreadyOnTrakt = true;
break;
}
if (date.diff(options.netflix.date, `days`) === 0) {
alreadyOnTrakt = true;
break;
Expand Down
11 changes: 7 additions & 4 deletions src/class/history-sync/ViewingActivityApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import TmdbImageContainer from '../tmdb/TmdbImageContainer';
import ActivityList from './ActivityList';
import ActivityStore from './ActivityStore';
import TraktWebAPIUtils from './TraktWebAPIUtils';
import ActivityActionCreators from './ActivityActionCreators';

/* global componentHandler */

export default class ViewingActivityApp extends React.Component {
getInitialState() {
return Object.assign(this.getStateFromStores(), {
addWithReleaseDate: this.props.addWithReleaseDate,
hideSynced: this.props.hideSynced,
use24Clock: this.props.use24Clock,
pagesToLoad: this.props.pagesToLoad
Expand All @@ -26,7 +26,8 @@ export default class ViewingActivityApp extends React.Component {
isLoadingTraktData: ActivityStore.isLoadingTraktData(),
loading: ActivityStore.isLoading(),
message: ActivityStore.getMessage(),
page: ActivityStore.getPage()
page: ActivityStore.getPage(),
addWithReleaseDate: ActivityStore.addWithReleaseDate(),
};
}

Expand All @@ -36,7 +37,9 @@ export default class ViewingActivityApp extends React.Component {
}

componentDidMount() {
ActivityActionCreators.addWithReleaseDate(this.props.addWithReleaseDate);
ActivityStore.addChangeListener(this._onChange.bind(this));
NetflixApiUtils.getActivities();
}

componentWillUnmount() {
Expand All @@ -58,7 +61,7 @@ export default class ViewingActivityApp extends React.Component {
const confirmationMessage = browser.i18n.getMessage(`confirmSync`);
if (confirm(confirmationMessage)) {
this.setState({loading: true});
TraktWebAPIUtils.addActivities(this.state.activities, this.state.addWithReleaseDate);
TraktWebAPIUtils.addActivities(this.state.activities);
}
}

Expand Down Expand Up @@ -96,7 +99,7 @@ export default class ViewingActivityApp extends React.Component {
storage.prefs.addWithReleaseDate = addWithReleaseDate;
BrowserStorage.set({prefs: storage.prefs}, true);
});
this.setState({addWithReleaseDate});
ActivityActionCreators.addWithReleaseDate(addWithReleaseDate);
}

_onToggleSynced(event) {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/history-sync/activity-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const ActionTypes = {
TOGGLE_ACTIVITY: `TOGGLE_ACTIVITY`,
SYNC_SUCCESS: `SYNC_SUCCESS`,
SYNC_FAILED: `SYNC_FAILED`,
UPDATE_ACTIVITY: `UPDATE_ACTIVITY`
UPDATE_ACTIVITY: `UPDATE_ACTIVITY`,
ADD_WITH_RELEASE_DATE: 'ADD_WITH_RELEASE_DATE',
};

export default ActionTypes;
2 changes: 0 additions & 2 deletions src/modules/history-sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import '../../assets';
import React from 'react';
import ReactDOM from 'react-dom';
import BrowserStorage from '../../class/BrowserStorage';
import NetflixApiUtils from '../../class/NetflixApiUtils';
import Rollbar from '../../class/Rollbar';
import ViewingActivityApp from '../../class/history-sync/ViewingActivityApp';
import shared from '../../class/Shared';
Expand All @@ -12,7 +11,6 @@ shared.setBackgroundPage(true);
// noinspection JSIgnoredPromiseFromCall
Rollbar.init();
BrowserStorage.get(`prefs`).then(storage => {
NetflixApiUtils.getActivities();
ReactDOM.render(
<ViewingActivityApp
addWithReleaseDate={(storage.prefs && storage.prefs.addWithReleaseDate) || false}
Expand Down

0 comments on commit 772b763

Please sign in to comment.