Skip to content
Merged

Dev #84

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
- https://www.scouting.org/commissioners/newsletter-eblast/
- https://www.scouting.org/international/resources/
- https://seascout.org/program-updates/
- https://www.scouting.org/commissioners/news-for-commissioners/
- https://www.scouting.org/commissioners/news-for-commissioners/monthly-all-commissioner-email/

## email newsletters

Expand Down
2 changes: 1 addition & 1 deletion src/features/feeds/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const feedConfigs: CreateFeedOpts[] = [
// }),
// },

// todo why did I disable this one?
// todo it looks like this is about to be shut down. I downloaded the rss and and all the episodes. set up an archived version later
// {
// name: "The Lookout",
// slug: "the-lookout",
Expand Down
23 changes: 23 additions & 0 deletions src/features/postsQuery/queryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import qs from "qs";
import { type QueryOpts, queryOptsSchema } from "@/features/postsQuery/query";

export const postsQueryParamsEncoder = {
encode,
decode,
};

/** encode a JSON query into a URLSearchParams query */
function encode(query: QueryOpts) {
const queryString = qs.stringify(query, { allowDots: true });

return new URLSearchParams(queryString);
}

/** decode a URLSearchParams query into a JSON query */
function decode(searchParams: URLSearchParams) {
const queryString = searchParams.toString();

const queryRawJson = qs.parse(queryString, { allowDots: true });

return queryOptsSchema.safeParse(queryRawJson);
}
24 changes: 6 additions & 18 deletions src/pages/api/posts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const prerender = false;
import type { APIRoute } from "astro";
import { queryPosts, queryOptsSchema } from "@/features/postsQuery/query";
import qs from "qs";
import { postsQueryParamsEncoder } from "@/features/postsQuery/queryParams";

export const POST: APIRoute = async (context) => {
const body = await context.request.json();
Expand All @@ -11,11 +11,7 @@ export const POST: APIRoute = async (context) => {
if (error) {
return new Response(
JSON.stringify({
errors: error.issues.map((i) => ({
path: i.path.join("."),
message: i.message,
code: i.code,
})),
errors: error.issues,
}),
{
status: 400,
Expand All @@ -37,22 +33,14 @@ export const POST: APIRoute = async (context) => {
};

export const GET: APIRoute = async (context) => {
const urlParams = context.url.searchParams.toString();

const queryRaw = qs.parse(urlParams, { allowDots: true });

console.log(queryRaw);

const { error, data: query } = queryOptsSchema.safeParse(queryRaw);
const { error, data: query } = postsQueryParamsEncoder.decode(
context.url.searchParams,
);

if (error) {
return new Response(
JSON.stringify({
errors: error.issues.map((i) => ({
path: i.path.join("."),
message: i.message,
code: i.code,
})),
errors: error.issues,
}),
{
status: 400,
Expand Down
16 changes: 3 additions & 13 deletions src/pages/pulse/browse/_page.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { CardList } from "@/components/react/cardList";
import { RenderPost as PostComponent } from "@/components/react/post";
import { queryPosts } from "@/features/postsQuery/query";
import { queryPosts, type QueryOpts } from "@/features/postsQuery/query";

export async function BrowsePage() {
const results = await queryPosts({
filter: {},
sort: {
mode: "date",
direction: "desc",
},
paginate: {
maxPageSize: 100,
page: 1,
},
});
export async function BrowsePage({ query }: { query: QueryOpts }) {
const results = await queryPosts(query);
return (
<>
<span>
Expand Down
18 changes: 17 additions & 1 deletion src/pages/pulse/browse/index.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
---
import UiLayout from "@/components/layout/UiLayout.astro";
import { BrowsePage } from "@/pages/pulse/browse/_page";
import { postsQueryParamsEncoder } from "@/features/postsQuery/queryParams";
import { jsonPrettyPrint } from "@/util/jsonPrettyPrint";

const { searchParams } = Astro.url;

const { error, data: query } = postsQueryParamsEncoder.decode(searchParams);

if (error) {
Astro.response.status = 400;
}

export const prerender = false;
---

<UiLayout>
<BrowsePage />
{
query ? (
<BrowsePage query={query} />
) : (
<p>{jsonPrettyPrint(error.issues)}</p>
)
}
</UiLayout>
5 changes: 5 additions & 0 deletions src/util/jsonPrettyPrint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** turns a JSON object into a string, pretty-printed with tabs */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function jsonPrettyPrint(obj: any) {
return JSON.stringify(obj, null, "\t");
}
Loading