-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from italosilva01/feat-38
Feat 38: Desenvolver componente de Upload de imagem de perfil
- Loading branch information
Showing
14 changed files
with
10,659 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
{ | ||
"extends": [ | ||
"development" | ||
], | ||
"hints": { | ||
"disown-opener": "off" | ||
} | ||
} |
This file contains 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,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "chrome", | ||
"request": "launch", | ||
"name": "Launch Chrome against localhost", | ||
"url": "http://localhost:3000", | ||
"webRoot": "${workspaceFolder}" | ||
} | ||
] | ||
} |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
17 changes: 17 additions & 0 deletions
17
frontend/src/components/Buttons/ButtonWhiteBlack/index.tsx
This file contains 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,17 @@ | ||
import { ReactNode } from 'react' | ||
|
||
interface ButtonWhiteBlackProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { | ||
children: ReactNode | ||
classList?: string | ||
} | ||
|
||
export const ButtonWhiteBlack = ({ children, classList, ...rest }: ButtonWhiteBlackProps) => { | ||
return ( | ||
<button | ||
className={`text-2xl text-center z-20 flex flex-col justify-center items-center border border-black p-4 w-3 h-3 rounded-full bg-white hover:bg-gray-950 hover:text-white transition duration-300 ease-in-out ${classList}`} | ||
{...rest} | ||
> | ||
{children} | ||
</button> | ||
) | ||
} |
This file contains 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,15 @@ | ||
import React from 'react'; | ||
import { useForm, FormProvider, UseFormReturn } from 'react-hook-form'; | ||
|
||
interface MockFormProviderProps { | ||
children: React.ReactNode; | ||
defaultValues?: Record<string, any>; | ||
methods?: Partial<UseFormReturn>; | ||
} | ||
|
||
export const MockFormProvider: React.FC<MockFormProviderProps> = ({ children, defaultValues = {}, methods = {} }) => { | ||
const formMethods = useForm({ defaultValues }); | ||
const combinedMethods = { ...formMethods, ...methods }; | ||
|
||
return <FormProvider {...combinedMethods}>{children}</FormProvider>; | ||
}; |
This file contains 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,88 @@ | ||
'use client' | ||
import Image from 'next/image' | ||
import { useRef, useState } from 'react' | ||
import { useDropzone } from 'react-dropzone' | ||
import { Controller, useFormContext } from 'react-hook-form' | ||
|
||
import { ButtonWhiteBlack } from '../Buttons/ButtonWhiteBlack' | ||
|
||
const ZERO = 0 | ||
|
||
interface UploadProps { | ||
onChange: (...event: any[]) => void, | ||
name:string | ||
} | ||
|
||
export const Upload =({name='upload', ...rest}: {name?: string}) =>{ | ||
const { control, } = useFormContext() | ||
|
||
return( | ||
<Controller | ||
render={({ field: { onChange } })=>( | ||
<UploadInput onChange={(e:any)=> | ||
onChange(e.target.files[ZERO])} name={name} {...rest}/> | ||
)} | ||
name={name} | ||
control={control} | ||
defaultValue="" | ||
/> | ||
) | ||
} | ||
|
||
const UploadInput = ({ onChange,name, ...rest | ||
}: UploadProps) => { | ||
const [imgPreviewUrl, setImgPreviewUrl] = useState<string | null>(null) | ||
const inputImageRef = useRef<HTMLInputElement | null>(null); | ||
const { setValue, } = useFormContext() | ||
|
||
const handleRemoveImage = () => { | ||
setImgPreviewUrl(null) | ||
if (inputImageRef.current) { | ||
inputImageRef.current.value = ''; | ||
setValue(name,'') | ||
} | ||
} | ||
const onDrop = (acceptedFiles: File[]) => { | ||
const file = acceptedFiles[ZERO]; | ||
if (file) { | ||
setImgPreviewUrl( URL.createObjectURL(file)); | ||
setValue(name,file) | ||
} | ||
}; | ||
const { getRootProps, getInputProps } = useDropzone({ onDrop, ...rest | ||
}); | ||
return ( | ||
<div className="flex flex-col w-fit h-fit relative"> | ||
<div className="z-10 w-11/12 flex justify-end absolute"> | ||
{imgPreviewUrl && <ButtonWhiteBlack data-testid="custom-element" onClick={handleRemoveImage}>×</ButtonWhiteBlack>} | ||
</div> | ||
<div | ||
{...getRootProps()} | ||
className="relative flex flex-col items-center justify-center border border-dashed h-56 w-56 rounded-full overflow-hidden border-cyan-600 z-0" | ||
onClick={()=>{inputImageRef.current && inputImageRef.current.click()}} | ||
> | ||
<label htmlFor="file-uploader" className="hidden">Upload file:</label> | ||
<input | ||
{...getInputProps({ onChange })} | ||
id="file-uploader" | ||
type="file" | ||
className="hidden" | ||
title="Upload your file" | ||
placeholder="Choose a file" | ||
ref={inputImageRef} | ||
/> | ||
{imgPreviewUrl ? ( | ||
<Image src={imgPreviewUrl} alt="photo preview" layout="fill" objectFit="cover" sizes="auto auto" /> | ||
) : ( | ||
<Image | ||
src={'/assets/person-unknow.png'} | ||
alt="person unknow" | ||
layout="fill" | ||
objectFit="cover" | ||
sizes="auto auto" | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains 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,58 @@ | ||
import { fireEvent, screen, waitFor } from "@testing-library/react" | ||
import userEvent from '@testing-library/user-event' | ||
import { expect } from "vitest"; | ||
|
||
import { FormStateChecker, renderWithFormProvider } from "@/test-utils"; | ||
|
||
import { Upload } from "." | ||
|
||
|
||
const ONE = 1; | ||
describe('Upload Component',()=>{ | ||
beforeEach(()=>{ | ||
global.URL.createObjectURL = ()=>'http://localhost:3000/e17fa82b-b0c5-4b24-82fc-132a8659d6adimg/preview/test' | ||
renderWithFormProvider( | ||
<> | ||
<Upload name="upload" /> | ||
<FormStateChecker name="upload" /> | ||
</> | ||
) | ||
}) | ||
it("should render the component without photo",()=>{ | ||
const formState = screen.getByTestId("form-state"); | ||
expect(screen.getByAltText("person unknow")).toBeInTheDocument(); | ||
expect(formState.textContent).toBe(''); | ||
}) | ||
it("should handle file upload", async()=>{ | ||
const inputUpload = screen.getByLabelText<HTMLInputElement>(/upload file/i); | ||
const file = new File(['hello'], 'hello.png', {type: 'image/png'}) | ||
expect(inputUpload).toBeInTheDocument(); | ||
userEvent.upload(inputUpload,file); | ||
await waitFor(() => { | ||
expect(inputUpload.files?.[0]).toStrictEqual(file); | ||
expect(inputUpload.files?.item(0)).toStrictEqual(file); | ||
expect(inputUpload.files).toHaveLength(ONE) | ||
}); | ||
}) | ||
it("should handle image removal", async()=>{ | ||
const inputUpload = screen.getByLabelText<HTMLInputElement>(/upload file/i); | ||
const file = new File(['hello'], 'hello.png', {type: 'image/png'}) | ||
userEvent.upload(inputUpload,file); | ||
await waitFor(()=>{ | ||
expect(inputUpload.files?.[0]).toStrictEqual(file); | ||
expect(inputUpload.files?.item(0)).toStrictEqual(file); | ||
expect(inputUpload.files).toHaveLength(ONE) | ||
}) | ||
const photoPreview = screen.getByAltText(/photo preview/i); | ||
const buttonRemoveImage = screen.getByTestId('custom-element'); | ||
expect(photoPreview).toBeInTheDocument() | ||
expect(buttonRemoveImage).toBeInTheDocument() | ||
userEvent.click(buttonRemoveImage); | ||
await waitFor(()=>{ | ||
expect(inputUpload.files?.[0]).toStrictEqual(undefined); | ||
expect(inputUpload.files?.item(0)).toStrictEqual(null); | ||
expect(inputUpload.files).toHaveLength(0) | ||
}) | ||
expect(screen.getByAltText("person unknow")).toBeInTheDocument(); | ||
}) | ||
}) |
This file contains 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,30 @@ | ||
|
||
import type { Meta, StoryObj } from '@storybook/react' | ||
|
||
import { Upload } from "./index"; | ||
import { MockFormProvider as FormProvider } from './MockFormProvider'; | ||
|
||
type Story = StoryObj<typeof Upload>; | ||
|
||
const meta: Meta<typeof Upload> = { | ||
component: Upload, | ||
title: 'Components/Upload', | ||
argTypes:{ | ||
name:{ | ||
control:{type:'text'}, | ||
description: 'Field identifier in React hook form' | ||
} | ||
}, | ||
parameters: { | ||
layout: 'centered' | ||
} | ||
}; | ||
export default meta | ||
|
||
export const Default:Story = { | ||
render: () => ( | ||
<FormProvider> | ||
<Upload/> | ||
</FormProvider> | ||
) | ||
} |
This file contains 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,21 @@ | ||
import { render } from "@testing-library/react"; | ||
import React, { FC, ReactElement } from "react"; | ||
import { FormProvider, useForm, useFormContext } from "react-hook-form" | ||
|
||
export const renderWithFormProvider = (ui:ReactElement) =>{ | ||
const Wrapper:FC<{ children: React.ReactNode }> = ( | ||
{ children } | ||
)=>{ | ||
const methods = useForm(); | ||
return (<FormProvider {...methods}> | ||
{children} | ||
</FormProvider>) | ||
}; | ||
|
||
return render(ui,{wrapper:Wrapper}) | ||
} | ||
|
||
export const FormStateChecker = ({ name }: {name: string }) =>{ | ||
const { getValues } = useFormContext(); | ||
return <div data-testid="form-state">{getValues(name)}</div>; | ||
}; |
This file contains 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
Oops, something went wrong.