Skip to content
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

fix: Update blog filtering #34

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 6 additions & 2 deletions app/routes/__main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import ReactMarkdown from 'react-markdown';
import * as blog from 'services/blog.server';

// UTILS
import { cachedLoaderResponse, stripParamsAndHash } from 'utils/utils.server';
import {
cachedLoaderResponse,
filterPostsByEnvironment,
stripParamsAndHash,
} from 'utils/utils.server';

// TYPES
import type { DynamicLinksFunction } from 'remix-utils';
Expand All @@ -24,7 +28,7 @@ import type { Post } from '@prisma/client';
// EXPORTS
export const loader: LoaderFunction = async ({ request }) => {
const bookmarks = await blog.getBookmarksAll();
const posts = await blog.getPostsAll();
const posts = filterPostsByEnvironment(await blog.getPostsAll());
const canonical = stripParamsAndHash(request.url);

// Only send 8 most recent bookmarks / posts
Expand Down
8 changes: 6 additions & 2 deletions app/routes/__main/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { ContainerCenter, links as containerCenterLinks } from 'components/Conta
import ReactMarkdown from 'react-markdown';

// UTILS
import { cachedLoaderResponse, stripParamsAndHash } from 'utils/utils.server';
import {
cachedLoaderResponse,
filterPostsByEnvironment,
stripParamsAndHash,
} from 'utils/utils.server';

// SERVICES
import * as blog from 'services/blog.server';
Expand All @@ -22,7 +26,7 @@ import type { Post } from '@prisma/client';

// EXPORTS
export const loader: LoaderFunction = async ({ request }) => {
const posts = await blog.getPostsAll();
const posts = filterPostsByEnvironment(await blog.getPostsAll());
const canonical = stripParamsAndHash(request.url);

const data = {
Expand Down
1 change: 1 addition & 0 deletions app/routes/admin/blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const loader: LoaderFunction = async ({ params }) => {
try {
const authors = await users.getUsersAreBlogAuthors();
const images = await media.getImagesAll();
// Not filtering because we want all posts in admin
const posts = await blog.getPostsAll();

return json({ authors, images: images.resources, posts }, { status: 200 });
Expand Down
36 changes: 18 additions & 18 deletions app/routes/posts/$slug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,26 @@ export default function Post(): React.ReactElement {
const document = getDocument();

// HOOKS - REF
const scrollToRef = React.useRef<HTMLDivElement>(null);
// const scrollToRef = React.useRef<HTMLDivElement>(null);

// HOOKS - EFFECTS
// Handles edge case of user not being quite at the bottom of the screen when opening accordion
React.useEffect(() => {
let timeout: NodeJS.Timeout | undefined;
// React.useEffect(() => {
// let timeout: NodeJS.Timeout | undefined;

if (search.get('mailing') !== null && scrollToRef.current instanceof HTMLDivElement) {
timeout = setTimeout(() => {
const dims = scrollToRef?.current?.getBoundingClientRect();
const bodyScrollHeight = document.body.scrollHeight;
// if (search.get('mailing') !== null && scrollToRef.current instanceof HTMLDivElement) {
// timeout = setTimeout(() => {
// const dims = scrollToRef?.current?.getBoundingClientRect();
// const bodyScrollHeight = document.body.scrollHeight;

window.scrollTo({ top: bodyScrollHeight + dims!.y, behavior: 'smooth' });
}, 400);
}
// window.scrollTo({ top: bodyScrollHeight + dims!.y, behavior: 'smooth' });
// }, 400);
// }

if (timeout) {
return () => clearTimeout(timeout);
}
}, [scrollToRef, search]);
// if (timeout) {
// return () => clearTimeout(timeout);
// }
// }, [scrollToRef, search]);

// VARS
const {
Expand Down Expand Up @@ -315,17 +315,17 @@ export default function Post(): React.ReactElement {
}}
/>

<hr className='jdg-post-break' />
{/* <hr className='jdg-post-break' /> */}

{/* TODO: abstract to separate component */}
<div className='jdg-post-email-list-signup'>
{/* TODO: Set up magic link to confirm signups. */}
{/* <div className='jdg-post-email-list-signup'>
<Accordion heading='Sign up for my newsletter' name='mailing'>
<subscribe.Form action='/action/subscribe' method='post'>
{subscribe.type === 'done' && !subscribe.data && (
<div className='jdg-post-email-list-signup-confirmation'>
<h4>You've successfully signed up!</h4>
<p>Thank you for subscribing to the newsletter.</p>
{/* TODO: Set up magic link to confirm signups. */}
</div>
)}
{(subscribe.type !== 'done' || subscribe.data) && (
Expand Down Expand Up @@ -374,7 +374,7 @@ export default function Post(): React.ReactElement {
</subscribe.Form>
</Accordion>
<div className='jdg-post-signup-scroll-to' ref={scrollToRef} />
</div>
</div> */}
</ContainerCenter>
</main>

Expand Down
15 changes: 1 addition & 14 deletions app/services/blog.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,7 @@ export const getBookmarksAll = async () => {
// READ - posts
export const getPostsAll = async () => {
try {
const now = new Date().toISOString();
const allPosts = await prisma.post.findMany();
const allPostsCurrentNoTests = allPosts.filter((post) => {
if (!post.slug.includes('test') && now > post.published_at.toISOString()) {
return post;
}
});

// If dev, return test posts and future posts
if (process.env.NODE_ENV === 'development') {
return allPosts;
}
// Otherwise, filtered posts only
return allPostsCurrentNoTests;
return await prisma.post.findMany();
} catch (error) {
throw json(error);
}
Expand Down
18 changes: 17 additions & 1 deletion app/utils/utils.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { json } from '@remix-run/node';

// TYPES
import { FormValue } from 'types/types.server';
import { Post } from '@prisma/client';

// TODO: ORGANIZE THIS

export function typedBoolean<T>(value: T): value is Exclude<T, '' | 0 | false | null | undefined> {
return Boolean(value);
}
Expand Down Expand Up @@ -103,3 +103,19 @@ export function cachedLoaderResponse(request: Request, data: any, maxAge: number
});
}
}
/**
* Removes test posts and posts scheduled for the future from prod env
* @param {Post[]} posts array of all post objects
* @returns {Post[]} filtered array of post objects
*/
export function filterPostsByEnvironment(posts: Post[]): Post[] {
if (process.env.NODE_ENV === 'production') {
const now = new Date().toISOString();
return posts.filter((post) => {
if (!post.slug.includes('test') && now > post.published_at.toISOString()) {
return post;
}
});
}
return posts;
}
9 changes: 7 additions & 2 deletions app/utils/utils.sitemap.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import * as blog from 'services/blog.server';

// UTILS
import { isEqual } from 'lodash';
import { getDomainUrl, removeDoubleSlashes, typedBoolean } from 'utils/utils.server';
import {
filterPostsByEnvironment,
getDomainUrl,
removeDoubleSlashes,
typedBoolean,
} from 'utils/utils.server';

// TYPES
import type { EntryContext } from '@remix-run/node';
Expand All @@ -24,7 +29,7 @@ export async function getSitemapXml(request: Request, remixContext: EntryContext
`.trim();
}

const siteMapEntriesFromPosts: SitemapEntry[] = await (
const siteMapEntriesFromPosts: SitemapEntry[] = await filterPostsByEnvironment(
await blog.getPostsAll()
).map((post) => {
return {
Expand Down