diff --git a/src/Database/queries/HistoryQueries.js b/src/Database/queries/HistoryQueries.js index b998e97b..19c56c5a 100644 --- a/src/Database/queries/HistoryQueries.js +++ b/src/Database/queries/HistoryQueries.js @@ -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) - ); - }); - }); -}; diff --git a/src/Database/tables/HistoryTable.js b/src/Database/tables/HistoryTable.js index e6311a24..988357ae 100644 --- a/src/Database/tables/HistoryTable.js +++ b/src/Database/tables/HistoryTable.js @@ -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 diff --git a/src/Screens/History/History.js b/src/Screens/History/History.js index 5bf6592d..f74fedeb 100644 --- a/src/Screens/History/History.js +++ b/src/Screens/History/History.js @@ -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 }) => ( dispatch(deleteHistoryAction())} navigation={navigation} /> ); diff --git a/src/Screens/History/components/HistoryCard.js b/src/Screens/History/components/HistoryCard.js index 27e2a03a..760abf78 100644 --- a/src/Screens/History/components/HistoryCard.js +++ b/src/Screens/History/components/HistoryCard.js @@ -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 ( { style={{ marginRight: 0, }} - onPress={() => deleteHistoryAction(item.novelId)} + onPress={() => + dispatch(deleteHistoryAction(item.historyId)) + } /> 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) => { diff --git a/src/redux/history/history.reducer.js b/src/redux/history/history.reducer.js index 02c37839..b6848398 100644 --- a/src/redux/history/history.reducer.js +++ b/src/redux/history/history.reducer.js @@ -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: