Skip to content

Commit

Permalink
Merge pull request #30 from Tamir198/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Tamir198 authored Nov 10, 2022
2 parents efd0897 + 41b710a commit c0d0f5c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions client/src/components/AllCharacters/useHandleCharacters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { fetchCharacters } from "redux/actions";
/**
* Returns the current characters page data
* Returns the current characters page data
*
* This hook will check redux store for already existing data, if not
* found - send GET request to the server and update the store.
*
@pageNum is the wanted page number (can be found inside AllCharacters component)
@pageNum is the wanted page number
*/
export const useHandleCharacters = ({ pageNum }) => {
const { characters } = useSelector((state) => state.characters);
Expand Down
10 changes: 2 additions & 8 deletions client/src/pages/CharacterGallery/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { useState, useCallback } from "react";
import ReactPaginate from "https://cdn.skypack.dev/[email protected]";
import { AllCharacters } from "components";
import { config } from "constants/config";
import { useCurrPage } from "./useCurrPage";

import styles from "./CharacterGallery.module.css";

const CharacterGallery = () => {
//TODO: use separated hook for this
const {
pageCount,
startingPageNum,
pageRangeDisplayed,
marginPagesDisplayed,
} = config;

const [currPage, setCurrPage] = useState(startingPageNum);

//todo change all handlers to use callback
const handlePageClick = useCallback(({ selected }) => {
setCurrPage(selected + 1);
}, []);
const { currPage, handlePageClick } = useCurrPage({ startingPageNum });

return (
<div>
Expand Down
11 changes: 11 additions & 0 deletions client/src/pages/CharacterGallery/useCurrPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useState, useCallback } from "react";

export const useCurrPage = ({ startingPageNum }) => {
const [currPage, setCurrPage] = useState(startingPageNum);

const handlePageClick = useCallback(({ selected }) => {
setCurrPage(selected + 1);
}, []);

return { currPage, handlePageClick };
};
10 changes: 10 additions & 0 deletions client/src/pages/CharacterSearch/useCurrentCharacter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useDispatch, useSelector } from "react-redux";

export const useCurrentCharacter = () => {
const dispatch = useDispatch();
const { id: characterId, data } = useSelector(
(state) => state.characters.currentCharacter
);

return { dispatch, id: characterId, data };
};
2 changes: 1 addition & 1 deletion client/src/pages/Home/homeStyle.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: end;
align-items: flex-end;
flex: 1fr 1fr
}

Expand Down
13 changes: 2 additions & 11 deletions client/src/pages/Home/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import homePageGif from "assets/homeGif.gif";
import { fetchGeneralInfo } from "redux/actions";
import { TopDescription } from "./TopDescription";

import styles from "./homeStyle.module.css";
import { useGeneralInfo } from "./useGeneralInfo";

const Home = () => {
//TODO: use separated hook for this
const { generalInfo } = useSelector((state) => state.generalInfo);
const dispatch = useDispatch();

useEffect(() => {
dispatch(fetchGeneralInfo());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const generalInfo = useGeneralInfo();

return (
<>
Expand Down
15 changes: 15 additions & 0 deletions client/src/pages/Home/useGeneralInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { fetchGeneralInfo } from "redux/actions";

export const useGeneralInfo = () => {
const { generalInfo } = useSelector((state) => state.generalInfo);
const dispatch = useDispatch();

useEffect(() => {
dispatch(fetchGeneralInfo());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return generalInfo;
};
11 changes: 4 additions & 7 deletions client/src/pages/characterSearch/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Character } from "components";
import { Character as CharacterModel } from "models";
import { useDispatch, useSelector } from "react-redux";
import { fetchCharacterById } from "redux/actions";

import styles from "./characterSearch.module.css";
import { searchCharacterById } from "redux/reducers/character";
import { useCurrentCharacter } from "./useCurrentCharacter";

import styles from "./characterSearch.module.css";

const CharacterSearch = () => {
//TODO: use separated hook for this
const dispatch = useDispatch();
const { id: characterId, data } = useSelector(
(state) => state.characters.currentCharacter
);
const { dispatch, id: characterId, data } = useCurrentCharacter();

return (
<>
Expand Down

1 comment on commit c0d0f5c

@vercel
Copy link

@vercel vercel bot commented on c0d0f5c Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.