diff --git a/FrontEnd/src/components/SearchPage/search_field/companies/CompanyCard.js b/FrontEnd/src/components/SearchPage/search_field/companies/CompanyCard.js index a2d57a383..28b1f1e0d 100644 --- a/FrontEnd/src/components/SearchPage/search_field/companies/CompanyCard.js +++ b/FrontEnd/src/components/SearchPage/search_field/companies/CompanyCard.js @@ -1,8 +1,10 @@ import { Link } from 'react-router-dom'; import { StarOutlined, StarFilled } from '@ant-design/icons'; import { useState, useEffect } from 'react'; +// import { useState } from 'react'; import { useSWRConfig } from 'swr'; import useSWRMutation from 'swr/mutation'; +// import useSWRImmutable from 'swr/immutable' // import useSWR from 'swr'; import axios from 'axios'; import styles from './CompanyCard.module.css'; @@ -30,11 +32,48 @@ const CompanyCard = ({ companyData, isAuthorized }) => { }).then(); } + async function getRequest(url) { + // if (isAuthorized.isAuth) { + const data = await axios + .get(url, { + withCredentials: true, + headers: { + Authorization: 'Token ' + authToken, + }, + }) + .then((response) => { + return response.data; + }); + const NewList = []; + for (let item of data.results) { + NewList.push(item['id']); + } + + setUsersSavedList(NewList); + if (usersSavedList.includes(companyData.id)) { + console.log( + companyData.id, + usersSavedList, + usersSavedList.includes(companyData.id) + ); + setStar(filledStar); + setIsSaved(true); + } else { + setIsSaved(false); + setStar(outlinedStar); + } + } + const { trigger } = useSWRMutation( `${process.env.REACT_APP_BASE_API_URL}/api/saved-list/`, sendRequest ); + const { trigger: triggerget } = useSWRMutation( + `${process.env.REACT_APP_BASE_API_URL}/api/profiles/?is_saved=True`, + getRequest + ); + const handleClick = async () => { try { await trigger( @@ -58,59 +97,37 @@ const CompanyCard = ({ companyData, isAuthorized }) => { ); useEffect(() => { - if (isAuthorized) - axios - .get( - `${process.env.REACT_APP_BASE_API_URL}/api/profiles/?is_saved=True`, - { - withCredentials: true, - headers: { - Authorization: 'Token ' + authToken, - }, - } - ) - .then((response) => { - const NewList = []; - for (let item of response.data.results) { - NewList.push(item['id']); - } - - setUsersSavedList(NewList); - if (usersSavedList.includes(companyData.id)) { - setStar(filledStar); - setIsSaved(true); - } else { - setIsSaved(false); - setStar(outlinedStar); - } - }) - .catch((error) => { - console.error('Error fetching search results:', error); - }); - }, [usersSavedList]); + if (isAuthorized.isAuth) { + try { + triggerget(); + } catch (error) { + console.error(error); + } + } + }, [companyData, isAuthorized, isSaved]); return ( -