import { PropertyValues, css, html, nothing } from "lit"; import { dataStore } from "../services/DataStore.js"; import { scheduler } from "../services/Scheduler.js"; import { FLitElement } from "../shared/FLitElement.js"; import { globalStyle } from "../shared/globalStyle.js"; import { redirectToHome } from "../shared/navigation.js"; import { pageStyle } from "../shared/pageStyle.js"; import { Card, Deck, DeckFrequency } from "../types/data.types.js"; import { createRandomId } from "../utils/utils.js"; import { newCardBack, newCardFront } from "./FCardPage.js"; import { MenuButton } from "./FMenu.js"; import { toTrainParams } from "./FTrainPage.js"; export class FDeckPage extends FLitElement { static styles = [ globalStyle, pageStyle, css` :host { .deck { display: flex; flex-direction: column; gap: 2rem; } header { display: flex; align-items: center; gap: 1rem; } h2 { flex-grow: 1; padding: 1rem; } .cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(13rem, 1fr)); gap: 1rem; } .cards > * { aspect-ratio: 2 / 3; } .new-card-button { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; font-size: 5rem; border: var(--big-border-thickness) dashed var(--border-color); background-color: var(--surface-color); } dialog { width: 90vw; max-width: 40rem; /* avoid jumps when the height dynamically changes */ margin-top: 20vh; section { padding: 1rem; } .field { display: flex; flex-direction: column; } label { font-style: italic; } p { font-style: italic; font-size: 1.4rem; } } } `, ]; declare deckId: string; declare frequency: DeckFrequency; declare toTrain: Card[]; static properties = { deckId: { type: Object }, frequency: { type: String }, toTrain: { type: Array }, }; #deck!: Deck; #cards!: Card[]; #settingsDialog!: HTMLDialogElement; willUpdate(changedProperties: PropertyValues) { if (changedProperties.has("deckId")) { const deck = dataStore.getDeck(this.deckId); if (!deck) { console.error(`deck ${this.deckId} not found`); redirectToHome(); return; } this.#deck = deck; this.frequency = deck.frequency; this.#cards = dataStore.getCardsFor(this.deckId); this.#setToTrain(); } } onTabVisible() { this.#setToTrain(); } render() { const navSettings = html` this.#settingsDialog.showModal()} class="symbol-button primary nav-button" > paramètres du paquet `; const navDelete = html` this.#deleteDeck()} .text=${"supprimer le paquet ?"} > supprimer le paquet `; const menu: MenuButton[] = [ { type: "home" }, { type: "back", url: "/" }, { type: "custom", template: navSettings }, { type: "custom", template: navDelete }, ]; return html` ${this.toTrain.length > 0 ? html` réviser ` : nothing} + ${this.#cards.map( (card) => html``, )} paramètres du paquet fréquence ${this.frequency === "low" ? "révisions moins fréquentes, intervalle maximum d’un an" : ""} ${this.frequency === "medium" ? "révisions moyennement fréquentes, intervalle maximum de 6 mois" : ""} ${this.frequency === "high" ? "révisions fréquentes, intervalle maximum de 3 mois" : ""} `; } firstUpdated() { this.#settingsDialog = this.renderRoot.querySelector("dialog")!; } #setToTrain() { this.toTrain = scheduler.getTrainingStack(this.#cards); } #onFocusName(e: Event) { const input = e.target as HTMLInputElement; if (input.value === newDeckName) { input.select(); } } #onInputName(e: Event) { const name = (e.target as HTMLInputElement).value; dataStore.editDeck(this.deckId, { name }); } #createCard() { const newCard = { id: createRandomId(), deckId: this.deckId, front: newCardFront, back: newCardBack, scheduling: scheduler.newScheduling(), }; dataStore.addCard(newCard); window.navigation.navigate(`/card/${newCard.id}`); } #deleteDeck() { dataStore.removeDeck(this.deckId); redirectToHome(); } #onInputFrequency(e: InputEvent) { const range = (e.target as HTMLInputElement).value; this.frequency = Object.entries(frequencyRange).find( ([, val]) => val === range, )![0] as DeckFrequency; dataStore.editDeck(this.#deck.id, { frequency: this.frequency }); } } declare global { interface HTMLElementTagNameMap { "f-deck-page": FDeckPage; } } export const newDeckName = "nouveau paquet"; const frequencyRange: Record = { low: "1", medium: "2", high: "3", };
${this.frequency === "low" ? "révisions moins fréquentes, intervalle maximum d’un an" : ""} ${this.frequency === "medium" ? "révisions moyennement fréquentes, intervalle maximum de 6 mois" : ""} ${this.frequency === "high" ? "révisions fréquentes, intervalle maximum de 3 mois" : ""}