Skip to content

Commit

Permalink
Describe assert
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Nov 9, 2023
1 parent 670fee3 commit 479571a
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/screens/deck-form/card-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CardFormView } from "./card-form-view.tsx";
export const CardForm = observer(() => {
const deckFormStore = useDeckFormStore();
const cardForm = deckFormStore.cardForm;
assert(cardForm);
assert(cardForm, "Card should not be empty before editing");

useMainButton("Save", () => {
deckFormStore.saveCardForm();
Expand Down
2 changes: 1 addition & 1 deletion src/screens/deck-list/main-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const MainScreen = observer(() => {
icon={"mdi-call-made"}
onClick={() => {
const channelLink = import.meta.env.VITE_CHANNEL_LINK;
assert(channelLink);
assert(channelLink, "Channel link env variable is empty");

WebApp.openTelegramLink(channelLink);
}}
Expand Down
2 changes: 1 addition & 1 deletion src/screens/deck-review/deck-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useMainButton } from "../../lib/telegram/use-main-button.tsx";
export const DeckPreview = observer(() => {
const reviewStore = useReviewStore();
const deck = deckListStore.selectedDeck;
assert(deck);
assert(deck, "Deck should not be empty before preview");

useBackButton(() => {
screenStore.navigateToMain();
Expand Down
2 changes: 1 addition & 1 deletion src/screens/deck-review/share-deck-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const ShareDeckButton = (props: Props) => {
const onClick = async () => {
if (shareId) {
const botUrl = import.meta.env.VITE_BOT_APP_URL;
assert(botUrl);
assert(botUrl, "Bot URL is not set");
const botUrlWithDeckId = `${trimEnd(botUrl, "/")}?startapp=${shareId}`;
const shareUrl = `https://t.me/share/url?text=&url=${botUrlWithDeckId}`;
WebApp.openTelegramLink(shareUrl);
Expand Down
2 changes: 1 addition & 1 deletion src/store/card-form-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class CardFormStore {
}

changeState(state: CardState) {
assert(this.isOpened);
assert(this.isOpened, "The card should be opened before changing state");
this.state = state;
}
}
2 changes: 1 addition & 1 deletion src/store/deck-form-store-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const DeckFormStoreProvider = (props: { children: ReactNode }) => {

export const useDeckFormStore = () => {
const store = useContext(Context);
assert(store);
assert(store, "DeckFormStoreProvider not found");
return store;
};
15 changes: 7 additions & 8 deletions src/store/deck-form-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class DeckFormStore {
const deck = deckListStore.myDecks.find(
(myDeck) => myDeck.id === screenStore.deckFormId,
);
assert(deck);
assert(deck, "Deck not found in deckListStore");
this.form = {
title: createDeckTitleField(deck.name),
description: new TextField(deck.description ?? ""),
Expand All @@ -81,7 +81,7 @@ export class DeckFormStore {
}

openNewCardForm() {
assert(this.form);
assert(this.form, 'openNewCardForm: form is empty');
this.cardFormIndex = this.form.cards.length;
this.form.cards.push({
front: createCardSideField(""),
Expand All @@ -101,7 +101,7 @@ export class DeckFormStore {
}

async onCardBack() {
assert(this.cardForm);
assert(this.cardForm, 'onCardBack: cardForm is empty');
if (isFormEmpty(this.cardForm)) {
this.quitCardForm();
return;
Expand All @@ -114,7 +114,7 @@ export class DeckFormStore {
}

async onDeckBack() {
assert(this.form);
assert(this.form, 'onDeckBack: form is empty');
if (isFormEmpty(this.form) || !isFormTouched(this.form)) {
screenStore.navigateToMain();
return;
Expand All @@ -127,14 +127,13 @@ export class DeckFormStore {
}

onDeckSave() {
assert(this.form);
assert(this.form, 'onDeckSave: form is empty');

if (this.form.cards.length === 0) {
showAlert("Please add at least 1 card to create a deck");
return;
}

assert(this.form);
formTouchAll(this.form);
if (!isFormValid(this.form)) {
return;
Expand Down Expand Up @@ -162,8 +161,8 @@ export class DeckFormStore {
}

quitCardForm() {
assert(this.cardFormIndex !== undefined);
assert(this.form);
assert(this.cardFormIndex !== undefined, 'quitCardForm: cardFormIndex is empty');
assert(this.form, 'quitCardForm: form is empty');
this.form.cards.splice(this.cardFormIndex, 1);
this.cardFormIndex = undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions src/store/deck-list-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class DeckListStore {

get canReview() {
const deck = this.selectedDeck;
assert(deck);
assert(deck, "canReview requires a deck to be selected");

return (
deck.cardsToReview.length > 0 || screenStore.screen === Screen.DeckPublic
Expand All @@ -100,7 +100,7 @@ export class DeckListStore {
return;
}

assert(deckListStore.selectedDeck);
assert(deckListStore.selectedDeck, "No selected deck for review");
if (screenStore.screen === Screen.DeckPublic) {
deckListStore.addDeckToMine(deckListStore.selectedDeck.id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/quick-add-card-form-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class QuickAddCardFormStore {
return;
}

assert(screenStore.cardQuickAddDeckId);
assert(screenStore.cardQuickAddDeckId, "cardQuickAddDeckId should not be empty");

this.isSending = true;

Expand Down
2 changes: 1 addition & 1 deletion src/store/review-store-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const ReviewStoreProvider = (props: { children: ReactNode }) => {

export const useReviewStore = () => {
const store = useContext(Context);
assert(store);
assert(store, "ReviewStoreProvider not found");
return store;
};
6 changes: 3 additions & 3 deletions src/store/review-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ export class ReviewStore {

open() {
const currentCard = this.currentCard;
assert(currentCard);
assert(currentCard, "Current card should not be empty");
currentCard.open();
}

changeState(cardState: CardState) {
const currentCard = this.currentCard;
assert(currentCard);
assert(currentCard, "currentCard should not be null while changing state in review");
currentCard.changeState(cardState);

const currentCardIdx = this.cardsToReview.findIndex(
(card) => card.id === currentCard.id,
);
assert(currentCardIdx !== -1);
assert(currentCardIdx !== -1, "currentCardIdx is empty");
this.cardsToReview.splice(currentCardIdx, 1);
if (currentCard.state === CardState.Forget) {
if (!this.result.forgotIds.includes(currentCard.id)) {
Expand Down

0 comments on commit 479571a

Please sign in to comment.