Skip to content

Commit

Permalink
use useUser user))
Browse files Browse the repository at this point in the history
  • Loading branch information
BelousSofiya committed Nov 24, 2023
1 parent aa9ad1f commit f90bff9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
4 changes: 3 additions & 1 deletion FrontEnd/src/components/SearchPage/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import PropTypes from 'prop-types';

const ITEMS_PER_PAGE = 6;

export function Search({ isAuthorized }) {
export function Search({ isAuthorized, userData }) {
Search.propTypes = {
isAuthorized: PropTypes.any.isRequired,
userData: PropTypes.any.isRequired,
};

const [searchResults, setSearchResults] = useState([]);
Expand Down Expand Up @@ -94,6 +95,7 @@ export function Search({ isAuthorized }) {
searchPerformed={searchPerformed}
displayedResults={displayedResults}
isAuthorized={isAuthorized}
userData={userData}
/>
<br />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import CompanyCard from './companies/CompanyCard';
import styles from './Text.module.css';
import PropTypes from 'prop-types';

const SearchResults = ({ results, displayedResults, isAuthorized }) => {
const SearchResults = ({ results, displayedResults, isAuthorized, userData }) => {
SearchResults.propTypes = {
results: PropTypes.array,
displayedResults: PropTypes.array,
isAuthorized: PropTypes.object,
userData: PropTypes.any.isRequired,
};

let error = null;
Expand All @@ -22,7 +23,7 @@ const SearchResults = ({ results, displayedResults, isAuthorized }) => {
<div className={styles['row']}>
{displayedResults.map((result, resultIndex) => (
<div key={resultIndex} className={styles['col-md-4']}>
<CompanyCard companyData={result} isAuthorized={isAuthorized} />
<CompanyCard companyData={result} isAuthorized={isAuthorized} userData={userData} />
</div>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import axios from 'axios';
import styles from './CompanyCard.module.css';
import PropTypes from 'prop-types';

const CompanyCard = ({ companyData, isAuthorized }) => {
const CompanyCard = ({ companyData, isAuthorized, userData }) => {
CompanyCard.propTypes = {
companyData: PropTypes.object,
isAuthorized: PropTypes.object,
userData: PropTypes.any.isRequired,
};

const { mutate } = useSWRConfig();
Expand Down Expand Up @@ -56,7 +57,7 @@ const CompanyCard = ({ companyData, isAuthorized }) => {
}

setUsersSavedList(NewList);
if (companyData.id == isAuthorized.UserId) {
if (companyData.id == userData.id) {
setStar(false);
setIsSaved(false);
setSearchPerformed(true);
Expand Down
5 changes: 4 additions & 1 deletion FrontEnd/src/components/basicPage/BasicPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import { RestorePasswordFailedPage } from '../RestorePassword/pages/RestorePassw
import ScrollToTopButton from '../PrivacyPolicyPage/privacy/ScrollToTopButton';
import TermsAndConditions from '../terms-and-conditions-app/terms_conditions/TermsAndConditionsComponent';
import { useAuth } from '../../hooks';
import { useUser } from '../../hooks';
import { Search } from '../SearchPage/Search';

function BasicPage() {
const auth = useAuth();
const user = useUser();
const userData = user.user;
return (
<ConfigProvider
theme={{
Expand Down Expand Up @@ -118,7 +121,7 @@ function BasicPage() {
<Route path="/privacy-policy" element={<PrivacyPolicy />} />
<Route path="/terms-and-conditions" element={<TermsAndConditions />} />
<Route path="/cookies-policy" element={<CookiesPolicyComponent />} />
<Route path="/search" element={<Search isAuthorized={auth} />} />
<Route path="/search" element={<Search isAuthorized={auth} userData={userData} />} />
</Routes>
<Footer />
<ScrollToTopButton />
Expand Down
9 changes: 3 additions & 6 deletions FrontEnd/src/hooks/useProvideAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import axios from 'axios';
export default function useProvideAuth() {
const [isAuth, setIsAuth] = useState(false);
const [isLoading, setLoading] = useState(true);
const [UserId, setUserId] = useState(null);
const validateToken = async (authToken) => {
try {
const userData = await axios.get(
await axios.get(
`${process.env.REACT_APP_BASE_API_URL}/api/auth/users/me/`,
{
headers: {
Authorization: `Token ${authToken}`,
},
}
)
.then((res) => res.data);
setUserId(userData.id);
);
return true;
} catch (error) {
if (error.response && error.response.status === 401) {
Expand Down Expand Up @@ -65,5 +62,5 @@ export default function useProvideAuth() {
}
});
});
return { login, logout, isAuth, isLoading, UserId };
return { login, logout, isAuth, isLoading };
}

0 comments on commit f90bff9

Please sign in to comment.