Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Book action bar checks for book’s real-time loan status & updates acc… #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internetarchive/ia-book-actions",
"version": "0.0.7",
"version": "0.0.7-2",
"description": "Bookreader actions for borrowable book",
"author": "Internet Archive",
"license": "AGPL-3.0-only",
Expand Down
31 changes: 31 additions & 0 deletions src/core/services/get-realtime-lending-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Helper function to fetch real-time lending status
*/
export default function getRealTimeLendingStatus(identifier) {
let url = `https://ia-petabox-neeraj.archive.org/services/book/lending-info.php?identifier=${identifier}`;

if (window.location.pathname === '/demo/') {
url = ''; // change URL to prevent error on localhost
}

fetch(url)
.then(response => {
// return { response_code: '400', data: 'error happened' };
return response.json();
})
.then(response => {
console.log(response);
if (response?.response_code === 200) {
const lendingInfo = response?.body?.data?.lendingInfo;
let newLendingInfo = lendingInfo;
const newLendingStatus = lendingInfo?.lendingStatus;
newLendingInfo = { ...newLendingInfo, ...newLendingStatus };
newLendingInfo.lendingStatus = '';
return newLendingInfo;
}
return null;
})
.catch(response => {
return response;
});
}
17 changes: 17 additions & 0 deletions src/ia-book-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import './components/text-group.js';
import './components/info-icon.js';

import GetLendingActions from './core/services/get-lending-actions.js';
import getRealTimeLendingStatus from './core/services/get-realtime-lending-status.js';

import { mobileContainerWidth } from './core/config/constants.js';

export default class IABookActions extends LitElement {
Expand Down Expand Up @@ -45,6 +47,7 @@ export default class IABookActions extends LitElement {
this.lendingOptions = {};
this.disableActionGroup = false;
this.modalConfig = {};
this.realtimeInternal = 0; // store intervalID
}

disconnectedCallback() {
Expand All @@ -62,6 +65,20 @@ export default class IABookActions extends LitElement {
this.modalConfig.headerColor = '#d9534f';
}
this.setupLendingToolbarActions();

// fetch latest lending status after an internal
if (
!this.lendingStatus.user_has_browsed &&
!this.lendingStatus.user_has_borrowed
) {
this.realtimeInternal = setInterval(() => {
const latestLendingStatus = getRealTimeLendingStatus(
'naturalhistoryof00unse_4'
);
console.log(latestLendingStatus);
if (latestLendingStatus) this.lendingStatus = latestLendingStatus;
}, 30000); // 60000 ms = 1 min
}
}

updated(changed) {
Expand Down