Skip to content

Commit

Permalink
chore: form action can have fields
Browse files Browse the repository at this point in the history
  • Loading branch information
devjoaov committed Jul 23, 2024
1 parent 38e3011 commit d683e0c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
49 changes: 48 additions & 1 deletion src/components/DataTable/DynamicActionComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
AlertDialogTrigger,
} from "#/components/ui/AlertDialog";
import { Button } from "#/components/ui/Button";
import { Form } from "#/components/ui/Form";
import { Dialog, DialogContent, DialogTrigger } from "../ui/Dialog";
import { Form } from "../ui/Form";
import { buildForm } from "../FormBuilder/buildForm";

export const DynamicActionComponent = ({ action, row }) => {
const renderActionButton = () => {
Expand Down Expand Up @@ -54,6 +56,7 @@ export const DynamicActionComponent = ({ action, row }) => {

interface ActionFormProps {
action: {
form?: any;
method: string;
name: string;
trigger_confirmation: boolean;
Expand All @@ -76,6 +79,50 @@ export const ActionForm: React.FC<ActionFormProps> = ({
const [isDialogOpen, setIsDialogOpen] = React.useState(false);
const [isSubmitting, setIsSubmitting] = React.useState(false);

if (action?.form?.fields) {
return (
<Dialog>
<DialogTrigger asChild>
{children || (
<Button variant="ghost" className="w-full">
{action.name}
</Button>
)}
</DialogTrigger>
<DialogContent className="max-h-[100%] max-w-lg overflow-auto">
<div className="px-2 flex flex-col">
<span className="text-xl font-semibold text-foreground mb-2">
{action.name}
</span>
<span className="text-sm text-muted-foreground">
<Trans>Fill in the fields to confirm the action</Trans>
</span>
</div>
<Form
onSubmit={() => setIsSubmitting(true)}
action={action.url_path.replace("RESOURCE_ID", row.original.id)}
method="post"
{...form}
>
<div className="p-2">
{action.method === "delete" && (
<input type="hidden" name="_method" value="delete" />
)}
<div className="flex flex-wrap gap-4">
{buildForm(action?.form?.fields, form)}
</div>
<div className="flex justify-start mt-4">
<SubmitButton type="submit" isSubmitting={isSubmitting}>
<Trans>Confirm</Trans>
</SubmitButton>
</div>
</div>
</Form>
</DialogContent>
</Dialog>
);
}

return (
<>
{action?.trigger_confirmation && (
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"Search": "Search...",
"View": "View",
"Toggle columns": "Toggle columns",
"Fill in the fields to confirm the action": "Fill in the fields to confirm the action",
"Confirm": "Confirm",
"Are you sure?": "Are you sure?",
"This action cannot be undone": "This action cannot be undone.",
"Cancel": "Cancel",
"Confirm": "Confirm",
"Open": "Open",
"Something went wrong!": "Something went wrong!",
"Pick a date": "Pick a date",
Expand Down
5 changes: 3 additions & 2 deletions src/locales/pt-BR/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"Search": "Pesquise por palavra chave...",
"View": "Visualizar",
"Toggle columns": "Alternar colunas",
"Fill in the fields to confirm the action": "Preencha os campos para confirmar a ação",
"Confirm": "Confirmar",
"Are you sure?": "Você tem certeza?",
"This action cannot be undone.": "Esta ação não pode ser desfeita.",
"This action cannot be undone": "Esta ação não pode ser desfeita.",
"Cancel": "Cancelar",
"Confirm": "Confirmar",
"Open": "Abrir",
"Something went wrong!": "Algo deu errado!",
"Pick a date": "Escolha uma data",
Expand Down
3 changes: 3 additions & 0 deletions src/locales/pt-BR/translation_old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"This action cannot be undone.": "Esta ação não pode ser desfeita."
}

0 comments on commit d683e0c

Please sign in to comment.