From f8ae2107eb1cfefb44ced9e28a59488929b8b385 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Thu, 16 Dec 2021 21:39:49 +0300 Subject: [PATCH] fix: fixed contacts updating --- .../UpdateUserContactsForm.tsx | 15 +++++++++------ src/pages/profile/components/index.tsx | 2 +- src/pages/profile/profile.tsx | 2 +- src/services/UserService.ts | 11 ++++++++--- src/stores/UserStore.ts | 11 ++++++++--- src/types/index.d.ts | 6 ++++++ 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/pages/profile/components/UpdateUserContactsForm/UpdateUserContactsForm.tsx b/src/pages/profile/components/UpdateUserContactsForm/UpdateUserContactsForm.tsx index 704293b..038e71d 100644 --- a/src/pages/profile/components/UpdateUserContactsForm/UpdateUserContactsForm.tsx +++ b/src/pages/profile/components/UpdateUserContactsForm/UpdateUserContactsForm.tsx @@ -1,8 +1,9 @@ -import React, { useState } from 'react'; +import React from 'react'; +import * as helpers from '@educt/helpers'; import { observer } from 'mobx-react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { MdSave } from 'react-icons/md'; -import { Text, Input, Button } from '@chakra-ui/react'; +import { Text, Input } from '@chakra-ui/react'; import { Box, Stack } from '@chakra-ui/layout'; import { FormControl, FormHelperText } from '@chakra-ui/form-control'; import { yupResolver } from '@hookform/resolvers/yup'; @@ -15,6 +16,7 @@ import { IUserContacts } from '@educt/interfaces'; /** * Hooks */ +import { useState } from 'react'; import { useToast } from '@chakra-ui/toast'; import { useRootStore } from '@educt/hooks/useRootStore'; import { useErrorHandler } from 'react-error-boundary'; @@ -62,7 +64,8 @@ const UpdateUserContactsForm: React.FC = ({ con const onSubmit: SubmitHandler = async data => { try { setIsLoading(true); - await userStore.updateCurrentUserContacts(data); + const updatedFields = helpers.getDirtyFields(dirtyFields, data); + await userStore.updateCurrentUserContacts(updatedFields); toast({ title: 'Contacts saved!', status: 'info' }); reset({ phone_number: data.phone_number ?? '', @@ -113,7 +116,7 @@ const UpdateUserContactsForm: React.FC = ({ con type='text' size='md' variant='flushed' - placeholder='@tw_username' + placeholder='@username' isInvalid={errors.twitter_id ? true : false} {...register('twitter_id')} /> @@ -128,7 +131,7 @@ const UpdateUserContactsForm: React.FC = ({ con type='text' size='md' variant='flushed' - placeholder='@tg_username' + placeholder='@username' isInvalid={errors.telegram_id ? true : false} {...register('telegram_id')} /> @@ -143,7 +146,7 @@ const UpdateUserContactsForm: React.FC = ({ con type='text' size='md' variant='flushed' - placeholder='VKontakte id' + placeholder='username' isInvalid={errors.vk_id ? true : false} {...register('vk_id')} /> diff --git a/src/pages/profile/components/index.tsx b/src/pages/profile/components/index.tsx index d531838..dd26063 100644 --- a/src/pages/profile/components/index.tsx +++ b/src/pages/profile/components/index.tsx @@ -51,7 +51,7 @@ export const ProfileDescription: React.FC = props => { const { fullname, email, roles, ...boxProps } = props; return ( - + {fullname} diff --git a/src/pages/profile/profile.tsx b/src/pages/profile/profile.tsx index da7b680..286998b 100644 --- a/src/pages/profile/profile.tsx +++ b/src/pages/profile/profile.tsx @@ -53,7 +53,7 @@ const ProfilePage: React.FC = () => { - + Account information My contacts diff --git a/src/services/UserService.ts b/src/services/UserService.ts index 4b357b6..5485a48 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -2,7 +2,12 @@ import { AxiosInstance } from 'axios'; import { ApiServiceInstance } from '.'; import * as helpers from '@educt/helpers'; import { IApiRespose, IMe, IUser, IUserContacts } from '@educt/interfaces'; -import { CreateUserParamsType, FetchUsersParamsType, UpdateUserParamsType } from '@educt/types'; +import { + CreateUserParamsType, + FetchUsersParamsType, + UpdateUserParamsType, + UpdateUserContactsParamsType, +} from '@educt/types'; class UserService { public api: AxiosInstance; @@ -87,8 +92,8 @@ class UserService { * @param data New values for contacts * @returns Updated contacts */ - public async updateContacts(data: IUserContacts): Promise> { - const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data)); + public async updateContacts(data: UpdateUserContactsParamsType): Promise> { + const result = await this.api.patch('v1/me/contacts', data); return result.data; } diff --git a/src/stores/UserStore.ts b/src/stores/UserStore.ts index 1e74717..423e175 100644 --- a/src/stores/UserStore.ts +++ b/src/stores/UserStore.ts @@ -4,8 +4,13 @@ import * as helpers from '@educt/helpers'; /** * Types */ -import { CreateUserParamsType, FetchUsersParamsType, UpdateUserParamsType } from '@educt/types'; -import { IUser, IUserContacts, IMe, IPaginationMeta } from '@educt/interfaces'; +import { + CreateUserParamsType, + FetchUsersParamsType, + UpdateUserContactsParamsType, + UpdateUserParamsType, +} from '@educt/types'; +import { IUser, IMe, IPaginationMeta } from '@educt/interfaces'; import { UserRoleEnum } from '@educt/enums'; /** @@ -135,7 +140,7 @@ export default class UserStore { * @param data New contacts * @returns Updated user contacts */ - public async updateCurrentUserContacts(data: IUserContacts) { + public async updateCurrentUserContacts(data: UpdateUserContactsParamsType) { const result = await UserServiceInstance.updateContacts(data); runInAction(() => { diff --git a/src/types/index.d.ts b/src/types/index.d.ts index b3492b7..7bda18f 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -117,6 +117,12 @@ export type CreateUserParamsType = { password: string; }; export type UpdateUserParamsType = { first_name?: string; last_name?: string; email?: string }; +export type UpdateUserContactsParamsType = { + phone_number?: string | null | undefined; + twitter_id?: string | null | undefined; + telegram_id?: string | null | undefined; + vk_id?: string | null | undefined; +}; export type CreateCourseParamsType = { title: string;