Compare commits

...

20 Commits

Author SHA1 Message Date
Jason Zhu 4a04834929 Chap04 -> Completing the home page styling -> Styling the Question component 2022-03-29 14:45:12 +11:00
Jason Zhu 1292870db9 Chap04 -> Completing the home page styling -> Styling the QuestionList component 2022-03-29 14:41:37 +11:00
Yiqing Zhu 055f653476 Chap04 -> Creating a reusable styled component with Emotion 2022-03-28 21:02:55 +11:00
Yiqing Zhu 691840dee3 Chap04 -> Styling pseudo-classes and nested elements with Emotion 2022-03-28 20:37:00 +11:00
Yiqing Zhu 26f7fc4582 Chap04 -> Styling components with Emotion -> Styling the Header component 2022-03-28 19:58:15 +11:00
Jason Zhu e4f41e5d8d Chap04 -> Styling components with Emotion -> Styling the App component (Emotion styles in head tag) 2022-03-28 19:36:40 +11:00
Jason Zhu c98972dec9 Chap04 -> Styling components with Emotion -> Styling the App component -> style css prop use tagged template literal 2022-03-28 17:56:13 +11:00
Jason Zhu 2005072dea Chap04 -> Styling components with Emotion -> Installing Emotion 2022-03-28 17:38:39 +11:00
Yiqing Zhu e90cc6b191 Chap04 -> Styling components with CSS modules 2022-03-26 17:56:51 +11:00
Yiqing Zhu 9830fb1af2 Chap04 -> Styling components with CSS -> Styling the Header component 2022-03-26 17:50:10 +11:00
Yiqing Zhu a5d1351f92 Chap04 -> Styling components with CSS -> Styling the App component 2022-03-26 17:38:20 +11:00
Yiqing Zhu 2b3cbdddd5 Chap04 -> Styling components with CSS -> Stlying the document body 2022-03-26 17:33:40 +11:00
Yiqing Zhu b4d0fed5cb Chap03 -> Handling events -> Handling an input change event 2022-03-26 17:00:21 +11:00
Yiqing Zhu 7dd02a76ea Chap03 -> Handling events -> Handling a button click event 2022-03-26 16:50:56 +11:00
Yiqing Zhu 0f31f492cc Chap03 -> Implementing component state -> Using useState to implement component state 2022-03-26 16:46:05 +11:00
Yiqing Zhu 2afb204673 Chap03 -> Implementing component state -> Using useEffect to execute logic 2022-03-26 16:03:57 +11:00
Yiqing Zhu cdecba7c2e Chap03 -> Implementing component state (Change getUnansweredQuestions to async function, result failure in transpiling) 2022-03-26 14:39:21 +11:00
Yiqing Zhu 072b7dcf88 Chap03 -> Creating function-based component -> Function props (aka how to use render prop) 2022-03-26 14:25:09 +11:00
Yiqing Zhu b6fdcbd740 Chap03 -> Creating function-based component -> Children props, added explanation 2022-03-26 14:12:16 +11:00
Yiqing Zhu 4affa35f00 Chap03 -> Creating function-based component -> Children props 2022-03-26 14:10:10 +11:00
15 changed files with 517 additions and 16349 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,8 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.8.2",
"@emotion/styled": "^11.8.1",
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",

View File

@ -1,38 +0,0 @@
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

View File

