From 05b8e9b6ba4c64f64ed503565848b6fb9c16bee3 Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Mon, 6 Nov 2023 14:57:49 +0700 Subject: [PATCH] Refactor - split submit finished deck & unfinished --- src/screens/deck-review/deck-finished.tsx | 2 +- src/screens/deck-review/review.tsx | 2 +- src/store/review-store.test.ts | 30 +++++++++---------- src/store/review-store.ts | 36 +++++++++++++---------- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/screens/deck-review/deck-finished.tsx b/src/screens/deck-review/deck-finished.tsx index 35741492..d4ba51b5 100644 --- a/src/screens/deck-review/deck-finished.tsx +++ b/src/screens/deck-review/deck-finished.tsx @@ -33,7 +33,7 @@ export const DeckFinished = observer(() => { const reviewStore = useReviewStore(); useMount(() => { - reviewStore.submit(); + reviewStore.submitFinished(); }); useMainButton("Go back", () => { screenStore.navigateToMain(); diff --git a/src/screens/deck-review/review.tsx b/src/screens/deck-review/review.tsx index ec463d3a..9472af6e 100644 --- a/src/screens/deck-review/review.tsx +++ b/src/screens/deck-review/review.tsx @@ -20,7 +20,7 @@ export const Review = observer(() => { const [isRotateAnimating, setIsRotateAnimating] = useState(false); useBackButton(() => { - reviewStore.submit(); + reviewStore.submitUnfinished(); }); const x = useMotionValue(0); diff --git a/src/store/review-store.test.ts b/src/store/review-store.test.ts index 09d26e97..f27fbb2b 100644 --- a/src/store/review-store.test.ts +++ b/src/store/review-store.test.ts @@ -180,42 +180,42 @@ describe("card form store", () => { expect(reviewStore.nextCard).toBeFalsy(); }); - it('hit wrong many times', () => { + it("hit wrong many times", () => { const reviewStore = new ReviewStore(); reviewStore.startDeckReview(deckCardsMock); expect(reviewStore.isFinished).toBeFalsy(); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Forget); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Forget); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Forget); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Forget); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Forget); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Forget); - expect(reviewStore.result.forgotIds).toHaveLength(3) - expect(reviewStore.result.rememberIds).toHaveLength(0) + expect(reviewStore.result.forgotIds).toHaveLength(3); + expect(reviewStore.result.rememberIds).toHaveLength(0); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Remember); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Remember); - reviewStore.open() + reviewStore.open(); reviewStore.changeState(CardState.Remember); - expect(reviewStore.result.forgotIds).toHaveLength(3) - expect(reviewStore.result.rememberIds).toHaveLength(0) - }) + expect(reviewStore.result.forgotIds).toHaveLength(3); + expect(reviewStore.result.rememberIds).toHaveLength(0); + }); }); diff --git a/src/store/review-store.ts b/src/store/review-store.ts index ba0a625b..b7fe84d1 100644 --- a/src/store/review-store.ts +++ b/src/store/review-store.ts @@ -110,15 +110,18 @@ export class ReviewStore { return this.result.forgotIds.length || this.result.rememberIds.length; } - async submit() { + submitUnfinished() { + screenStore.navigateToMain(); + if (!this.hasResult) { - screenStore.navigateToMain(); return; } - this.isReviewSending = true; + return reviewCardsRequest({ cards: this.cardsToSend }); + } - const cards: Array<{ id: number; outcome: ReviewOutcome }> = [ + get cardsToSend(): Array<{ id: number; outcome: ReviewOutcome }> { + return [ ...this.result.forgotIds.map((forgotId) => ({ id: forgotId, outcome: "wrong" as const, @@ -128,17 +131,20 @@ export class ReviewStore { outcome: "correct" as const, })), ]; + } - return reviewCardsRequest({ - cards, - }) - .then(() => { - deckListStore.load(); - }) - .finally( - action(() => { - this.isReviewSending = false; - }), - ); + async submitFinished() { + if (!this.hasResult) { + screenStore.navigateToMain(); + return; + } + + this.isReviewSending = true; + + return reviewCardsRequest({ cards: this.cardsToSend }).finally( + action(() => { + this.isReviewSending = false; + }), + ); } }