Move update logic into CardInfo.svelte

This commit is contained in:
RumovZ 2021-10-17 20:29:32 +02:00
parent 4b5ea6c110
commit 5062024974
4 changed files with 38 additions and 45 deletions

View File

@ -22,6 +22,7 @@ card-stats-review-log-type-review = Review
card-stats-review-log-type-relearn = Relearn
card-stats-review-log-type-filtered = Filtered
card-stats-review-log-type-manual = Manual
card-stats-no-card = (No card to display.)
## Window Titles

View File

@ -64,11 +64,13 @@ class CardInfoDialog(QDialog):
self.setLayout(layout)
self.web.eval(
f"let cardInfo = anki.cardInfo(document.getElementById('main'), {card_id}, true);"
"let cardInfo = anki.cardInfo(document.getElementById('main'));\n" \
"cardInfo.then((c) => c.$set({ includeRevlog: true }));"
)
self.update_card(card_id)
def update_card(self, card_id: CardId) -> None:
self.web.eval(f"anki.updateCardInfo(cardInfo, {card_id}, true);")
self.web.eval(f"cardInfo.then((c) => c.$set({{ cardId: {card_id} }}));")
def reject(self) -> None:
if self._on_close:

View File

@ -3,17 +3,39 @@ Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import * as tr from "../lib/ftl";
import type { Stats } from "../lib/proto";
import { getCardStats } from "./lib";
import CardStats from "./CardStats.svelte";
import Revlog from "./Revlog.svelte";
export let stats: Stats.CardStatsResponse;
export let cardId: number | undefined = undefined;
export let includeRevlog: boolean | undefined = undefined;
let stats: Stats.CardStatsResponse | undefined = undefined;
let _updatingQueue: Promise<void> = Promise.resolve();
$: _updatingQueue = _updatingQueue.then(() => {
if (cardId === undefined) {
stats = undefined;
} else {
getCardStats(cardId).then((s) => {
stats = s;
});
}
});
</script>
<div class="container">
<div>
<CardStats {stats} />
<Revlog {stats} />
{#if stats}
<CardStats {stats} />
{#if includeRevlog}
<Revlog {stats} />
{/if}
{:else}
<span class="placeholder">{tr.cardStatsNoCard()}</span>
{/if}
</div>
</div>
@ -21,4 +43,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
.container {
max-width: 40em;
}
.placeholder {
text-align: center;
}
</style>

View File

@ -1,51 +1,15 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { getCardStats } from "./lib";
import { setupI18n, ModuleName } from "../lib/i18n";
import { checkNightMode } from "../lib/nightmode";
import CardInfo from "./CardInfo.svelte";
const _updatingQueue: Promise<void> = Promise.resolve();
export async function cardInfo(
target: HTMLDivElement,
cardId: number,
includeRevlog: boolean
): Promise<CardInfo> {
export async function cardInfo(target: HTMLDivElement): Promise<CardInfo> {
checkNightMode();
const [stats] = await Promise.all([
getCardStats(cardId),
setupI18n({
modules: [
ModuleName.CARD_STATS,
ModuleName.SCHEDULING,
ModuleName.STATISTICS,
],
}),
]);
if (!includeRevlog) {
stats.revlog = [];
}
return new CardInfo({
target,
props: { stats },
});
}
export async function updateCardInfo(
cardInfo: Promise<CardInfo>,
cardId: number,
includeRevlog: boolean
): Promise<void> {
_updatingQueue.then(async () => {
cardInfo.then(async (cardInfo) => {
const stats = await getCardStats(cardId);
if (!includeRevlog) {
stats.revlog = [];
}
cardInfo.$set({ stats });
});
await setupI18n({
modules: [ModuleName.CARD_STATS, ModuleName.SCHEDULING, ModuleName.STATISTICS],
});
return new CardInfo({ target });
}