Files
docmost/apps/client/src/features/space/components/edit-space-form.tsx
lleohao 670ee64179 Support I18n (#243)
* feat: support i18n

* feat: wip support i18n

* feat: complete space translation

* feat: complete page translation

* feat: update space translation

* feat: update workspace translation

* feat: update group translation

* feat: update workspace translation

* feat: update page translation

* feat: update user translation

* chore: update pnpm-lock

* feat: add query translation

* refactor: merge to single file

* chore: remove necessary code

* feat: save language to BE

* fix: only load current language

* feat: save language to locale column

* fix: cleanups

* add language menu to preferences page

* new translations

* translate editor

* Translate editor placeholders

* translate space selection component

---------

Co-authored-by: Philip Okugbe <phil@docmost.com>
Co-authored-by: Philip Okugbe <16838612+Philipinho@users.noreply.github.com>
2025-01-04 13:17:17 +00:00

110 lines
2.8 KiB
TypeScript

import { Group, Box, Button, TextInput, Stack, Textarea } from "@mantine/core";
import React from "react";
import { useForm, zodResolver } from "@mantine/form";
import * as z from "zod";
import { useUpdateSpaceMutation } from "@/features/space/queries/space-query.ts";
import { ISpace } from "@/features/space/types/space.types.ts";
import { useTranslation } from "react-i18next";
const formSchema = z.object({
name: z.string().min(2).max(50),
description: z.string().max(250),
slug: z
.string()
.min(2)
.max(50)
.regex(
/^[a-zA-Z0-9]+$/,
"Space slug must be alphanumeric. No special characters",
),
});
type FormValues = z.infer<typeof formSchema>;
interface EditSpaceFormProps {
space: ISpace;
readOnly?: boolean;
}
export function EditSpaceForm({ space, readOnly }: EditSpaceFormProps) {
const { t } = useTranslation();
const updateSpaceMutation = useUpdateSpaceMutation();
const form = useForm<FormValues>({
validate: zodResolver(formSchema),
initialValues: {
name: space?.name,
description: space?.description || "",
slug: space.slug,
},
});
const handleSubmit = async (values: {
name?: string;
description?: string;
slug?: string;
}) => {
const spaceData: Partial<ISpace> = {
spaceId: space.id,
};
if (form.isDirty("name")) {
spaceData.name = values.name;
}
if (form.isDirty("description")) {
spaceData.description = values.description;
}
if (form.isDirty("slug")) {
spaceData.slug = values.slug;
}
await updateSpaceMutation.mutateAsync(spaceData);
form.resetDirty();
};
return (
<>
<Box>
<form onSubmit={form.onSubmit((values) => handleSubmit(values))}>
<Stack>
<TextInput
id="name"
label={t("Name")}
placeholder={t("e.g Sales")}
variant="filled"
readOnly={readOnly}
{...form.getInputProps("name")}
/>
<TextInput
id="slug"
label={t("Slug")}
variant="filled"
readOnly={readOnly}
{...form.getInputProps("slug")}
/>
<Textarea
id="description"
label={t("Description")}
placeholder={t("e.g Space for sales team to collaborate")}
variant="filled"
readOnly={readOnly}
autosize
minRows={1}
maxRows={3}
{...form.getInputProps("description")}
/>
</Stack>
{!readOnly && (
<Group justify="flex-end" mt="md">
<Button type="submit" disabled={!form.isDirty()}>
{t("Save")}
</Button>
</Group>
)}
</form>
</Box>
</>
);
}