-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from wanted-fork-fork/feature/info-edit-page
[Feature] 프로필 수정 페이지
- Loading branch information
Showing
20 changed files
with
495 additions
and
174 deletions.
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 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
File renamed without changes.
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,91 @@ | ||
import { getInfo, updateInfo } from 'src/types'; | ||
import { authenticate } from 'src/app/server/authenticate'; | ||
import { useLoaderData } from '@remix-run/react'; | ||
import { MyProfileProvider } from 'src/entities/profile/model/myProfileStore'; | ||
import { IdealPartnerProvider } from 'src/entities/ideal_partner/model/idealPartnerStore'; | ||
import { useMemo } from 'react'; | ||
import { convertDtoToProfile } from 'src/entities/profile/model/convertProfileToDto'; | ||
import { convertDtoToIdealPartner } from 'src/entities/ideal_partner/model/convertIdealPartnerToDto'; | ||
import { ActionFunctionArgs, json, LoaderFunction, redirect } from '@remix-run/node'; | ||
import { commitSession } from 'src/app/server/sessions'; | ||
import { EditInfoPage } from 'src/pages/edit_info/EditInfoPage'; | ||
|
||
export const loader: LoaderFunction = async ({ request, params }) => { | ||
const { accessToken, newSession } = await authenticate(request); | ||
|
||
const { key } = params; | ||
|
||
if (!key) { | ||
throw new Response('', { | ||
status: 404, | ||
statusText: 'Not Found', | ||
}); | ||
} | ||
|
||
const { data } = await getInfo(key, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
|
||
return json( | ||
{ profile: data }, | ||
{ | ||
headers: { | ||
...(newSession && { 'Set-Cookie': await commitSession(newSession) }), | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
export const action = async ({ request }: ActionFunctionArgs) => { | ||
const { accessToken, newSession } = await authenticate(request); | ||
const body = await request.formData(); | ||
|
||
const id = body.get('id') as string; | ||
|
||
try { | ||
await updateInfo( | ||
// TODO: zod로 타입 체크 | ||
{ | ||
id, | ||
userInfo: JSON.parse(body.get('userInfo') as string), | ||
idealPartner: JSON.parse(body.get('idealPartner') as string), | ||
}, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}, | ||
); | ||
|
||
return redirect(`/profile/${id}`, { | ||
headers: { | ||
...(newSession && { 'Set-Cookie': await commitSession(newSession) }), | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error(e, { | ||
userInfo: JSON.parse(body.get('userInfo') as string), | ||
idealPartner: JSON.parse(body.get('idealPartner') as string), | ||
}); | ||
return { status: 500 }; | ||
} | ||
}; | ||
|
||
export default function Page() { | ||
const { profile } = useLoaderData<typeof loader>(); | ||
const profileInitialState = useMemo(() => convertDtoToProfile(profile.userInfo), [profile.userInfo]); | ||
const idealPartnerInitialState = useMemo( | ||
() => (profile.idealPartner ? convertDtoToIdealPartner(profile.idealPartner) : undefined), | ||
[profile.idealPartner], | ||
); | ||
|
||
return ( | ||
<MyProfileProvider initialState={profileInitialState}> | ||
<IdealPartnerProvider initialState={idealPartnerInitialState}> | ||
<EditInfoPage infoId={profile.id} /> | ||
</IdealPartnerProvider> | ||
</MyProfileProvider> | ||
); | ||
} |
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
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
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
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
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,51 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { ImageDto, uploadImage } from 'src/types'; | ||
|
||
export const useUploadProfileImage = () => { | ||
const [progress, setProgress] = useState<number>(0); | ||
|
||
const { mutateAsync: uploadMutation } = useMutation({ | ||
mutationFn: uploadImage, | ||
}); | ||
|
||
const uploadFileList = useCallback( | ||
async (files: File[], onUpload: () => void) => { | ||
const results: ImageDto[] = []; | ||
// 한번에 너무 많이 요청하지 않도록 하나씩 업로드 요청 | ||
for (const file of files) { | ||
try { | ||
const result = await uploadMutation({ image: file }); | ||
onUpload(); | ||
results.push(result.data); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
return results; | ||
}, | ||
[uploadMutation], | ||
); | ||
|
||
const upload = useCallback( | ||
async (profileImages: File[], idealPartnerImages: File[]) => { | ||
setProgress(0); | ||
|
||
const total = profileImages.length + idealPartnerImages.length + 1; | ||
|
||
const [profileImageResults, idealImageResults] = await Promise.all([ | ||
uploadFileList(profileImages, () => { | ||
setProgress((prev) => prev + 1 / total); | ||
}), | ||
uploadFileList(idealPartnerImages, () => { | ||
setProgress((prev) => prev + 1 / total); | ||
}), | ||
]); | ||
|
||
return { profileImageResults, idealImageResults }; | ||
}, | ||
[uploadFileList], | ||
); | ||
|
||
return { progress, upload }; | ||
}; |
Oops, something went wrong.