Skip to content

Commit

Permalink
🐛 Deleted todo not removed from important list
Browse files Browse the repository at this point in the history
  • Loading branch information
homostellaris committed Jul 14, 2024
1 parent 0dab544 commit b3f5b3b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
10 changes: 10 additions & 0 deletions components/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Dexie, { Table } from 'dexie'

import dexieCloud from 'dexie-cloud-addon'

export interface Todo {
Expand Down Expand Up @@ -62,3 +63,12 @@ export class DexieStarfocus extends Dexie {
}

export const db = new DexieStarfocus()

// Helpful for inspecting the database to debug errors in production
declare global {
interface Window {
db: DexieStarfocus
}
}

window.db = db
17 changes: 17 additions & 0 deletions components/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,30 @@ export const MiscMenu = () => {
<IonButton
id="clean-database"
onClick={async () => {
// Remove invalid notes from todos
await db.todos.toCollection().modify(todo => {
delete todo['uri']
if (todo.note && !todo.note.uri) {
delete todo.note
}
})

// Remove empty todos
await db.todos.where('title').equals('').delete()

// Remove todos from important list that don't exist
const important = await db.lists.get('#important')
important?.order.forEach(async id => {
const todo = await db.todos.get(id)
if (todo) {
await db.lists.update('#important', {
order: removeItemFromArray(
important.order,
important.order.indexOf(id),
),
})
}
})
}}
>
Clean database
Expand Down
10 changes: 9 additions & 1 deletion components/todos/TodoActionSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ export function TodoActionSheetProvider({ children }: PropsWithChildren) {
action: 'delete',
},
handler: async () => {
await db.todos.delete(todo?.id)
db.transaction('rw', db.todos, async () => {
await db.todos.delete(todo!.id)
const important = await db.lists.get('#important')
if (important!.order.includes(todo!.id!)) {
await db.lists.update('#important', list => {
list.order = list.order.filter(id => id !== todo!.id)
})
}
})
},
},
]}
Expand Down

0 comments on commit b3f5b3b

Please sign in to comment.