Skip to content

Commit

Permalink
fix: minor crate post fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdibha committed Jan 22, 2024
1 parent ee58db9 commit 7dd3418
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/components/ui/input-images.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ interface InputImagesProps {
}[]
>
>;
onChangeFilesLength: (length: number) => boolean;
}

export const InputImages = (props: InputImagesProps) => {
const { value: images, onImagesChange } = props;
const { value: images, onImagesChange, onChangeFilesLength } = props;

const onChangePicture = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
if (event.target.files) {
const res = onChangeFilesLength(event.target.files.length);
if (!res) return;
for (const file of event.target.files) {
if (file) {
if (file.size / 1024 / 1024 > 5) {
Expand All @@ -45,7 +48,7 @@ export const InputImages = (props: InputImagesProps) => {
event.currentTarget.value = "";
}
},
[onImagesChange]
[onChangeFilesLength, onImagesChange]
);

const handleRemove = (index: number) => {
Expand Down
2 changes: 1 addition & 1 deletion src/config/site-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const siteConfig = {
externalLinks: [
{ icon: FacebookIcon, url: "https://www.facebook.com/groups/160143328137207" },
{ icon: MailIcon, url: "mailto:hello@mehdibha.com" },
{ icon: GithubIcon, url: "https://github.com/mehdibha/vapi" },
{ icon: GithubIcon, url: "https://github.com/mehdibha/vapi.tn" },
],
},
header: {
Expand Down
11 changes: 10 additions & 1 deletion src/modules/posts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ export const postRouter = createTRPCRouter({
};
}),
create: protectedProcedure
.input(z.object({ content: z.string().min(1), images: z.array(z.string().url()) }))
.input(z.object({ content: z.string(), images: z.array(z.string().url()) }))
.mutation(async ({ ctx, input }) => {
if (input.images.length === 0 && input.content === "") {
throw new TRPCError({ code: "BAD_REQUEST", message: "Post cannot be empty" });
}
if (input.images.length > 5) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Post cannot have more than 5 images",
});
}
return ctx.db.post.create({
data: {
content: input.content,
Expand Down
19 changes: 16 additions & 3 deletions src/modules/posts/components/create-post-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export const CreatePostModal = (props: CreatePostModalProps) => {
});
const utils = api.useUtils();
const createPost = api.post.create.useMutation({
onError: () => {
toast("Une erreur est survenue lors de la publication");
onError: (error) => {
setIsLoading(false);
toast(`Une erreur est survenue lors de la publication (${error.message})`);
},
onSuccess: async (addedPost) => {
form.reset();
Expand Down Expand Up @@ -97,6 +98,14 @@ export const CreatePostModal = (props: CreatePostModalProps) => {
}
};

const handleLimitImages = (length: number) => {
if (length + images.length > 5) {
toast("Vous ne pouvez pas ajouter plus de 5 images");
return false;
}
return true;
};

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{children}</DialogTrigger>
Expand Down Expand Up @@ -140,7 +149,11 @@ export const CreatePostModal = (props: CreatePostModalProps) => {
);
}}
/>
<InputImages value={images} onImagesChange={setImages} />
<InputImages
value={images}
onImagesChange={setImages}
onChangeFilesLength={handleLimitImages}
/>
</div>
</div>
<div className="mt-4 flex justify-end px-6">
Expand Down
2 changes: 1 addition & 1 deletion src/modules/posts/components/feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const Feed = () => {
<CreatePostCard />
{posts.map((post, index) => (
<PostCard
key={index}
key={post.id}
ref={posts.length - 2 === index ? ref : undefined}
postId={post.id}
author={{
Expand Down
2 changes: 1 addition & 1 deletion src/modules/posts/components/post-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const PostCard = React.forwardRef(
<div>
<p className="p-6 pt-0">{content}</p>
{images.length > 0 && (
<Carousel className="h-[300px] w-full">
<Carousel className="h-[300px] bg-muted w-full">
<CarouselContent>
{images.map((image, index) => (
<CarouselItem key={index}>
Expand Down

0 comments on commit 7dd3418

Please sign in to comment.