2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-03-28 02:31:50 -05:00
import React , { useEffect , useState } from "react" ;
import CollectionSelection from "@/components/InputSelect/CollectionSelection" ;
import TagSelection from "@/components/InputSelect/TagSelection" ;
2023-02-24 22:22:33 -06:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
import { faPlus } from "@fortawesome/free-solid-svg-icons" ;
2023-03-28 02:31:50 -05:00
import { ExtendedLink , NewLink } from "@/types/global" ;
2023-03-22 18:11:54 -05:00
import useLinkStore from "@/store/links" ;
2023-03-28 02:31:50 -05:00
import { useRouter } from "next/router" ;
import useCollectionStore from "@/store/collections" ;
2023-05-01 15:00:23 -05:00
import RequiredBadge from "../RequiredBadge" ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
type Props = {
toggleLinkModal : Function ;
} ;
2023-03-05 15:03:20 -06:00
2023-03-28 02:31:50 -05:00
export default function AddLink ( { toggleLinkModal } : Props ) {
const router = useRouter ( ) ;
2023-02-24 11:32:28 -06:00
const [ newLink , setNewLink ] = useState < NewLink > ( {
name : "" ,
url : "" ,
tags : [ ] ,
2023-03-28 02:31:50 -05:00
collection : {
id : undefined ,
name : "" ,
ownerId : undefined ,
} ,
2023-02-24 11:32:28 -06:00
} ) ;
2023-03-22 18:11:54 -05:00
const { addLink } = useLinkStore ( ) ;
2023-03-28 02:31:50 -05:00
const { collections } = useCollectionStore ( ) ;
useEffect ( ( ) = > {
if ( router . query . id ) {
const currentCollection = collections . find (
( e ) = > e . id == Number ( router . query . id )
) ;
setNewLink ( {
. . . newLink ,
collection : {
id : currentCollection?.id ,
name : currentCollection?.name ,
ownerId : currentCollection?.ownerId ,
} ,
} ) ;
}
} , [ ] ) ;
2023-03-05 15:03:20 -06:00
2023-02-24 11:32:28 -06:00
const setTags = ( e : any ) = > {
const tagNames = e . map ( ( e : any ) = > {
2023-03-28 02:31:50 -05:00
return { name : e.label } ;
2023-02-24 11:32:28 -06:00
} ) ;
setNewLink ( { . . . newLink , tags : tagNames } ) ;
} ;
const setCollection = ( e : any ) = > {
2023-03-28 02:31:50 -05:00
if ( e ? . __isNew__ ) e . value = null ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
setNewLink ( {
. . . newLink ,
collection : { id : e?.value , name : e?.label , ownerId : e?.ownerId } ,
} ) ;
2023-02-24 11:32:28 -06:00
} ;
2023-04-28 16:10:29 -05:00
const submit = async ( ) = > {
2023-03-28 02:31:50 -05:00
console . log ( newLink ) ;
2023-04-24 16:30:40 -05:00
const response = await addLink ( newLink as NewLink ) ;
2023-02-24 11:32:28 -06:00
2023-03-08 15:31:24 -06:00
if ( response ) toggleLinkModal ( ) ;
2023-02-24 11:32:28 -06:00
} ;
return (
2023-04-07 20:10:55 -05:00
< div className = "flex flex-col gap-3 sm:w-96 w-80" >
2023-02-24 11:32:28 -06:00
< p className = "font-bold text-sky-300 mb-2 text-center" > New Link < / p >
< div className = "flex gap-5 items-center justify-between" >
2023-05-01 15:00:23 -05:00
< p className = "text-sm font-bold text-sky-300" >
Name
< RequiredBadge / >
< / p >
2023-02-24 11:32:28 -06:00
< input
value = { newLink . name }
onChange = { ( e ) = > setNewLink ( { . . . newLink , name : e.target.value } ) }
type = "text"
placeholder = "e.g. Example Link"
2023-05-01 15:00:23 -05:00
className = "w-60 rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
2023-02-24 11:32:28 -06:00
/ >
< / div >
< div className = "flex gap-5 items-center justify-between" >
2023-05-01 15:00:23 -05:00
< p className = "text-sm font-bold text-sky-300" >
URL
< RequiredBadge / >
< / p >
2023-02-24 11:32:28 -06:00
< input
value = { newLink . url }
onChange = { ( e ) = > setNewLink ( { . . . newLink , url : e.target.value } ) }
type = "text"
placeholder = "e.g. http://example.com/"
2023-05-01 15:00:23 -05:00
className = "w-60 rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
2023-02-24 11:32:28 -06:00
/ >
< / div >
< div className = "flex gap-5 items-center justify-between" >
2023-05-01 15:00:23 -05:00
< p className = "text-sm font-bold text-sky-300" >
Collection
< RequiredBadge / >
< / p >
2023-03-28 02:31:50 -05:00
< CollectionSelection
defaultValue = {
newLink . collection . name && newLink . collection . id
? { value : newLink.collection.id , label : newLink.collection.name }
: undefined
}
onChange = { setCollection }
/ >
2023-02-24 11:32:28 -06:00
< / div >
2023-05-01 15:00:23 -05:00
< div className = "flex gap-5 items-center justify-between" >
< p className = "text-sm font-bold text-sky-300" > Tags < / p >
< TagSelection onChange = { setTags } / >
< / div >
2023-02-24 11:32:28 -06:00
< div
2023-03-25 09:17:34 -05:00
className = "mx-auto mt-2 bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded-md select-none font-bold cursor-pointer duration-100 hover:bg-sky-400"
2023-04-28 16:10:29 -05:00
onClick = { submit }
2023-02-24 11:32:28 -06:00
>
2023-02-24 22:22:33 -06:00
< FontAwesomeIcon icon = { faPlus } className = "h-5" / >
2023-02-24 11:32:28 -06:00
Add Link
< / div >
< / div >
) ;
}