Skip to content

Commit e93bdf0

Browse files
bugfix: ignore if the user already tracked this criterion, and use session storage instead of local
1 parent 49890b1 commit e93bdf0

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

includes/ProgressService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public function getProgress( int $articleId, int $tableId, UserIdentity $user ):
5555
* Track the progress of a user on a specific article and table.
5656
* This function handles only 1 entity at a time.
5757
*
58-
* @todo handle duplicate entries, don't return an error, just return a success status
59-
*
6058
* @param int $articleId The ID of the article.
6159
* @param int $tableId The ID of the table.
6260
* @param UserIdentity $user The user whose progress is being tracked.
@@ -76,6 +74,7 @@ public function trackProgress( int $articleId, int $tableId, UserIdentity $user,
7674
'tpt_timestamp' => $dbw->timestamp(),
7775
] )
7876
->caller( __METHOD__ )
77+
->ignore()
7978
->execute();
8079

8180
if ( !$dbw->insertId() ) {

resources/index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var ProgressTracker = {
2626
}
2727

2828
// bail if the user is not logged in, nothing we can do here
29-
// @TODO: maybe redirect them to the sign in page instead?
3029
if ( mw.user.isAnon() ) {
3130
this.addPopover();
3231
return;
@@ -47,7 +46,7 @@ var ProgressTracker = {
4746
// if we don't have IntersectionObserver (on older browsers), then return, nothing we can do
4847
// at this point. Eventually will add a fallback
4948
if ( !window.IntersectionObserver ) {
50-
console.error( 'TableProgressTracking: IntersectionObserver is not supported. Progress tracking is unavailable.' );
49+
console.error( '[TableProgressTracking]: IntersectionObserver is not supported. Progress tracking is unavailable.' );
5150
return;
5251
}
5352

@@ -74,6 +73,12 @@ var ProgressTracker = {
7473
} );
7574
},
7675

76+
/**
77+
* Load progress, either from the browser session storage, or from the API
78+
* @TODO: use mw.log or something rather than console.error
79+
* @param tableId
80+
* @returns {*|Promise}
81+
*/
7782
loadTableProgress: function( tableId ) {
7883
if ( this.progressData.has( tableId ) ) {
7984
return Promise.resolve( this.progressData.get( tableId ) );
@@ -85,14 +90,14 @@ var ProgressTracker = {
8590
// first let us try local storage, if the user has visited this page previously then their data
8691
// will be stored in local storage to avoid a backend trip
8792
try {
88-
stored = localStorage.getItem( `${this.options.storageKey}-${this.pageId}-${tableId}` );
93+
stored = sessionStorage.getItem( `${this.options.storageKey}-${this.pageId}-${tableId}` );
8994
if ( stored ) {
9095
const parsedData = JSON.parse( stored );
9196
this.progressData.set( tableId, parsedData );
9297
return Promise.resolve( parsedData );
9398
}
9499
} catch ( e ) {
95-
console.error( "Could not read from LocalStorage.", e );
100+
console.error( "[TableProgressTracking]: Could not read from SessionStorage.", e );
96101
}
97102

98103
// we didn't have anything in the local storage, so lets make a backend request to get the data
@@ -106,9 +111,9 @@ var ProgressTracker = {
106111
this.progressData.set( tableId, progress );
107112

108113
try {
109-
localStorage.setItem( `${this.options.storageKey}-${this.pageId}-${tableId}`, JSON.stringify( progress ) );
114+
sessionStorage.setItem( `${this.options.storageKey}-${this.pageId}-${tableId}`, JSON.stringify( progress ) );
110115
} catch ( e ) {
111-
console.error( "Could not write to LocalStorage.", e );
116+
console.error( "[TableProgressTracking]: Could not write to SessionStorage.", e );
112117
}
113118

114119
return progress;
@@ -168,7 +173,7 @@ var ProgressTracker = {
168173
let checkbox = event.target;
169174

170175
if ( !rowId ) {
171-
console.error( 'TableProgressTracking: Checkbox does not have a data-row-id attribute.' );
176+
console.error( '[TableProgressTracking]: Checkbox does not have a data-row-id attribute.' );
172177
return;
173178
}
174179

@@ -204,16 +209,16 @@ var ProgressTracker = {
204209
}
205210

206211
try {
207-
localStorage.setItem( `${this.options.storageKey}-${this.pageId}-${tableId}`, JSON.stringify( progress ) );
212+
sessionStorage.setItem( `${this.options.storageKey}-${this.pageId}-${tableId}`, JSON.stringify( progress ) );
208213
} catch ( e ) {
209-
console.error( "TableProgressTracking: Could not write to LocalStorage.", e );
214+
console.error( "[TableProgressTracking]: Could not write to SessionStorage.", e );
210215
}
211216

212217
this.progressData.set( tableId, progress );
213218
this.syncTableCheckboxes( table, tableId );
214219

215220
} catch ( error ) {
216-
console.error( 'TableProgressTracking: Error updating progress.', error );
221+
console.error( '[TableProgressTracking]: Error updating progress.', error );
217222

218223
// lets revert back to the orginal state
219224
checkbox.checked = !originalState;
@@ -261,4 +266,4 @@ var ProgressTracker = {
261266

262267
(function () {
263268
mw.hook('wikipage.content').add( ProgressTracker.init.bind( ProgressTracker ) );
264-
})();
269+
})();

0 commit comments

Comments
 (0)