forked from housseindjirdeh/angular2-hn
-
Notifications
You must be signed in to change notification settings - Fork 15
User profile (migration 6/8) #694
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
charityquinn-cognition
wants to merge
3
commits into
devin/react-migration-5-item-details
from
devin/react-migration-6-user
+162
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,89 @@ | ||
| @import "../shared/scss/media"; | ||
| @import "../shared/scss/theme_variables"; | ||
|
|
||
| .other-details pre { | ||
| white-space: pre-wrap; | ||
| } | ||
|
|
||
| .profile { | ||
| padding: 30px; | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .profile { | ||
| padding: 110px 15px 0 15px; | ||
| } | ||
| .title-block { | ||
| font-size: 15px; | ||
| text-align: center; | ||
| text-overflow: ellipsis; | ||
| white-space: nowrap; | ||
| overflow: hidden; | ||
| margin: 0 75px; | ||
| } | ||
| .back-button { | ||
| 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); | ||
| } | ||
| .item-header { | ||
| padding-bottom: 10px; | ||
| background-color: #fff; | ||
| padding: 10px 0 10px 0; | ||
| position: fixed; | ||
| width: 100%; | ||
| left: 0; | ||
| top: 62px; | ||
| height: 20px; | ||
| } | ||
| } | ||
|
|
||
| @media #{$laptop-only} { | ||
| .mobile { | ||
| display: none; | ||
| } | ||
| } | ||
|
|
||
| .main-details { | ||
| .name { | ||
| font-weight: bold; | ||
| font-size: 32px; | ||
| letter-spacing: 2px; | ||
| } | ||
| .age { | ||
| font-weight: bold; | ||
| color: #696969; | ||
| padding-bottom: 0; | ||
| } | ||
| .right { | ||
| float: right; | ||
| font-weight: bold; | ||
| font-size: 32px; | ||
| letter-spacing: 2px; | ||
| } | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .main-details { | ||
| margin-top: 20px; | ||
| .name { | ||
| font-size: 18px; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @media #{$mobile-only} { | ||
| .main-details .right { | ||
| font-size: 18px; | ||
| } | ||
| } | ||
|
|
||
| .other-details { | ||
| word-wrap: break-word; | ||
| } |
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,71 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { useNavigate, useParams } from 'react-router-dom'; | ||
|
|
||
| import { fetchUser } from '../shared/api/hackernewsApi'; | ||
| import ErrorMessage from '../shared/components/error-message/ErrorMessage'; | ||
| import Loader from '../shared/components/loader/Loader'; | ||
| import type { User as UserModel } from '../shared/models'; | ||
| import { sanitizeHtml } from '../shared/utils/sanitizeHtml'; | ||
| import './User.scss'; | ||
|
|
||
| function User() { | ||
| const params = useParams<{ id?: string }>(); | ||
| const userID = params.id ?? ''; | ||
| const navigate = useNavigate(); | ||
| const [user, setUser] = useState<UserModel>(); | ||
| const [errorMessage, setErrorMessage] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| let ignore = false; | ||
| setErrorMessage(''); | ||
|
|
||
| fetchUser(userID) | ||
| .then((nextUser) => { | ||
| if (!ignore) { | ||
| setUser(nextUser); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| if (!ignore) { | ||
| setErrorMessage(`Could not load user ${userID}.`); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| ignore = true; | ||
| }; | ||
| }, [userID]); | ||
|
|
||
| const goBack = () => { | ||
| navigate(-1); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| {!user && !errorMessage && <Loader />} | ||
| {!user && errorMessage !== '' && <ErrorMessage message={errorMessage} />} | ||
| {user && ( | ||
| <div className="profile"> | ||
| <div className="mobile item-header"> | ||
| <p className="title-block"> | ||
| <span className="back-button" onClick={goBack} /> | ||
| Profile: {user.id} | ||
| </p> | ||
| </div> | ||
| <div className="main-details"> | ||
| <span className="name">{user.id}</span> | ||
| <span className="right">{user.karma} ★</span> | ||
| <p className="age">Created {user.created}</p> | ||
| </div> | ||
| {user.about && ( | ||
| <div className="other-details"> | ||
| <p dangerouslySetInnerHTML={{ __html: sanitizeHtml(user.about) }} /> | ||
| </div> | ||
| )} | ||
| </div> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default User; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Stale profile persists across navigation
On
userIDchange the effect resetserrorMessagebut never clearsuser, so navigating from a loaded profile to one that errors keeps the previous profile on screen. This mirrorsItemDetailsand the Angular original.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentional — as you note, it matches the Angular original (which also only assigned
useron success) and the siblingItemDetails. Changing it would be a behavior change rather than a port, so I'm leaving it consistent across the three views; worth a follow-up if the stale-view-on-error transition is considered a bug in its own right.