Skip to content

Commit

Permalink
implement changes and toast errors from error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoalzate committed May 16, 2024
1 parent a159a82 commit 1ea2bc5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/api/src/postUsersToGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PostUsersToGroupsRequest, PostUsersToGroupsResponse } from './types';
async function postUserToGroups({
secret,
groupId,
}: PostUsersToGroupsRequest): Promise<PostUsersToGroupsResponse | null> {
}: PostUsersToGroupsRequest): Promise<PostUsersToGroupsResponse | { errors: string[] } | null> {
try {
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/api/users-to-groups`, {
method: 'POST',
Expand All @@ -15,7 +15,11 @@ async function postUserToGroups({
});

if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
if (response.status < 500) {
const errors = (await response.json()) as { errors: string[] };
return errors;
}
throw new Error(`HTTP error! Status: ${response.status}`);
}

const group = (await response.json()) as { data: PostUsersToGroupsResponse };
Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/putUsersToGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PostUsersToGroupsResponse, PutUsersToGroupsRequest } from './types';
async function postUsersToGroups({
groupId,
userToGroupId,
}: PutUsersToGroupsRequest): Promise<PostUsersToGroupsResponse | null> {
}: PutUsersToGroupsRequest): Promise<PostUsersToGroupsResponse | { errors: string[] } | null> {
try {
const response = await fetch(
`${import.meta.env.VITE_SERVER_URL}/api/users-to-groups/${userToGroupId}`,
Expand All @@ -18,7 +18,11 @@ async function postUsersToGroups({
);

if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
if (response.status < 500) {
const errors = (await response.json()) as { errors: string[] };
return errors;
}
throw new Error(`HTTP error! Status: ${response.status}`);
}

const group = (await response.json()) as { data: PostUsersToGroupsResponse };
Expand Down
37 changes: 34 additions & 3 deletions packages/berlin/src/pages/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ function AccountForm({
const { mutate: mutateUserData } = useMutation({
mutationFn: putUser,
onSuccess: async (body) => {
console.log({ body });
if (!body) {
return;
}
Expand All @@ -191,6 +190,38 @@ function AccountForm({
},
});

const { mutate: mutatePostUsersToGroups } = useMutation({
mutationFn: postUsersToGroups,
onSuccess: async (body) => {
if (!body) {
return;
}

if ('errors' in body) {
toast.error(`There was an error: ${body.errors.join(', ')}`);
return;
}

await queryClient.invalidateQueries({ queryKey: ['user', user?.id, 'users-to-groups'] });
},
});

const { mutate: mutatePutUsersToGroups } = useMutation({
mutationFn: putUsersToGroups,
onSuccess: async (body) => {
if (!body) {
return;
}

if ('errors' in body) {
toast.error(`There was an error: ${body.errors.join(', ')}`);
return;
}

await queryClient.invalidateQueries({ queryKey: ['user', user?.id, 'users-to-groups'] });
},
});

const {
control,
register,
Expand Down Expand Up @@ -260,11 +291,11 @@ function AccountForm({

// Create user to group if it doesn't exist
if (!value.userToGroupId) {
await postUsersToGroups({
await mutatePostUsersToGroups({
groupId: value.groupId,
});
} else {
await putUsersToGroups({
await mutatePutUsersToGroups({
groupId: value.groupId,
userToGroupId: value.userToGroupId,
});
Expand Down
6 changes: 6 additions & 0 deletions packages/berlin/src/pages/PublicGroupRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ function PublicGroupRegistration() {
if (!body) {
return;
}

if ('errors' in body) {
toast.error(body.errors.join(', '));
return;
}

queryClient.invalidateQueries({ queryKey: ['user', user?.id, 'groups'] });
toast.success(`Updated ${groupCategoryNameParam} group successfully!`);
},
Expand Down
5 changes: 5 additions & 0 deletions packages/berlin/src/pages/SecretGroupRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ function SecretGroupRegistration() {
if (!body) {
return;
}
if ('errors' in body) {
toast.error(body.errors[0]);
return;
}

queryClient.invalidateQueries({ queryKey: ['user', 'groups'] });
toast.success(`Joined ${groupName} group succesfully!`);
},
Expand Down

0 comments on commit 1ea2bc5

Please sign in to comment.