History fixes

This commit is contained in:
Rajarshee Chatterjee
2021-05-31 15:37:07 +05:30
parent f8199b4e4e
commit 4f1aa7c2cc
6 changed files with 32 additions and 41 deletions
+11 -20
View File
@@ -8,7 +8,10 @@ const getHistoryQuery = `
ON history.historyChapterId = chapters.chapterId
JOIN novels
ON history.historyNovelId = novels.novelId
ORDER BY history.historyTimeRead DESC`;
GROUP BY novels.novelId
HAVING history.historyTimeRead = MAX(history.historyTimeRead)
ORDER BY history.historyTimeRead DESC
`;
export const getHistory = async () => {
return new Promise((resolve, reject) => {
@@ -41,11 +44,14 @@ export const insertHistory = async (novelId, chapterId) => {
});
};
export const deleteHistory = async (novelId) => {
export const deleteHistory = async (historyId) => {
db.transaction((tx) => {
tx.executeSql("DELETE FROM history WHERE historyNovelId = ?", [
novelId,
]);
tx.executeSql(
"DELETE FROM history WHERE historyId = ?",
[historyId],
(txObj, res) => {},
(txObj, error) => console.log("Error ", error)
);
});
};
@@ -54,18 +60,3 @@ export const deleteAllHistory = async () => {
tx.executeSql("DELETE FROM history; VACCUM;");
});
};
export const getHistoryDates = async () => {
return new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(
"SELECT DISTINCT strftime('%d-%m-%Y', historyTimeRead) as day from history",
null,
(txObj, { rows: { _array } }) => {
resolve(_array);
},
(txObj, error) => console.log("Error ", error)
);
});
});
};
+2 -2
View File
@@ -1,8 +1,8 @@
export const createHistoryTableQuery = `
CREATE TABLE IF NOT EXISTS history(
historyId INTEGER PRIMARY KEY AUTOINCREMENT,
historyChapterId INTEGER NOT NULL,
historyNovelId INTEGER NOT NULL UNIQUE,
historyChapterId INTEGER NOT NULL UNIQUE,
historyNovelId INTEGER NOT NULL,
historyTimeRead DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (historyChapterId) REFERENCES chapters(chapterId)
ON DELETE CASCADE
+1 -2
View File
@@ -8,7 +8,6 @@ import EmptyView from "../../Components/EmptyView";
import {
getHistoryAction,
deleteHistoryAction,
clearAllHistoryAction,
} from "../../redux/history/history.actions";
@@ -40,9 +39,9 @@ const History = ({ navigation }) => {
const renderHistoryCard = ({ item }) => (
<HistoryCard
dispatch={dispatch}
item={item}
theme={theme}
deleteHistoryAction={() => dispatch(deleteHistoryAction())}
navigation={navigation}
/>
);
@@ -8,9 +8,9 @@ import moment from "moment";
import { useDispatch } from "react-redux";
import { setNovel } from "../../../redux/novel/novel.actions";
import { parseChapterNumber } from "../../../Services/utils/helpers";
import { deleteHistoryAction } from "../../../redux/history/history.actions";
const HistoryCard = ({ item, deleteHistoryAction, navigation, theme }) => {
const dispatch = useDispatch();
const HistoryCard = ({ item, navigation, theme, dispatch }) => {
return (
<TouchableRipple
style={styles.historyCard}
@@ -65,7 +65,9 @@ const HistoryCard = ({ item, deleteHistoryAction, navigation, theme }) => {
style={{
marginRight: 0,
}}
onPress={() => deleteHistoryAction(item.novelId)}
onPress={() =>
dispatch(deleteHistoryAction(item.historyId))
}
/>
<IconButton
icon="play"
+6 -7
View File
@@ -40,6 +40,8 @@ export const getHistoryAction = () => async (dispatch) => {
};
});
// console.log(groupedHistory);
dispatch({ type: GET_HISTORY, payload: groupedHistory });
};
@@ -54,13 +56,10 @@ export const insertHistoryAction = (novelId, chapterId) => async (dispatch) => {
dispatch(getHistoryAction());
};
export const deleteHistoryAction = (novelId) => async (dispatch) => {
await deleteHistory(novelId);
dispatch({
type: CLEAR_NOVEL_HISTORY,
payload: { novelId },
});
export const deleteHistoryAction = (historyId) => async (dispatch) => {
await deleteHistory(historyId);
console.log("Delete History Action Called");
dispatch(getHistoryAction());
};
export const clearAllHistoryAction = () => async (dispatch) => {
+7 -7
View File
@@ -18,13 +18,13 @@ const historyReducer = (state = initialState, action) => {
return { ...state, loading: true };
case GET_HISTORY:
return { ...state, history: payload, loading: false };
case CLEAR_NOVEL_HISTORY:
return {
...state,
history: state.history.filter(
(item) => item.novelId !== payload.novelId
),
};
// case CLEAR_NOVEL_HISTORY:
// return {
// ...state,
// history: state.history.filter(
// (item) => item.novelId !== payload.novelId
// ),
// };
case CLEAR_HISTORY:
return { ...state, history: [] };
default: