-
Notifications
You must be signed in to change notification settings - Fork 2
use live collections to fetch grammar #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d12e2ac
use live collections to fetch gp data
SillyCoon b7cc1fe
Merge branch 'master' into use-live-collections
SillyCoon a0576ee
add error page
SillyCoon aba7830
improve grammar live collection
SillyCoon 2e470f2
add error page
SillyCoon fc650d6
improve live collection error handling
SillyCoon ec88adc
remove error prerender
SillyCoon 6f70182
add better cache key
SillyCoon 7393405
improve user feedback for not found gp
SillyCoon 2ab23c5
fix formatting
SillyCoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| export class GrammarPointsLoaderError extends Error { | ||
| constructor( | ||
| message: string, | ||
| public code?: number, | ||
| ) { | ||
| super(message); | ||
| this.name = "GrammarPointsLoaderError"; | ||
| } | ||
| } | ||
| export class GrammarPointNotFoundError extends GrammarPointsLoaderError { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = "GrammarPointNotFoundError"; | ||
| this.code = 404; | ||
| } | ||
| } |
This file contains hidden or 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,82 @@ | ||
| import type { LiveLoader } from "astro/loaders"; | ||
| import { | ||
| type Context, | ||
| type GrammarPoint, | ||
| fetchAllGrammarPoints, | ||
| fetchGrammarPoint, | ||
| fetchGrammarPoints, | ||
| } from "grammar-sdk"; | ||
| import logger from "libs/logger"; | ||
| import { GrammarPointNotFoundError, GrammarPointsLoaderError } from "./errors"; | ||
|
|
||
| const grammarPointsToEntries = (grammarPoints: GrammarPoint[]) => ({ | ||
| entries: grammarPoints.map(grammarPointToEntry), | ||
| }); | ||
|
|
||
| const grammarPointToEntry = (grammarPoint: GrammarPoint) => ({ | ||
| id: grammarPoint.id, | ||
| data: grammarPoint, | ||
| cacheHint: { | ||
| tags: [`grammar-point-${grammarPoint.id}`], | ||
| }, | ||
| }); | ||
|
|
||
| export function grammarPointsLoader(): LiveLoader< | ||
| GrammarPoint, | ||
| { id: string; context: Context }, | ||
| { ids: string[]; context: Context } | { context: Context } | ||
| > { | ||
| return { | ||
| name: "grammarPoints", | ||
| loadCollection: async ({ filter }) => { | ||
| try { | ||
| if (filter && "ids" in filter) { | ||
| return { | ||
| ...grammarPointsToEntries( | ||
| await fetchGrammarPoints(filter.ids, filter.context), | ||
| ), | ||
| cacheHint: { | ||
| tags: [ | ||
| `grammar-points-${filter.context.user.role}-${filter.ids.join(",")}`, | ||
| ], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const context = filter?.context ?? { user: { role: "guest" } }; | ||
|
|
||
| return { | ||
| ...grammarPointsToEntries(await fetchAllGrammarPoints(context)), | ||
| cacheHint: { | ||
| tags: [`grammar-points-${context.user.role}`], | ||
| }, | ||
| }; | ||
| } catch (error) { | ||
| const message = "Failed to load grammar points"; | ||
| logger.error(error, message); | ||
| return { | ||
| error: new GrammarPointsLoaderError(message, 500), | ||
| }; | ||
| } | ||
| }, | ||
| loadEntry: async ({ filter: { id, context } }) => { | ||
| try { | ||
| const entry = await fetchGrammarPoint(id, context); | ||
| if (!entry) { | ||
| return { | ||
| error: new GrammarPointNotFoundError( | ||
| `Grammar point with id ${id} not found`, | ||
| ), | ||
| }; | ||
| } | ||
| return grammarPointToEntry(entry); | ||
| } catch (error) { | ||
| const message = `Failed to load grammar point with id ${id}`; | ||
| logger.error(error, message); | ||
| return { | ||
| error: new GrammarPointsLoaderError(message, 500), | ||
| }; | ||
| } | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or 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,8 @@ | ||
| import { defineLiveCollection } from "astro:content"; | ||
| import { grammarPointsLoader } from "./features/grammar-point/loader"; | ||
|
|
||
| const grammarPoints = defineLiveCollection({ | ||
| loader: grammarPointsLoader(), | ||
| }); | ||
|
|
||
| export const collections = { grammarPoints }; |
This file contains hidden or 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 hidden or 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,14 @@ | ||
| --- | ||
| import Layout from "~/layouts/Layout.astro"; | ||
| --- | ||
|
|
||
| <Layout title="Error"> | ||
| <div class="grid place-items-center w-full h-full"> | ||
| <div class="flex flex-col items-center gap-4"> | ||
| <p class="text-xl text-secondary"> | ||
| We're experiencing technical difficulties. Please try again later. | ||
| </p> | ||
| <a href="/" class="text-blue-500 hover:underline">Go back to home</a> | ||
| </div> | ||
| </div> | ||
| </Layout> |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,16 +1,28 @@ | ||
| --- | ||
| import { GET as fetchGpInReview } from "@api/sr/review/list"; | ||
| import { fetchAllGrammarPoints } from "grammar-sdk"; | ||
| import { Grammar } from "../../components/solid/grammar/Grammar"; | ||
| import Layout from "../../layouts/Layout.astro"; | ||
| import { contextFromAstro } from "~/libs/context"; | ||
| import { getLiveCollection } from "astro:content"; | ||
|
|
||
| const { entries, error } = await getLiveCollection("grammarPoints", { | ||
| context: contextFromAstro(Astro), | ||
| }); | ||
|
|
||
| if (error) { | ||
| return Astro.redirect("/error"); | ||
| } | ||
|
|
||
| const grammar = await fetchAllGrammarPoints(contextFromAstro(Astro)); | ||
| const inReview = (await (await fetchGpInReview(Astro)).json()) as string[]; | ||
| const maybeMode = Astro.url.searchParams.get("mode"); | ||
| const mode = maybeMode === "cram" ? "cram" : "nav"; | ||
| --- | ||
|
|
||
| <Layout title="Grammar" topPadding={false} transition:persist> | ||
| <Grammar client:load grammar={grammar} inReview={inReview} mode={mode} /> | ||
| <Grammar | ||
| client:load | ||
| grammar={entries?.map((entry) => entry.data) ?? []} | ||
| inReview={inReview} | ||
| mode={mode} | ||
| /> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| </Layout> | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| --- | ||
| import { SessionResult } from "@components/solid/Exercise/SessionResult"; | ||
| import Layout from "@layouts/Layout.astro"; | ||
| import { fetchAllGrammarPoints } from "grammar-sdk"; | ||
| import { getLiveCollection } from "astro:content"; | ||
| import { contextFromAstro } from "~/libs/context"; | ||
|
|
||
| const grammar = await fetchAllGrammarPoints(contextFromAstro(Astro)); | ||
| const { entries: grammar, error } = await getLiveCollection("grammarPoints", { | ||
| context: contextFromAstro(Astro), | ||
| }); | ||
|
|
||
| if (error) { | ||
| return Astro.redirect("/error"); | ||
| } | ||
| --- | ||
|
|
||
| <Layout title="Practice results"> | ||
| <SessionResult | ||
| client:only="solid-js" | ||
| sessionResult={undefined} | ||
| grammar={grammar} | ||
| grammar={grammar?.map((entry) => entry.data) ?? []} | ||
| /> | ||
| </Layout> |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.