-
Notifications
You must be signed in to change notification settings - Fork 0
feat: adicionando botão de editar resposta no forms #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| "use client" | ||
|
|
||
| import { useState } from "react" | ||
| import { Button } from "@/components/ui/button" | ||
| import { Edit } from "lucide-react" | ||
| import { EditResponseModal } from "./edit-response-modal" | ||
|
|
||
| interface EditResponseButtonProps { | ||
| responseId: string | ||
| formId: string | ||
| isOwner: boolean | ||
| isAuthor: boolean | ||
| } | ||
|
|
||
| export function EditResponseButton({ responseId, formId, isOwner, isAuthor }: EditResponseButtonProps) { | ||
| const [isModalOpen, setIsModalOpen] = useState(false) | ||
|
|
||
| // Só mostrar o botão se o usuário for o dono do formulário ou o autor da resposta | ||
| if (!isOwner && !isAuthor) { | ||
| return null | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| onClick={() => setIsModalOpen(true)} | ||
| > | ||
| <Edit className="h-4 w-4 mr-1" /> | ||
| Editar Resposta | ||
| </Button> | ||
|
|
||
| <EditResponseModal | ||
| responseId={responseId} | ||
| formId={formId} | ||
| isOpen={isModalOpen} | ||
| onClose={() => setIsModalOpen(false)} | ||
| /> | ||
| </> | ||
| ) | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| "use client" | ||
|
|
||
| import { useEffect, useState } from "react" | ||
| import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog" | ||
| import { FormResponseComponent } from "./form-response" | ||
| import { api } from "@/trpc/react" | ||
| import { toast } from "sonner" | ||
| import { Button } from "@/components/ui/button" | ||
| import { Loader2 } from "lucide-react" | ||
| import type { Field } from "@/lib/form-types" | ||
| import type { FormResponse } from "@/types/form-responses" | ||
|
|
||
| interface EditResponseModalProps { | ||
| responseId: string | ||
| formId: string | ||
| isOpen: boolean | ||
| onClose: () => void | ||
| } | ||
|
|
||
| export function EditResponseModal({ responseId, formId, isOpen, onClose }: EditResponseModalProps) { | ||
| const [isLoading, setIsLoading] = useState(true) | ||
| const [response, setResponse] = useState<FormResponse | null>(null) | ||
| const [fields, setFields] = useState<Field[]>([]) | ||
|
|
||
| // Buscar dados da resposta e campos do formulário | ||
| const { data: responseData, isLoading: isResponseLoading } = api.formResponse.getById.useQuery( | ||
| { responseId }, | ||
| { | ||
| enabled: isOpen && !!responseId, | ||
| } | ||
| ) | ||
|
|
||
| const { data: formData } = api.form.getById.useQuery( | ||
| { id: formId }, | ||
| { | ||
| enabled: isOpen && !!formId, | ||
| } | ||
| ) | ||
|
|
||
| // useEffect para carregar os dados quando o modal abre | ||
| useEffect(() => { | ||
| if (isOpen && responseData && formData) { | ||
| setResponse(responseData) | ||
| // @ts-expect-error - JsonValue to Field conversion | ||
| setFields(formData.fields as Field[]) | ||
| setIsLoading(false) | ||
| } else if (isOpen && !isResponseLoading) { | ||
| setIsLoading(false) | ||
| } | ||
| }, [isOpen, responseData, formData, isResponseLoading]) | ||
|
|
||
| // Mutation para atualizar a resposta | ||
| const updateResponse = api.formResponse.update.useMutation({ | ||
| onSuccess: () => { | ||
| toast.success("Resposta atualizada com sucesso!") | ||
| onClose() | ||
| }, | ||
| onError: (error: unknown) => { | ||
| const errorMessage = error instanceof Error ? error.message : 'Erro desconhecido' | ||
| toast.error(`Erro ao atualizar resposta: ${errorMessage}`) | ||
| }, | ||
| }) | ||
|
|
||
| const handleSubmit = (data: Record<string, unknown>) => { | ||
| updateResponse.mutate({ | ||
| responseId, | ||
| responses: [data] as Record<string, unknown>[], | ||
| }) | ||
| } | ||
|
|
||
| const handleClose = () => { | ||
| setIsLoading(true) | ||
| setResponse(null) | ||
| setFields([]) | ||
| onClose() | ||
| } | ||
|
|
||
| return ( | ||
| <Dialog open={isOpen} onOpenChange={handleClose}> | ||
| <DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto"> | ||
| <DialogHeader> | ||
| <DialogTitle>Editar Resposta</DialogTitle> | ||
| <DialogDescription> | ||
| Faça as alterações necessárias na resposta do formulário. | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
|
|
||
| {isLoading ? ( | ||
| <div className="flex items-center justify-center py-8"> | ||
| <Loader2 className="h-8 w-8 animate-spin" /> | ||
| <span className="ml-2">Carregando dados...</span> | ||
| </div> | ||
| ) : response && fields.length > 0 ? ( | ||
| <FormResponseComponent | ||
| formId={formId} | ||
| fields={fields} | ||
| existingResponse={response.responses[0] as Record<string, unknown>} | ||
| onSubmit={handleSubmit} | ||
| isEditing={true} | ||
| isSubmitting={updateResponse.isPending} | ||
| /> | ||
| ) : ( | ||
| <div className="flex flex-col items-center justify-center py-8"> | ||
| <p className="text-muted-foreground">Erro ao carregar dados da resposta.</p> | ||
| <Button variant="outline" onClick={handleClose} className="mt-4"> | ||
| Fechar | ||
| </Button> | ||
| </div> | ||
| )} | ||
| </DialogContent> | ||
| </Dialog> | ||
| ) | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disable submit when editing without handler; keep labels consistent.
Prevents accidental submission path and improves UX text.
<Button type="submit" className="mt-6" - disabled={customIsSubmitting ?? isSubmitting} + disabled={(customIsSubmitting ?? isSubmitting) || (isEditing && !customOnSubmit)} > - {customIsSubmitting !== undefined - ? (customIsSubmitting ? "Salvando..." : "Salvar Alterações") - : (isSubmitting ? "Enviando..." : (isEditing ? "Salvar Alterações" : "Enviar Resposta")) - } + {customIsSubmitting !== undefined + ? customIsSubmitting + ? "Salvando..." + : "Salvar Alterações" + : isSubmitting + ? "Enviando..." + : isEditing + ? (customOnSubmit ? "Salvar Alterações" : "Indisponível") + : "Enviar Resposta"} </Button>