Skip to content

Commit eba3c5f

Browse files
committed
Edit card example via server bot
1 parent 8ce7d6b commit eba3c5f

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

functions/db/user/user-set-server-bot-state.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export type ServerBotState =
99
type: "deckSelected";
1010
cardFront: string;
1111
cardBack: string;
12+
cardExample: string | null;
1213
deckId: number;
13-
editingField?: "cardFront" | "cardBack";
14+
editingField?: "cardFront" | "cardBack" | "cardExample";
1415
};
1516

1617
export const userSetServerBotState = async (

functions/server-bot/callback-query-type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export enum CallbackQueryType {
22
Deck = "deck",
33
ConfirmCreateCard = "confirm",
44
EditFront = "edit-front",
5+
EditExample = "edit-example",
56
EditBack = "edit-back",
67
Cancel = "cancel",
78
}

functions/server-bot/escape-markdown.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const SPECIAL_CHARS = [
1818
"{",
1919
"}",
2020
".",
21-
"!"
21+
"!",
2222
];
2323

2424
// https://core.telegram.org/bots/api#markdownv2-style
2525
export const escapeMarkdown = (text: string) => {
26-
SPECIAL_CHARS.forEach(char => {
27-
const regex = new RegExp(`\\${char}`, 'g');
26+
SPECIAL_CHARS.forEach((char) => {
27+
const regex = new RegExp(`\\${char}`, "g");
2828
text = text.replace(regex, `\\${char}`);
2929
});
3030
return text;

functions/server-bot/on-callback-query.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@ import {
1010
import { sendCardCreateConfirmMessage } from "./send-card-create-confirm-message.ts";
1111
import { DatabaseException } from "../db/database-exception.ts";
1212

13+
type CallbackQueryEdit =
14+
| CallbackQueryType.EditFront
15+
| CallbackQueryType.EditBack
16+
| CallbackQueryType.EditExample;
17+
18+
const callbackQueryEditTypeToField = (data: CallbackQueryEdit) => {
19+
switch (data) {
20+
case CallbackQueryType.EditFront:
21+
return "cardFront";
22+
case CallbackQueryType.EditBack:
23+
return "cardBack";
24+
case CallbackQueryType.EditExample:
25+
return "cardExample";
26+
default:
27+
return data satisfies never;
28+
}
29+
};
30+
31+
const callbackQueryToHumanReadable = (data: CallbackQueryEdit) => {
32+
switch (data) {
33+
case CallbackQueryType.EditFront:
34+
return "front";
35+
case CallbackQueryType.EditBack:
36+
return "back";
37+
case CallbackQueryType.EditExample:
38+
return "example";
39+
default:
40+
return data satisfies never;
41+
}
42+
};
43+
1344
export const onCallbackQuery = (envSafe: EnvSafe) => async (ctx: Context) => {
1445
assert(ctx.callbackQuery);
1546
assert(ctx.from);
@@ -34,6 +65,7 @@ export const onCallbackQuery = (envSafe: EnvSafe) => async (ctx: Context) => {
3465
type: "deckSelected",
3566
cardBack: state.cardBack,
3667
cardFront: state.cardFront,
68+
cardExample: null,
3769
deckId,
3870
});
3971

@@ -44,19 +76,19 @@ export const onCallbackQuery = (envSafe: EnvSafe) => async (ctx: Context) => {
4476

4577
if (
4678
data === CallbackQueryType.EditFront ||
47-
data === CallbackQueryType.EditBack
79+
data === CallbackQueryType.EditBack ||
80+
data === CallbackQueryType.EditExample
4881
) {
49-
const isFront = data === CallbackQueryType.EditFront;
5082
const state = await userGetServerBotState(envSafe, ctx.from.id);
5183
assert(state?.type === "deckSelected", "State is not deckSelected");
84+
const editingField = callbackQueryEditTypeToField(data);
85+
const editingFieldHuman = callbackQueryToHumanReadable(data);
5286
await userSetServerBotState(envSafe, ctx.from.id, {
5387
...state,
54-
editingField: isFront ? "cardFront" : "cardBack",
88+
editingField,
5589
});
5690
await ctx.deleteMessage();
57-
await ctx.reply(
58-
`Send a message with the new ${isFront ? "front" : "back"}:`,
59-
);
91+
await ctx.reply(`Send a message with the new ${editingFieldHuman}:`);
6092
return;
6193
}
6294

@@ -75,13 +107,14 @@ export const onCallbackQuery = (envSafe: EnvSafe) => async (ctx: Context) => {
75107
deck_id: state.deckId,
76108
front: state.cardFront,
77109
back: state.cardBack,
110+
example: state.cardExample,
78111
});
79112

80113
if (createCardsResult.error) {
81114
throw new DatabaseException(createCardsResult.error);
82115
}
83116

84-
await ctx.reply('Card has been created');
117+
await ctx.reply("Card has been created");
85118
await ctx.deleteMessage();
86119
await userSetServerBotState(envSafe, ctx.from.id, null);
87120
return;

functions/server-bot/send-card-create-confirm-message.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import {
88
import { CallbackQueryType } from "./callback-query-type.ts";
99
import { escapeMarkdown } from "./escape-markdown.ts";
1010

11+
const renderFieldValue = (value: string | null) => {
12+
if (!value) {
13+
return "_None_";
14+
}
15+
16+
return escapeMarkdown(value);
17+
};
18+
1119
export const sendCardCreateConfirmMessage = async (
1220
envSafe: EnvSafe,
1321
ctx: Context,
@@ -21,14 +29,17 @@ export const sendCardCreateConfirmMessage = async (
2129
cardBack: state.cardBack,
2230
cardFront: state.cardFront,
2331
deckId: state.deckId,
32+
cardExample: state.cardExample,
2433
});
2534

2635
await ctx.deleteMessage();
2736

2837
await ctx.reply(
29-
`Confirm card creation:\n\n*Front:*\n${escapeMarkdown(
38+
`Confirm card creation:\n\n*Front:* ${renderFieldValue(
3039
state.cardFront,
31-
)}\n\n*Back:*\n${escapeMarkdown(state.cardBack)}`,
40+
)}\n\n*Back:* ${renderFieldValue(
41+
state.cardBack,
42+
)}\n\n*Example:* ${renderFieldValue(state.cardExample)}`,
3243
{
3344
parse_mode: "MarkdownV2",
3445
reply_markup: InlineKeyboard.from([
@@ -41,6 +52,7 @@ export const sendCardCreateConfirmMessage = async (
4152
[
4253
InlineKeyboard.text(`✏️ Edit front`, CallbackQueryType.EditFront),
4354
InlineKeyboard.text(`✏️ Edit back`, CallbackQueryType.EditBack),
55+
InlineKeyboard.text(`✏️ Edit example`, CallbackQueryType.EditExample),
4456
],
4557
[InlineKeyboard.text(`❌ Cancel`, CallbackQueryType.Cancel)],
4658
]),

0 commit comments

Comments
 (0)