
import React, {useState} from "react";
import {AiFillStar} from "react-icons/ai";
import {getLocalItem} from "utils/storage";
import * as EgovNet from "../../../api/egovFetch";

function FavoriteIcon({item}){

    const [favoriteChk, setFavoriteChk] = useState(item.favoriteChk);

    function favoriteStateChange(groupSeq, checked){
        EgovNet.requestFetch(
            '/standardCode/document-favorite',
            {
                method: "POST",
                headers: {
                    'Content-type': 'application/json',
                },
                body:JSON.stringify({groupSeq: groupSeq, active: checked})
            },
            (resp) => {

            },
            function (resp) {
                console.log("err response : ", resp);
            }
        );
    }

    return (
        <div className="star clickable"
             onClick={()=>{
                 const accessToken = getLocalItem('accessToken')
                 if(accessToken) {
                     favoriteStateChange(item.groupSeq, !favoriteChk)
                     setFavoriteChk(!favoriteChk)
                 }else{
                     alert("로그인 후 이용 가능한 서비스 입니다.")
                 }
             }}>
            <AiFillStar color={favoriteChk?'#FFC000':''}/>
        </div>
    );
}

export default FavoriteIcon;