Skip to content

Commit

Permalink
Added book deletion from local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rimutaka committed Jul 26, 2024
1 parent d4c2b19 commit 41fba0f
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 16 deletions.
8 changes: 4 additions & 4 deletions build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"files": {
"main.css": "./static/css/main.788f695e.css",
"main.js": "./static/js/main.dd435e29.js",
"static/media/isbn_mod_bg.wasm": "./static/media/isbn_mod_bg.a7c8bb5abba51b42f96e.wasm",
"main.js": "./static/js/main.a9cf33ba.js",
"static/media/isbn_mod_bg.wasm": "./static/media/isbn_mod_bg.5e4cff1b0fc1eee8991f.wasm",
"static/media/buy.svg": "./static/media/buy.a0ebbd4b83f7c8afd5d9.svg",
"static/media/icomoon.woff": "./static/media/icomoon.25908258b2a9c6e6da52.woff",
"index.html": "./index.html",
"static/media/about.svg": "./static/media/about.472d9c94914ce88e8d8f.svg",
"static/media/borrow.svg": "./static/media/borrow.f8356d8f6c1fc40fa23b.svg",
"main.788f695e.css.map": "./static/css/main.788f695e.css.map",
"main.dd435e29.js.map": "./static/js/main.dd435e29.js.map"
"main.a9cf33ba.js.map": "./static/js/main.a9cf33ba.js.map"
},
"entrypoints": [
"static/css/main.788f695e.css",
"static/js/main.dd435e29.js"
"static/js/main.a9cf33ba.js"
]
}
2 changes: 1 addition & 1 deletion build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head prefix="og: http://ogp.me/ns#"><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="referrer" content="no-referrer"/><meta name="theme-color" content="#ffffff"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>📖📚📚</title><meta name="description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:title" content="Scan ISBN to record or share a book"/><meta property="og:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:site_name" content="Bookworm Food"><meta property="og:type" content="website"><meta property="og:url" content="https://bookwormfood.com"><meta property="og:image" itemprop="image" content="http://bookwormfood.com/img/og-image-400.png"/><meta property="og:image:secure_url" itemprop="image" content="https://bookwormfood.com/img/og-image-400.png"><meta property="og:image:width" content="400"><meta property="og:image:height" content="400"><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="Scan ISBN to record or share a book"/><meta name="twitter:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."/><meta name="twitter:image" content="https://bookwormfood.com/img/og-image-400.png"/><link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png"><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Amatic+SC:wght@400;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=News+Cycle:wght@400;700&display=swap" rel="stylesheet"><script defer="defer" src="/static/js/main.dd435e29.js"></script><link href="/static/css/main.788f695e.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="app"></div></body></html>
<!doctype html><html lang="en"><head prefix="og: http://ogp.me/ns#"><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="referrer" content="no-referrer"/><meta name="theme-color" content="#ffffff"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>📖📚📚</title><meta name="description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:title" content="Scan ISBN to record or share a book"/><meta property="og:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:site_name" content="Bookworm Food"><meta property="og:type" content="website"><meta property="og:url" content="https://bookwormfood.com"><meta property="og:image" itemprop="image" content="http://bookwormfood.com/img/og-image-400.png"/><meta property="og:image:secure_url" itemprop="image" content="https://bookwormfood.com/img/og-image-400.png"><meta property="og:image:width" content="400"><meta property="og:image:height" content="400"><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="Scan ISBN to record or share a book"/><meta name="twitter:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."/><meta name="twitter:image" content="https://bookwormfood.com/img/og-image-400.png"/><link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png"><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Amatic+SC:wght@400;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=News+Cycle:wght@400;700&display=swap" rel="stylesheet"><script defer="defer" src="/static/js/main.a9cf33ba.js"></script><link href="/static/css/main.788f695e.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="app"></div></body></html>

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/static/js/main.a9cf33ba.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion build/static/js/main.dd435e29.js.map

This file was deleted.

Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion build/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if ("function" === typeof importScripts) {
self.addEventListener("install", event => {
self.skipWaiting();
});
workbox.precaching.precacheAndRoute([{"revision":"09459323ac2ff6809a0fd98a9a5324bf","url":"asset-manifest.json"},{"revision":"4020571efe44dc33d271798e6a18e0c1","url":"favicon.ico"},{"revision":"f98923403f5c78d689d56b18f947c520","url":"img/apple-touch-icon.png"},{"revision":"a62aa63bb4d0a3dd08820787bd7e118f","url":"img/favicon-16x16.png"},{"revision":"a5229a03fcfe584a3031846fe3c19ccf","url":"img/favicon-32x32.png"},{"revision":"37ff8dc0d50cd7705fc65fc84837c2aa","url":"img/og-image-400.png"},{"revision":"def763a1f4c71359c2fc4208c3c555c5","url":"index.html"},{"revision":"2e93299ab8aad4f6859ba711fc9147c2","url":"static/css/main.788f695e.css"},{"revision":"457b004a59e2a7b69ff293bbf56131b7","url":"static/js/main.dd435e29.js"},{"revision":"3311ddb0ad85b9240262a753f5a667b7","url":"static/media/about.472d9c94914ce88e8d8f.svg"},{"revision":"8d00e8b89883db0a8a11d9f91b427d10","url":"static/media/borrow.f8356d8f6c1fc40fa23b.svg"},{"revision":"fb577b765b6c263191ff525af5f4f175","url":"static/media/buy.a0ebbd4b83f7c8afd5d9.svg"},{"revision":"206ffa0a25fdb90ba848db2d5e0446a4","url":"static/media/isbn_mod_bg.a7c8bb5abba51b42f96e.wasm"},{"revision":"24f2b115d3964c9f977462cdd38b066a","url":"wasm/koder.js"},{"revision":"6f11e7db4fe9aca82cac7150bfc33769","url":"wasm/zbar.js"},{"revision":"e8789bf03df9c2c85e9c59ab0a0cd0c6","url":"wasm/zbar.wasm"},{"revision":"bb1c649a95ffa80369254cc3e51b9a41","url":"wasmWorker.js"}]);
workbox.precaching.precacheAndRoute([{"revision":"c90d4df5be017f31cc976126a1fc7dff","url":"asset-manifest.json"},{"revision":"4020571efe44dc33d271798e6a18e0c1","url":"favicon.ico"},{"revision":"f98923403f5c78d689d56b18f947c520","url":"img/apple-touch-icon.png"},{"revision":"a62aa63bb4d0a3dd08820787bd7e118f","url":"img/favicon-16x16.png"},{"revision":"a5229a03fcfe584a3031846fe3c19ccf","url":"img/favicon-32x32.png"},{"revision":"37ff8dc0d50cd7705fc65fc84837c2aa","url":"img/og-image-400.png"},{"revision":"1a2ecfbbcdc88b327e311eefa990c852","url":"index.html"},{"revision":"2e93299ab8aad4f6859ba711fc9147c2","url":"static/css/main.788f695e.css"},{"revision":"60a484f106631fe2f5d2f38620144f07","url":"static/js/main.a9cf33ba.js"},{"revision":"3311ddb0ad85b9240262a753f5a667b7","url":"static/media/about.472d9c94914ce88e8d8f.svg"},{"revision":"8d00e8b89883db0a8a11d9f91b427d10","url":"static/media/borrow.f8356d8f6c1fc40fa23b.svg"},{"revision":"fb577b765b6c263191ff525af5f4f175","url":"static/media/buy.a0ebbd4b83f7c8afd5d9.svg"},{"revision":"bc891d02fd927978db117b9d0d34a2c4","url":"static/media/isbn_mod_bg.5e4cff1b0fc1eee8991f.wasm"},{"revision":"24f2b115d3964c9f977462cdd38b066a","url":"wasm/koder.js"},{"revision":"6f11e7db4fe9aca82cac7150bfc33769","url":"wasm/zbar.js"},{"revision":"e8789bf03df9c2c85e9c59ab0a0cd0c6","url":"wasm/zbar.wasm"},{"revision":"bb1c649a95ffa80369254cc3e51b9a41","url":"wasmWorker.js"}]);
workbox.routing.registerRoute(
new RegExp("https://fonts.(?:.googlepis|gstatic).com/(.*)"),
new workbox.strategies.CacheFirst({
Expand Down
37 changes: 37 additions & 0 deletions isbn_wasm_mod/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,40 @@ pub async fn update_book_status(isbn: String, status: Option<storage::BookStatus
// send the response back to the UI thread
report_progress(resp.to_string());
}

/// Deletes a book from the local storage.
/// Returns error or success via an async message.
#[wasm_bindgen]
pub async fn delete_book(isbn: String) {
log!("Deleting book from local storage");

// need the runtime for the global context and fetch
let runtime = match get_runtime().await {
Ok(v) => v,

// if this happened it would be a bug
Err(e) => {
log!("Failed to get runtime: {:?}", e);
return;
}
};

// get Books from local storage and wrap them into a response struct
let resp = match storage::Book::delete(&runtime, &isbn).await {
Ok(_) => {
log!("Book deleted");
WasmResponse::Deleted(Box::new(Some(WasmResult::Ok(isbn))))
}
Err(e) => {
log!("Failed to delete book {isbn}");
log!("{:?}", e);
WasmResponse::Deleted(Box::new(Some(WasmResult::Err(format!("{:?}", e)))))
}
};

// log!("Book data below:");
// log!("{:?}", resp);

// send the response back to the UI thread
report_progress(resp.to_string());
}
26 changes: 26 additions & 0 deletions isbn_wasm_mod/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,32 @@ impl Book {

Ok(Some(book))
}

/// Deletes the book from the local storage.
/// Does nothing if the book is not found in the local storage.
pub(crate) async fn delete(runtime: &Window, isbn: &str) -> Result<()> {
// connect to the local storage
let ls = match runtime.local_storage() {
Ok(Some(v)) => v,
Err(e) => {
bail!("Failed to get local storage: {:?}", e);
}
_ => {
bail!("Local storage not available (OK(None))");
}
};

// delete the book from LS by isbn
match ls.remove_item(isbn) {
Ok(()) => log!("Book {isbn} removed from local storage"),
Err(e) => {
log!("Failed to remove local storage book record for {isbn}: {:?}", e);
bail!("Failed to remove local storage book record for {isbn}");
}
};

Ok(())
}
}

impl Books {
Expand Down
4 changes: 4 additions & 0 deletions isbn_wasm_mod/src/wasm_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ pub enum WasmResponse {
// everything has to be boxed because of the potential large difference in size
// between the different types of responses
// the memory is allocated based on the largest struct
/// A list of book records.
LocalBooks(Box<Option<WasmResult<crate::storage::Books>>>),
/// A single book record.
LocalBook(Box<Option<WasmResult<crate::storage::Book>>>),
/// Result of a deletion operation for the enclosed ISBN.
Deleted(Box<Option<WasmResult<String>>>)
}

impl fmt::Display for WasmResponse {
Expand Down
12 changes: 10 additions & 2 deletions src/components/scanResult.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import useState from 'react-usestateref';
import { useNavigate, useLocation } from "react-router-dom";
import initWasmModule, { get_book_data, BookStatus, update_book_status } from '../wasm-rust/isbn_mod.js';
import initWasmModule, { get_book_data, BookStatus, update_book_status, delete_book } from '../wasm-rust/isbn_mod.js';

function HtmlP({ text }) {

Expand Down Expand Up @@ -110,6 +110,10 @@ export default function ScanResult() {
let url = build_book_url(title, authors, isbn);
navigate(`/${url}`);
}
else if (data?.deleted?.Ok) {
console.log("Book deletion confirmed");
navigate(`/`);
}
else {
// console.log(data);
setTitle("Cannot get data from Google for this book");
Expand All @@ -133,6 +137,10 @@ export default function ScanResult() {
update_book_status(isbn, BookStatus.Liked);
};

const onClickStatusBin = (e) => {
e.preventDefault();
delete_book(isbn);
};

const renderQrCodeResult = () => {
// update page title
Expand All @@ -150,7 +158,7 @@ export default function ScanResult() {
<i title="Read later" id="status-later" className={"icon-alarm" + (status == BookStatus[0] ? " active" : "")} onClick={onClickStatusToRead}></i>
<i title="Done reading it" id="status-read" className={"icon-checkmark" + (status == BookStatus[1] ? " active" : "")} onClick={onClickStatusRead}></i>
<i title="Liked it!" id="status-liked" className={"icon-heart" + (status == BookStatus[2] ? " active" : "")} onClick={onClickStatusLiked}></i>
<i title="Bin it" id="status-bin" className="icon-bin text-slate-500"></i>
<i title="Bin it" id="status-bin" className="icon-bin text-slate-500" onClick={onClickStatusBin}></i>
</div>
<div className="result-table">
<div>
Expand Down
24 changes: 20 additions & 4 deletions src/wasm-rust/isbn_mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,27 @@ export function update_book_status(isbn, status) {
return takeObject(ret);
}

/**
* Deletes a book from the local storage.
* Returns error or success via an async message.
* @param {string} isbn
* @returns {Promise<void>}
*/
export function delete_book(isbn) {
const ptr0 = passStringToWasm0(isbn, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.delete_book(ptr0, len0);
return takeObject(ret);
}

function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_114(arg0, arg1, arg2, arg3) {
function __wbg_adapter_117(arg0, arg1, arg2, arg3) {
_assertNum(arg0);
_assertNum(arg1);
wasm.wasm_bindgen__convert__closures__invoke2_mut__h7e0de6a9770ded74(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
Expand Down Expand Up @@ -543,6 +556,9 @@ function __wbg_get_imports() {
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
}, arguments) };
imports.wbg.__wbg_removeItem_c0321116dc514363 = function() { return handleError(function (arg0, arg1, arg2) {
getObject(arg0).removeItem(getStringFromWasm0(arg1, arg2));
}, arguments) };
imports.wbg.__wbg_setItem_ba2bb41d73dac079 = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) {
getObject(arg0).setItem(getStringFromWasm0(arg1, arg2), getStringFromWasm0(arg3, arg4));
}, arguments) };
Expand Down Expand Up @@ -628,7 +644,7 @@ function __wbg_get_imports() {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_114(a, state0.b, arg0, arg1);
return __wbg_adapter_117(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
Expand Down Expand Up @@ -729,8 +745,8 @@ function __wbg_get_imports() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper2252 = function() { return logError(function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 177, __wbg_adapter_44);
imports.wbg.__wbindgen_closure_wrapper2287 = function() { return logError(function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 182, __wbg_adapter_44);
return addHeapObject(ret);
}, arguments) };

Expand Down
Binary file modified src/wasm-rust/isbn_mod_bg.wasm
Binary file not shown.

0 comments on commit 41fba0f

Please sign in to comment.