Skip to content

Commit

Permalink
Merge branch 'main' into fix/routes-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtouha committed Apr 16, 2024
2 parents 115f025 + c484e20 commit 8182b28
Show file tree
Hide file tree
Showing 17 changed files with 337 additions and 302 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
"use client";

import * as React from "react";
import { type z } from "zod";

import { Button } from "@/components/ui/button";
import { type manageSubscriptionSchema } from "@/lib/validators/stripe";
import type { ManageSubscriptionInput } from "@/server/api/routers/stripe/stripe.input";
import { api } from "@/trpc/react";
import { toast } from "sonner";

type ManageSubscriptionFormProps = z.infer<typeof manageSubscriptionSchema>;

export function ManageSubscriptionForm({
isPro,
stripeCustomerId,
stripeSubscriptionId,
stripePriceId,
}: ManageSubscriptionFormProps) {
}: ManageSubscriptionInput) {
const [isPending, startTransition] = React.useTransition();
const managePlanMutation = api.stripe.managePlan.useMutation();

Expand Down
14 changes: 6 additions & 8 deletions src/app/(main)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { env } from "@/env";
import { api } from "@/trpc/server";
import { type Metadata } from "next";
import * as React from "react";
import { z } from "zod";
import { Posts } from "./_components/posts";
import { PostsSkeleton } from "./_components/posts-skeleton";
import { validateRequest } from "@/lib/auth/validate-request";
import { Paths } from "@/lib/constants";
import { myPostsSchema } from "@/server/api/routers/post/post.input";

export const metadata: Metadata = {
metadataBase: new URL(env.NEXT_PUBLIC_APP_URL),
Expand All @@ -19,13 +19,8 @@ interface Props {
searchParams: Record<string, string | string[] | undefined>;
}

const schmea = z.object({
page: z.coerce.number().default(1).optional(),
perPage: z.coerce.number().default(12).optional(),
});

export default async function DashboardPage({ searchParams }: Props) {
const { page } = schmea.parse(searchParams);
const { page, perPage } = myPostsSchema.parse(searchParams);

const { user } = await validateRequest();
if (!user) redirect(Paths.Login);
Expand All @@ -36,7 +31,10 @@ export default async function DashboardPage({ searchParams }: Props) {
* @see https://www.youtube.com/shorts/A7GGjutZxrs
* @see https://nextjs.org/docs/app/building-your-application/data-fetching/patterns#parallel-data-fetching
*/
const promises = Promise.all([api.post.myPosts.query({ page }), api.stripe.getPlan.query()]);
const promises = Promise.all([
api.post.myPosts.query({ page, perPage }),
api.stripe.getPlan.query(),
]);

return (
<div>
Expand Down
37 changes: 10 additions & 27 deletions src/app/(main)/editor/[postId]/_components/post-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";
import { useRef } from "react";
import { z } from "zod";
import { type RouterOutputs } from "@/trpc/shared";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
Expand All @@ -21,22 +20,14 @@ import { api } from "@/trpc/react";
import { Pencil2Icon } from "@/components/icons";
import { LoadingButton } from "@/components/loading-button";
import Link from "next/link";
import { createPostSchema } from "@/server/api/routers/post/post.input";

const markdownlink = "https://remarkjs.github.io/react-markdown/" // Can also be changed for something like /markdown
const markdownlink = "https://remarkjs.github.io/react-markdown/";

interface Props {
post: RouterOutputs["post"]["get"];
}

const schema = z.object({
title: z.string().min(3).max(255),
excerpt: z.string().min(3).max(255),
content: z
.string()
.min(3)
.max(2048 * 2),
});

export const PostEditor = ({ post }: Props) => {
if (!post) return null;
const formRef = useRef<HTMLFormElement>(null);
Expand All @@ -47,7 +38,7 @@ export const PostEditor = ({ post }: Props) => {
excerpt: post.excerpt,
content: post.content,
},
resolver: zodResolver(schema),
resolver: zodResolver(createPostSchema),
});
const onSubmit = form.handleSubmit(async (values) => {
updatePost.mutate({ id: post.id, ...values });
Expand All @@ -70,11 +61,7 @@ export const PostEditor = ({ post }: Props) => {
</div>
<div className="h-6"></div>
<Form {...form}>
<form
ref={formRef}
onSubmit={onSubmit}
className="block max-w-screen-md space-y-4"
>
<form ref={formRef} onSubmit={onSubmit} className="block max-w-screen-md space-y-4">
<FormField
control={form.control}
name="title"
Expand All @@ -98,9 +85,7 @@ export const PostEditor = ({ post }: Props) => {
<FormControl>
<Textarea {...field} rows={2} className="min-h-0" />
</FormControl>
<FormDescription>
A short description of your post
</FormDescription>
<FormDescription>A short description of your post</FormDescription>
<FormMessage />
</FormItem>
)}
Expand All @@ -127,13 +112,11 @@ export const PostEditor = ({ post }: Props) => {
<PostPreview text={form.watch("content") || post.content} />
</div>
</TabsContent>
<Link
href={markdownlink}
>
<span className="text-[0.8rem] text-muted-foreground underline underline-offset-4">
Supports markdown
</span>
</Link>
<Link href={markdownlink}>
<span className="text-[0.8rem] text-muted-foreground underline underline-offset-4">
Supports markdown
</span>
</Link>
</Tabs>
)}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/editor/[postId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async function EditPostPage({ params }: Props) {
const { user } = await validateRequest();
if (!user) redirect(Paths.Login);

const post = await api.post.get.query(params.postId);
const post = await api.post.get.query({ id: params.postId });
if (!post) notFound();

return (
Expand Down
3 changes: 2 additions & 1 deletion src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";
import { env } from "@/env.js";
import { db } from "@/server/db";
import { sessions, users, type User as DbUser } from "@/server/db/schema";
import { absoluteUrl } from "@/lib/utils"

// Uncomment the following lines if you are using nodejs 18 or lower. Not required in Node.js 20, CloudFlare Workers, Deno, Bun, and Vercel Edge Functions.
// import { webcrypto } from "node:crypto";
Expand Down Expand Up @@ -39,7 +40,7 @@ export const lucia = new Lucia(adapter, {
export const discord = new Discord(
env.DISCORD_CLIENT_ID,
env.DISCORD_CLIENT_SECRET,
env.NEXT_PUBLIC_APP_URL + "/login/discord/callback",
absoluteUrl("/login/discord/callback")
);

declare module "lucia" {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ export function formatPrice(
}

export function absoluteUrl(path: string) {
return `${env.NEXT_PUBLIC_APP_URL}${path}`;
return new URL(path, env.NEXT_PUBLIC_APP_URL).href
}
6 changes: 3 additions & 3 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { postRouter } from "./routers/post";
import { stripeRouter } from "./routers/stripe";
import { userRouter } from "./routers/user";
import { postRouter } from "./routers/post/post.procedure";
import { stripeRouter } from "./routers/stripe/stripe.procedure";
import { userRouter } from "./routers/user/user.procedure";
import { createTRPCRouter } from "./trpc";

export const appRouter = createTRPCRouter({
Expand Down
112 changes: 0 additions & 112 deletions src/server/api/routers/post.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/server/api/routers/post/post.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { z } from "zod";

export const listPostsSchema = z.object({
page: z.number().int().default(1),
perPage: z.number().int().default(12),
});
export type ListPostsInput = z.infer<typeof listPostsSchema>;

export const getPostSchema = z.object({
id: z.string(),
});
export type GetPostInput = z.infer<typeof getPostSchema>;

export const createPostSchema = z.object({
title: z.string().min(3).max(255),
excerpt: z.string().min(3).max(255),
content: z.string().min(3),
});
export type CreatePostInput = z.infer<typeof createPostSchema>;

export const updatePostSchema = createPostSchema.extend({
id: z.string(),
});
export type UpdatePostInput = z.infer<typeof updatePostSchema>;

export const deletePostSchema = z.object({
id: z.string(),
});
export type DeletePostInput = z.infer<typeof deletePostSchema>;

export const myPostsSchema = z.object({
page: z.number().int().default(1),
perPage: z.number().int().default(12),
});
export type MyPostsInput = z.infer<typeof myPostsSchema>;
29 changes: 29 additions & 0 deletions src/server/api/routers/post/post.procedure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createTRPCRouter, protectedProcedure } from "../../trpc";
import * as inputs from "./post.input";
import * as services from "./post.service";

export const postRouter = createTRPCRouter({
list: protectedProcedure
.input(inputs.listPostsSchema)
.query(({ ctx, input }) => services.listPosts(ctx, input)),

get: protectedProcedure
.input(inputs.getPostSchema)
.query(({ ctx, input }) => services.getPost(ctx, input)),

create: protectedProcedure
.input(inputs.createPostSchema)
.mutation(({ ctx, input }) => services.createPost(ctx, input)),

update: protectedProcedure
.input(inputs.updatePostSchema)
.mutation(({ ctx, input }) => services.updatePost(ctx, input)),

delete: protectedProcedure
.input(inputs.deletePostSchema)
.mutation(async ({ ctx, input }) => services.deletePost(ctx, input)),

myPosts: protectedProcedure
.input(inputs.myPostsSchema)
.query(({ ctx, input }) => services.myPosts(ctx, input)),
});
Loading

0 comments on commit 8182b28

Please sign in to comment.