Skip to content

Commit

Permalink
client: Use Date internally everywhere
Browse files Browse the repository at this point in the history
It should be (de)serialized from/into string as soon as possible,
e.g. in HTTP api functions or when getting data from template.
  • Loading branch information
jtojnar committed Dec 17, 2020
1 parent ce4ec93 commit 12ff916
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
33 changes: 30 additions & 3 deletions assets/js/requests/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ export function mark(id, read) {
return ajax.post(`${read ? 'unmark' : 'mark'}/${id}`).promise;
}

/**
* Converts some values like dates in response into a objects.
*/
function enrichItemsResponse(data) {
return {
...data,
lastUpdate: data.lastUpdate ? new Date(data.lastUpdate) : data.lastUpdate,
entries: data.entries.map(entry => {
return {
...entry,
datetime: new Date(entry.datetime),
updatetime: entry.updatetime ? new Date(entry.updatetime) : entry.updatetime
};
})
};
}

/**
* Get all items matching given filter.
*/
Expand All @@ -47,21 +64,31 @@ export function getItems(filter) {

return {
controller,
promise: promise.then(response => response.json())
promise: promise.then(response => response.json()).then(enrichItemsResponse)
};
}

/**
* Synchronize changes between client and server.
*/
export function sync(updatedStatuses, syncParams) {
let params = {
...syncParams,
updatedStatuses: syncParams.updatedStatuses ? syncParams.updatedStatuses.map((status) => {
return {
...status,
datetime: status.datetime.toISOString()
};
}) : syncParams.updatedStatuses
};

const { controller, promise } = ajax.fetch('items/sync', {
method: updatedStatuses ? 'POST' : 'GET',
body: ajax.makeSearchParams(syncParams)
body: ajax.makeSearchParams(params)
});

return {
controller,
promise: promise.then(response => response.json())
promise: promise.then(response => response.json()).then(enrichItemsResponse)
};
}
13 changes: 5 additions & 8 deletions assets/js/selfoss-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ selfoss.dbOnline = {

if (selfoss.db.enableOffline) {
syncParams.itemsSinceId = selfoss.dbOffline.lastItemId;
syncParams.itemsNotBefore = selfoss.dbOffline.newestGCedEntry.toISOString();
syncParams.itemsNotBefore = selfoss.dbOffline.newestGCedEntry;
syncParams.itemsHowMany = selfoss.filter.itemsPerPage;
}

Expand All @@ -131,15 +131,14 @@ selfoss.dbOnline = {
selfoss.db.lastSync = Date.now();
selfoss.dbOnline.firstSync = false;

var dataDate = new Date(data.lastUpdate);
var dataDate = data.lastUpdate;

var storing = false;

if (selfoss.db.enableOffline) {
if ('newItems' in data) {
var maxId = 0;
data.newItems.forEach(function(item) {
item.datetime = new Date(item.datetime);
maxId = Math.max(item.id, maxId);
});

Expand Down Expand Up @@ -259,7 +258,7 @@ selfoss.dbOnline = {

if (!selfoss.db.enableOffline) {
selfoss.db.lastSync = Date.now();
selfoss.db.lastUpdate = new Date(data.lastUpdate);
selfoss.db.lastUpdate = data.lastUpdate;
}

selfoss.refreshStats(data.all, data.unread, data.starred);
Expand Down Expand Up @@ -307,8 +306,7 @@ selfoss.dbOnline = {
selfoss.dbOffline = {


// the datetime of the newest garbage collected entry, i.e. deleted
// because not of interest.
/** @var Date the datetime of the newest garbage collected entry, i.e. deleted because not of interest. */
newestGCedEntry: null,
offlineDays: 10,

Expand Down Expand Up @@ -558,7 +556,6 @@ selfoss.dbOffline = {
var fromId = selfoss.filter.fromId;
if (fromDatetime && fromId) {
seek = true;
fromDatetime = new Date(fromDatetime);
}
var isMore = false;
var alwaysInDb = selfoss.filter.type === 'starred'
Expand Down Expand Up @@ -688,7 +685,7 @@ selfoss.dbOffline = {
selfoss.dbOffline.needsSync = true;
}

var d = new Date().toISOString();
var d = new Date();
let newQueuedStatuses = statuses.map(newStatus => ({
entryId: parseInt(newStatus.entryId),
name: newStatus.name,
Expand Down
2 changes: 1 addition & 1 deletion assets/js/selfoss-events-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ selfoss.events.entries = function() {
var lastEntry = $('.entry').filter(':last');
selfoss.events.setHash();
selfoss.filter.extraIds.length = 0;
selfoss.filter.fromDatetime = lastEntry.data('entry-datetime');
selfoss.filter.fromDatetime = new Date(lastEntry.data('entry-datetime'));
selfoss.filter.fromId = lastEntry.data('entry-id');

selfoss.db.reloadList(true);
Expand Down
2 changes: 1 addition & 1 deletion assets/js/templates/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Item({item}) {
return (
<div data-entry-id={item.id}
data-entry-source={item.source}
data-entry-datetime={item.datetime}
data-entry-datetime={item.datetime.toISOString()}
data-entry-url={item.link}
class={['entry', item.unread == 1 ? 'unread' : null ]} role="article">

Expand Down

0 comments on commit 12ff916

Please sign in to comment.