Fix history table

This commit is contained in:
Rajarshee Chatterjee
2021-03-31 09:35:21 +05:30
parent 05511e073e
commit 3cb41c8bfb
10 changed files with 46 additions and 25 deletions
+2 -6
View File
@@ -17,7 +17,6 @@ export const getHistory = async () => {
getHistoryQuery,
null,
(txObj, { rows: { _array } }) => {
// console.log(_array);
resolve(_array);
},
(txObj, error) => console.log("Error ", error)
@@ -31,14 +30,11 @@ export const getHistory = async () => {
export const insertHistory = async (novelId, chapterId) => {
db.transaction((tx) => {
tx.executeSql(
"INSERT OR REPLACE INTO history (historyNovelId, historyChapterId) VALUEs (?, ?)",
"INSERT OR REPLACE INTO history (historyNovelId, historyChapterId) VALUES (?, ?)",
[novelId, chapterId],
(txObj, { rows: { _array } }) => {},
(txObj, res) => {},
(txObj, error) => console.log("Error ", error)
);
tx.executeSql("UPDATE chapters SET `read` = 1 WHERE chapterId = ?", [
chapterId,
]);
tx.executeSql("UPDATE novels SET unread = 0 WHERE novelId = ?", [
novelId,
]);
-1
View File
@@ -2,7 +2,6 @@ import * as SQLite from "expo-sqlite";
const db = SQLite.openDatabase("lnreader.db");
const getLibraryQuery = `
SELECT novels.*, C.chaptersUnread, D.chaptersDownloaded
FROM novels
LEFT JOIN (
+3 -2
View File
@@ -8,8 +8,9 @@ export const createChapterTableQuery = `
bookmark BOOLEAN NOT NULL DEFAULT 0,
\`read\` INTEGER NOT NULL DEFAULT 0,
downloaded INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (novelId) REFERENCES novels(novelId),
UNIQUE(novelId, chapterUrl)
UNIQUE(novelId, chapterUrl),
FOREIGN KEY (novelId) REFERENCES novels(novelId)
ON DELETE CASCADE
)`;
export const createNovelIdIndexQuery = `CREATE INDEX IF NOT EXISTS chapterNovelIdIndex ON chapters(novelId)`;
+1 -1
View File
@@ -2,7 +2,7 @@ export const createHistoryTableQuery = `
CREATE TABLE IF NOT EXISTS history(
historyId INTEGER PRIMARY KEY AUTOINCREMENT,
historyChapterId INTEGER NOT NULL,
historyNovelId INTEGER NOT NULL,
historyNovelId INTEGER NOT NULL UNIQUE,
historyTimeRead DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (historyChapterId) REFERENCES chapters(chapterId)
ON DELETE CASCADE
+13
View File
@@ -1,3 +1,4 @@
import { FOLLOW_NOVEL, UNFOLLOW_NOVEL } from "../novel/novel.types";
import { GET_LIBRARY_NOVELS } from "./library.types";
const initialState = {
@@ -11,6 +12,18 @@ const libraryReducer = (state = initialState, action) => {
switch (type) {
case GET_LIBRARY_NOVELS:
return { novels: payload, loading: false };
case FOLLOW_NOVEL:
return {
...state,
novels: [...state.novels, { ...payload, followed: 1 }],
};
case UNFOLLOW_NOVEL:
return {
...state,
novels: state.novels.filter(
(novel) => novel.novelId !== payload
),
};
default:
return state;
}
+18 -4
View File
@@ -15,6 +15,8 @@ import {
CHAPTER_DELETED,
UPDATE_NOVEL,
UPDATE_LAST_READ,
FOLLOW_NOVEL,
UNFOLLOW_NOVEL,
} from "./novel.types";
import { updateNovel } from "../../services/updates";
@@ -100,16 +102,28 @@ export const sortAndFilterChapters = (novelUrl, filter, sort) => async (
// });
};
export const followNovelAction = (followed, novelId) => async (dispatch) => {
await followNovel(followed, novelId);
export const followNovelAction = (novel) => async (dispatch) => {
await followNovel(novel.followed, novel.novelId);
if (!novel.followed) {
dispatch({
type: FOLLOW_NOVEL,
payload: novel,
});
} else {
dispatch({
type: UNFOLLOW_NOVEL,
payload: novel.novelId,
});
}
dispatch({
type: UPDATE_IN_LIBRARY,
payload: !followed,
payload: !novel.followed,
});
ToastAndroid.show(
!followed ? "Added to library" : "Removed from library",
!novel.followed ? "Added to library" : "Removed from library",
ToastAndroid.SHORT
);
};
+3
View File
@@ -14,3 +14,6 @@ export const CHAPTER_DOWNLOADING = "CHAPTER_DOWNLOADING";
export const CHAPTER_READ = "CHAPTER_READ";
export const CHAPTER_LOADING = "CHAPTER_LOADING";
export const GET_CHAPTER = "GET_CHAPTER";
export const FOLLOW_NOVEL = "FOLLOW_NOVEL";
export const UNFOLLOW_NOVEL = "UNFOLLOW_NOVEL";
+1 -1
View File
@@ -25,7 +25,7 @@ const History = ({
useFocusEffect(
useCallback(() => {
getHistoryAction();
}, [])
}, [getHistoryAction])
);
const renderHistoryCard = ({ item }) => (
+4 -7
View File
@@ -1,4 +1,4 @@
import React, { useState, useCallback } from "react";
import React, { useState, useCallback, useEffect } from "react";
import {
StyleSheet,
Text,
@@ -35,12 +35,9 @@ const LibraryScreen = ({
const [searchText, setSearchText] = useState("");
useFocusEffect(
useCallback(() => {
setSearchText("");
getLibraryAction();
}, [])
);
useEffect(() => {
getLibraryAction();
}, [getLibraryAction]);
/**
* TODO: fix refreshing
+1 -3
View File
@@ -128,9 +128,7 @@ const NovelInfoHeader = ({
color={theme.colorAccentDark}
/>
)}
onPress={() =>
followNovelAction(novel.followed, novel.novelId)
}
onPress={() => followNovelAction(novel)}
style={[
{ backgroundColor: theme.colorPrimaryDark },
styles.toggleFavourite,