Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
itizawa committed Dec 1, 2024
1 parent 7aa2fba commit ba36ce6
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 15 deletions.
23 changes: 23 additions & 0 deletions apps/web/src/app/dashboards/blogs/[blogId]/settings/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Stack, Typography } from '@mui/material';
import { notFound } from 'next/navigation';
import { getBlog } from '~/actions/blog';
import { getCurrentUser } from '~/actions/user';
import { generateWisblogMetadata } from '~/libs/generateWisblogMetadata';

export const metadata = generateWisblogMetadata({ title: '記事一覧' });

export default async function Page({ params }: { params: { blogId: string } }) {
const { currentUser } = await getCurrentUser();
if (!currentUser) return null;

const blog = await getBlog({ id: params.blogId });

if (!blog) return notFound();

return (
<Stack maxWidth={900} mx='auto' pt={2} pb={4} gap={3} px={2}>
<Typography variant='h5'>ブログ設定</Typography>
{/* */}
</Stack>
);
}
4 changes: 2 additions & 2 deletions apps/web/src/app/dashboards/blogs/new/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Stack, Typography } from '@mui/material';
import { CreateBlogForm } from '~/components/models/blog/CreateBlogForm';
import { EditBlogForm } from '~/components/models/blog/EditBlogForm';

export default function Page() {
return (
<Stack maxWidth={600} mx='auto' pt={4} gap={3} px={2}>
<Typography variant='h5'>新規作成</Typography>
<CreateBlogForm />
<EditBlogForm />
</Stack>
);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { NoteAdd } from '@mui/icons-material';
import { LoadingButton } from '@mui/lab';
import { Stack, TextField, useTheme } from '@mui/material';
import { BlogSchema } from '@repo/types';
import { type Blog, BlogSchema } from '@repo/types';
import { useSnackbar } from 'notistack';
import type { FC } from 'react';
import { Controller, useForm } from 'react-hook-form';
Expand All @@ -15,27 +15,45 @@ import { generateSubDomainUrl } from '~/utils/generateSubDomainUrl';
const inputSchema = BlogSchema.pick({ name: true, subDomain: true });
type InputState = z.infer<typeof inputSchema>;

export const CreateBlogForm: FC = () => {
type Props = {
existingBlog?: Blog;
};

export const EditBlogForm: FC<Props> = ({ existingBlog }) => {
const { enqueueSnackbar } = useSnackbar();
const { palette } = useTheme();

const { control, formState, handleSubmit } = useForm<InputState>({
defaultValues: {
name: '',
subDomain: '',
},
defaultValues: existingBlog
? {
name: existingBlog.name,
subDomain: existingBlog.subDomain,
}
: {
name: '',
subDomain: '',
},
resolver: zodResolver(inputSchema),
mode: 'onChange',
});

const onSubmit = handleSubmit(async ({ name, subDomain }) => {
try {
await createBlog({
name,
subDomain,
});
enqueueSnackbar({ message: 'ブログを作成しました', variant: 'success' });
window.location.href = generateSubDomainUrl(subDomain);
if (existingBlog) {
// await updateBlog({
// id: existingBlog.id,
// name,
// subDomain,
// });
enqueueSnackbar({ message: 'ブログを更新しました', variant: 'success' });
} else {
await createBlog({
name,
subDomain,
});
enqueueSnackbar({ message: 'ブログを作成しました', variant: 'success' });
window.location.href = generateSubDomainUrl(subDomain);
}
} catch (error) {
enqueueSnackbar({ message: (error as Error).message, variant: 'error' });
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/models/blog/EditBlogForm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EditBlogForm } from './EditBlogForm';
9 changes: 9 additions & 0 deletions packages/api/src/controllers/blogRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ export const blogRouter = router({

return blogs.map(blog => blog.subDomain);
}),
update: protectedProcedure
.input(BlogSchema.pick({ name: true, subDomain: true }))
.mutation(async ({ ctx, input }) => {
return await createBlogUseCase.execute({
name: input.name,
subDomain: input.subDomain,
ownerId: ctx.user.id,
});
}),
});
24 changes: 24 additions & 0 deletions packages/api/src/usecases/blog/UpdateBlogUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { PrismaClient } from '@prisma/client';
import { BadRequest, type Blog } from '@repo/types';

export class CreateBlogUseCase {
constructor(private readonly prismaClient: PrismaClient) {}

async execute(args: Pick<Blog, 'name' | 'subDomain' | 'ownerId'>): Promise<{ createdBlog: Blog }> {
const sameSubDomainBlog = await this.prismaClient.blog.findFirst({
where: {
subDomain: args.subDomain,
},
});

if (sameSubDomainBlog) {
throw new BadRequest('同一のサブドメインのブログが存在します');
}

const createdBlog = await this.prismaClient.blog.create({
data: args,
});

return { createdBlog };
}
}

0 comments on commit ba36ce6

Please sign in to comment.