Skip to content

Commit

Permalink
Removed unnecessary data fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoladevelops committed Nov 13, 2023
1 parent b063e1e commit c23c8ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
18 changes: 10 additions & 8 deletions Components/GenerateHabitButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import StreakCounter from "./StreakCounter";
import DeleteHabitModal from "../Modals/DeleteHabitModal";
import EditHabitModal from "../Modals/EditHabitModal";
import { useHabitsState } from "../Contexts/AllHabitsContext";
import { markHabitAsCompletedTodayAsync, getAllHabitsAsync } from "../db/db";
import { markHabitAsCompletedTodayAsync } from "../db/db";

const HabitButton = ({ habit, buttonBackgroundColor, btnClicked, inEditState, openEditModal, openDeleteModal }) => {
return (
Expand Down Expand Up @@ -41,10 +41,6 @@ const GenerateHabitButtons = ({ data }) => {
const [isEditModalVisible, setIsEditModalVisible] = useState(false);
const [habit, setHabit] = useState(null);

const refreshData = useCallback(async () => {
setHabitData(await getAllHabitsAsync());
}, []);

const checkIfDateISOIsToday = useCallback((habitDateISO) => {
if (!habitDateISO || habitDateISO === "") {
return false;
Expand All @@ -70,12 +66,18 @@ const GenerateHabitButtons = ({ data }) => {
}

try {
await markHabitAsCompletedTodayAsync(habit.id, habit.streakCount);
refreshData();
const dateTodayISO = await markHabitAsCompletedTodayAsync(habit.id, habit.streakCount);
setHabitData(data.map((currHabit)=>{
if (currHabit.id != habit.id){
return currHabit
}

return {...habit, lastCompletedDate:dateTodayISO, streakCount: habit.streakCount+1}
}))
} catch (error) {
console.log(error);
}
}, [inEditState]);
}, [inEditState, data]);

const openDeleteModal = useCallback((habit) => {
setHabit(habit);
Expand Down
7 changes: 4 additions & 3 deletions db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ export const updateHabitDescriptionAsync = (habitId, newDescription) => {
* Mark a habit as completed today.
* @param {number} habitId The ID of the habit to mark as complete.
* @param {number} currentStreakCount The current streak count of the habit.
* @returns {Promise<void>} A Promise that resolves on successful change of the lastCompletedDate value.
* @returns {Promise<string>} A Promise that resolves on successful change of the lastCompletedDate value. Returns the today date in ISO format that was used.
*/
export const markHabitAsCompletedTodayAsync = (habitId, currentStreakCount)=>{
return new Promise((resolve,reject)=>{
const dateTodayISO = new Date().toISOString()
db.transaction((tx)=>{
tx.executeSql("UPDATE Habits SET lastCompletedDate=?, streakCount=? WHERE id = ?",
[new Date().toISOString(), currentStreakCount+1, habitId],
()=>resolve(),
[dateTodayISO, currentStreakCount+1, habitId],
()=>resolve(dateTodayISO),
(err)=>reject(err)
)
})
Expand Down

0 comments on commit c23c8ae

Please sign in to comment.