diff --git a/src/model/database.ts b/src/model/database.ts index d2f15dc..f04a13d 100644 --- a/src/model/database.ts +++ b/src/model/database.ts @@ -73,11 +73,11 @@ export async function addCategory(category: Category) { export async function removeExpression(expression_id: number) { return await database.transaction( "rw", - database.expression_sets, + database.expressions, database.expression_to_category, database.expression_to_expression_set, () => { - database.expression_sets.where({ id: expression_id }).delete(); + database.expressions.where({ id: expression_id }).delete(); database.expression_to_category.where({ expression_id }).delete(); database.expression_to_expression_set.where({ expression_id }).delete(); } diff --git a/src/views/ExpressionPracticeView/PromoteExpressionButton.tsx b/src/views/ExpressionPracticeView/PromoteExpressionButton.tsx index abca71e..498c232 100644 --- a/src/views/ExpressionPracticeView/PromoteExpressionButton.tsx +++ b/src/views/ExpressionPracticeView/PromoteExpressionButton.tsx @@ -1,22 +1,30 @@ import { useCallback } from "react"; import { useExpressionSetQueryId } from "../../hooks"; -import { assignExpressionToSet } from "../../model"; - -// TODO fix promotion algorithm so it uses a destination expression_set_id +import { assignExpressionToSet, database, removeExpression } from "../../model"; export interface PromoteExpressionButtonProps { expression_id: number; } +// "Promote" action will destroy the card if it's learning level is already at top level +// TODO notify user of card deletion + export function PromoteExpressionButton({ expression_id, }: PromoteExpressionButtonProps) { const expression_set_id = useExpressionSetQueryId(); - const handleClick = useCallback(() => { - assignExpressionToSet({ - expression_id, - expression_set_id: expression_set_id + 1, - }); + const handleClick = useCallback(async () => { + const existing_next_level = await database.expression_sets + .where({ id: expression_set_id + 1 }) + .first(); + if (existing_next_level) { + assignExpressionToSet({ + expression_id, + expression_set_id: expression_set_id + 1, + }); + } else { + removeExpression(expression_id); + } }, [expression_id, expression_set_id]); return (