-
Notifications
You must be signed in to change notification settings - Fork 1
Add profile page #97
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
TheArchons
wants to merge
8
commits into
main
Choose a base branch
from
web-interface-profile2
base: main
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
Add profile page #97
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce3a1b1
Add profile page
notjackl3 5d5275a
Replace edit button with a cancel button
TheArchons b345561
Refactor profile buttons to use the Button component
TheArchons 6198724
Add error handling to profile fields and disable save button when err…
TheArchons 78fe562
Add avatar upload functionality to profile page
TheArchons 25a456b
show dropdown above page content
TheArchons 744fe43
fix getUserErrors typing
TheArchons e6a5e83
use a fontSizeStyles object instead of directly injecting javascript
TheArchons 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,26 +1,43 @@ | ||
| import type { ReactNode } from 'react' | ||
|
|
||
| const variantStyles = { | ||
| success: 'bg-green-200 text-green-800 hover:bg-green-300', | ||
| warning: 'bg-yellow-200 text-yellow-800 hover:bg-yellow-300', | ||
| danger: 'bg-red-200 text-red-800 hover:bg-red-300', | ||
| normal: 'bg-blue-200 text-blue-800 hover:bg-blue-300', | ||
| disabled: 'bg-gray-200 text-gray-800', | ||
| } | ||
|
|
||
| const fontSizeStyles = { | ||
| xs: 'text-xs', | ||
| sm: 'text-sm', | ||
| base: 'text-base', | ||
| lg: 'text-lg', | ||
| xl: 'text-xl', | ||
| }; | ||
|
|
||
|
|
||
| type ButtonVariant = keyof typeof variantStyles | ||
| type ButtonFontSize = keyof typeof fontSizeStyles | ||
|
|
||
| export function Button({ | ||
| children, | ||
| onClick, | ||
| text, | ||
| variant, | ||
| fontSize, | ||
| }: { | ||
| children: ReactNode | ||
| onClick: () => void | ||
| text: string | ||
| variant: ButtonVariant | ||
| fontSize: ButtonFontSize | ||
| }) { | ||
| return ( | ||
| <button | ||
| onClick={onClick} | ||
| className={`px-3 py-1 text-xs font-semibold rounded ${variantStyles[variant]}`} | ||
| className={`flex items-center gap-1.5 px-3 py-1 ${fontSizeStyles[fontSize]} rounded ${variantStyles[variant]}`} | ||
| disabled={variant === 'disabled'} | ||
| > | ||
| {text} | ||
| {children} | ||
| </button> | ||
| ) | ||
| } | ||
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
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 |
|---|---|---|
| @@ -1,9 +1,220 @@ | ||
| import { createFileRoute } from '@tanstack/react-router' | ||
| import { SquarePen } from 'lucide-react' | ||
| import { getUser } from '#/util.ts' | ||
| import { useImmer } from 'use-immer' | ||
| import { Button } from '#/components/Buttons.tsx' | ||
| import { useRef } from 'react' | ||
| import type { ChangeEvent } from 'react' | ||
|
|
||
| export const Route = createFileRoute('/profile')({ | ||
| component: RouteComponent, | ||
| component: ProfilePage, | ||
| loader: getUser, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| return <div>Hello "/profile"!</div> | ||
| function ProfileField({ | ||
| label, | ||
| value, | ||
| type = 'text', | ||
| onChange, | ||
| error, | ||
| }: { | ||
| label: string | ||
| value: string | ||
| type?: string | ||
| onChange: (val: string) => void | ||
| error?: string | ||
| }) { | ||
| return ( | ||
| <div> | ||
| <label className="block text-base font-medium mb-1">{label}</label> | ||
| <input | ||
| type={type} | ||
| value={value} | ||
| onChange={(e) => onChange(e.target.value)} | ||
| className={`w-full border ${error ? 'border-red-600' : 'border-gray-300'} rounded-lg px-4 py-2 text-sm disabled:bg-white disabled:text-gray-700`} | ||
| /> | ||
| <p className="text-red-600 min-h-6">{error}</p> | ||
TheArchons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function ProfilePage() { | ||
| const loaderData = Route.useLoaderData() | ||
| const [user, setUser] = useImmer({ | ||
| ...loaderData, | ||
| password: '', | ||
| confirmPassword: '', | ||
| }) | ||
| const avatarUploadRef = useRef<HTMLInputElement>(null) | ||
|
|
||
| function handleAvatarUpload(event: ChangeEvent<HTMLInputElement>) { | ||
| const file = event.target.files?.[0] | ||
|
|
||
| if (!file) return | ||
|
|
||
| // TODO: call API to upload avatar | ||
| console.log(`Uploaded image ${file.name}`) | ||
| } | ||
|
|
||
| function handleSave() { | ||
| // TODO: call API to update profile | ||
| console.log('Save profile:', { | ||
| username: user.username, | ||
| role: user.role, | ||
| email: user.email, | ||
| password: user.password, | ||
TheArchons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| } | ||
|
|
||
| function handleCancel() { | ||
| const confirmCancel = confirm('Are you sure you want to cancel?') | ||
|
|
||
| if (confirmCancel) { | ||
| setUser({ | ||
| ...loaderData, | ||
| password: '', | ||
| confirmPassword: '', | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| function getUserErrors(field: keyof typeof user): string | undefined { | ||
| switch (field) { | ||
| case 'username': | ||
| case 'role': | ||
| case 'email': | ||
| if (user[field] === '') { | ||
| return `Field cannot be empty.` | ||
| } | ||
| break | ||
| case 'password': | ||
| if (user['password'].length !== 0) { | ||
| if (user['password'].length < 8) { | ||
| return 'Password must be at least 8 characters long' | ||
| } | ||
| } | ||
| break | ||
| case 'confirmPassword': | ||
| if ( | ||
| user['password'].length !== 0 && | ||
| user['confirmPassword'] !== user['password'] | ||
| ) { | ||
| return 'Passwords do not match.' | ||
| } | ||
| break | ||
| } | ||
| } | ||
|
|
||
| function hasError(): boolean { | ||
| for (const field in user) { | ||
| if (getUserErrors(field as keyof typeof user)) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| return ( | ||
| <div className="w-fit mx-auto py-8 px-8"> | ||
| <div className="flex gap-16 items-start"> | ||
| {/* Left: form fields */} | ||
| <div className="flex-1 min-w-80 flex flex-col"> | ||
| <ProfileField | ||
| label="Username" | ||
| value={user.username} | ||
| onChange={(username) => | ||
| setUser((draft) => { | ||
| draft.username = username | ||
| }) | ||
| } | ||
| error={getUserErrors('username')} | ||
| /> | ||
| <ProfileField | ||
| label="Role" | ||
| value={user.role} | ||
| onChange={(role) => | ||
| setUser((draft) => { | ||
| draft.role = role | ||
| }) | ||
| } | ||
| error={getUserErrors('role')} | ||
| /> | ||
| <ProfileField | ||
| label="Email" | ||
| value={user.email} | ||
| type="email" | ||
| onChange={(email) => | ||
| setUser((draft) => { | ||
| draft.email = email | ||
| }) | ||
| } | ||
| error={getUserErrors('email')} | ||
| /> | ||
| <ProfileField | ||
| label="Password" | ||
| value={user.password} | ||
| type="password" | ||
| onChange={(password) => | ||
| setUser((draft) => { | ||
| draft.password = password | ||
| }) | ||
| } | ||
| error={getUserErrors('password')} | ||
| /> | ||
| <ProfileField | ||
| label="Confirm Password" | ||
| value={user.confirmPassword} | ||
| type="password" | ||
| onChange={(confirmPassword) => | ||
| setUser((draft) => { | ||
| draft.confirmPassword = confirmPassword | ||
| }) | ||
| } | ||
| error={getUserErrors('confirmPassword')} | ||
| /> | ||
|
|
||
| <div className="flex gap-3 mt-2"> | ||
| <Button | ||
| onClick={handleSave} | ||
| variant={hasError() ? 'disabled' : 'normal'} | ||
| fontSize="base" | ||
| > | ||
| Save | ||
| </Button> | ||
| <Button onClick={handleCancel} variant="danger" fontSize="base"> | ||
| Cancel | ||
| </Button> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Right: profile picture */} | ||
| <div className="flex flex-col items-center gap-3 pt-6"> | ||
| <div className="relative"> | ||
| <img | ||
| src={user.profilePicture} | ||
| alt="Profile Picture" | ||
| className="w-48 h-48 rounded-full object-cover border-2 border-gray-200" | ||
| /> | ||
| <div className="absolute bottom-3 left-3"> | ||
| <Button | ||
| onClick={() => avatarUploadRef.current?.click()} | ||
| variant="normal" | ||
| fontSize="base" | ||
| > | ||
| Edit <SquarePen size={16} /> | ||
| </Button> | ||
| <input | ||
| type="file" | ||
| className="hidden" | ||
| accept="image/*" | ||
| ref={avatarUploadRef} | ||
| onChange={handleAvatarUpload} | ||
| /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| type User = { | ||
| export type User = { | ||
| username: string | ||
| role: string | ||
| profilePicture: string | ||
| email: string | ||
| } | ||
|
|
||
| export function getUser(): User { | ||
|
|
@@ -10,6 +12,8 @@ export function getUser(): User { | |
| const user = { | ||
| username: 'TheArchons', | ||
| profilePicture: '/sample-avatar.png', // real avatars should probably be stored in a bucket | ||
| role: 'Software Developer', | ||
| email: '[email protected]', | ||
| } | ||
|
|
||
| return user | ||
|
|
||
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.