Skip to content

Commit

Permalink
refactor: remove withRouter, refactor routing components (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark86092 authored Jun 19, 2024
1 parent 8cb4573 commit 17dcddf
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 193 deletions.
21 changes: 8 additions & 13 deletions src/components/App/Header/Searchbar.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import cn from 'classnames';
import { withRouter } from 'react-router-dom';
import qs from 'qs';

import { useHistory } from 'react-router-dom';
import SearchTextInput from 'common/form/TextInput/SearchTextInput';
import Magnifiner from 'common/icons/Magnifiner';
import { keywordFromQuerySelector } from 'selectors/routing/keyword';
import { useQuery } from 'hooks/routing';
import styles from './Searchbar.module.css';

const getInitialSearchTextFromLocation = location =>
qs.parse(location.search, { ignoreQueryPrefix: true }).q || '';

const Searchbar = ({ className, placeholder, history, location }) => {
const [searchText, setSearchText] = useState(
getInitialSearchTextFromLocation(location),
);
const Searchbar = ({ className, placeholder }) => {
const history = useHistory();
const query = useQuery();
const [searchText, setSearchText] = useState(keywordFromQuerySelector(query));
const [isActive, setActive] = useState(false);

const handleFormFocus = useCallback(() => {
Expand Down Expand Up @@ -63,9 +60,7 @@ const Searchbar = ({ className, placeholder, history, location }) => {

Searchbar.propTypes = {
className: PropTypes.string.isRequired,
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
placeholder: PropTypes.string,
};

export default withRouter(Searchbar);
export default Searchbar;
9 changes: 2 additions & 7 deletions src/components/Buy/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React, { useState, useMemo, useEffect } from 'react';
import { useLocation } from 'react-router';
import { useDispatch } from 'react-redux';
import qs from 'qs';
import { contains, head, prop } from 'ramda';
import { Section, Wrapper } from 'common/base';
import SubscriptionsSection from './SubscriptionsSection';
import PaymentSection from './PaymentSection';
import TitleSection from './TitleSection';
import styles from './Buy.module.css';
import { useSubscriptionPlans } from 'hooks/payment/usePayment';
import { useQuery } from 'hooks/routing';
import { isFetched } from 'utils/fetchBox';
import Loading from 'common/Loader';
import { subscriptionType } from 'constants/subscription';
Expand All @@ -23,11 +22,7 @@ const sanitizeSkuId = plans => skuId => {
};

const Buy = () => {
const location = useLocation();
const query = useMemo(
() => qs.parse(location.search, { ignoreQueryPrefix: true }),
[location.search],
);
const query = useQuery();
const plansBox = useSubscriptionPlans();
const ready = isFetched(plansBox);
const plans = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Company/CompanyIndexProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { querySelector } from 'common/routing/selectors';
import { pageFromQuerySelector } from 'common/routing/page';
import { pageFromQuerySelector } from 'selectors/routing/page';
import CompanyAndJobTitleIndexPage from '../CompanyAndJobTitle/IndexPage';
import usePagination from '../CompanyAndJobTitle/IndexPage/usePagination';
import { pageType } from 'constants/companyJobTitle';
Expand Down
17 changes: 6 additions & 11 deletions src/components/EmailVerification/VerifyEmailForm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import cn from 'classnames';
import { withRouter } from 'react-router';
import { useLocation } from 'react-router-dom';

import Heading from 'common/base/Heading';
import P from 'common/base/P';
Expand All @@ -19,12 +19,10 @@ const stageMap = {
LOADING: 'LOADING',
};

const VerifyEmailForm = ({
userEmail,
onSubmit,
closeModal,
location: { pathname },
}) => {
const VerifyEmailForm = ({ userEmail, onSubmit, closeModal }) => {
const location = useLocation();
const pathname = location.pathname;

const [emailValue, setEmailValue] = useState(userEmail || '');
const [stage, setStage] = useState(stageMap.FORM);
const [emailValueFormatValid, setEmailValueFormatValid] = useState(true);
Expand Down Expand Up @@ -193,11 +191,8 @@ const VerifyEmailForm = ({

VerifyEmailForm.propTypes = {
closeModal: PropTypes.func,
location: PropTypes.shape({
pathname: PropTypes.string,
}),
onSubmit: PropTypes.func,
userEmail: PropTypes.string,
};

export default withRouter(VerifyEmailForm);
export default VerifyEmailForm;
2 changes: 1 addition & 1 deletion src/components/JobTitle/JobTitleIndexProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { querySelector } from 'common/routing/selectors';
import { pageFromQuerySelector } from 'common/routing/page';
import { pageFromQuerySelector } from 'selectors/routing/page';
import CompanyAndJobTitleIndexPage from '../CompanyAndJobTitle/IndexPage';
import usePagination from '../CompanyAndJobTitle/IndexPage/usePagination';
import { pageType } from 'constants/companyJobTitle';
Expand Down
13 changes: 4 additions & 9 deletions src/components/LandingPage/CallToActionBlock.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import { Heading } from 'common/base';

import SearchTextInput from 'common/form/TextInput/SearchTextInput';
import Button from 'common/button/ButtonRect';
import { useShareLink } from 'hooks/experiments';
import styles from './CallToActionBlock.module.css';
import textInputStyle from 'common/form/TextInput/TextInput.module.css';

const CallToActionBlock = ({ history }) => {
const CallToActionBlock = () => {
const history = useHistory();
const [companyName, setCompanyName] = useState('');
const shareLink = useShareLink(companyName);

Expand Down Expand Up @@ -46,8 +45,4 @@ const CallToActionBlock = ({ history }) => {
);
};

CallToActionBlock.propTypes = {
history: PropTypes.object.isRequired,
};

export default withRouter(CallToActionBlock);
export default CallToActionBlock;
13 changes: 5 additions & 8 deletions src/components/ShareExperience/Entry.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { useHistory } from 'react-router';
import { Wrapper } from 'common/base';
import ShareExpSection from 'common/ShareExpSection';
import X from 'common/icons/X';
import styles from './Entry.module.css';
import StaticHelmet from 'common/StaticHelmet';

const Entry = ({ history }) => {
const Entry = () => {
const history = useHistory();

return (
<div>
<StaticHelmet.Share />
Expand All @@ -21,8 +22,4 @@ const Entry = ({ history }) => {
);
};

Entry.propTypes = {
history: PropTypes.object.isRequired,
};

export default withRouter(Entry);
export default Entry;
29 changes: 8 additions & 21 deletions src/components/TimeAndSalary/SearchBar.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import cn from 'classnames';
import R from 'ramda';
import { withRouter } from 'react-router-dom';

import { useHistory } from 'react-router-dom';
import SearchTextInput from 'common/form/TextInput/SearchTextInput';
import Magnifiner from 'common/icons/Magnifiner';

import { useQuery } from 'hooks/routing';
import styles from './SearchBar.module.css';
import { searchKeywordSelector } from './common/selectors';

const getInitialSearchTextFromLocation = R.compose(
R.defaultTo(''),
searchKeywordSelector,
);
import { keywordFromQuerySelector } from 'selectors/routing/keyword';

const SearchBar = ({ history, location }) => {
const [searchText, setSearchText] = useState(
getInitialSearchTextFromLocation({ location }),
);
const SearchBar = () => {
const history = useHistory();
const query = useQuery();
const [searchText, setSearchText] = useState(keywordFromQuerySelector(query));

const gotoSearchResult = useCallback(
searchText => {
Expand Down Expand Up @@ -63,9 +55,4 @@ const SearchBar = ({ history, location }) => {
);
};

SearchBar.propTypes = {
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
};

export default withRouter(SearchBar);
export default SearchBar;
15 changes: 6 additions & 9 deletions src/components/TimeAndSalary/SearchScreen/SearchScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ import Pagination from 'common/Pagination';
import { isFetching, isFetched, isUnfetched } from 'constants/status';
import { pageType } from 'constants/companyJobTitle';
import { useQuery } from 'hooks/routing';
import { usePage } from 'hooks/routing/page';
import { queryKeyword, keywordMinLength } from 'actions/timeAndSalarySearch';
import {
keywordSelector,
pageSelector,
dataSelector,
statusSelector,
} from './selectors';
import { keywordFromQuerySelector } from 'selectors/routing/keyword';
import { dataSelector, statusSelector } from './selectors';
import * as urlBuilder from './urlBuilder';
import WorkingHourBlock from './WorkingHourBlock';
import Helmet from './Helmet';
Expand Down Expand Up @@ -48,8 +45,8 @@ const SearchScreen = () => {
const history = useHistory();
const dispath = useDispatch();
const query = useQuery();
const keyword = useMemo(() => keywordSelector(query), [query]);
const page = useMemo(() => pageSelector(query), [query]);
const keyword = useMemo(() => keywordFromQuerySelector(query), [query]);
const page = usePage();
const data = useSelector(dataSelector(keyword));
const status = useSelector(statusSelector(keyword));

Expand Down Expand Up @@ -104,7 +101,7 @@ const SearchScreen = () => {

SearchScreen.fetchData = ({ store: { dispatch }, ...props }) => {
const query = querySelector(props);
const keyword = keywordSelector(query);
const keyword = keywordFromQuerySelector(query);
return dispatch(queryKeyword({ keyword }));
};

Expand Down
16 changes: 0 additions & 16 deletions src/components/TimeAndSalary/SearchScreen/selectors.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
import { compose } from 'recompose';
import fetchingStatus from 'constants/status';
import { validatePage, validateSearchKeyword } from '../common/validators';
import {
keywordFromQuerySelector,
pageFromQuerySelector,
} from '../common/selectors';

export const keywordSelector = compose(
validateSearchKeyword,
keywordFromQuerySelector,
);

export const pageSelector = compose(
validatePage,
pageFromQuerySelector,
);

// 當 state 狀態跟目前要查詢的 key 不合,回傳預設值
export const dataSelector = keyword => state => {
Expand Down
17 changes: 0 additions & 17 deletions src/components/TimeAndSalary/common/selectors.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/components/TimeAndSalary/common/validators.js

This file was deleted.

Loading

0 comments on commit 17dcddf

Please sign in to comment.