mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-06-10 07:16:01 +00:00
feat(lançamentos): escopo "period" na ação em lote e correção do fluxo de anexos em séries
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -125,6 +125,9 @@ interface AttachmentItemProps {
|
||||
url: string;
|
||||
onDeleted: () => void;
|
||||
readonly?: boolean;
|
||||
isPendingDelete?: boolean;
|
||||
onPendingDelete?: (attachmentId: string) => void;
|
||||
onUndoPendingDelete?: (attachmentId: string) => void;
|
||||
}
|
||||
|
||||
export function AttachmentItem({
|
||||
@@ -136,6 +139,9 @@ export function AttachmentItem({
|
||||
url,
|
||||
onDeleted,
|
||||
readonly = false,
|
||||
isPendingDelete = false,
|
||||
onPendingDelete,
|
||||
onUndoPendingDelete,
|
||||
}: AttachmentItemProps) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [previewOpen, setPreviewOpen] = useState(false);
|
||||
@@ -145,6 +151,11 @@ export function AttachmentItem({
|
||||
mimeType === "application/pdf" || mimeType.startsWith("image/");
|
||||
|
||||
function handleDelete() {
|
||||
if (onPendingDelete) {
|
||||
onPendingDelete(attachmentId);
|
||||
setConfirmOpen(false);
|
||||
return;
|
||||
}
|
||||
startTransition(async () => {
|
||||
const result = await detachTransactionAttachmentAction({
|
||||
attachmentId,
|
||||
@@ -162,9 +173,18 @@ export function AttachmentItem({
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex min-w-0 items-center gap-2 overflow-hidden rounded-md border px-3 py-2 text-sm">
|
||||
<div
|
||||
className={`flex min-w-0 items-center gap-2 overflow-hidden rounded-md border px-3 py-2 text-sm transition-opacity ${isPendingDelete ? "opacity-50 border-dashed" : ""}`}
|
||||
>
|
||||
<AttachmentIcon mimeType={mimeType} />
|
||||
{canPreview ? (
|
||||
{isPendingDelete ? (
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="truncate font-medium line-through">{fileName}</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Será removido ao salvar
|
||||
</p>
|
||||
</div>
|
||||
) : canPreview ? (
|
||||
<button
|
||||
type="button"
|
||||
className="min-w-0 flex-1 text-left"
|
||||
@@ -184,29 +204,42 @@ export function AttachmentItem({
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-7 shrink-0"
|
||||
asChild
|
||||
>
|
||||
<a href={url} target="_blank" rel="noreferrer" download={fileName}>
|
||||
<RiDownloadLine className="size-4" />
|
||||
</a>
|
||||
</Button>
|
||||
{!readonly && (
|
||||
{!isPendingDelete && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-7 shrink-0 text-destructive hover:text-destructive"
|
||||
onClick={() => setConfirmOpen(true)}
|
||||
disabled={isPending}
|
||||
className="size-7 shrink-0"
|
||||
asChild
|
||||
>
|
||||
<RiDeleteBinLine className="size-4" />
|
||||
<a href={url} target="_blank" rel="noreferrer" download={fileName}>
|
||||
<RiDownloadLine className="size-4" />
|
||||
</a>
|
||||
</Button>
|
||||
)}
|
||||
{!readonly &&
|
||||
(isPendingDelete ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="shrink-0 text-xs h-7 px-2"
|
||||
onClick={() => onUndoPendingDelete?.(attachmentId)}
|
||||
>
|
||||
Desfazer
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-7 shrink-0 text-destructive hover:text-destructive"
|
||||
onClick={() => setConfirmOpen(true)}
|
||||
disabled={isPending}
|
||||
>
|
||||
<RiDeleteBinLine className="size-4" />
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{canPreview && (
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { RiFileAddLine } from "@remixicon/react";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { fetchTransactionAttachmentsAction } from "@/features/transactions/actions/attachments";
|
||||
import { Button } from "@/shared/components/ui/button";
|
||||
import { AttachmentItem } from "./attachment-item";
|
||||
import { AttachmentUpload } from "./attachment-upload";
|
||||
|
||||
@@ -16,16 +18,28 @@ type AttachmentRow = {
|
||||
|
||||
interface AttachmentSectionProps {
|
||||
transactionId: string;
|
||||
seriesId: string | null;
|
||||
readonly?: boolean;
|
||||
onLoaded?: (count: number) => void;
|
||||
pendingDetachIds?: string[];
|
||||
onPendingDetach?: (attachmentId: string) => void;
|
||||
onUndoPendingDetach?: (attachmentId: string) => void;
|
||||
pendingUploadFiles?: File[];
|
||||
onPendingUpload?: (file: File) => void;
|
||||
onCancelPendingUpload?: (file: File) => void;
|
||||
maxSizeMb?: number;
|
||||
}
|
||||
|
||||
export function AttachmentSection({
|
||||
transactionId,
|
||||
seriesId,
|
||||
readonly = false,
|
||||
onLoaded,
|
||||
pendingDetachIds,
|
||||
onPendingDetach,
|
||||
onUndoPendingDetach,
|
||||
pendingUploadFiles,
|
||||
onPendingUpload,
|
||||
onCancelPendingUpload,
|
||||
maxSizeMb,
|
||||
}: AttachmentSectionProps) {
|
||||
const [items, setItems] = useState<AttachmentRow[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
@@ -45,42 +59,70 @@ export function AttachmentSection({
|
||||
load();
|
||||
}, [load]);
|
||||
|
||||
if (isLoading) {
|
||||
return <p className="text-xs text-muted-foreground">Carregando...</p>;
|
||||
}
|
||||
|
||||
const hasPendingUploads = (pendingUploadFiles?.length ?? 0) > 0;
|
||||
|
||||
return (
|
||||
<div className="min-w-0 space-y-2 overflow-hidden">
|
||||
{isLoading ? (
|
||||
<p className="text-xs text-muted-foreground">Carregando...</p>
|
||||
) : (
|
||||
<>
|
||||
{items.length > 0 ? (
|
||||
<div className="min-w-0 space-y-1.5">
|
||||
{items.map((item) => (
|
||||
<AttachmentItem
|
||||
key={item.attachmentId}
|
||||
attachmentId={item.attachmentId}
|
||||
transactionId={transactionId}
|
||||
fileName={item.fileName}
|
||||
fileSize={item.fileSize}
|
||||
mimeType={item.mimeType}
|
||||
url={item.url}
|
||||
onDeleted={load}
|
||||
readonly={readonly}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
readonly && (
|
||||
<p className="text-xs text-muted-foreground">Nenhum anexo.</p>
|
||||
)
|
||||
)}
|
||||
{items.length === 0 && !hasPendingUploads && readonly && (
|
||||
<p className="text-xs text-muted-foreground">Nenhum anexo.</p>
|
||||
)}
|
||||
|
||||
{!readonly && (
|
||||
<AttachmentUpload
|
||||
{(items.length > 0 || hasPendingUploads) && (
|
||||
<div className="min-w-0 space-y-1.5">
|
||||
{items.map((item) => (
|
||||
<AttachmentItem
|
||||
key={item.attachmentId}
|
||||
attachmentId={item.attachmentId}
|
||||
transactionId={transactionId}
|
||||
seriesId={seriesId}
|
||||
onUploaded={load}
|
||||
fileName={item.fileName}
|
||||
fileSize={item.fileSize}
|
||||
mimeType={item.mimeType}
|
||||
url={item.url}
|
||||
onDeleted={load}
|
||||
readonly={readonly}
|
||||
isPendingDelete={pendingDetachIds?.includes(item.attachmentId)}
|
||||
onPendingDelete={onPendingDetach}
|
||||
onUndoPendingDelete={onUndoPendingDetach}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
))}
|
||||
|
||||
{pendingUploadFiles?.map((file) => (
|
||||
<div
|
||||
key={`${file.name}-${file.size}`}
|
||||
className="flex min-w-0 items-center gap-2 overflow-hidden rounded-md border border-dashed px-3 py-2 text-sm opacity-60"
|
||||
>
|
||||
<RiFileAddLine className="size-4 shrink-0 text-muted-foreground" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="truncate font-medium">{file.name}</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Será adicionado ao salvar
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="shrink-0 text-xs h-7 px-2"
|
||||
onClick={() => onCancelPendingUpload?.(file)}
|
||||
>
|
||||
Cancelar
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!readonly && (
|
||||
<AttachmentUpload
|
||||
transactionId={transactionId}
|
||||
onUploaded={load}
|
||||
onPendingUpload={onPendingUpload}
|
||||
maxSizeMb={maxSizeMb}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { RiAttachment2 } from "@remixicon/react";
|
||||
import { useRef, useState, useTransition } from "react";
|
||||
import { useRef, useTransition } from "react";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
confirmAttachmentUploadAction,
|
||||
@@ -9,27 +9,25 @@ import {
|
||||
} from "@/features/transactions/actions/attachments";
|
||||
import {
|
||||
ALLOWED_MIME_TYPES,
|
||||
MAX_FILE_SIZE,
|
||||
DEFAULT_MAX_FILE_SIZE_MB,
|
||||
} from "@/features/transactions/attachments-config";
|
||||
import { Button } from "@/shared/components/ui/button";
|
||||
import { Checkbox } from "@/shared/components/ui/checkbox";
|
||||
import { Label } from "@/shared/components/ui/label";
|
||||
|
||||
interface AttachmentUploadProps {
|
||||
transactionId: string;
|
||||
seriesId: string | null;
|
||||
onUploaded: () => void;
|
||||
onPendingUpload?: (file: File) => void;
|
||||
maxSizeMb?: number;
|
||||
}
|
||||
|
||||
export function AttachmentUpload({
|
||||
transactionId,
|
||||
seriesId,
|
||||
onUploaded,
|
||||
onPendingUpload,
|
||||
maxSizeMb = DEFAULT_MAX_FILE_SIZE_MB,
|
||||
}: AttachmentUploadProps) {
|
||||
const maxFileSizeBytes = maxSizeMb * 1024 * 1024;
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [applyToSeries, setApplyToSeries] = useState(false);
|
||||
const [pendingFile, setPendingFile] = useState<File | null>(null);
|
||||
|
||||
function handleFileChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
const file = e.target.files?.[0];
|
||||
@@ -49,19 +47,16 @@ export function AttachmentUpload({
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
toast.error("O arquivo deve ter no máximo 50MB.");
|
||||
if (file.size > maxFileSizeBytes) {
|
||||
toast.error(`O arquivo deve ter no máximo ${maxSizeMb}MB.`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (seriesId) {
|
||||
setPendingFile(file);
|
||||
} else {
|
||||
uploadFile(file, false);
|
||||
if (onPendingUpload) {
|
||||
onPendingUpload(file);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function uploadFile(file: File, toSeries: boolean) {
|
||||
startTransition(async () => {
|
||||
const presignResult = await getPresignedUploadUrlAction({
|
||||
fileName: file.name,
|
||||
@@ -88,13 +83,10 @@ export function AttachmentUpload({
|
||||
|
||||
const confirmResult = await confirmAttachmentUploadAction({
|
||||
uploadToken: presignResult.uploadToken,
|
||||
applyToSeries: toSeries,
|
||||
});
|
||||
|
||||
if (confirmResult.success) {
|
||||
toast.success(confirmResult.message);
|
||||
setPendingFile(null);
|
||||
setApplyToSeries(false);
|
||||
onUploaded();
|
||||
} else {
|
||||
toast.error(confirmResult.error);
|
||||
@@ -102,56 +94,6 @@ export function AttachmentUpload({
|
||||
});
|
||||
}
|
||||
|
||||
function handleConfirmPending() {
|
||||
if (pendingFile) uploadFile(pendingFile, applyToSeries);
|
||||
}
|
||||
|
||||
function handleCancelPending() {
|
||||
setPendingFile(null);
|
||||
setApplyToSeries(false);
|
||||
}
|
||||
|
||||
if (pendingFile) {
|
||||
return (
|
||||
<div className="min-w-0 space-y-2 rounded-md border border-dashed p-3 text-sm">
|
||||
<div className="min-w-0 overflow-hidden">
|
||||
<p className="truncate font-medium" title={pendingFile.name}>
|
||||
{pendingFile.name}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="apply-series"
|
||||
checked={applyToSeries}
|
||||
onCheckedChange={(v) => setApplyToSeries(Boolean(v))}
|
||||
/>
|
||||
<Label htmlFor="apply-series" className="cursor-pointer text-xs">
|
||||
Aplicar a todas as parcelas da série
|
||||
</Label>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
onClick={handleConfirmPending}
|
||||
disabled={isPending}
|
||||
>
|
||||
{isPending ? "Enviando..." : "Confirmar"}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={handleCancelPending}
|
||||
disabled={isPending}
|
||||
>
|
||||
Cancelar
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
@@ -172,7 +114,9 @@ export function AttachmentUpload({
|
||||
{isPending ? "Enviando..." : "Adicionar anexo"}
|
||||
</span>
|
||||
{!isPending && (
|
||||
<span className="text-xs">PDF, JPEG, PNG ou WebP · máx. 50 MB</span>
|
||||
<span className="text-xs">
|
||||
PDF, JPEG, PNG ou WebP · máx. {maxSizeMb} MB
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user