Skip to content
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

Download PDF #43

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/www/.content-collections/generated/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// generated by content-collections at Tue Jul 30 2024 11:03:01 GMT+0200 (Central European Summer Time)
// generated by content-collections at Wed Jul 31 2024 07:46:36 GMT+0200 (Central European Summer Time)

import allDocs from "./allDocs.js";
import allMetas from "./allMetas.js";
Expand Down
1 change: 1 addition & 0 deletions apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"react-leaflet": "^4.2.1",
"react-syntax-highlighter": "^15.5.0",
"react-textarea-autosize": "^8.5.3",
"react-to-pdf": "^1.0.1",
"react-wrap-balancer": "^1.1.0",
"recharts": "^2.12.7",
"sharp": "^0.32.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ const ConnectorButton: React.FC<ConnectorButtonProps> = ({
const handleOAuth = async () => {
setIsLoading(true)
try {
const response = await axios.get(`/api/oauth/${serviceName}/initiate`)
const response = await axios.get(`/api/oauth/${serviceName}/initiate`, {
headers: {
"Cache-Control": "no-cache",
Pragma: "no-cache",
Expires: "0",
},
params: {
_: new Date().getTime(),
},
})
console.debug("YYY: Axios URL content:", response)
const data = response.data
window.location.href = data.url
Expand Down
33 changes: 0 additions & 33 deletions apps/www/src/app/(tenant)/tenant/[id]/contract/editor/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,6 @@ export default async function TenantEditPage({

try {
const tenantDetails = await getTenantDetails(tenantId)

// const missingFields: string[] = []
// if (!tenantDetails?.contacts || tenantDetails.contacts.length === 0) {
// missingFields.push("Du mΓ₯ legge til kontaktperson")
// }
// if (!tenantDetails?.contracts || tenantDetails.contracts.length === 0) {
// missingFields.push("Du mΓ₯ legge til kontrakt")
// }

// if (missingFields.length > 0) {
// return (
// <DashboardShell>
// <DashboardHeader
// heading="Kontrakter"
// text="Du mΓ₯ fikse fΓΈlgende fΓΈr du kan lage kontrakt."
// />
// <EmptyPlaceholder>
// <EmptyPlaceholder.Icon name="help" />
// <EmptyPlaceholder.Title>Du mangler fΓΈlgende</EmptyPlaceholder.Title>
// <EmptyPlaceholder.Description>
// <ul>
// {missingFields.map((field, index) => (
// <li className="font-bold" key={index}>
// {field}
// </li>
// ))}
// </ul>
// </EmptyPlaceholder.Description>
// </EmptyPlaceholder>
// </DashboardShell>
// )
// }

const contractContent = generateContractContent(tenantDetails)

return (
Expand Down
55 changes: 52 additions & 3 deletions apps/www/src/components/editor/TenantEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,69 @@
"use client"

import React, { useState } from "react"
import React, { useEffect, useRef, useState } from "react"
import { FileDown, Loader2 } from "lucide-react"
import { usePDF } from "react-to-pdf"

import { Button } from "@dingify/ui/components/button"

import Editor from "@/components/editor/editor"

const TenantEditor = ({ contractContent }) => {
const [value, setValue] = useState(contractContent)
const [isExporting, setIsExporting] = useState(false)
const pdfContentRef = useRef(null)

const tenantName =
contractContent.match(/2\.1 Navn\/Firma: (.*?) \(Leietaker\)/)?.[1] ||
"Leieavtale"

const { toPDF, targetRef } = usePDF({
filename: `${tenantName.replace(/\s+/g, "_")}_kontrakt.pdf`,
method: "save",
page: { format: "A4" },
})

useEffect(() => {
if (pdfContentRef.current) {
pdfContentRef.current.innerHTML = value
}
}, [value])

const exportToPDF = async () => {
setIsExporting(true)
await toPDF()
setIsExporting(false)
}

return (
<div>
<div className="space-y-4">
<div className="flex justify-end">
<Button onClick={exportToPDF} disabled={isExporting}>
{isExporting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Lager PDF...
</>
) : (
<>
<FileDown className="mr-2 h-4 w-4" />
Last ned PDF
</>
)}
</Button>
</div>
<Editor
content={value}
onChange={setValue}
placeholder="Write your post here..."
/>
{/* <Tiptap /> */}
<div ref={targetRef} style={{ position: "absolute", left: "-9999px" }}>
<div
ref={pdfContentRef}
className="prose max-w-none"
style={{ width: "210mm", padding: "20mm" }}
/>
</div>
</div>
)
}
Expand Down
36 changes: 21 additions & 15 deletions apps/www/src/components/editor/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import React from "react";
import { EditorContent, useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import React from "react"
import { EditorContent, useEditor } from "@tiptap/react"
import StarterKit from "@tiptap/starter-kit"

import EditorToolbar from "./toolbar/editor-toolbar";
import EditorToolbar from "./toolbar/editor-toolbar"

interface EditorProps {
content: string;
placeholder?: string;
onChange: (value: string) => void;
content: string
placeholder?: string
onChange: (value: string) => void
hideToolbar?: boolean
}

export const Editor = ({ content, placeholder, onChange }: EditorProps) => {
export const Editor = ({
content,
placeholder,
onChange,
hideToolbar = false,
}: EditorProps) => {
const editor = useEditor({
extensions: [StarterKit],
content: content,
onUpdate: ({ editor }) => {
onChange(editor.getHTML());
onChange(editor.getHTML())
},
});
})

if (!editor) return <></>;
if (!editor) return <></>

return (
<div className="prose w-full max-w-none rounded-md border border-gray-200 bg-white p-4 dark:border-gray-700 dark:bg-gray-800">
<EditorToolbar editor={editor} />
{!hideToolbar && <EditorToolbar editor={editor} />}
<div className="rounded-md border border-gray-200 bg-white p-4 dark:border-gray-700 dark:bg-gray-800">
<EditorContent editor={editor} placeholder={placeholder} />
</div>
</div>
);
};
)
}

export default Editor;
export default Editor
Loading