Skip to content
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

Profile image upload #94

Merged
merged 20 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-progress": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"class-variance-authority": "^0.7.0",
Expand Down
2 changes: 2 additions & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ Roboto
subheadline
linecap
linejoin
mingcute
shadcn
clsx
2 changes: 1 addition & 1 deletion src/app/auth/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function Auth() {
<section className="h-screen w-screen bg-gradient-blue-white">
<Content className="flex items-center justify-center dark:bg-none dark:bg-base-gray-8">
<div className="p-7 sm:p-10 flex gap-7 md:gap-[3.75rem] rounded-3xl bg-base-white dark:bg-base-black">
<ImageCard className="-my-[4.375rem] flex-shrink-0 hidden sm:flex items-center justify-center">
<ImageCard className="-my-[4.375rem] flex-shrink-0 hidden sm:flex items-center justify-center pt-24 pr-8 pb-36 pl-5">
dEdmishka marked this conversation as resolved.
Show resolved Hide resolved
<Icons.golub />
</ImageCard>
<div>
Expand Down
23 changes: 23 additions & 0 deletions src/app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import FileInput from '@/components/pages/profile/file-input'
dEdmishka marked this conversation as resolved.
Show resolved Hide resolved
import FormProfile from '@/components/pages/profile/form-profile'
import Content from '@/components/ui/content'
import { ThemeToggler } from '@/components/ui/theme-toggler'

export default function Profile() {
return (
<section className="h-screen w-screen bg-gradient-blue-white">
<Content className="flex items-center justify-center dark:bg-none dark:bg-base-gray-8">
<div className="p-7 sm:p-10 flex gap-7 md:gap-[3.75rem] rounded-3xl bg-base-white dark:bg-base-black">
<FileInput />
dEdmishka marked this conversation as resolved.
Show resolved Hide resolved
<div>
<div className="flex justify-between pb-2 mt-8">
<h1 className="title-lg">Profile</h1>
<ThemeToggler />
</div>
<FormProfile className="mt-5" />
</div>
</div>
</Content>
</section>
)
}
2 changes: 1 addition & 1 deletion src/components/pages/auth/image-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ImageCard: FC<ImageCardProps> = ({ children, className }) => {
return (
<div
className={classnames(
'pt-24 pr-8 pb-36 pl-5 flex items-center justify-center rounded-3xl bg-gradient-purple-blue',
'flex items-center justify-center rounded-3xl bg-gradient-purple-blue',
className
)}
>
Expand Down
114 changes: 114 additions & 0 deletions src/components/pages/profile/file-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
'use client'

import ImageCard from '@/components/pages/auth/image-card'
import { Icons } from '@/components/ui/icons'
import { Progress } from '@/components/ui/progress'
import { cn } from '@/lib/utils'
import { FC, FormEvent, useRef, useState } from 'react'

type FileInputProps = {
className?: string
}

const FileInput: FC<FileInputProps> = ({ className }) => {
const fileInputRef = useRef<HTMLInputElement>(null)

const [_, setImage] = useState<File | undefined>()

dEdmishka marked this conversation as resolved.
Show resolved Hide resolved
const [preview, setPreview] = useState<string | undefined>()

const [progress, setProgress] = useState<number>(0)

const clearSelectFile = () => {
setPreview(undefined)
}

const handleSelectFile = () => {
fileInputRef.current?.click()
}

const handleImageLoad = (file: File) => {
setImage(file)

const fileReader = new FileReader()

fileReader.readAsDataURL(file)

fileReader.onloadstart = () => {
setProgress(0)
}

fileReader.onprogress = (progress) => {
setProgress((progress.loaded / progress.total) * 100)
}

fileReader.onload = () => {
setPreview(fileReader.result as string)
setTimeout(() => setProgress(0), 500)
dEdmishka marked this conversation as resolved.
Show resolved Hide resolved
}
}

const handleImageChange = (event: FormEvent<HTMLInputElement>) => {
const target = event.target as HTMLElement & {
files: FileList
}

if (target.files.length > 0) {
handleImageLoad(target.files[0])
}
}

return (
<ImageCard
className={cn(
'-my-[4.375rem] flex-shrink-0 hidden sm:flex items-center justify-center py-32 px-[3.25rem]',
className
)}
>
dEdmishka marked this conversation as resolved.
Show resolved Hide resolved
<div className="relative">
{preview ? (
<div>
<img className="w-[144px] h-[144px] rounded-3xl" src={preview} />

Check warning on line 71 in src/components/pages/profile/file-input.tsx

View workflow job for this annotation

GitHub Actions / test

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element

Check warning on line 71 in src/components/pages/profile/file-input.tsx

View workflow job for this annotation

GitHub Actions / test

img elements must have an alt prop, either with meaningful text, or an empty string for decorative images
<Icons.cross
className="absolute z-10 top-4 right-4 fill-[#F0F2F8] dark:fill-[#1C1D1F]"
onClick={clearSelectFile}
/>
</div>
) : (
<div>
<Icons.photo_profile className="fill-[#F0F2F8] dark:fill-[#1C1D1F]" />
</div>
)}
<div
className="absolute -bottom-4 right-1/2 translate-x-1/2"
onClick={handleSelectFile}
>
<Icons.mingcute_camera className="fill-white dark:fill-[#7F92DC]" />
</div>

<div
className={cn(
'absolute z-[11] top-1/2 left-1/2 w-full',
!progress && 'w-0'
)}
>
<Progress
className="w-5/6 relative mx-auto transition-[width] duration-500 ease-in-out -left-1/2"
value={progress}
/>
</div>

<input
className="absolute block h-full w-full top-0 opacity-0 rounded-3xl"
name="image"
onChange={handleImageChange}
ref={fileInputRef}
type="file"
/>
<span className="sr-only">File input</span>
</div>
</ImageCard>
)
}

export default FileInput
72 changes: 72 additions & 0 deletions src/components/pages/profile/form-profile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use client'

import { Button } from '@/components/ui/button'
import {
Form,
FormControl,
FormField,
FormItem,
FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { PROFILE_NAME_LENGTH } from '@/constants'
import { classnames } from '@/utils'
import { zodResolver } from '@hookform/resolvers/zod'
import { FC } from 'react'
import { useForm } from 'react-hook-form'
import * as z from 'zod'

type FormProfileProps = { className?: string }

const formSchema = z.object({
profileName: z
.string()
.min(PROFILE_NAME_LENGTH, 'The field must not be empty'),
})

const ProfileForm: FC<FormProfileProps> = ({ className }) => {
const form = useForm<z.infer<typeof formSchema>>({
defaultValues: {
profileName: '',
},
resolver: zodResolver(formSchema),
})

function onSubmit(values: z.infer<typeof formSchema>) {
alert(JSON.stringify(values))
console.log(values)
}

return (
<Form {...form}>
<form
className={classnames('space-y-8 pr-[9.75rem]', className)}
onSubmit={form.handleSubmit(onSubmit)}
>
<FormField
control={form.control}
name="profileName"
render={({ field }) => (
<FormItem>
<FormControl>
<Input
className="sm:min-w-[343px]"
id="profile_name"
placeholder="Profile Name"
type="text"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button className="!mt-8 w-full" type="submit">
Submit
</Button>
</form>
</Form>
)
}

export default ProfileForm
Loading
Loading