Skip to content

Commit

Permalink
Refactor - split submit finished deck & unfinished
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Nov 6, 2023
1 parent f6735b2 commit 05b8e9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/screens/deck-review/deck-finished.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const DeckFinished = observer(() => {
const reviewStore = useReviewStore();

useMount(() => {
reviewStore.submit();
reviewStore.submitFinished();
});
useMainButton("Go back", () => {
screenStore.navigateToMain();
Expand Down
2 changes: 1 addition & 1 deletion src/screens/deck-review/review.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Review = observer(() => {
const [isRotateAnimating, setIsRotateAnimating] = useState(false);

useBackButton(() => {
reviewStore.submit();
reviewStore.submitUnfinished();
});

const x = useMotionValue(0);
Expand Down
30 changes: 15 additions & 15 deletions src/store/review-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
36 changes: 21 additions & 15 deletions src/store/review-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}),
);
}
}

0 comments on commit 05b8e9b

Please sign in to comment.