This commit is contained in:
Michael Zhang 2023-06-11 15:08:19 -05:00
parent 492215d17c
commit 6c0f35bf01
4 changed files with 27 additions and 7 deletions

View file

@ -64,6 +64,7 @@ async fn main() -> Result<()> {
srs::get_srs_stats, srs::get_srs_stats,
srs::add_srs_item, srs::add_srs_item,
srs::generate_review_batch, srs::generate_review_batch,
srs::update_srs_item,
kanji::get_kanji, kanji::get_kanji,
]) ])
.on_window_event(|event| match event.event() { .on_window_event(|event| match event.event() {

View file

@ -1,7 +1,4 @@
use std::{ use std::time::Duration;
ops::Add,
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};
use sqlx::{Row, SqlitePool}; use sqlx::{Row, SqlitePool};
use tauri::State; use tauri::State;
@ -85,7 +82,6 @@ pub async fn add_srs_item(
options: Option<AddSrsItemOptions>, options: Option<AddSrsItemOptions>,
) -> Result<(), String> { ) -> Result<(), String> {
let opts = options.unwrap_or_default(); let opts = options.unwrap_or_default();
println!("Opts: {opts:?}");
let query_string = format!( let query_string = format!(
r#" r#"
@ -122,6 +118,7 @@ fn default_batch_size() -> u32 {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct SrsEntry { pub struct SrsEntry {
id: u32,
current_grade: u32, current_grade: u32,
meanings: Vec<String>, meanings: Vec<String>,
readings: Vec<String>, readings: Vec<String>,
@ -152,6 +149,8 @@ pub async fn generate_review_batch(
let result = result let result = result
.into_iter() .into_iter()
.map(|row| { .map(|row| {
let id = row.get("ID");
let meanings: String = row.get("Meanings"); let meanings: String = row.get("Meanings");
let meanings = meanings.split(",").map(|s| s.to_owned()).collect(); let meanings = meanings.split(",").map(|s| s.to_owned()).collect();
@ -159,6 +158,7 @@ pub async fn generate_review_batch(
let readings = readings.split(",").map(|s| s.to_owned()).collect(); let readings = readings.split(",").map(|s| s.to_owned()).collect();
SrsEntry { SrsEntry {
id,
current_grade: row.get("CurrentGrade"), current_grade: row.get("CurrentGrade"),
meanings, meanings,
readings, readings,
@ -169,3 +169,12 @@ pub async fn generate_review_batch(
Ok(result) Ok(result)
} }
#[tauri::command]
pub async fn update_srs_item(
srs_db: State<'_, SrsDb>,
item_id: u32,
correct: bool,
) -> Result<(), String> {
todo!()
}

View file

@ -50,11 +50,13 @@ export function Component() {
.then((result) => { .then((result) => {
const newReviews: ReviewItem[] = result.flatMap((srsEntry) => [ const newReviews: ReviewItem[] = result.flatMap((srsEntry) => [
{ {
associatedId: srsEntry.id,
type: ReviewItemType.MEANING, type: ReviewItemType.MEANING,
challenge: srsEntry.associated_kanji, challenge: srsEntry.associated_kanji,
possibleAnswers: srsEntry.meanings, possibleAnswers: srsEntry.meanings,
}, },
{ {
associatedId: srsEntry.id,
type: ReviewItemType.READING, type: ReviewItemType.READING,
challenge: srsEntry.associated_kanji, challenge: srsEntry.associated_kanji,
possibleAnswers: srsEntry.readings, possibleAnswers: srsEntry.readings,
@ -76,14 +78,20 @@ export function Component() {
const nextItem = reviewQueue[0]; const nextItem = reviewQueue[0];
const possibleAnswers = new Set(nextItem.possibleAnswers); const possibleAnswers = new Set(nextItem.possibleAnswers);
const formSubmit = (evt: FormEvent) => { const formSubmit = async (evt: FormEvent) => {
evt.preventDefault(); evt.preventDefault();
if (!reviewQueue) return; if (!reviewQueue) return;
const isCorrect = possibleAnswers.has(currentAnswer);
// Update the backend
await invoke("update_srs_item", { item_id: nextItem.associatedId, correct: isCorrect });
// Check the answer // Check the answer
if (!possibleAnswers.has(currentAnswer)) { if (!isCorrect) {
setIsIncorrect(true); setIsIncorrect(true);
// push it to the back of the queue
const lastItem = reviewQueue[reviewQueue.length - 1]; const lastItem = reviewQueue[reviewQueue.length - 1];
if (!_.isEqual(lastItem, nextItem)) setReviewQueue([...reviewQueue, nextItem]); if (!_.isEqual(lastItem, nextItem)) setReviewQueue([...reviewQueue, nextItem]);
return; return;

View file

@ -1,4 +1,5 @@
export interface SrsEntry { export interface SrsEntry {
id: number;
associated_kanji: string; associated_kanji: string;
current_grade: number; current_grade: number;
meanings: string[]; meanings: string[];
@ -11,6 +12,7 @@ export enum ReviewItemType {
} }
export interface ReviewItem { export interface ReviewItem {
associatedId: number;
type: ReviewItemType; type: ReviewItemType;
challenge: string; challenge: string;
possibleAnswers: string[]; possibleAnswers: string[];