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::add_srs_item,
srs::generate_review_batch,
srs::update_srs_item,
kanji::get_kanji,
])
.on_window_event(|event| match event.event() {

View file

@ -1,7 +1,4 @@
use std::{
ops::Add,
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};
use std::time::Duration;
use sqlx::{Row, SqlitePool};
use tauri::State;
@ -85,7 +82,6 @@ pub async fn add_srs_item(
options: Option<AddSrsItemOptions>,
) -> Result<(), String> {
let opts = options.unwrap_or_default();
println!("Opts: {opts:?}");
let query_string = format!(
r#"
@ -122,6 +118,7 @@ fn default_batch_size() -> u32 {
#[derive(Debug, Serialize, Deserialize)]
pub struct SrsEntry {
id: u32,
current_grade: u32,
meanings: Vec<String>,
readings: Vec<String>,
@ -152,6 +149,8 @@ pub async fn generate_review_batch(
let result = result
.into_iter()
.map(|row| {
let id = row.get("ID");
let meanings: String = row.get("Meanings");
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();
SrsEntry {
id,
current_grade: row.get("CurrentGrade"),
meanings,
readings,
@ -169,3 +169,12 @@ pub async fn generate_review_batch(
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) => {
const newReviews: ReviewItem[] = result.flatMap((srsEntry) => [
{
associatedId: srsEntry.id,
type: ReviewItemType.MEANING,
challenge: srsEntry.associated_kanji,
possibleAnswers: srsEntry.meanings,
},
{
associatedId: srsEntry.id,
type: ReviewItemType.READING,
challenge: srsEntry.associated_kanji,
possibleAnswers: srsEntry.readings,
@ -76,14 +78,20 @@ export function Component() {
const nextItem = reviewQueue[0];
const possibleAnswers = new Set(nextItem.possibleAnswers);
const formSubmit = (evt: FormEvent) => {
const formSubmit = async (evt: FormEvent) => {
evt.preventDefault();
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
if (!possibleAnswers.has(currentAnswer)) {
if (!isCorrect) {
setIsIncorrect(true);
// push it to the back of the queue
const lastItem = reviewQueue[reviewQueue.length - 1];
if (!_.isEqual(lastItem, nextItem)) setReviewQueue([...reviewQueue, nextItem]);
return;

View file

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