feat: add document sharing functionality across various modules
- Introduced ShareDocumentButton component for sharing documents. - Added ShareDocumentDialog for email and WhatsApp sharing options. - Integrated document sharing in estimates, invoices, inspections, job cards, bills, and purchase orders. - Implemented useDocumentShare hook for handling share logic. - Created DocumentShareClient for API interactions related to document sharing. - Updated layouts and actions to include sharing options for relevant entities.
This commit is contained in:
@@ -5,11 +5,13 @@ import { confirm } from '@/shared/components/confirm-dialog';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/shared/components/ui/dropdown-menu'
|
||||
import { Button } from '@/shared/components/ui/button'
|
||||
import { toast } from 'sonner'
|
||||
import { CalendarPlus, Ellipsis, FileText, Pencil, Printer, Trash2 } from 'lucide-react';
|
||||
import { CalendarPlus, Ellipsis, FileText, Loader2, Pencil, Printer, Share2, Trash2 } from 'lucide-react';
|
||||
import { useDocumentPrint } from '@/shared/hooks/use-document-print';
|
||||
import { useJobCard } from './job-card-context';
|
||||
import { useState } from 'react';
|
||||
import { useAuthApi } from '@/shared/useApi';
|
||||
import { ShareDocumentDialog } from '@/shared/components/share-document-dialog';
|
||||
import { ShareDocumentButton } from '@/shared/components/share-document-button';
|
||||
|
||||
// TODO: setting a sales person not working
|
||||
// TODO: unable to set a Primary technician for the job card. Need to investigate and fix it.
|
||||
@@ -21,6 +23,7 @@ export default function JobCardDropdown({ id }: { id: string }) {
|
||||
const { print, isPrinting } = useDocumentPrint()
|
||||
const jobCard = useJobCard()
|
||||
const [isConverting, setIsConverting] = useState(false)
|
||||
const [shareOpen, setShareOpen] = useState(false)
|
||||
|
||||
const handleEdit = () => {
|
||||
router.push(`/sales/job-cards/${id}/edit`)
|
||||
@@ -43,24 +46,32 @@ export default function JobCardDropdown({ id }: { id: string }) {
|
||||
if (!confirmed) return
|
||||
|
||||
setIsConverting(true)
|
||||
const promise = api.jobCards.convertToInvoice(id, {}) as Promise<any>
|
||||
toast.promise(promise, {
|
||||
loading: "Converting job card to invoice...",
|
||||
success: "Job card converted to invoice successfully",
|
||||
error: (err: any) => {
|
||||
const conflictId = err?.response?.data?.data?.invoice_id ?? err?.data?.data?.invoice_id
|
||||
return conflictId
|
||||
? "An invoice already exists for this job card."
|
||||
: (err?.message || "Failed to convert job card to invoice")
|
||||
},
|
||||
})
|
||||
try {
|
||||
const res = await api.jobCards.convertToInvoice(id, {}) as any
|
||||
const res = await promise
|
||||
const invoiceId = res?.data?.id
|
||||
toast.success("Job card converted to invoice successfully")
|
||||
if (invoiceId) {
|
||||
router.push(`/sales/invoice/${invoiceId}`)
|
||||
return
|
||||
}
|
||||
} catch (err: any) {
|
||||
const conflictId = err?.response?.data?.data?.invoice_id ?? err?.data?.data?.invoice_id
|
||||
if (conflictId) {
|
||||
toast.info("An invoice already exists for this job card.")
|
||||
router.push(`/sales/invoice/${conflictId}`)
|
||||
} else {
|
||||
toast.error(err?.message || "Failed to convert job card to invoice")
|
||||
return
|
||||
}
|
||||
} finally {
|
||||
setIsConverting(false)
|
||||
}
|
||||
setIsConverting(false)
|
||||
}
|
||||
|
||||
const handleDelete = async () => {
|
||||
@@ -85,7 +96,7 @@ export default function JobCardDropdown({ id }: { id: string }) {
|
||||
<div className="flex items-center gap-2">
|
||||
{jobCard?.status !== "draft" && (
|
||||
<Button variant="outline" onClick={handleConvertToInvoice} disabled={isConverting}>
|
||||
<FileText className="size-4" />
|
||||
{isConverting ? <Loader2 className="size-4 animate-spin" /> : <FileText className="size-4" />}
|
||||
{isConverting ? "Converting..." : "Convert to Invoice"}
|
||||
</Button>
|
||||
)}
|
||||
@@ -95,6 +106,8 @@ export default function JobCardDropdown({ id }: { id: string }) {
|
||||
Create Appointment
|
||||
</Button>
|
||||
|
||||
<ShareDocumentButton type="job_card" id={id} />
|
||||
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="icon" className="self-stretch h-auto aspect-square">
|
||||
@@ -110,6 +123,10 @@ export default function JobCardDropdown({ id }: { id: string }) {
|
||||
<Printer className="size-4" />
|
||||
{isPrinting ? "Printing..." : "Print"}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setShareOpen(true)}>
|
||||
<Share2 className="size-4" />
|
||||
Share
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem onClick={handleCreateAppointment}>
|
||||
<CalendarPlus className="size-4" />
|
||||
@@ -128,6 +145,8 @@ export default function JobCardDropdown({ id }: { id: string }) {
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
<ShareDocumentDialog type="job_card" id={id} open={shareOpen} onOpenChange={setShareOpen} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user