-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Quick add card + Telegram SDK interaction refactoring
- Loading branch information
Showing
29 changed files
with
457 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { handleError } from "./lib/handle-error/handle-error.ts"; | ||
import { getUser } from "./services/get-user.ts"; | ||
import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; | ||
import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; | ||
import { z } from "zod"; | ||
import { canEditDeck } from "./db/deck/can-edit-deck.ts"; | ||
import { envSchema } from "./env/env-schema.ts"; | ||
import { getDatabase } from "./db/get-database.ts"; | ||
import { tables } from "./db/tables.ts"; | ||
import { DatabaseException } from "./db/database-exception.ts"; | ||
import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts"; | ||
import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; | ||
|
||
const requestSchema = z.object({ | ||
deckId: z.number(), | ||
card: z.object({ | ||
front: z.string(), | ||
back: z.string(), | ||
id: z.number().nullable().optional(), | ||
}), | ||
}); | ||
|
||
export type AddCardRequest = z.infer<typeof requestSchema>; | ||
export type AddCardResponse = null; | ||
|
||
export const onRequestPost = handleError(async ({ request, env }) => { | ||
const user = await getUser(request, env); | ||
if (!user) return createAuthFailedResponse(); | ||
|
||
const input = requestSchema.safeParse(await request.json()); | ||
if (!input.success) { | ||
return createBadRequestResponse(); | ||
} | ||
|
||
const envSafe = envSchema.parse(env); | ||
|
||
const canEdit = await canEditDeck(envSafe, input.data.deckId, user.id); | ||
if (!canEdit) { | ||
return createForbiddenRequestResponse(); | ||
} | ||
|
||
const db = getDatabase(envSafe); | ||
const { data } = input; | ||
|
||
const createCardsResult = await db.from(tables.deckCard).insert({ | ||
deck_id: data.deckId, | ||
front: data.card.front, | ||
back: data.card.back, | ||
}); | ||
|
||
if (createCardsResult.error) { | ||
throw new DatabaseException(createCardsResult.error); | ||
} | ||
|
||
return createJsonResponse<AddCardResponse>(null, 200); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { EnvType } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { tables } from "../tables.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
|
||
export const canEditDeck = async ( | ||
envSafe: EnvType, | ||
deckId: number, | ||
userId: number, | ||
) => { | ||
const db = getDatabase(envSafe); | ||
|
||
const canEditDeckResult = await db | ||
.from(tables.deck) | ||
.select() | ||
.eq("author_id", userId) | ||
.eq("id", deckId); | ||
|
||
if (canEditDeckResult.error) { | ||
throw new DatabaseException(canEditDeckResult.error); | ||
} | ||
|
||
return !!canEditDeckResult.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import WebApp from "@twa-dev/sdk"; | ||
|
||
export const showAlert = (text: string) => { | ||
WebApp.showAlert(text); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import WebApp from "@twa-dev/sdk"; | ||
|
||
export const showConfirm = (text: string): Promise<boolean> => { | ||
return new Promise((resolve) => { | ||
WebApp.showConfirm(text, (confirmed) => { | ||
resolve(confirmed); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useMount } from "../react/use-mount.ts"; | ||
import { autorun } from "mobx"; | ||
import WebApp from "@twa-dev/sdk"; | ||
|
||
export const useTelegramProgress = (cb: () => boolean) => { | ||
return useMount(() => { | ||
return autorun(() => { | ||
if (cb()) { | ||
WebApp.MainButton.showProgress(); | ||
} else { | ||
WebApp.MainButton.hideProgress(); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { observer } from "mobx-react-lite"; | ||
import { css } from "@emotion/css"; | ||
import { Label } from "../../ui/label.tsx"; | ||
import { Input } from "../../ui/input.tsx"; | ||
import React from "react"; | ||
import { CardFormType } from "../../store/deck-form-store.ts"; | ||
|
||
type Props = { | ||
cardForm: CardFormType; | ||
}; | ||
export const CardFormView = observer((props: Props) => { | ||
const { cardForm } = props; | ||
|
||
return ( | ||
<div | ||
className={css({ | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: 6, | ||
marginBottom: 16, | ||
position: "relative", | ||
})} | ||
> | ||
<h3 className={css({ textAlign: "center" })}>Add card</h3> | ||
<Label text={"Front"}> | ||
<Input {...cardForm.front.props} rows={7} type={"textarea"} /> | ||
</Label> | ||
|
||
<Label text={"Back"}> | ||
<Input {...cardForm.back.props} rows={7} type={"textarea"} /> | ||
</Label> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.