├── .gitignore
├── .prettierrc
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
└── manifest.json
└── src
├── Store.js
├── elements
├── AppNav
│ ├── index.js
│ └── style.css
├── CommentItem
│ ├── index.js
│ └── style.css
├── HackerNews
│ ├── index.js
│ └── style.css
├── StoriesPage
│ ├── index.js
│ └── style.css
├── StoryItem
│ ├── index.js
│ └── style.css
├── StoryPage
│ ├── index.js
│ └── style.css
└── UserPage
│ ├── index.js
│ └── style.css
├── index.js
├── serviceWorker.js
└── setupRoutes.js
/.gitignore:
--------------------------------------------------------------------------------
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*
24 |
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "trailingComma": "none",
3 | "tabWidth": 2,
4 | "semi": true,
5 | "singleQuote": false,
6 | "printWidth": 100
7 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Solid Hacker News App
2 |
3 | This is an older webcomponent demo. If you want to see the latest Solid Hackernews look [here](https://github.com/solidjs/solid-hackernews)
4 |
5 | Demo app based on [NX-JS Hacker News Example](https://github.com/nx-js/hackernews-example). Use Solid Element and WebComponent Router with Solid.
6 |
7 | You can view it [here](http://ryansolid.github.io/solid-hackernews-app).
8 |
9 | This project was bootstrapped with [Create Solid](https://github.com/ryansolid/create-solid).
10 |
11 |
12 | ## Testing Locally:
13 | First, you'll need to clone this repo, then cd into the `solid-hackernews-app` folder
14 |
15 | Then, run `npm install` to install all dependencies
16 |
17 | Lastly, run `npm run start` and the web-app will open in your default browser at `http://localhost:3000/`
18 |
19 | Happy Hacking!
20 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "solid-hackernews-app",
3 | "version": "0.1.2",
4 | "homepage": "http://ryansolid.github.io/solid-hackernews-app",
5 | "private": true,
6 | "dependencies": {
7 | "solid-element": "1.0.0",
8 | "solid-scripts": "0.0.58",
9 | "webcomponent-router": "0.4.3"
10 | },
11 | "scripts": {
12 | "start": "solid-scripts start",
13 | "build": "solid-scripts build",
14 | "test": "solid-scripts test"
15 | },
16 | "browserslist": [
17 | "Chrome 74",
18 | "Firefox 63",
19 | "Safari 11",
20 | "Edge 17",
21 | "Node 12"
22 | ]
23 | }
24 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryansolid/solid-hackernews-app/fbfaee44b663b4a749343d6029a7a9fad6a09d76/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | Hacker News
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "Hacker News",
3 | "name": "Solid Hacker News App",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/src/Store.js:
--------------------------------------------------------------------------------
1 | import { createContext, useContext } from "solid-js";
2 |
3 | const StoreContext = createContext();
4 | export function StoreProvider(props) {
5 | return {props.children};
6 | }
7 |
8 | export function useStore() {
9 | return useContext(StoreContext);
10 | }
11 |
12 | const mapStories = {
13 | top: "news",
14 | new: "newest",
15 | show: "show",
16 | ask: "ask",
17 | job: "jobs"
18 | };
19 | function createStore() {
20 | const cache = {};
21 |
22 | const get = (path) =>
23 | cache[path] ||
24 | (cache[path] = fetch(`https://node-hnapi.herokuapp.com/${path}`).then((r) => r.json()));
25 |
26 | return {
27 | getItem: (id) => get(`item/${id}`),
28 | getUser: (id) => get(`user/${id}`),
29 | getStories: (type, page) => get(`${mapStories[type]}?page=${page}`)
30 | };
31 | }
32 |
--------------------------------------------------------------------------------
/src/elements/AppNav/index.js:
--------------------------------------------------------------------------------
1 | import { customElement } from 'solid-element';
2 |
3 | import style from './style.css';
4 |
5 | const AppNav = () => (
6 | <>
7 |
8 | Hacker News
9 | new{" "}
10 | | show{" "}
11 | | ask{" "}
12 | | jobs
13 |
14 | Built with Solid |{" "}
15 | Source
16 |
17 | >
18 | )
19 |
20 | export default customElement('app-nav', AppNav);
21 |
--------------------------------------------------------------------------------
/src/elements/AppNav/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | display: block;
3 | background-color: rgb(255, 102, 0);
4 | padding: 4px;
5 | overflow: hidden;
6 | contain: content;
7 | }
8 |
9 | a {
10 | color: inherit;
11 | text-decoration: none;
12 | cursor: pointer;
13 | }
14 |
15 | a.active {
16 | color: white;
17 | }
18 |
19 | b {
20 | padding: 0 4px;
21 | }
22 |
23 | span {
24 | color: white;
25 | display: inline-block;
26 | float: right;
27 | font-size: 9pt;
28 | }
29 |
30 | span a:hover {
31 | text-decoration: underline;
32 | }
--------------------------------------------------------------------------------
/src/elements/CommentItem/index.js:
--------------------------------------------------------------------------------
1 | import { createSignal } from "solid-js";
2 | import { customElement } from "solid-element";
3 | import style from "./style.css";
4 |
5 | const CommentItem = ({ comment }) => {
6 | const [hidden, setHidden] = createSignal(false);
7 |
8 | return (
9 | <>
10 |
11 |
12 |
19 |
20 |
21 |
22 |
23 | {(child) => (
24 | -
25 |
26 |
27 | )}
28 |
29 |
30 |
31 |
32 | >
33 | );
34 | };
35 |
36 | export default customElement("comment-item", { comment: null }, CommentItem);
37 |
--------------------------------------------------------------------------------
/src/elements/CommentItem/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | margin: 20px 0;
3 | font-size: 9pt;
4 | }
5 |
6 | .header {
7 | margin-bottom: 5px;
8 | }
9 |
10 | .body a {
11 | text-decoration: underline;
12 | }
13 |
14 | a {
15 | color: inherit;
16 | text-decoration: none;
17 | cursor: pointer;
18 | }
19 |
20 | li {
21 | list-style-type: none;
22 | }
23 |
24 | .light {
25 | color: #828282;
26 | }
27 |
28 | .light a:hover {
29 | text-decoration: underline;
30 | }
31 |
32 | .subtext {
33 | font-size: 7pt;
34 | }
--------------------------------------------------------------------------------
/src/elements/HackerNews/index.js:
--------------------------------------------------------------------------------
1 | import { customElement } from "solid-element";
2 | import Router from "webcomponent-router";
3 |
4 | import { StoreProvider } from "../../Store";
5 | import setupRoutes from "../../setupRoutes";
6 | import style from "./style.css";
7 |
8 | const HackerNews = (_, { element }) => {
9 | const router = new Router(element, {
10 | location: "hash",
11 | root: process.env.NODE_ENV === "production" ? "solid-hackernews-app/" : ""
12 | });
13 | setupRoutes(router);
14 | router.start();
15 |
16 | return (
17 | <>
18 |
19 |
20 |
21 |
22 |
23 | >
24 | );
25 | };
26 |
27 | export default customElement("hacker-news", HackerNews);
28 |
--------------------------------------------------------------------------------
/src/elements/HackerNews/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | display: block;
3 | width: 85%;
4 | margin: auto;
5 | color: black;
6 | background-color: rgb(246, 246, 239);
7 | font: 10pt Verdana, Geneva, sans-serif;
8 | }
9 |
10 | a {
11 | color: inherit;
12 | text-decoration: none;
13 | cursor: pointer;
14 | }
15 |
16 | .light {
17 | color: #828282;
18 | }
19 |
20 | .light a:hover {
21 | text-decoration: underline;
22 | }
23 |
24 | .subtext {
25 | font-size: 7pt;
26 | }
27 |
28 | @media all and (max-width: 750px) {
29 | :host {
30 | width: 100%
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/elements/StoriesPage/index.js:
--------------------------------------------------------------------------------
1 | import { createComputed } from "solid-js";
2 | import { createStore } from "solid-js/store";
3 | import { customElement } from "solid-element";
4 |
5 | import(/*webpackChunkName: "story-item"*/ "../StoryItem");
6 |
7 | import { useStore } from "../../Store";
8 | import style from "./style.css";
9 |
10 | const StoriesPage = props => {
11 | const [state, setState] = createStore(),
12 | { getStories } = useStore();
13 |
14 | createComputed(async () => {
15 | const stories = await getStories(props.type, props.page);
16 | setState({ stories });
17 | });
18 |
19 | return (
20 | <>
21 |
22 | {story => }
23 |
24 | More
25 |
26 | >
27 | );
28 | };
29 |
30 | export default customElement(
31 | "stories-page",
32 | { type: "top", page: 1 },
33 | StoriesPage
34 | );
35 |
--------------------------------------------------------------------------------
/src/elements/StoriesPage/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | display: block;
3 | position: relative;
4 | padding: 10px;
5 | padding-bottom: 30px;
6 | min-height: 1000px;
7 | }
8 |
9 | story-item {
10 | margin-bottom: 8px;
11 | display: block;
12 | min-height: 27px;
13 | }
14 |
15 | .paginator {
16 | position: absolute;
17 | padding: 10px 0;
18 | bottom: 0;
19 | }
--------------------------------------------------------------------------------
/src/elements/StoryItem/index.js:
--------------------------------------------------------------------------------
1 | import { customElement } from 'solid-element';
2 | import style from './style.css';
3 |
4 | const StoryItem = ({ story }) =>
5 | <>
6 |
7 | {story.title}
11 | }
12 | >
13 | {story.title}
14 | ({story.domain})
15 |
16 |
17 |
{story.time_ago}
21 |
22 | }
23 | >
24 | {story.points && `${story.points} points by `}
25 | {story.user}{" "}
26 | {
27 | story.time_ago
28 | } |{" "}
29 | {
30 | story.comments_count ? `${story.comments_count} comments` : 'discuss'
31 | }
32 |
33 |
34 | >
35 |
36 | export default customElement('story-item', {story: {}}, StoryItem);
37 |
--------------------------------------------------------------------------------
/src/elements/StoryItem/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | display: block;
3 | min-height: 27px;
4 | }
5 |
6 | a {
7 | color: inherit;
8 | text-decoration: none;
9 | cursor: pointer;
10 | }
11 |
12 | .light {
13 | color: #828282;
14 | }
15 |
16 | .light a:hover {
17 | text-decoration: underline;
18 | }
19 |
20 | .subtext {
21 | font-size: 7pt;
22 | }
--------------------------------------------------------------------------------
/src/elements/StoryPage/index.js:
--------------------------------------------------------------------------------
1 | import { createComputed } from "solid-js";
2 | import { createStore } from "solid-js/store";
3 | import { customElement } from "solid-element";
4 |
5 | import(/*webpackChunkName: "story-item"*/ "../StoryItem");
6 | import(/*webpackChunkName: "comment-item"*/ "../CommentItem");
7 | import { useStore } from "../../Store";
8 | import style from "./style.css";
9 |
10 | const StoryPage = props => {
11 | const [state, setState] = createStore(),
12 | { getItem } = useStore();
13 |
14 | createComputed(async () => {
15 | const story = await getItem(props.storyId)
16 | setState({ story });
17 | });
18 |
19 | return (
20 | <>
21 |
22 |
23 |
24 |
25 |
26 |
27 | {comment => (
28 | -
29 |
30 |
31 | )}
32 |
33 |
34 |
35 | >
36 | );
37 | };
38 |
39 | export default customElement("story-page", { storyId: 0 }, StoryPage);
40 |
--------------------------------------------------------------------------------
/src/elements/StoryPage/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | display: block;
3 | padding: 10px;
4 | }
5 |
6 | .body {
7 | display: block;
8 | margin: 20px 0;
9 | }
10 |
11 | li {
12 | list-style-type: none;
13 | }
--------------------------------------------------------------------------------
/src/elements/UserPage/index.js:
--------------------------------------------------------------------------------
1 | import { createStore } from "solid-js/store";
2 | import { customElement } from "solid-element";
3 |
4 | import { useStore } from "../../Store";
5 | import style from "./style.css";
6 |
7 | const UserPage = ({ userId }) => {
8 | const [state, setState] = createStore(),
9 | { getUser } = useStore();
10 |
11 | getUser(userId).then(user => setState({ user }));
12 |
13 | return (
14 | <>
15 |
16 |
17 | user: {state.user.id}
18 | created: {state.user.created}
19 | karma: {state.user.karma}
20 |
21 | about:
22 |
23 |
24 | >
25 | );
26 | };
27 |
28 | export default customElement("user-page", { userId: "" }, UserPage);
29 |
--------------------------------------------------------------------------------
/src/elements/UserPage/style.css:
--------------------------------------------------------------------------------
1 | :host {
2 | display: block;
3 | padding: 10px;
4 | }
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import 'webcomponent-router/components';
2 |
3 | import(/*webpackChunkName: "hacker-news", webpackPreload: true*/ "./elements/HackerNews");
4 | import(/*webpackChunkName: "app-nav", webpackPreload: true*/ "./elements/AppNav");
5 | import * as serviceWorker from './serviceWorker';
6 |
7 | // If you want your app to work offline and load faster, you can change
8 | // unregister() to register() below. Note this comes with some pitfalls.
9 | // Learn more about service workers: https://bit.ly/CRA-PWA
10 | serviceWorker.unregister();
11 |
--------------------------------------------------------------------------------
/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read https://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.1/8 is considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl)
104 | .then(response => {
105 | // Ensure service worker exists, and that we really are getting a JS file.
106 | const contentType = response.headers.get('content-type');
107 | if (
108 | response.status === 404 ||
109 | (contentType != null && contentType.indexOf('javascript') === -1)
110 | ) {
111 | // No service worker found. Probably a different app. Reload the page.
112 | navigator.serviceWorker.ready.then(registration => {
113 | registration.unregister().then(() => {
114 | window.location.reload();
115 | });
116 | });
117 | } else {
118 | // Service worker found. Proceed as normal.
119 | registerValidSW(swUrl, config);
120 | }
121 | })
122 | .catch(() => {
123 | console.log(
124 | 'No internet connection found. App is running in offline mode.'
125 | );
126 | });
127 | }
128 |
129 | export function unregister() {
130 | if ('serviceWorker' in navigator) {
131 | navigator.serviceWorker.ready.then(registration => {
132 | registration.unregister();
133 | });
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/src/setupRoutes.js:
--------------------------------------------------------------------------------
1 | export default (router) => {
2 | router.map(r => {
3 | r.notFound(() => ['index']);
4 | r.index({
5 | tag: 'stories-page',
6 | attributes: {type: 'top'},
7 | onEnter: () => import(/*webpackChunkName: "stories-page", webpackPrefetch: true*/ "./elements/StoriesPage")
8 | });
9 | r.route('new', {
10 | tag: 'stories-page',
11 | attributes: {type: 'new'},
12 | onEnter: () => import(/*webpackChunkName: "stories-page", webpackPrefetch: true*/ "./elements/StoriesPage")
13 | });
14 | r.route('show', {
15 | tag: 'stories-page',
16 | attributes: {type: 'show'},
17 | onEnter: () => import(/*webpackChunkName: "stories-page", webpackPrefetch: true*/ "./elements/StoriesPage")
18 | });
19 | r.route('ask', {
20 | tag: 'stories-page',
21 | attributes: {type: 'ask'},
22 | onEnter: () => import(/*webpackChunkName: "stories-page", webpackPrefetch: true*/ "./elements/StoriesPage")
23 | });
24 | r.route('job', {
25 | tag: 'stories-page',
26 | attributes: {type: 'job'},
27 | onEnter: () => import(/*webpackChunkName: "stories-page", webpackPrefetch: true*/ "./elements/StoriesPage")
28 | });
29 | r.route('user', {
30 | path: '/users/:userId',
31 | tag: 'user-page',
32 | onEnter: () => import(/*webpackChunkName: "user-page"*/ "./elements/UserPage")
33 | });
34 | r.route('story', {
35 | path: '/stories/:storyId',
36 | tag: 'story-page',
37 | onEnter: () => import(/*webpackChunkName: "story-page"*/ "./elements/StoryPage")
38 | });
39 | });
40 | }
--------------------------------------------------------------------------------