forked from housseindjirdeh/angular2-hn
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: migrate ItemDetails and Comment components from Angular to React + TypeScript #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
4
commits into
master
Choose a base branch
from
devin/1778794643-angular-to-react-migration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b4e835b
feat: migrate ItemDetails and Comment components from Angular to Reac…
devin-ai-integration[bot] 8f0b40f
fix: add error handling for poll fetch promises
devin-ai-integration[bot] 560ad21
fix: scope bare element selectors and guard poll division by zero
devin-ai-integration[bot] f66557d
fix: reset item and errorMessage state when id changes
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| @import "../shared/scss/media"; | ||
| @import "../shared/scss/theme_variables"; | ||
|
|
||
| .mainContent { | ||
| position: relative; | ||
| width: 100%; | ||
| min-height: 100vh; | ||
| -webkit-transition: opacity .2s ease; | ||
| transition: opacity .2s ease; | ||
| box-sizing: border-box; | ||
| padding: 8px 0; | ||
| z-index: 0; | ||
| } | ||
|
|
||
| .item { | ||
| box-sizing: border-box; | ||
| padding: 10px 40px 0 40px; | ||
| z-index: 0; | ||
| } | ||
|
|
||
| @media #{$tablet-only} { | ||
| .item { | ||
| padding: 10px 20px 0 40px; | ||
| } | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .item { | ||
| box-sizing: border-box; | ||
| padding: 110px 15px 0 15px; | ||
| } | ||
| } | ||
|
|
||
| .headMargin { | ||
| margin-bottom: 15px; | ||
| } | ||
|
|
||
| .item p { | ||
| margin: 2px 0; | ||
| } | ||
|
|
||
| .subject { | ||
| word-wrap: break-word; | ||
| margin-top: 20px; | ||
| } | ||
|
|
||
| .item a { | ||
| cursor: pointer; | ||
| text-decoration: none; | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .laptop { | ||
| display: none; | ||
| } | ||
| } | ||
|
|
||
| @media #{$laptop-only} { | ||
| .mobile { | ||
| display: none; | ||
| } | ||
| } | ||
|
|
||
| .title { | ||
| font-size: 16px; | ||
| font-family: Verdana, Geneva, sans-serif; | ||
| } | ||
|
|
||
| .titleBlock { | ||
| text-align: center; | ||
| text-overflow: ellipsis; | ||
| white-space: nowrap; | ||
| overflow: hidden; | ||
| margin: 0 75px; | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .title { | ||
| font-size: 15px; | ||
| } | ||
| .backButton { | ||
| position: absolute; | ||
| top: 52%; | ||
| width: 0.6rem; | ||
| height: 0.6rem; | ||
| background: transparent; | ||
| box-shadow: 0 0 0 lightgray; | ||
| transition: all 200ms ease; | ||
| left: 4%; | ||
| transform: translate3d(0, -50%, 0) rotate(-135deg); | ||
| } | ||
| } | ||
|
|
||
| .subtext { | ||
| font-size: 12px; | ||
| font-weight: bold; | ||
| letter-spacing: 0.5px; | ||
| } | ||
|
|
||
| .domain { | ||
| letter-spacing: 0.5px; | ||
| } | ||
|
|
||
| .subtext a { | ||
| &:hover { | ||
| text-decoration: underline; | ||
| } | ||
| } | ||
|
|
||
| .itemDetails { | ||
| padding: 10px; | ||
| } | ||
|
|
||
| .itemHeader { | ||
| padding-bottom: 10px; | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .itemHeader { | ||
| padding: 10px 0 10px 0; | ||
| position: fixed; | ||
| width: 100%; | ||
| left: 0; | ||
| top: 62px; | ||
| } | ||
| } | ||
|
|
||
| .pollResults { | ||
| margin-bottom: 1em; | ||
| } | ||
|
|
||
| .pollContent { | ||
| * { | ||
| padding-bottom: 0; | ||
| margin-bottom: -1em; | ||
| margin-top: 1em; | ||
| } | ||
| .pollBar { | ||
| height: 10px; | ||
| margin-bottom: 1em; | ||
| } | ||
| } | ||
|
|
||
| .commentList { | ||
| list-style-type: none; | ||
| padding: 10px 0; | ||
| } | ||
|
|
||
| .commentList li { | ||
| display: list-item; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { useParams, useNavigate, Link } from 'react-router-dom'; | ||
| import { useSettings } from '../shared/hooks/useSettings'; | ||
| import { Story } from '../shared/models/story'; | ||
| import { Comment as CommentComponent } from './comment/Comment'; | ||
| import { Loader } from '../shared/components/Loader'; | ||
| import { ErrorMessage } from '../shared/components/ErrorMessage'; | ||
| import { formatCommentCount } from '../shared/utils/formatCommentCount'; | ||
| import styles from './ItemDetails.module.scss'; | ||
|
|
||
| export function ItemDetails() { | ||
| const { id } = useParams<{ id: string }>(); | ||
| const navigate = useNavigate(); | ||
| const { settings } = useSettings(); | ||
| const [item, setItem] = useState<Story | null>(null); | ||
| const [errorMessage, setErrorMessage] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| const controller = new AbortController(); | ||
| setItem(null); | ||
| setErrorMessage(''); | ||
|
|
||
| if (id) { | ||
| fetch(`https://node-hnapi.herokuapp.com/item/${id}`, { | ||
| signal: controller.signal, | ||
| }) | ||
| .then((res) => res.json()) | ||
| .then((story: Story) => { | ||
| if (story.type === 'poll' && story.poll) { | ||
| const numberOfPollOptions = story.poll.length; | ||
| let pollVotesCount = 0; | ||
| const pollPromises = Array.from( | ||
| { length: numberOfPollOptions }, | ||
| (_, i) => | ||
| fetch( | ||
| `https://node-hnapi.herokuapp.com/item/${story.id + i + 1}`, | ||
| { signal: controller.signal } | ||
| ).then((res) => res.json()) | ||
| ); | ||
| Promise.all(pollPromises).then((pollResults) => { | ||
| pollResults.forEach((result, i) => { | ||
| story.poll[i] = result; | ||
| pollVotesCount += result.points; | ||
| }); | ||
| story.poll_votes_count = pollVotesCount; | ||
| setItem({ ...story }); | ||
| }).catch((err) => { | ||
| if (err.name !== 'AbortError') { | ||
| setErrorMessage('Could not load item comments.'); | ||
| } | ||
| }); | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
| } else { | ||
| setItem(story); | ||
| } | ||
| }) | ||
| .catch((err) => { | ||
| if (err.name !== 'AbortError') { | ||
| setErrorMessage('Could not load item comments.'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| window.scrollTo(0, 0); | ||
|
|
||
| return () => { | ||
| controller.abort(); | ||
| }; | ||
| }, [id]); | ||
|
|
||
| const hasUrl = item?.url?.startsWith('http') ?? false; | ||
| const goBack = () => navigate(-1); | ||
|
|
||
| const linkTarget = settings.openLinkInNewTab ? '_blank' : undefined; | ||
| const linkRel = settings.openLinkInNewTab ? 'noopener' : undefined; | ||
|
|
||
| return ( | ||
| <div className={styles.mainContent}> | ||
| {!item && !errorMessage && <Loader />} | ||
| {!item && errorMessage !== '' && <ErrorMessage message={errorMessage} />} | ||
|
|
||
| {item && ( | ||
| <div className={styles.item}> | ||
| {/* Mobile header */} | ||
| <div className={`${styles.mobile} ${styles.itemHeader}`}> | ||
| <p className={styles.titleBlock}> | ||
| <span className={styles.backButton} onClick={goBack} /> | ||
| {hasUrl ? ( | ||
| <a | ||
| className={styles.title} | ||
| href={item.url} | ||
| target={linkTarget} | ||
| rel={linkRel} | ||
| > | ||
| {item.title} | ||
| </a> | ||
| ) : ( | ||
| <Link className={styles.title} to={`/item/${item.id}`}> | ||
| {item.title} | ||
| </Link> | ||
| )} | ||
| </p> | ||
| </div> | ||
|
|
||
| {/* Laptop header */} | ||
| <div | ||
| className={`${styles.laptop}${ | ||
| item.comments_count > 0 || item.type === 'job' | ||
| ? ` ${styles.itemHeader}` | ||
| : '' | ||
| }${item.content ? ` ${styles.headMargin}` : ''}`} | ||
| > | ||
| {hasUrl ? ( | ||
| <p> | ||
| <a | ||
| className={styles.title} | ||
| href={item.url} | ||
| target={linkTarget} | ||
| rel={linkRel} | ||
| > | ||
| {item.title} | ||
| </a> | ||
| {item.domain && ( | ||
| <span className={styles.domain}> ({item.domain})</span> | ||
| )} | ||
| </p> | ||
| ) : ( | ||
| <p> | ||
| <Link className={styles.title} to={`/item/${item.id}`}> | ||
| {item.title} | ||
| </Link> | ||
| </p> | ||
| )} | ||
| <div className={styles.subtext}> | ||
| {item.type !== 'job' && ( | ||
| <span> | ||
| {item.points} points by{' '} | ||
| <Link to={`/user/${item.user}`}>{item.user}</Link> | ||
| </span> | ||
| )} | ||
| <span | ||
| className={item.type !== 'job' ? styles.itemDetails : undefined} | ||
| > | ||
| {item.time_ago} | ||
| {item.type !== 'job' && ( | ||
| <span> | ||
| {' '} | ||
| |{' '} | ||
| <Link to={`/item/${item.id}`}> | ||
| {formatCommentCount(item.comments_count)} | ||
| </Link> | ||
| </span> | ||
| )} | ||
| </span> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Poll results */} | ||
| {item.type === 'poll' && item.poll && ( | ||
| <div className={styles.pollResults}> | ||
| {item.poll.map((pollResult, index) => ( | ||
| <div key={index} className={styles.pollContent}> | ||
| <div | ||
| dangerouslySetInnerHTML={{ __html: pollResult.content }} | ||
| /> | ||
| <div className={styles.subtext}> | ||
| {pollResult.points} points | ||
| </div> | ||
| <div | ||
| className={styles.pollBar} | ||
| style={{ | ||
| width: `${ | ||
| item.poll_votes_count > 0 ? (pollResult.points / item.poll_votes_count) * 100 : 0 | ||
| }%`, | ||
| }} | ||
| /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| )} | ||
|
|
||
| {/* Content */} | ||
| {item.content && ( | ||
| <p | ||
| className={styles.subject} | ||
| dangerouslySetInnerHTML={{ __html: item.content }} | ||
| /> | ||
| )} | ||
|
|
||
| {/* Comments */} | ||
| <ul className={styles.commentList}> | ||
| {item.comments?.map((comment) => ( | ||
| <li key={comment.id}> | ||
| <CommentComponent comment={comment} /> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.