Skip to content

Commit

Permalink
Merge pull request #283 from Team-TenTen/feature/#282/image-compression
Browse files Browse the repository at this point in the history
이미지 용량 최적화
  • Loading branch information
dudwns authored Jan 3, 2024
2 parents 15af935 + 48a3be0 commit 8bef20f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
14 changes: 14 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 @@ -14,6 +14,7 @@
"@heroicons/react": "^2.0.18",
"@tanstack/react-query": "^5.8.4",
"@types/js-cookie": "^3.0.6",
"browser-image-compression": "^2.0.2",
"dayjs": "^1.11.10",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
Expand Down
9 changes: 6 additions & 3 deletions src/components/Space/SpaceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fetchSettingSpace,
} from '@/services/space/space'
import { CreateSpaceReqBody, SpaceDetailResBody } from '@/types'
import imageCompression from 'browser-image-compression'
import Image from 'next/image'
import { usePathname, useRouter } from 'next/navigation'
import Button from '../common/Button/Button'
Expand All @@ -35,7 +36,6 @@ const SpaceForm = ({ spaceType, space }: SpaceFormProps) => {
const spaceId = Number(path.split('/')[2])
const router = useRouter()
const getSpace = useGetSpace()

const {
register,
getValues,
Expand All @@ -58,15 +58,18 @@ const SpaceForm = ({ spaceType, space }: SpaceFormProps) => {
setThumnail(space?.spaceImagePath)
}, [space])

const handleFileChange = (e?: ChangeEvent<HTMLInputElement>) => {
const handleFileChange = async (e?: ChangeEvent<HTMLInputElement>) => {
e?.preventDefault()
if (e?.target.files) {
const blob = new Blob([e.target.files[0]], {
type: e.target.files[0].type,
})
const thumbNailImage = URL.createObjectURL(blob)
setThumnail(thumbNailImage)
setImageFile(e?.target.files[0])
const resizingBlob = await imageCompression(e?.target.files[0], {
maxSizeMB: 0.5,
})
setImageFile(resizingBlob)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/components/UserInfoForm/UserInfoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fetchPostUserProfile } from '@/services/user/profile/profile'
import { UserProfileResBody } from '@/types'
import { cls } from '@/utils'
import { CheckIcon } from '@heroicons/react/24/solid'
import imageCompression from 'browser-image-compression'
import Cookies from 'js-cookie'
import { useRouter } from 'next/navigation'
import Button from '../common/Button/Button'
Expand Down Expand Up @@ -67,7 +68,7 @@ const UserInfoForm = ({ userData, formType }: UserInfoFormProps) => {
},
})

const handleFileChange = (e?: ChangeEvent<HTMLInputElement>) => {
const handleFileChange = async (e?: ChangeEvent<HTMLInputElement>) => {
e?.preventDefault()

if (e?.target.files) {
Expand All @@ -77,7 +78,10 @@ const UserInfoForm = ({ userData, formType }: UserInfoFormProps) => {

const thumbNailImage = URL.createObjectURL(blob)
setThumnail(thumbNailImage)
setImageFile(e.target.files[0])
const resizingBlob = await imageCompression(e?.target.files[0], {
maxSizeMB: 0.5,
})
setImageFile(resizingBlob)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/common/SearchModal/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const SearchModal = ({ onClose }: SearchModalProps) => {
</div>
<div className="grow">
<Input
{...register('search', { required: true })}
{...register('search')}
placeholder="검색어를 입력하세요."
inputButton={true}
buttonText="검색"
Expand Down
6 changes: 6 additions & 0 deletions src/components/common/SearchModal/hooks/useSearchModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'react-hook-form'
import { mock_trendData } from '@/data'
import { useRouter } from 'next/navigation'
import { notify } from '../../Toast/Toast'
import { SearchFormValues } from '../SearchModal'

export interface useSearchModalProps {
Expand Down Expand Up @@ -43,6 +44,11 @@ const useSearchModal = ({
}

const onSubmit: SubmitHandler<SearchFormValues> = (data) => {
if (data.search.length === 1) {
notify('info', '검색어는 최소 2글자여야 합니다.')
return
}

router.push(`/search?target=${data.target}&keyword=${data.search}`)
}

Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useProfileSpacesSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SubmitHandler, UseFormSetValue } from 'react-hook-form'
import { SearchFormValue } from '@/app/(routes)/user/[userId]/space/page'
import { notify } from '@/components/common/Toast/Toast'
import { useRouter } from 'next/navigation'

export interface UseSpaceSearchProps {
Expand All @@ -18,6 +19,10 @@ const useProfileSpacesSearch = ({
type === 'space' ? `/user/${userId}/space?` : `/user/${userId}/favorite?`

const onSubmit: SubmitHandler<SearchFormValue> = (data) => {
if (data.keyword.length === 1) {
notify('info', '검색어는 최소 2글자여야 합니다.')
return
}
if (category) {
router.push(`${basePath}category=${category}&keyword=${data.keyword}`)
} else {
Expand Down

0 comments on commit 8bef20f

Please sign in to comment.