Skip to content

Commit

Permalink
fix: update profile after edit profile
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Aug 22, 2021
1 parent eabf0a0 commit 48055f6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
10 changes: 6 additions & 4 deletions client/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type CreatePostResponse = {

export type EditProfileResponse = {
__typename?: 'EditProfileResponse';
ok: Scalars['Boolean'];
user?: Maybe<User>;
errors?: Maybe<Array<FieldError>>;
};

Expand Down Expand Up @@ -317,7 +317,7 @@ export type EditProfileMutationVariables = Exact<{
}>;


export type EditProfileMutation = { __typename?: 'Mutation', editProfile: { __typename?: 'EditProfileResponse', ok: boolean, errors?: Maybe<Array<{ __typename?: 'FieldError', path: string, message: string }>> } };
export type EditProfileMutation = { __typename?: 'Mutation', editProfile: { __typename?: 'EditProfileResponse', user?: Maybe<{ __typename?: 'User', id: string, username: string, email: string, isFollowing: boolean, profile: { __typename?: 'Profile', id: string, name: string, website: string, bio: string, gender: string, imgURL: string, followersCount: number, followingsCount: number } }>, errors?: Maybe<Array<{ __typename?: 'FieldError', path: string, message: string }>> } };

export type LoginMutationVariables = Exact<{
username: Scalars['String'];
Expand Down Expand Up @@ -654,14 +654,16 @@ export const EditProfileDocument = gql`
gender: $gender
email: $email
) {
ok
user {
...UserWithProfile
}
errors {
path
message
}
}
}
`;
${UserWithProfileFragmentDoc}`;
export type EditProfileMutationFn = Apollo.MutationFunction<EditProfileMutation, EditProfileMutationVariables>;

/**
Expand Down
6 changes: 0 additions & 6 deletions client/src/graphql/fragments/UserWIthProfile.graphql

This file was deleted.

4 changes: 3 additions & 1 deletion client/src/graphql/mutations/editProfile.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ mutation EditProfile(
gender: $gender
email: $email
) {
ok
user {
...UserWithProfile
}
errors {
path
message
Expand Down
17 changes: 12 additions & 5 deletions client/src/routes/EditProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Button from '../components-ui/Button';
import Container from '../components-ui/Container';
import InputField from '../components-ui/InputField';
import { useEditProfileMutation, useMeQuery } from '../generated/graphql';
import {
MeDocument,
useEditProfileMutation,
useMeQuery,
} from '../generated/graphql';
import ChangeProfilePhoto from '../components/ChangeProfilePhoto';
import { Field, Form, Formik } from 'formik';
import { useMessageCtx } from '../context/MessageContext';
Expand Down Expand Up @@ -42,18 +46,21 @@ const EditProfile: React.FC = () => {
}}
onSubmit={async (values, action) => {
try {
const res = await editProfile({
await editProfile({
variables: values,
update: (cache, { data }) => {
if (data) {
const { errors, ok } = data.editProfile;
const { errors, user } = data.editProfile;
if (errors) {
errors.forEach(({ path, message }) => {
action.setFieldError(path, message);
});
}
if (ok) {
cache.evict({ id: cache.identify(user) });
if (user) {
cache.writeQuery({
query: MeDocument,
data: { me: user },
});
setMessage('Profile updated');
}
}
Expand Down
9 changes: 4 additions & 5 deletions server/src/resolvers/ProfileResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class EditProfileArgs {

@ObjectType()
export class EditProfileResponse {
@Field()
ok: boolean;
@Field(() => User, { nullable: true })
user?: User;
@Field(() => [FieldError], { nullable: true })
errors?: FieldError[];
}
Expand Down Expand Up @@ -158,21 +158,20 @@ export class ProfileResolver {
const profileErrors = await validate(profile);
if (userErrors.length > 0 || profileErrors.length > 0) {
const errors = formatErrors([...userErrors, ...profileErrors]);
return { ok: false, errors };
return { errors };
}
await getManager().transaction(async (tem) => {
await tem.save(user);
await tem.save(profile);
});
return { ok: true };
return { user };
} catch (err) {
console.log(err);
}
} else {
console.log(profile, user);
}
return {
ok: false,
errors: [{ path: 'unknown', message: 'Server Error' }],
};
}
Expand Down

1 comment on commit 48055f6

@vercel
Copy link

@vercel vercel bot commented on 48055f6 Aug 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.