Compare commits

...

25 Commits

Author SHA1 Message Date
Jason Zhu c913b32ddb Chap05 -> Lazy loading routers 2022-03-30 17:05:28 +11:00
Jason Zhu 3580e9b0b4 Chap05 -> Using query parameters -> 7 & 8: Render search criteria and questions that returned from search 2022-03-30 16:43:26 +11:00
Jason Zhu 2319706d66 Chap05 -> Using query parameters -> 6. Invoke search when component first renders and when the search variable changes using the useEffect hook 2022-03-30 16:39:03 +11:00
Jason Zhu 7f5df7cdf3 Chap05 -> Usinig query parameters -> 2 & 3: configure import in SearchPage and add explicit return statement 2022-03-30 16:32:39 +11:00
Jason Zhu 62e7510193 Chap05 -> Using query parameters -> 1. Create function to simulate a search via web request 2022-03-30 16:30:54 +11:00
Jason Zhu 3414f20798 Chap05 -> Using route parameters -> Creating an AnswerList component 2022-03-30 16:18:22 +11:00
Jason Zhu a2d892793c Chap05 -> Using route parameter -> Creating an AnswerList component -> 4 & 5 & 6: Created Answer component 2022-03-30 15:08:09 +11:00
Jason Zhu 943e22ffdb Chap05 -> Using route parameters -> Creating an AnswerList component -> 1 & 2 & 3: created AnswerList.tsx 2022-03-30 14:58:26 +11:00
Jason Zhu 40b20ae730 Chap05 -> Using route paramters -> Implementinig more of the question page (fix background-color) 2022-03-30 14:53:04 +11:00
Jason Zhu b71ce601d7 Chap05 -> Using route paramters -> Implementinig more of the question page -> 8. Add when question asked and who asked into the Fragment 2022-03-30 14:51:48 +11:00
Jason Zhu dbb3d2f9b5 Chap05 -> Using route parameters -> Implementing more of the question page -> 7. Implement question content 2022-03-30 14:38:16 +11:00
Jason Zhu fa83caf8a7 Chap05 -> Using route parameters -> Implementiing more of the question page -> Implement JSX for Question Page component by adding container element for the page and question title 2022-03-30 14:34:23 +11:00
Jason Zhu f50dd73596 Chap05 -> Using route parameters -> Implementing more of the question page -> 5. call getQuestion function during initial render 2022-03-30 14:30:37 +11:00
Jason Zhu a51437c7cc Chap05 -> Using route parameters -> Implementing more of the question page -> 2 & 3 & 4: Create a state for question 2022-03-30 14:14:23 +11:00
Jason Zhu 191d87662d Chap05 -> Using route parameters -> Implementing more of the question pagee -> 1. Add a function that will simulate a web request to get a question 2022-03-30 14:04:55 +11:00
Jason Zhu e87108a892 Fixed problem in Chap04 2022-03-29 23:15:33 +11:00
Jason Zhu f986272027 Chap05 -> Using route parameters -> Adding the question page route (Finished) 2022-03-29 16:33:58 +11:00
Jason Zhu 4f9e43b95e Chap05 -> Using route parameters -> Adding the question page route 2022-03-29 16:25:56 +11:00
Jason Zhu 61660bf83d Chap05 -> Implementing links -> Navigating programmatically 2022-03-29 16:24:10 +11:00
Jason Zhu 6d5af0a856 Chap05 -> Implementing links -> Using the Link component 2022-03-29 16:22:23 +11:00
Jason Zhu 5c08042544 (Edit) Chap05 -> Declaring routes -> Creating a component containing routes: put Header component back to App 2022-03-29 16:19:21 +11:00
Jason Zhu 5b79adea19 Chap05 -> Handling routes not found 2022-03-29 15:55:55 +11:00
Jason Zhu fbc2541230 Chap05 -> Declaring routes -> Creating a component containing routes 2022-03-29 15:52:21 +11:00
Jason Zhu 7c3215867f Chap05 -> Declaring routes -> Creating some blank pages 2022-03-29 15:41:31 +11:00
Jason Zhu 4963def0bd Chap05 -> Install React Router 2022-03-29 15:06:09 +11:00
13 changed files with 283 additions and 34 deletions

View File

@ -12,8 +12,10 @@
"@types/node": "^16.11.26",
"@types/react": "^17.0.41",
"@types/react-dom": "^17.0.14",
"history": "^5.3.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.2",
"react-scripts": "5.0.0",
"typescript": "^4.6.2",
"web-vitals": "^2.1.4"

View File

@ -0,0 +1,38 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import { AnswerData } from './QuestionsData';
import { gray3 } from './Styles';
interface Props {
data: AnswerData;
}
export const Answer = ({ data }: Props) => (
<div
css={css`
padding: 10px 0px;
`}
>
<div
css={css`
padding: 10px 0px;
font-size: 13px;
`}
>
{data.content}
</div>
<div
css={css`
font-size: 12px;
font-style: italic;
color: ${gray3};
`}
>
{`Answered by ${
data.userName
} on ${data.created.toLocaleDateString()} ${data.created.toLocaleTimeString()}`}
</div>
</div>
);

View File

@ -0,0 +1,31 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import { AnswerData } from './QuestionsData';
import { Answer } from './Answer';
import { gray5 } from './Styles';
interface Props {
data: AnswerData[];
}
export const AnswerList = ({ data }: Props) => (
<ul
css={css`
list-style: none;
margin: 10px 0 0 0;
padding: 0;
`}
>
{data.map((answer) => (
<li
css={css`
border-top: 1px solid ${gray5};
`}
key={answer.answertId}
>
<Answer data={answer} />
</li>
))}
</ul>
);

View File

@ -2,29 +2,58 @@
import { css } from '@emotion/react';
import React from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { Header } from './Header';
import { HomePage } from './HomePage';
import { fontFamily, fontSize, gray2 } from './Styles';
import { SearchPage } from './SearchPage';
import { SignInPage } from './SignInPage';
import { NotFoundPage } from './NotFoundPage';
import { QuestionPage } from './QuestionPage';
const AskPage = React.lazy(() => import('./AskPage'));
function App() {
return (
<div
css={css`
font-family: ${fontFamily};
font-size: ${fontSize};
color: ${gray2};
`}
>
<BrowserRouter>
<Header />
<HomePage />
</div>
<div
css={css`
font-family: ${fontFamily};
font-size: ${fontSize};
color: ${gray2};
`}
>
<Routes>
<Route path="" element={<HomePage />} />
<Route path="search" element={<SearchPage />} />
<Route
path="ask"
element={
<React.Suspense
fallback={
<div
css={css`
margin-top: 100px;
text-align: center;
`}
>
Loading...
</div>
}
>
<AskPage />
</React.Suspense>
}
/>
<Route path="signin" element={<SignInPage />} />
<Route path="questions/:questionId" element={<QuestionPage />} />
<Route path="*" element={<NotFoundPage />} />
</Routes>
</div>
</BrowserRouter>
);
}
class ProblemComponent extends React.Component {
render() {
return <div ref="div" />;
}
}
export default App;

View File

@ -0,0 +1,6 @@
import React from 'react';
import { Page } from './Page';
export const AskPage = () => <Page title="Ask a question">{null}</Page>;
export default AskPage;

View File

@ -1,9 +1,10 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { fontFamily, fontSize, gray1, gray2, gray5 } from './Styles';
import React from 'react';
import { Link } from 'react-router-dom';
import { UserIcon } from './Icons';
import { fontFamily, fontSize, gray1, gray2, gray5 } from './Styles';
export const Header = () => {
const handleSearchInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@ -26,8 +27,8 @@ export const Header = () => {
box-shadow: 0 3px 7px 0 rgba(100, 112, 114, 0.21);
`}
>
<a
href="./"
<Link
to="/"
css={css`
font-size: 24px;
font-weight: bold;
@ -36,7 +37,7 @@ export const Header = () => {
`}
>
Q & A
</a>
</Link>
<input
type="text"
placeholder="Search ..."
@ -57,8 +58,8 @@ export const Header = () => {
}
`}
/>
<a
href="./signin"
<Link
to="signin"
css={css`
font-family: ${fontFamily};
font-size: ${fontSize};
@ -76,14 +77,8 @@ export const Header = () => {
`}
>
<UserIcon />
<span
css={css`
margin-left: 7px;
`}
>
Sign In
</span>
</a>
<span>Sign In</span>
</Link>
</div>
);
};

View File

@ -1,5 +1,6 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { useNavigate } from 'react-router-dom';
import React from 'react';
import { PageTitle } from './PageTitle';
@ -21,8 +22,9 @@ export const HomePage = () => {
doGetUnansweredQuestion();
}, []);
const navigate = useNavigate();
const handleAskQuestionClick = () => {
console.log('TODO - move to the AskPage');
navigate('ask');
};
return (

View File

@ -0,0 +1,4 @@
import React from 'react';
import { Page } from './Page';
export const NotFoundPage = () => <Page title="Page Not Found">{null}</Page>;

View File

@ -1,8 +1,9 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { gray2, gray3 } from './Styles';
import { Link } from 'react-router-dom';
import React from 'react';
import { gray2, gray3 } from './Styles';
import { QuestionData } from './QuestionsData';
interface Props {
@ -18,7 +19,15 @@ export const Question = ({ data, showContent }: Props) => (
font-size: 19px;
`}
>
{data.title}
<Link
css={css`
text-decoration: none;
color: ${gray2};
`}
to={`/questions/${data.questionId}`}
>
{data.title}
</Link>
</div>
{showContent && (
<div

View File

@ -0,0 +1,72 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { gray3, gray6 } from './Styles';
import React from 'react';
import { useParams } from 'react-router-dom';
import { Page } from './Page';
import { QuestionData, getQuestion } from './QuestionsData';
import { AnswerList } from './AnswerList';
export const QuestionPage = () => {
const [question, setQuestion] = React.useState<QuestionData | null>(null);
const { questionId } = useParams();
React.useEffect(() => {
const doGetQuestion = async (questionId: number) => {
const foundQuestion = await getQuestion(questionId);
setQuestion(foundQuestion);
};
if (questionId) {
doGetQuestion(Number(questionId));
}
}, [questionId]);
return (
<Page>
<div
css={css`
background-color: white;
padding: 15px 20px 20px 20px;
border-radius: 4px;
border: 1px solid ${gray6};
box-shadow: 0 3px 5px 0 rgba(0, 0, 0, 0.16);
`}
>
<div
css={css`
font-size: 19px;
font-weight: bold;
margin: 10px 0px 5px;
`}
>
{question === null ? '' : question.title}
</div>
{question !== null && (
<React.Fragment>
<p
css={css`
margin-top: 0px;
background-color: white;
`}
>
{question.content}
</p>
<div
css={css`
font-size: 12px;
font-style: italic;
color: ${gray3};
`}
>
{`Asked by ${
question.userName
} on ${question.created.toLocaleDateString()} ${question.created.toLocaleTimeString()}`}
</div>
<AnswerList data={question.answers} />
</React.Fragment>
)}
</div>
Question Page {questionId}
</Page>
);
};

View File

@ -56,3 +56,22 @@ export const getUnansweredQuestions = async (): Promise<QuestionData[]> => {
const wait = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const getQuestion = async (
questionId: number,
): Promise<QuestionData | null> => {
await wait(500);
const results = questions.filter((q) => q.questionId === questionId);
return results.length === 0 ? null : results[0];
};
export const searchQuestions = async (
criteria: string,
): Promise<QuestionData[]> => {
await wait(500);
return questions.filter(
(q) =>
q.title.toLowerCase().indexOf(criteria.toLowerCase()) >= 0 ||
q.content.toLowerCase().indexOf(criteria.toLowerCase()) >= 0,
);
};

View File

@ -0,0 +1,38 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import { useSearchParams } from 'react-router-dom';
import { Page } from './Page';
import { QuestionList } from './QuestionList';
import { searchQuestions, QuestionData } from './QuestionsData';
export const SearchPage = () => {
const [searchParams] = useSearchParams();
const [questions, setQuestions] = React.useState<QuestionData[]>([]);
const search = searchParams.get('criteria') || '';
React.useEffect(() => {
const doSearch = async (criteria: string) => {
const foundResults = await searchQuestions(criteria);
setQuestions(foundResults);
};
doSearch(search);
}, [search]);
return (
<Page title="Search Results">
{search && (
<p
css={css`
font-size: 16px;
font-style: italic;
margin-top: 0px;
`}
>
<QuestionList data={questions} />
</p>
)}
</Page>
);
};

View File

@ -0,0 +1,4 @@
import React from 'react';
import { Page } from './Page';
export const SignInPage = () => <Page title="Sign In">{null}</Page>;