@ -1,11 +1,20 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import { Header } from './Header';
import './App.css';
import { HomePage } from './HomePage';
import { fontFamily, fontSize, gray2 } from './Styles';
function App() {
return (
<div className="App">
<div
css={css`
font-family: ${fontFamily};
font-size: ${fontSize};
color: ${gray2};
`}
>
<Header />
<HomePage />
</div>

View File

@ -0,0 +1,13 @@
.container {
position: fixed;
box-sizing: border-box;
top: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 20px;
background-color: #fff;
border-bottom: 1px solid #e3e2e2;
box-shadow: 0 3px 7px 0 rgba(110, 112, 114, 0.21);
}

View File

@ -1,13 +1,89 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { fontFamily, fontSize, gray1, gray2, gray5 } from './Styles';
import React from 'react';
import { UserIcon } from './Icons';
export const Header = () => (
<div>
<a href="./">Q & A</a>
<input type="text" placeholder="Search ..." />
<a href="./signin">
<UserIcon />
<span>Sign In</span>
</a>
</div>
);
export const Header = () => {
const handleSearchInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
console.log(e.currentTarget.value);
};
return (
<div
css={css`
position: fixed;
box-sizing: border-box;
top: 0;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 20px;
background-color: #fff;
border-bottom: 1px solid ${gray5};
box-shadow: 0 3px 7px 0 rgba(100, 112, 114, 0.21);
`}
>
<a
href="./"
css={css`
font-size: 24px;
font-weight: bold;
color: ${gray1};
text-decoration: none;
`}
>
Q & A
</a>
<input
type="text"
placeholder="Search ..."
onChange={handleSearchInputChange}
css={css`
box-sizing: border-box;
font-family: ${fontFamily};
font-size: ${fontSize};
padding: 8px 10px;
border: 1px solid ${gray5};
border-radius: 3px;
color: ${gray2};
background-color: white;
width: 200px;
height: 30px;
:focus {
outline-color: ${gray5};
}
`}
/>
<a
href="./signin"
css={css`
font-family: ${fontFamily};
font-size: ${fontSize};
padding: 5px 10px;
background-color: transparent;
color: ${gray2};
text-decoration: none;
cursor: pointer;
:focus {
outline-color: ${gray5};
}
span {
margin-left: 7px;
}
`}
>
<UserIcon />
<span
css={css`
margin-left: 7px;
`}
>
Sign In
</span>
</a>
</div>
);
};

View File

@ -1,13 +1,49 @@
import React from 'react';
import { QuestionList } from './QuestionList';
import { getUnansweredQuestions } from './QuestionsData';
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
export const HomePage = () => (
<div>
<div>
<h2>Unanswered Questions</h2>
<button>Ask a question</button>
</div>
<QuestionList data={getUnansweredQuestions()} />
</div>
);
import React from 'react';
import { PageTitle } from './PageTitle';
import { Page } from './Page';
import { QuestionList } from './QuestionList';
import { getUnansweredQuestions, QuestionData } from './QuestionsData';
import { PrimaryButton } from './Styles';
export const HomePage = () => {
const [questions, setQuestions] = React.useState<QuestionData[]>([]);
const [questionsLoading, setQuestionsLoading] = React.useState(true);
React.useEffect(() => {
const doGetUnansweredQuestion = async () => {
const unanswereedQuestions = await getUnansweredQuestions();
setQuestions(unanswereedQuestions);
setQuestionsLoading(false);
};
doGetUnansweredQuestion();
}, []);
const handleAskQuestionClick = () => {
console.log('TODO - move to the AskPage');
};
return (
<Page>
<div
css={css`
display: flex;
align-items: center;
justify-content: space-between;
`}
>
<PageTitle>Unanswered Questions</PageTitle>
<PrimaryButton onClick={handleAskQuestionClick}>
Ask a question
</PrimaryButton>
</div>
{questionsLoading ? (
<div>Loading...</div>
) : (
<QuestionList data={questions || []} />
)}
</Page>
);
};

View File

@ -1,3 +1,15 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import user from './user.svg';
export const UserIcon = () => <img src={user} alt="User" width="12px" />;
export const UserIcon = () => (
<img
src={user}
alt="User"
css={css`
width: 12px;
opacity: 0.6;
`}
/>
);

View File

@ -0,0 +1,24 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import { PageTitle } from './PageTitle';
interface Props {
title?: string;
children: React.ReactNode;
}
export const Page = ({ title, children }: Props) => (
<div
css={css`
margin: 50px auto 20px auto;
padding: 30px 20px;
max-width: 600px;
`}
>
{title && <PageTitle>{title}</PageTitle>}
{children}
{/* Component nested within Page component will be rendere here */}
</div>
);

View File

@ -0,0 +1,20 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
interface Props {
children: React.ReactNode;
}
export const PageTitle = ({ children }: Props) => (
<h2
css={css`
font-size: 15px;
font-weight: bold;
margin: 10px 0px 5px;
text-align: center;
text-transform: uppercase;
`}
>
{children}
</h2>
);

View File

@ -1,3 +1,7 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { gray2, gray3 } from './Styles';
import React from 'react';
import { QuestionData } from './QuestionsData';
@ -8,15 +12,34 @@ interface Props {
export const Question = ({ data, showContent }: Props) => (
<div>
<div>{data.title}</div>
<div
css={css`
padding: 10px 0px;
font-size: 19px;
`}
>
{data.title}
</div>
{showContent && (
<div>
<div
css={css`
padding-bottom: 10px;
font-size: 15px;
color: ${gray2};
`}
>
{data.content.length > 50
? `${data.content.substring(0, 50)}`
: data.content}
</div>
)}
<div>
<div
css={css`
font-size: 12px;
font-style: italic;
color: ${gray3};
`}
>
{`Ask by ${
data.userName
} on ${data.created.toLocaleDateString()} ${data.created.toLocaleTimeString()}`}

View File

@ -1,15 +1,38 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import React from 'react';
import { Question } from './Question';
import { QuestionData } from './QuestionsData';
import { accent2, gray5 } from './Styles';
interface Props {
data: QuestionData[];
}
export const QuestionList = ({ data }: Props) => (
<ul>
<ul
css={css`
list-style: none;
margin: 10px 0 0 0;
padding: 0px 20px;
background-color: #fff;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-top: 3px solid ${accent2};
box-shadow: 0 3px 5px 0 rgba(0, 0, 0, 0.16);
`}
>
{data.map((question) => (
<li key={question.questionId}>
<li
key={question.questionId}
css={css`
border-top: 1px solid ${gray5};
:first-of-type {
border-top: none;
}
`}
>
<Question data={question} />
</li>
))}

View File

@ -48,6 +48,11 @@ const questions: QuestionData[] = [
},
];
export const getUnansweredQuestions = (): QuestionData[] => {
export const getUnansweredQuestions = async (): Promise<QuestionData[]> => {
await wait(500);
return questions.filter((q) => q.answers.length === 0);
};
const wait = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

View File

@ -0,0 +1,35 @@
import styled from '@emotion/styled';
export const gray1 = '#383737';
export const gray2 = '#5c5a5a';
export const gray3 = '#857c81';
export const gray4 = '#b9b9b9';
export const gray5 = '#e3e2e2';
export const gray6 = '#f7f8fa';
export const primary1 = '#681c41';
export const primary2 = '#824c67';
export const accent1 = '#dbb365';
export const accent2 = '#efd197';
export const fontFamily = "'Segoe UI', 'Helvetica Neue',sans-serif";
export const fontSize = '16px';
export const PrimaryButton = styled.button`
background-color: ${primary2};
border-color: ${primary2};
border-style: solid;
border-radius: 5px;
font-family: ${fontFamily};
font-family: ${fontSize};
padding: 5px 10px;
color: white;
cursor: pointer;
:hover {
background-color: ${primary1};
}
:focus {
outline-color: ${primary2};
}
:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`;

View File

@ -1,13 +1,4 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
background-color: #f7f8fa;
}