1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00
vault-frontend/src/hooks/auth/usePatchUser.ts
muerwre 080d59858c
Отрефакторил бэк, исправил ошибки (#138)
* fixed paths to match refactored backend

* fixed some paths according to new backend

* fixed auth urls for new endpoints

* fixed urls

* fixed error handling

* fixes

* fixed error handling on user form

* fixed error handling on oauth

* using fallback: true on node pages

* type button for comment attach buttons

* fixed return types of social delete

* changed the way we upload user avatars
2022-09-16 14:53:52 +07:00

46 lines
1.2 KiB
TypeScript

import { useCallback } from 'react';
import { apiUpdatePhoto, apiUpdateUser } from '~/api/auth';
import { ApiUpdateUserRequest } from '~/api/auth/types';
import { UploadSubject, UploadTarget } from '~/constants/uploads';
import { useUser } from '~/hooks/auth/useUser';
import { useUploader } from '~/hooks/data/useUploader';
import { IFile } from '~/types';
import { showErrorToast } from '~/utils/errors/showToast';
export const usePatchUser = () => {
const { update } = useUser();
const { uploadFile } = useUploader(
UploadSubject.Avatar,
UploadTarget.Profiles,
);
const save = useCallback(
async (user: Partial<ApiUpdateUserRequest['user']>) => {
const result = await apiUpdateUser({ user });
await update(result.user);
return result.user;
},
[update],
);
const updatePhoto = useCallback(
async (photo: File) => {
try {
const file = await uploadFile(photo);
if (!file) {
return;
}
const result = await apiUpdatePhoto({ file: file! });
await update(result.user);
} catch (error) {
showErrorToast(error);
}
},
[uploadFile, save],
);
return { updatePhoto, save };
};