mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
changed the way we upload user avatars
This commit is contained in:
parent
be8eaac6c5
commit
00a16766bb
4 changed files with 24 additions and 5 deletions
|
@ -15,6 +15,7 @@ import {
|
||||||
ApiLoginWithSocialResult,
|
ApiLoginWithSocialResult,
|
||||||
ApiRestoreCodeRequest,
|
ApiRestoreCodeRequest,
|
||||||
ApiRestoreCodeResult,
|
ApiRestoreCodeResult,
|
||||||
|
ApiUpdatePhotoRequest,
|
||||||
ApiUpdateUserRequest,
|
ApiUpdateUserRequest,
|
||||||
ApiUpdateUserResult,
|
ApiUpdateUserResult,
|
||||||
ApiUserLoginRequest,
|
ApiUserLoginRequest,
|
||||||
|
@ -51,6 +52,12 @@ export const apiAuthGetUpdates = ({
|
||||||
export const apiUpdateUser = ({ user }: ApiUpdateUserRequest) =>
|
export const apiUpdateUser = ({ user }: ApiUpdateUserRequest) =>
|
||||||
api.patch<ApiUpdateUserResult>(API.USER.ME, user).then(cleanResult);
|
api.patch<ApiUpdateUserResult>(API.USER.ME, user).then(cleanResult);
|
||||||
|
|
||||||
|
export const apiUpdatePhoto = ({ file }: ApiUpdatePhotoRequest) =>
|
||||||
|
api.post<ApiUpdateUserResult>(API.USER.UPDATE_PHOTO, file).then(cleanResult);
|
||||||
|
|
||||||
|
export const apiUpdateCover = ({ file }: ApiUpdatePhotoRequest) =>
|
||||||
|
api.post<ApiUpdateUserResult>(API.USER.UPDATE_COVER, file).then(cleanResult);
|
||||||
|
|
||||||
export const apiRequestRestoreCode = (field: string) =>
|
export const apiRequestRestoreCode = (field: string) =>
|
||||||
api
|
api
|
||||||
.post<{ field: string }>(API.USER.REQUEST_CODE(), { field })
|
.post<{ field: string }>(API.USER.REQUEST_CODE(), { field })
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { INotification } from '~/types';
|
import { IFile, INotification } from '~/types';
|
||||||
import { ISocialAccount, IUser } from '~/types/auth';
|
import { ISocialAccount, IUser } from '~/types/auth';
|
||||||
|
|
||||||
export type ApiUserLoginRequest = Record<'username' | 'password', string>;
|
export type ApiUserLoginRequest = Record<'username' | 'password', string>;
|
||||||
|
@ -8,6 +8,9 @@ export type ApiAuthGetUserResult = { user: IUser };
|
||||||
export type ApiUpdateUserRequest = {
|
export type ApiUpdateUserRequest = {
|
||||||
user: Partial<IUser & { password: string; newPassword: string }>;
|
user: Partial<IUser & { password: string; newPassword: string }>;
|
||||||
};
|
};
|
||||||
|
export type ApiUpdatePhotoRequest = {
|
||||||
|
file: IFile;
|
||||||
|
};
|
||||||
export type ApiUpdateUserResult = {
|
export type ApiUpdateUserResult = {
|
||||||
user: IUser;
|
user: IUser;
|
||||||
errors: Record<Partial<keyof IUser>, string>;
|
errors: Record<Partial<keyof IUser>, string>;
|
||||||
|
|
|
@ -9,6 +9,8 @@ export const API = {
|
||||||
OAUTH_WINDOW: (provider: OAuthProvider) =>
|
OAUTH_WINDOW: (provider: OAuthProvider) =>
|
||||||
`${CONFIG.apiHost}oauth/${provider}/redirect`,
|
`${CONFIG.apiHost}oauth/${provider}/redirect`,
|
||||||
ME: '/auth',
|
ME: '/auth',
|
||||||
|
UPDATE_PHOTO: '/auth/photo',
|
||||||
|
UPDATE_COVER: '/auth/photo',
|
||||||
PROFILE: (username: string) => `/users/${username}/profile`,
|
PROFILE: (username: string) => `/users/${username}/profile`,
|
||||||
MESSAGES: (username: string) => `/users/${username}/messages`,
|
MESSAGES: (username: string) => `/users/${username}/messages`,
|
||||||
MESSAGE_SEND: (username: string) => `/users/${username}/messages`,
|
MESSAGE_SEND: (username: string) => `/users/${username}/messages`,
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
import { apiUpdateUser } from '~/api/auth';
|
import { apiUpdatePhoto, apiUpdateUser } from '~/api/auth';
|
||||||
import { ApiUpdateUserRequest } from '~/api/auth/types';
|
import { ApiUpdateUserRequest } from '~/api/auth/types';
|
||||||
import { UploadSubject, UploadTarget } from '~/constants/uploads';
|
import { UploadSubject, UploadTarget } from '~/constants/uploads';
|
||||||
import { useUser } from '~/hooks/auth/useUser';
|
import { useUser } from '~/hooks/auth/useUser';
|
||||||
import { useUploader } from '~/hooks/data/useUploader';
|
import { useUploader } from '~/hooks/data/useUploader';
|
||||||
|
import { IFile } from '~/types';
|
||||||
import { showErrorToast } from '~/utils/errors/showToast';
|
import { showErrorToast } from '~/utils/errors/showToast';
|
||||||
|
|
||||||
export const usePatchUser = () => {
|
export const usePatchUser = () => {
|
||||||
|
@ -24,10 +25,16 @@ export const usePatchUser = () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
const updatePhoto = useCallback(
|
const updatePhoto = useCallback(
|
||||||
async (file: File) => {
|
async (photo: File) => {
|
||||||
try {
|
try {
|
||||||
const photo = await uploadFile(file);
|
const file = await uploadFile(photo);
|
||||||
await save({ photo });
|
|
||||||
|
if (!file) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await apiUpdatePhoto({ file: file! });
|
||||||
|
await update(result.user);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showErrorToast(error);
|
showErrorToast(error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue