Skip to content

Commit f00bc1a

Browse files
committed
chore: initial commit for code challenge
0 parents  commit f00bc1a

20 files changed

+12261
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Publicist TypeScript Challenge
2+
3+
## Setup
4+
5+
This project was created with React's Create React App scripts. To
6+
install the dependencies, please run:
7+
8+
```
9+
yarn install
10+
```
11+
12+
The development server can then be run via:
13+
14+
```
15+
yarn start
16+
```
17+
18+
## Goals
19+
20+
The goal of this challenge is to take a rich JSON document and render
21+
some HTML from it. This challenge is loosely based upon how we manage
22+
and include written content within our front-end applications.
23+
24+
An emphasis will be placed on fully typing the different kinds of nodes
25+
that must be rendered. Please try to use the type system to its fullest
26+
extent and fully leverage the types and interfaces you will define when
27+
rendering React components.
28+
29+
### Content Node Types
30+
31+
All Content Nodes will have a `type` property which dictates what type
32+
of node it is. From there, Content Nodes are divided into three main
33+
groups:
34+
35+
1. Block Content Nodes: Contain child nodes via their `nodes` property.
36+
They also can have arbitrary `attributes`.
37+
2. Text Content Nodes: Have a `text` property and are essentially leaf
38+
nodes in our content tree.
39+
3. Media Content Nodes: Have an `attributes` property and are leaf nodes
40+
as well.
41+
42+
#### Block Content Nodes
43+
44+
The general structure of for these nodes is as follows:
45+
46+
|property|description|
47+
|--------|-----------|
48+
|`type`|Type of content type|
49+
|`nodes`|List of child nodes for block content node|
50+
|`attributes`|Optional attribute object where the values are always strings|
51+
52+
Below are all of the possible Block Content Node types and their
53+
corresponding HTML element type:
54+
55+
|type|HTML element|
56+
|----|------------|
57+
|`header1`|`<h1>`|
58+
|`header2`|`<h2>`|
59+
|`header3`|`<h3>`|
60+
|`paragraph`|`<p>`|
61+
|`hyperlink`|`<a>`|
62+
|`numbered_list`|`<ol>`|
63+
|`bullet_list`|`<ul>`|
64+
|`list_item`|`<li>`|
65+
66+
#### Text Content Nodes
67+
68+
The general structure of for these nodes is:
69+
70+
|property|description|
71+
|--------|-----------|
72+
|`type`|Type of content type|
73+
|`text`|String of text. Not defined for `break` type|
74+
75+
Below are all of the possible Text Content Node types and their
76+
corresponding HTML element type:
77+
78+
|type|HTML element|
79+
|----|------------|
80+
|`bold`|`<strong>`|
81+
|`italic`|`<em>`|
82+
|`underline`|`<u>`|
83+
|`break`|`<br>`|
84+
|`text`|**No element**. This represents a Text Node in the DOM|
85+
86+
#### Media Content Nodes
87+
88+
The general structure of for these nodes is:
89+
90+
|property|description|
91+
|--------|-----------|
92+
|`type`|Type of content type|
93+
|`attributes`|Required attribute object where the values are always strings. A `src` is always included|
94+
95+
Below are all of the possible Block Content Node types and their
96+
corresponding HTML element type:
97+
98+
|type|HTML element|
99+
|----|------------|
100+
|`image`|`<img>`|
101+
|`video`|`<video>`|
102+
103+
When rendering these types of content nodes, the `attributes` property
104+
should be used as the corresponding HTML element's set of attributes.
105+
For example:
106+
107+
```json
108+
{
109+
"type": "image",
110+
"attributes": {
111+
"src": "https://www.publicist.co/images/default-avatar-v1.png",
112+
"alt": "Default avatar"
113+
}
114+
}
115+
```
116+
117+
Would produce the following rendered HTML:
118+
119+
```html
120+
<img src="https://www.publicist.co/images/default-avatar-v1.png" alt="Default avatar" />
121+
```
122+
123+
### Bonus
124+
125+
As a bonus, please create a simple carousel slider that wrap the 3
126+
`<TextSection>` components and periodically slides between the 3
127+
elements.
128+
129+
The goal of the carousel bonus would be to:
130+
131+
1. Use a bit of React state (or a reducer) to manage which element is
132+
shown and to trigger the transition from one `<TextSection>` element
133+
to another.
134+
2. Use CSS to animate and transition between the elements. The
135+
`@emotion/styled` library has been included for this purpose and a
136+
couple of Styled components could be created to achieve this feature.
137+
138+
References:
139+
- https://emotion.sh/docs/styled

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "publicist-typescript-challenge",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^5.11.4",
7+
"@testing-library/react": "^11.1.0",
8+
"@testing-library/user-event": "^12.1.10",
9+
"@types/jest": "^26.0.15",
10+
"@types/node": "^12.0.0",
11+
"@types/react": "^17.0.0",
12+
"@types/react-dom": "^17.0.0",
13+
"react": "^17.0.1",
14+
"react-dom": "^17.0.1",
15+
"react-scripts": "4.0.2",
16+
"typescript": "^4.1.2",
17+
"web-vitals": "^1.0.1"
18+
},
19+
"scripts": {
20+
"start": "react-scripts start",
21+
"build": "react-scripts build",
22+
"test": "react-scripts test",
23+
"eject": "react-scripts eject"
24+
},
25+
"eslintConfig": {
26+
"extends": [
27+
"react-app",
28+
"react-app/jest"
29+
]
30+
},
31+
"browserslist": {
32+
"production": [
33+
">0.2%",
34+
"not dead",
35+
"not op_mini all"
36+
],
37+
"development": [
38+
"last 1 chrome version",
39+
"last 1 firefox version",
40+
"last 1 safari version"
41+
]
42+
}
43+
}

public/favicon.ico

3.78 KB
Binary file not shown.

public/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

public/logo192.png

5.22 KB
Loading

public/logo512.png

9.44 KB
Loading

public/manifest.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

public/robots.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:

src/App.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import App from './App';
4+
5+
test('renders learn react link', () => {
6+
render(<App />);
7+
const linkElement = screen.getByText(/learn react/i);
8+
expect(linkElement).toBeInTheDocument();
9+
});

src/App.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
3+
import { TextSection } from './TextSection';
4+
5+
function App() {
6+
return (
7+
<React.Fragment>
8+
<TextSection id="welcome" />
9+
<TextSection id="blog_post" />
10+
<TextSection id="kitchen_sink" />
11+
</React.Fragment>
12+
);
13+
}
14+
15+
export default App;

src/TextSection/ErrorBoundary.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
3+
interface Props {
4+
id: string;
5+
}
6+
7+
interface State {
8+
hasError: boolean;
9+
}
10+
11+
export class ErrorBoundary extends React.Component<Props, State> {
12+
constructor(props: Props) {
13+
super(props);
14+
15+
this.state = { hasError: false };
16+
}
17+
18+
static getDerivedStateFromError(error: any): State {
19+
// Update state so the next render will show the fallback UI.
20+
return { hasError: true };
21+
}
22+
23+
render() {
24+
if (this.state.hasError) {
25+
// You can render any custom fallback UI
26+
return <h1>Failed to render for: {this.props.id}</h1>;
27+
}
28+
29+
return this.props.children;
30+
}
31+
}

src/TextSection/index.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
3+
import { default as Content } from '../content.json';
4+
import { ErrorBoundary } from './ErrorBoundary';
5+
6+
interface Props {
7+
id: string;
8+
}
9+
10+
function Root(props: any) {
11+
return (
12+
<div>
13+
<p>Content ID: <code>{props.id}</code></p>
14+
<pre>{JSON.stringify(props.nodes, null, 2) }</pre>
15+
</div>
16+
);
17+
}
18+
19+
export function TextSection({ id }: Props) {
20+
const nodes: any = (Content as any)[id];
21+
22+
return (
23+
<ErrorBoundary id={id}>
24+
<Root id={id} nodes={nodes} />
25+
</ErrorBoundary>
26+
);
27+
}

0 commit comments

Comments
 (0)