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-02-24 11:32:28 -06:00
import React , { useState } from "react" ;
2023-03-28 02:31:50 -05:00
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" ;
2023-03-28 02:31:50 -05:00
import { ExtendedLink } from "@/types/global" ;
import { faPenToSquare } from "@fortawesome/free-regular-svg-icons" ;
2023-03-22 18:11:54 -05:00
import useLinkStore from "@/store/links" ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
type Props = {
toggleLinkModal : Function ;
link : ExtendedLink ;
} ;
2023-03-05 15:03:20 -06:00
2023-03-28 02:31:50 -05:00
export default function EditLink ( { toggleLinkModal , link } : Props ) {
const [ currentLink , setCurrentLink ] = useState < ExtendedLink > ( link ) ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
const { updateLink } = useLinkStore ( ) ;
2023-03-05 15:03:20 -06:00
2023-03-28 10:11:34 -05:00
const shortendURL = new URL ( link . url ) . host . toLowerCase ( ) ;
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
} ) ;
2023-03-28 02:31:50 -05:00
setCurrentLink ( { . . . currentLink , tags : tagNames } ) ;
2023-02-24 11:32:28 -06:00
} ;
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
setCurrentLink ( {
. . . currentLink ,
collection : { id : e?.value , name : e?.label , ownerId : e?.ownerId } as any ,
} ) ;
2023-02-24 11:32:28 -06:00
} ;
2023-03-08 15:31:24 -06:00
const submitLink = async ( ) = > {
2023-03-28 02:31:50 -05:00
updateLink ( currentLink ) ;
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-03-28 10:11:34 -05:00
< p className = "font-bold text-sky-300 mb-2 text-center" > Edit Link < / p >
2023-03-28 21:45:25 -05:00
< p className = "text-sky-700" >
2023-04-07 20:10:55 -05:00
< b > { shortendURL } < / b > | { link . title }
2023-03-28 21:45:25 -05:00
< / p >
2023-02-24 11:32:28 -06:00
< div className = "flex gap-5 items-center justify-between" >
< p className = "text-sm font-bold text-sky-300" > Name < / p >
< input
2023-03-28 02:31:50 -05:00
value = { currentLink . name }
onChange = { ( e ) = >
setCurrentLink ( { . . . currentLink , name : e.target.value } )
}
2023-02-24 11:32:28 -06:00
type = "text"
placeholder = "e.g. Example Link"
2023-03-25 09:17:34 -05:00
className = "w-60 rounded-md p-3 border-sky-100 border-solid border text-sm 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" >
< p className = "text-sm font-bold text-sky-300" > Tags < / p >
2023-03-28 02:31:50 -05:00
< TagSelection
onChange = { setTags }
defaultValue = { link . tags . map ( ( e ) = > {
return { label : e.name , value : e.id } ;
} ) }
/ >
2023-02-24 11:32:28 -06:00
< / div >
< div className = "flex gap-5 items-center justify-between" >
< p className = "text-sm font-bold text-sky-300" > Collection < / p >
2023-03-28 02:31:50 -05:00
< CollectionSelection
onChange = { setCollection }
defaultValue = { {
label : link.collection.name ,
value : link.collection.id ,
} }
/ >
2023-02-24 11:32:28 -06:00
< / div >
< 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-03-08 15:31:24 -06:00
onClick = { submitLink }
2023-02-24 11:32:28 -06:00
>
2023-03-28 02:31:50 -05:00
< FontAwesomeIcon icon = { faPenToSquare } className = "h-5" / >
Edit Link
2023-02-24 11:32:28 -06:00
< / div >
< / div >
) ;
}