@@ -10,6 +10,13 @@ import { useTeam } from "@/context/team-context";
1010import { toast } from "sonner" ;
1111import { mutate } from "swr" ;
1212
13+ import {
14+ DocumentData ,
15+ createAgreementDocument ,
16+ } from "@/lib/documents/create-document" ;
17+ import { putFile } from "@/lib/files/put-file" ;
18+ import { getSupportedContentType } from "@/lib/utils/get-content-type" ;
19+
1320import DocumentUpload from "@/components/document-upload" ;
1421import { Button } from "@/components/ui/button" ;
1522import { Input } from "@/components/ui/input" ;
@@ -24,33 +31,55 @@ import {
2431 SheetTitle ,
2532} from "@/components/ui/sheet" ;
2633
27- import {
28- DocumentData ,
29- createAgreementDocument ,
30- } from "@/lib/documents/create-document" ;
31- import { putFile } from "@/lib/files/put-file" ;
32- import { getSupportedContentType } from "@/lib/utils/get-content-type" ;
33-
3434import LinkItem from "../link-item" ;
3535
3636export default function AgreementSheet ( {
37+ defaultData,
3738 isOpen,
3839 setIsOpen,
40+ isOnlyView = false ,
41+ onClose,
3942} : {
43+ defaultData ?: { name : string ; link : string ; requireName : boolean } | null ;
4044 isOpen : boolean ;
4145 setIsOpen : Dispatch < SetStateAction < boolean > > ;
46+ isOnlyView ?: boolean ;
47+ onClose ?: ( ) => void ;
4248} ) {
4349 const teamInfo = useTeam ( ) ;
4450 const teamId = teamInfo ?. currentTeam ?. id ;
4551 const [ data , setData ] = useState ( { name : "" , link : "" , requireName : true } ) ;
4652 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
4753 const [ currentFile , setCurrentFile ] = useState < File | null > ( null ) ;
48- const [ currentLink , setCurrentLink ] = useState < string | null > ( null ) ;
54+
55+ useEffect ( ( ) => {
56+ if ( defaultData ) {
57+ setData ( {
58+ name : defaultData ?. name || "" ,
59+ link : defaultData ?. link || "" ,
60+ requireName : defaultData ?. requireName || true ,
61+ } ) ;
62+ }
63+ } , [ defaultData ] ) ;
64+
65+ const handleClose = ( open : boolean ) => {
66+ setIsOpen ( open ) ;
67+ setData ( { name : "" , link : "" , requireName : true } ) ;
68+ setCurrentFile ( null ) ;
69+ setIsLoading ( false ) ;
70+ if ( onClose ) {
71+ onClose ( ) ;
72+ }
73+ } ;
4974
5075 const handleBrowserUpload = async ( ) => {
5176 // event.preventDefault();
5277 // event.stopPropagation();
53-
78+ if ( isOnlyView ) {
79+ handleClose ( false ) ;
80+ toast . error ( "Cannot upload file in view mode!" ) ;
81+ return ;
82+ }
5483 // Check if the file is chosen
5584 if ( ! currentFile ) {
5685 toast . error ( "Please select a file to upload." ) ;
@@ -112,6 +141,11 @@ export default function AgreementSheet({
112141 const handleSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
113142 e . preventDefault ( ) ;
114143 e . stopPropagation ( ) ;
144+ if ( isOnlyView ) {
145+ handleClose ( false ) ;
146+ toast . error ( "Agreement cannot be created in view mode" ) ;
147+ return ;
148+ }
115149
116150 setIsLoading ( true ) ;
117151
@@ -151,13 +185,16 @@ export default function AgreementSheet({
151185 } , [ currentFile ] ) ;
152186
153187 return (
154- < Sheet open = { isOpen } onOpenChange = { setIsOpen } >
188+ < Sheet open = { isOpen } onOpenChange = { handleClose } >
155189 < SheetContent className = "flex h-full w-[85%] flex-col justify-between bg-background px-4 text-foreground sm:w-[500px] md:px-5" >
156190 < SheetHeader className = "text-start" >
157- < SheetTitle > Create a new agreement</ SheetTitle >
191+ < SheetTitle >
192+ { isOnlyView ? "View Agreement" : "Create a new agreement" }
193+ </ SheetTitle >
158194 < SheetDescription >
159- An agreement is a special document that visitors must accept before
160- accessing your link. You can create a new agreement here.
195+ { isOnlyView
196+ ? "View the details of this agreement."
197+ : "An agreement is a special document that visitors must accept before accessing your link. You can create a new agreement here." }
161198 </ SheetDescription >
162199 </ SheetHeader >
163200
@@ -182,6 +219,7 @@ export default function AgreementSheet({
182219 name : e . target . value ,
183220 } )
184221 }
222+ disabled = { isOnlyView }
185223 />
186224 </ div >
187225
@@ -192,6 +230,7 @@ export default function AgreementSheet({
192230 action = { ( ) =>
193231 setData ( { ...data , requireName : ! data . requireName } )
194232 }
233+ isAllowed = { ! isOnlyView }
195234 />
196235 </ div >
197236
@@ -220,28 +259,38 @@ export default function AgreementSheet({
220259 "Please enter a valid URL starting with https://" ,
221260 )
222261 }
262+ disabled = { isOnlyView }
223263 />
224264 </ div >
225265
226- < div className = "space-y-12" >
227- < div className = "space-y-2 pb-6" >
228- < Label > Or upload an agreement</ Label >
229- < div className = "grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6" >
230- < DocumentUpload
231- currentFile = { currentFile }
232- setCurrentFile = { setCurrentFile }
233- />
266+ { ! isOnlyView ? (
267+ < div className = "space-y-12" >
268+ < div className = "space-y-2 pb-6" >
269+ < Label > Or upload an agreement</ Label >
270+ < div className = "grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6" >
271+ < DocumentUpload
272+ currentFile = { currentFile }
273+ setCurrentFile = { setCurrentFile }
274+ />
275+ </ div >
234276 </ div >
235277 </ div >
236- </ div >
278+ ) : null }
237279 </ div >
238280 </ div >
239-
240- < SheetFooter className = "flex-shrink-0" >
281+ < SheetFooter
282+ className = { `flex-shrink-0 ${ isOnlyView ? "mt-6" : "" } ` }
283+ >
241284 < div className = "flex items-center" >
242- < Button type = "submit" loading = { isLoading } >
243- Create Agreement
244- </ Button >
285+ { isOnlyView ? (
286+ < Button type = "button" onClick = { ( ) => handleClose ( false ) } >
287+ Close
288+ </ Button >
289+ ) : (
290+ < Button type = "submit" loading = { isLoading } >
291+ Create Agreement
292+ </ Button >
293+ ) }
245294 </ div >
246295 </ SheetFooter >
247296 </ form >
0 commit comments