Skip to content

Commit

Permalink
Merge pull request #244 from aXenDeveloper/fix/forum_name_length
Browse files Browse the repository at this point in the history
fix(forum): Add max length for forum name
  • Loading branch information
aXenDeveloper authored Mar 1, 2024
2 parents 47e9c38 + f9a455d commit 169ff8f
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion backend/src/apps/admin/forum/database/schema/forums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const forum_forums_name = pgTable(
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value").notNull()
value: varchar("value", { length: 50 }).notNull()
},
table => ({
forum_id_idx: index("forum_forums_name_forum_id_idx").on(table.forum_id),
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/frontend/components/editor.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ You can set this field as **required**:

```ts
const formSchema = z.object({
content: zodInput.languageInputRequired
content: zodInput.languageInput.min(1)
});
```

Expand Down
23 changes: 9 additions & 14 deletions docs/pages/frontend/i18n.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,17 @@ const formSchema = z.object({
});
```

You can add more functions to validate translations.

```ts {8, 11}
import * as z from "zod";

import { zodInput } from "@/functions/zod";
Or you can set **maxLength **:

```ts
const formSchema = z.object({
name: z
.array(
z.object({
language_code: zodInput.string,
value: zodInput.string.min(3).max(50)
})
)
.min(1)
content: zodInput.languageInput
.min(1, {
message: t("errors.required")
})
.refine(value => value.every(item => item.value.length <= 100), {
message: t("errors.max_length", { length: 100 })
})
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useCreateEditFormGroupsMembersAdmin = ({
const { push } = useRouter();

const formSchema = z.object({
name: zodInput.languageInputRequired,
name: zodInput.languageInput.min(1),
test: zodInput.string,
color: zodInput.string
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export const useCreateEditFormForumAdmin = ({
const { setOpen } = useDialog();

const formSchema = z.object({
name: zodInput.languageInputRequired,
name: zodInput.languageInput
.min(1, {
message: t("errors.required")
})
.refine(value => value.every(item => item.value.length <= 50), {
message: t("errors.max_length", { length: 50 })
}),
description: zodInput.languageInput,
permissions: z.object({
can_all_view: z.boolean(),
Expand All @@ -42,13 +48,14 @@ export const useCreateEditFormForumAdmin = ({
name: data?.name || [],
description: data?.description || [],
permissions: {
can_all_view: data?.permissions.can_all_view || false,
can_all_read: data?.permissions.can_all_read || false,
can_all_create: data?.permissions.can_all_create || false,
can_all_reply: data?.permissions.can_all_reply || false,
can_all_view: data?.permissions.can_all_view || true,
can_all_read: data?.permissions.can_all_read || true,
can_all_create: data?.permissions.can_all_create || true,
can_all_reply: data?.permissions.can_all_reply || true,
groups: data?.permissions.groups || []
}
}
},
mode: "onTouched"
});

const onSubmit = async (values: z.infer<typeof formSchema>) => {
Expand Down
8 changes: 0 additions & 8 deletions frontend/functions/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@ export const zodInput = {
value: z.string().trim()
})
),
languageInputRequired: z
.array(
z.object({
language_code: z.string(),
value: z.string().trim().min(1)
})
)
.min(1),
string: z.string().trim()
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useCreatePost = ({ setOpen }: Args) => {
const { id } = useParams();

const formSchema = z.object({
content: zodInput.languageInputRequired
content: zodInput.languageInput.min(1)
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
Expand Down
15 changes: 9 additions & 6 deletions frontend/hooks/forums/forum/topics/create/use-create-topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ export const useCreateTopic = ({ forumId }: Props) => {
const { convertNameToLink } = useTextLang();

const formSchema = z.object({
title: zodInput.languageInputRequired.refine(
value => value.every(item => item.value.length <= 100),
{
title: zodInput.languageInput
.min(1, {
message: t("errors.required")
})
.refine(value => value.every(item => item.value.length <= 100), {
message: t("errors.max_length", { length: 100 })
}
),
content: zodInput.languageInputRequired
}),
content: zodInput.languageInput.min(1, {
message: t("errors.required")
})
});

const form = useForm<z.infer<typeof formSchema>>({
Expand Down
1 change: 1 addition & 0 deletions frontend/langs/en/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
"no_connection_api": "No connection to the API",
"access_denied": "Access denied",
"internal_server_error": "Internal server error",
"required": "This field is required.",
"min_length": "This field must be at least {length} characters.",
"max_length": "This field must be less than {length} characters.",
"403": "Access denied",
Expand Down
1 change: 1 addition & 0 deletions frontend/langs/pl/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
"no_connection_api": "Brak połączenia z API",
"access_denied": "Brak dostępu",
"internal_server_error": "Wewnętrzny błąd serwera",
"required": "To pole jest wymagane.",
"min_length": "Minimalna długość to {length} znaków.",
"max_length": "Maksymalna długość to {length} znaków.",
"403": "Brak dostępu",
Expand Down
4 changes: 2 additions & 2 deletions frontend/themes/1/forum/views/forum/forums/item/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ItemForum = ({

return (
<WrapperItemForum
className="px-6 py-4 border-t hover:bg-muted/50 flex gap-4 cursor-pointer flex-col md:flex-row pointer-events-none md:pointer-events-auto"
className="px-6 py-4 border-t hover:bg-muted/50 flex gap-4 cursor-pointer flex-col md:flex-row select-none md:select-auto"
href={href}
>
<div className="flex gap-4 flex-1">
Expand All @@ -48,7 +48,7 @@ export const ItemForum = ({
{description.length > 0 && (
<ReadOnlyEditor
id={`${id}_description`}
className="text-muted-foreground text-sm [&_p]:m-0"
className="text-muted-foreground text-sm [&_p]:m-0 pointer-events-none md:pointer-events-auto"
value={description}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export const ItemTopicListForum = ({
const href = `/topic/${convertNameToLink({ id, name: title })}`;

return (
<WrapperItemTopicListForum href={href}>
<WrapperItemTopicListForum
href={href}
className="px-6 py-4 hover:bg-muted/50 cursor-pointer select-none md:select-auto"
>
<div className="flex flex-col">
<h3 className="font-semibold text-base">
<Link href={href} className="text-foreground no-underline">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import { useRouter } from "@/i18n";
interface Props {
children: ReactNode;
href: string;
className?: string;
}

export const WrapperItemTopicListForum = ({ children, href }: Props) => {
export const WrapperItemTopicListForum = ({
children,
className,
href
}: Props) => {
const { push } = useRouter();

return (
<div
onClick={() => push(href)}
className="px-6 py-4 hover:bg-muted/50 cursor-pointer pointer-events-none md:pointer-events-auto"
>
<div onClick={() => push(href)} className={className}>
{children}
</div>
);
Expand Down

0 comments on commit 169ff8f

Please sign in to comment.