Skip to content

Commit

Permalink
fix: upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
Barresi committed Jul 10, 2024
1 parent 90714fb commit bfba70a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
9 changes: 1 addition & 8 deletions src/features/input-create-post/ui/input-create-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ const InputCreatePost: FC<IInputCreatePostProps> = ({ className }) => {
const user = useAppSelector(selectUser)
const dispatch = useAppDispatch()
const handleCreatePost = async (text: string, pictures?: FileList): Promise<void> => {
const formData = new FormData()
formData.append(`text`, text)
if (pictures)
Array.from(pictures).forEach((file, index) => {
formData.append(`file${index}`, file)
})

await createPost(formData)
await createPost({ text, pictures })
dispatch(getAllPostsThunk())
}
return (
Expand Down
13 changes: 11 additions & 2 deletions src/shared/api/news/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ export const getAllPosts = async (): Promise<IGetPostsResponse> => {
return res.data
}

export const createPost = async (form: ICreatePostForm): Promise<ICreatePostResponse> => {
const res = await apiWithAuth.post<ICreatePostResponse>('/posts', form)
export const createPost = async ({
text,
pictures
}: ICreatePostForm): Promise<ICreatePostResponse> => {
const formData = new FormData()
formData.append(`text`, text)
if (pictures)
Array.from(pictures).forEach((file) => {
formData.append('pictures', file)
})
const res = await apiWithAuth.post<ICreatePostResponse>('/posts', formData)

return res.data
}
Expand Down
9 changes: 6 additions & 3 deletions src/shared/api/user-info/user-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export const getUserInfo = async (): Promise<IGetUserInfoResponse> => {
export const editUserInfo = async (
form: IEditUserInfoForm
): Promise<IEditUserInfoResponse> => {
const res = await apiWithAuth.patch<IEditUserInfoResponse>('/auth/account', form, {
headers: { 'Content-Type': 'multipart/form-data' }
})
const formData = new FormData()
for (const property in form) {
formData.append(property, form[property as keyof IEditUserInfoForm] as File | string)
}

const res = await apiWithAuth.patch<IEditUserInfoResponse>('/auth/account', formData)

return res.data
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/ui/input-send-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import send from '@assets/ui/send.svg'
interface IInputSendMessageProps extends IInputProps {
avatar?: string | null
withPicture: boolean
sendMessage: (message: string, pictures?: FileList) => void
sendMessage: (message: string, pictures?: FileList) => Promise<void>
}

// Todo Переписать в textarea
Expand Down

0 comments on commit bfba70a

Please sign in to comment.