initial version random-quote-machine

This commit is contained in:
2020-04-02 01:21:47 -07:00
parent 5b48f673c4
commit 5fba3f09d7
45 changed files with 713 additions and 149 deletions

View File

@@ -2,37 +2,26 @@
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);
}
.red-background {
background-color: #ff0000;
color: black;
}
.blue-background {
background-color: #0000ff;
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.yellow-background {
background-color: #ffff00;
color: black;
}

View File

@@ -1,26 +1,23 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';
import React from "react";
import { connect } from "react-redux";
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
import "./App.css";
import QuoteBox from "./QuoteBox";
const mapStateToProps = (state) => ({ ...state });
class App extends React.Component {
render() {
const colors = ["red-background", "blue-background", "yellow-background"];
const appHeaderClass = "App-header " + colors[this.props.colorCount];
return (
<div className="App">
<header className={appHeaderClass}>
<QuoteBox />
</header>
</div>
);
}
}
export default App;
export default connect(mapStateToProps)(App);

View File

@@ -1,8 +1,8 @@
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';
import React from "react";
import { render } from "@testing-library/react";
import App from "./App";
test('renders learn react link', () => {
test("renders learn react link", () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();

View File

@@ -0,0 +1,28 @@
.NewQuote {
text-align: center;
height: 60%;
width: 45%;
border: none;
border-radius: 5px;
font-size: 0.8em;
font-weight: bold;
}
.NewQuote:focus {
outline: 0;
}
.red-background {
background-color: #ff0000;
color: #000000;
}
.blue-background {
background-color: #0000ff;
color: #ff8000;
}
.yellow-background {
background-color: #ffff00;
color: #0080ff;
}

View File

@@ -0,0 +1,44 @@
import React from "react";
import { connect } from "react-redux";
import "./NewQuote.css";
import { colorCounterAction } from "./actions/colorCounterAction";
import { quoteFetchActionCreator } from "./actions/quoteFetchActionCreator";
const mapStateToProps = (state) => ({ ...state });
const mapDispatchToProps = (dispatch) => ({
colorCounterAction: (count) => dispatch(colorCounterAction(count)),
quoteFetch: () => dispatch(quoteFetchActionCreator()),
});
class NewQuote extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.props.colorCounterAction(
this.props.colorCount > 1 ? 0 : this.props.colorCount + 1
);
this.props.quoteFetch();
}
render() {
const colors = ["red-background", "blue-background", "yellow-background"];
const newQuoteClass = "NewQuote " + colors[this.props.colorCount];
return (
<button
onClick={this.handleClick}
className={newQuoteClass}
id="new-quote"
>
Click Me!
</button>
);
}
}
export default connect(mapStateToProps, mapDispatchToProps)(NewQuote);

View File

@@ -0,0 +1,53 @@
.QuoteBox {
background-color: white;
text-align: center;
max-width: 80%;
max-height: 60vh;
width: 500px;
height: 500px;
border-radius: 10px;
box-shadow: 4px 3px black;
}
.red-complimentary-background {
background-color: #0080ff;
color: white;
}
.blue-complimentary-background {
background-color: #7fff00;
color: white;
}
.yellow-complimentary-background {
background-color: #8000ff;
color: black;
}
.quote-box-top {
height: 70%;
display: flex;
flex-direction: column;
justify-content: space-around;
}
.quote-box-bottom {
height: 30%;
display: flex;
flex-direction: row;
justify-content: flex-start;
}
.quote-box-bottom-left {
width: 50%;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.quote-box-bottom-right {
width: 50%;
display: flex;
flex-direction: row;
justify-content: space-around;
}

View File

@@ -0,0 +1,37 @@
import React from "react";
import { connect } from "react-redux";
import "./QuoteBox.css";
import NewQuote from "./NewQuote";
import TweetQuote from "./TweetQuote";
import TextAuthor from "./TextAuthor";
const mapStateToProps = (state) => ({ ...state });
class QuoteBox extends React.Component {
render() {
const colors = [
"red-complimentary-background",
"blue-complimentary-background",
"yellow-complimentary-background",
];
const quoteBoxClass = "QuoteBox " + colors[this.props.colorCount];
return (
<div className={quoteBoxClass} id="quote-box">
<div className="quote-box-top">
<TextAuthor />
</div>
<div className="quote-box-bottom">
<div className="quote-box-bottom-left">
<TweetQuote />
</div>
<div className="quote-box-bottom-right">
<NewQuote />
</div>
</div>
</div>
);
}
}
export default connect(mapStateToProps)(QuoteBox);

View File

@@ -0,0 +1,23 @@
.red {
color: #00ff80;
}
.blue {
color: #0000ff;
}
.yellow {
color: #ffff00;
}
.text {
margin-left: 10%;
margin-right: 10%;
font-weight: bold;
}
.author {
margin-left: 10%;
margin-right: 10%;
font-style: oblique;
}

View File

@@ -0,0 +1,42 @@
import React from "react";
import { connect } from "react-redux";
import "./TextAuthor.css";
import { quoteFetchActionCreator } from "./actions/quoteFetchActionCreator";
const mapStateToProps = state => ({ ...state });
const mapDispatchToProps = dispatch => ({
quoteFetch: () => dispatch(quoteFetchActionCreator())
});
class TextAuthor extends React.Component {
componentDidMount() {
if (this.props.newQuote.quote === "") {
this.props.quoteFetch();
}
}
render() {
let quote = "";
let author = "";
if (typeof this.props.newQuote.quote === "object") {
quote = this.props.newQuote.quote.quote;
author = this.props.newQuote.quote.author;
}
const colors = ["red", "blue", "yellow"];
const textClass = "text " + colors[this.props.colorCount];
const authorClass = "author " + colors[this.props.colorCount];
return (
<div style={{ fontSize: ".7em" }}>
<p className={textClass} id="text">
{quote}
</p>
<p className={authorClass} id="author">
--{author}
</p>
</div>
);
}
}
export default connect(mapStateToProps, mapDispatchToProps)(TextAuthor);

View File

@@ -0,0 +1,39 @@
.TweetQuote {
height: 60%;
width: 45%;
border: none;
border-radius: 5px;
font-size: 2.2em;
font-weight: bold;
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
}
.tweetQuoteContainer {
height: 100%;
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.TweetQuote:focus {
outline: 0;
}
.red-background {
background-color: #ff0000;
color: #000000;
}
.blue-background {
background-color: #0000ff;
color: #ff8000;
}
.yellow-background {
background-color: #ffff00;
color: #0080ff;
}

View File

@@ -0,0 +1,43 @@
import React from "react";
import { connect } from "react-redux";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTwitter } from "@fortawesome/free-brands-svg-icons";
import "./TweetQuote.css";
const mapStateToProps = state => ({ ...state });
class TweetQuote extends React.Component {
render() {
const colors = ["red-background", "blue-background", "yellow-background"];
const tweetQuoteClass = "TweetQuote " + colors[this.props.colorCount];
let href = "https://twitter.com/intent/tweet?text=";
if (typeof this.props.newQuote.quote === "object") {
href += encodeURIComponent(
this.props.newQuote.quote.quote +
"\n--" +
this.props.newQuote.quote.author
);
}
return (
<div className="tweetQuoteContainer">
<div className={tweetQuoteClass}>
<a
href={href}
target="_blank"
rel="noopener noreferrer"
id="tweet-quote"
>
<FontAwesomeIcon
icon={faTwitter}
className={colors[this.props.colorCount]}
/>
</a>
</div>
</div>
);
}
}
export default connect(mapStateToProps)(TweetQuote);

View File

@@ -0,0 +1,8 @@
export const INCCOLORCOUNT = "INCCOLORCOUNT";
export const colorCounterAction = (count) => {
return {
type: INCCOLORCOUNT,
count: count,
};
};

View File

@@ -0,0 +1,32 @@
export const RECEIVED_QUOTE = "RECEIVED_QUOTE";
export const REQUESTING_QUOTE = "REQUESTING_QUOTE";
export const receivedQuoteAction = quote => {
return {
type: RECEIVED_QUOTE,
newQuote: quote
};
};
export const requestingQuoteAction = () => {
return {
type: REQUESTING_QUOTE
};
};
export const quoteFetchActionCreator = () => {
return function (dispatch) {
dispatch(requestingQuoteAction());
fetch(
"https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json"
)
.then(response => response.json())
.then(quotes => {
dispatch(
receivedQuoteAction(
quotes.quotes[Math.floor(Math.random() * quotes.quotes.length)]
)
);
});
};
};

View File

@@ -1,13 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
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',
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

View File

@@ -1,14 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import store from "./store";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</React.StrictMode>,
document.getElementById('root')
</Provider>,
document.getElementById("root")
);
// If you want your app to work offline and load faster, you can change

View File

@@ -1,7 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
<g fill="#61DAFB">
<path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/>
<circle cx="420.9" cy="296.5" r="45.7"/>
<path d="M520.5 78.1z"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 2.6 KiB

View File

@@ -0,0 +1,13 @@
import { REQUESTING_QUOTE } from "../actions/quoteFetchActionCreator";
import { RECEIVED_QUOTE } from "../actions/quoteFetchActionCreator";
export default (state = { fetching: false, quote: "" }, action) => {
switch (action.type) {
case REQUESTING_QUOTE:
return { fetching: true, quote: "" };
case RECEIVED_QUOTE:
return { fetching: false, quote: action.newQuote };
default:
return state;
}
};

View File

@@ -0,0 +1,10 @@
import { INCCOLORCOUNT } from "../actions/colorCounterAction";
export default (state = 1, action) => {
switch (action.type) {
case INCCOLORCOUNT:
return action.count;
default:
return state;
}
};

View File

@@ -0,0 +1,8 @@
import { combineReducers } from "redux";
import colorCounterReducer from "./colorCounterReducer";
import asyncQuoteReducer from "./asyncQuoteReducer";
export default combineReducers({
colorCount: colorCounterReducer,
newQuote: asyncQuoteReducer,
});

View File

@@ -11,9 +11,9 @@
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
window.location.hostname === "localhost" ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
window.location.hostname === "[::1]" ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
@@ -21,7 +21,7 @@ const isLocalhost = Boolean(
);
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
@@ -31,7 +31,7 @@ export function register(config) {
return;
}
window.addEventListener('load', () => {
window.addEventListener("load", () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
@@ -42,8 +42,8 @@ export function register(config) {
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
"This web app is being served cache-first by a service " +
"worker. To learn more, visit https://bit.ly/CRA-PWA"
);
});
} else {
@@ -57,21 +57,21 @@ export function register(config) {
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
.then((registration) => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (installingWorker.state === "installed") {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
"New content is available and will be used when all " +
"tabs for this page are closed. See https://bit.ly/CRA-PWA."
);
// Execute callback
@@ -82,7 +82,7 @@ function registerValidSW(swUrl, config) {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
console.log("Content is cached for offline use.");
// Execute callback
if (config && config.onSuccess) {
@@ -93,25 +93,25 @@ function registerValidSW(swUrl, config) {
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
.catch((error) => {
console.error("Error during service worker registration:", error);
});
}
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
headers: { "Service-Worker": "script" },
})
.then(response => {
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
const contentType = response.headers.get("content-type");
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
(contentType != null && contentType.indexOf("javascript") === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
});
@@ -123,18 +123,18 @@ function checkValidServiceWorker(swUrl, config) {
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
"No internet connection found. App is running in offline mode."
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.ready
.then(registration => {
.then((registration) => {
registration.unregister();
})
.catch(error => {
.catch((error) => {
console.error(error.message);
});
}

View File

@@ -2,4 +2,4 @@
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';
import "@testing-library/jest-dom/extend-expect";

View File

@@ -0,0 +1,7 @@
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers/rootReducer";
const store = createStore(rootReducer, applyMiddleware(thunk));
export default store;