Last active
June 26, 2021 21:38
-
-
Save primaryobjects/962129bd695ec0ea7076eabe0ef6e034 to your computer and use it in GitHub Desktop.
React example in browser using useState, useEffect, ReactJs from a web page. https://jsbin.com/yudorurupi/edit?html,js,output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>Authors</title> | |
| <script src="https://code.jquery.com/jquery-3.1.0.js"></script> | |
| <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script> | |
| <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script> | |
| <script src='https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser-polyfill.min.js'></script> | |
| </head> | |
| <body> | |
| <div id='root'></div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // noprotect | |
| const Author = props => { | |
| return ( | |
| <li className="list-item" data-is-card-enabled="false"> | |
| <div className="list-item-media" style={{marginBottom: '6%', width: '100%'}}> | |
| <div className="list-item-media-inner" data-aspect-ratio="4:3" data-animation-role="image"> | |
| <img data-image-focal-point="NaN,NaN" data-src={props.url} data-image={props.url} data-image-dimensions="5000x3333" className="list-image" data-load="false" data-mode="cover" data-use-advanced-positioning="true" data-parent-ratio="1.3" data-image-resolution="300w" src={props.url} data-loaded="true" style={{width: '100%', height: '100%', objectFit: 'cover'}}></img> | |
| </div> | |
| </div> | |
| <div className="list-item-content"> | |
| <div className="list-item-content__text-wrapper"> | |
| <h2 className="list-item-content__title" style={{maxWidth: '100%'}}> | |
| {props.name} | |
| </h2> | |
| <div className="list-item-content__description" style={{marginTop: '6%', maxWidth: '100%'}}> | |
| <p style={{whiteSpace: 'pre-wrap'}}> | |
| {props.date} | |
| </p> | |
| </div> | |
| </div> | |
| </div> | |
| </li> | |
| ); | |
| } | |
| const AuthorCollection = props => { | |
| const { useState, useEffect } = React; | |
| const [data, updateData] = useState(); | |
| useEffect(() => { | |
| const getData = async () => { | |
| const authors = await load(props.url); | |
| updateData(authors); | |
| }; | |
| getData(); | |
| }, []); | |
| const load = async url => { | |
| const authors = []; | |
| const res = await fetch(url); | |
| const data = await res.json(); | |
| console.log(data.feed.entry); | |
| data.feed.entry.forEach(item => { | |
| const author = { | |
| date: item['gsx$date']['$t'], | |
| name: item['gsx$name']['$t'], | |
| url: item['gsx$imageurl']['$t'], | |
| }; | |
| authors.push(author); | |
| }); | |
| return authors; | |
| }; | |
| return ( | |
| <div id="author-collection"> | |
| <h3>Author List</h3> | |
| <ul id="custom-list" className="user-items-list-item-container user-items-list-simple" style={{ gridGap: '100px 100px' }} data-controller="UserItemsListSimple" data-num-columns="3" data-content-order="media-first"> | |
| { data && data.map(author => { | |
| return ( | |
| <Author | |
| url={author.url} | |
| name={author.name} | |
| date={author.date} | |
| /> | |
| ); | |
| }) } | |
| </ul> | |
| </div> | |
| ); | |
| }; | |
| $(function() { | |
| ReactDOM.render(<AuthorCollection url='https://spreadsheets.google.com/feeds/list/1BK4XdUygRcUGAiQSne10aNjV_uLaLPT2u0NPFicCX6Q/od6/public/values?alt=json' />, document.getElementById('root')); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // noprotect | |
| // Generic version with swappable Author control and parser logic. | |
| // https://jsbin.com/cohavikeso/1/edit?js,output | |
| const Author = props => { | |
| const author = props.data; | |
| return ( | |
| <li className="list-item" data-is-card-enabled="false"> | |
| <div className="list-item-media" style={{marginBottom: '6%', width: '100%'}}> | |
| <div className="list-item-media-inner" data-aspect-ratio="4:3" data-animation-role="image"> | |
| <img data-image-focal-point="NaN,NaN" data-src={author.url} data-image={author.url} data-image-dimensions="5000x3333" className="list-image" data-load="false" data-mode="cover" data-use-advanced-positioning="true" data-parent-ratio="1.3" data-image-resolution="300w" src={author.url} data-loaded="true" style={{width: '100%', height: '100%', objectFit: 'cover'}}></img> | |
| </div> | |
| </div> | |
| <div className="list-item-content"> | |
| <div className="list-item-content__text-wrapper"> | |
| <h2 className="list-item-content__title" style={{maxWidth: '100%'}}> | |
| {author.name} | |
| </h2> | |
| <div className="list-item-content__description" style={{marginTop: '6%', maxWidth: '100%'}}> | |
| <p style={{whiteSpace: 'pre-wrap'}}> | |
| {author.date} | |
| </p> | |
| </div> | |
| </div> | |
| </div> | |
| </li> | |
| ); | |
| } | |
| const AuthorCollection = props => { | |
| const { useState, useEffect } = React; | |
| const [data, updateData] = useState(); | |
| const Inner = props.manager.control; | |
| useEffect(() => { | |
| // Load data-source and process rows. | |
| const getData = async () => updateData(await load(props.manager.url)); | |
| getData(); | |
| }, []); | |
| const load = async url => { | |
| const rows = []; | |
| if (url) { | |
| try { | |
| // Download the data-source. | |
| const res = await fetch(url); | |
| // Convert to JSON. | |
| const data = await res.json(); | |
| //console.log(data.feed.entry); | |
| // Parse each row into an object. | |
| data.feed.entry.forEach(row => rows.push(props.manager.parse(row))); | |
| } | |
| catch (ex) { | |
| console.error(`Error downloading and parsing url ${url}\n${ex}`); | |
| } | |
| } | |
| return rows; | |
| }; | |
| return ( | |
| <div id="author-collection"> | |
| <h3>Author List</h3> | |
| <ul id="custom-list" className="user-items-list-item-container user-items-list-simple" style={{ gridGap: '100px 100px' }} data-controller="UserItemsListSimple" data-num-columns="3" data-content-order="media-first"> | |
| { data && data.length ? data.map(item => { | |
| return ( | |
| <Inner data={item} /> | |
| ); | |
| }) : <div>No authors found.</div> } | |
| </ul> | |
| </div> | |
| ); | |
| }; | |
| const authorManager = { | |
| url: 'https://spreadsheets.google.com/feeds/list/1BK4XdUygRcUGAiQSne10aNjV_uLaLPT2u0NPFicCX6Q/od6/public/values?alt=json', | |
| control: Author, | |
| parse: row => { | |
| return { | |
| date: row['gsx$date']['$t'], | |
| name: row['gsx$name']['$t'], | |
| url: row['gsx$imageurl']['$t'], | |
| }; | |
| } | |
| }; | |
| $(function() { | |
| // Render a collection of authors using a specific spreadsheet parser and inner renderer. | |
| ReactDOM.render(<AuthorCollection manager={authorManager} />, document.getElementById('root')); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment