diff --git a/packages/api/src/postUsersToGroups.ts b/packages/api/src/postUsersToGroups.ts index 2c8c35a2..a4357b9b 100644 --- a/packages/api/src/postUsersToGroups.ts +++ b/packages/api/src/postUsersToGroups.ts @@ -3,7 +3,7 @@ import { PostUsersToGroupsRequest, PostUsersToGroupsResponse } from './types'; async function postUserToGroups({ secret, groupId, -}: PostUsersToGroupsRequest): Promise { +}: PostUsersToGroupsRequest): Promise { try { const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/api/users-to-groups`, { method: 'POST', @@ -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 }; diff --git a/packages/api/src/putUsersToGroups.ts b/packages/api/src/putUsersToGroups.ts index 6d4984a1..94e3fd08 100644 --- a/packages/api/src/putUsersToGroups.ts +++ b/packages/api/src/putUsersToGroups.ts @@ -3,7 +3,7 @@ import { PostUsersToGroupsResponse, PutUsersToGroupsRequest } from './types'; async function postUsersToGroups({ groupId, userToGroupId, -}: PutUsersToGroupsRequest): Promise { +}: PutUsersToGroupsRequest): Promise { try { const response = await fetch( `${import.meta.env.VITE_SERVER_URL}/api/users-to-groups/${userToGroupId}`, @@ -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 }; diff --git a/packages/berlin/src/pages/Account.tsx b/packages/berlin/src/pages/Account.tsx index 762e59ca..0c477572 100644 --- a/packages/berlin/src/pages/Account.tsx +++ b/packages/berlin/src/pages/Account.tsx @@ -167,7 +167,6 @@ function AccountForm({ const { mutate: mutateUserData } = useMutation({ mutationFn: putUser, onSuccess: async (body) => { - console.log({ body }); if (!body) { return; } @@ -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, @@ -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, }); diff --git a/packages/berlin/src/pages/PublicGroupRegistration.tsx b/packages/berlin/src/pages/PublicGroupRegistration.tsx index 314e19fb..47c54042 100644 --- a/packages/berlin/src/pages/PublicGroupRegistration.tsx +++ b/packages/berlin/src/pages/PublicGroupRegistration.tsx @@ -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!`); }, diff --git a/packages/berlin/src/pages/SecretGroupRegistration.tsx b/packages/berlin/src/pages/SecretGroupRegistration.tsx index 0ea9ec6a..1ca6b0c3 100644 --- a/packages/berlin/src/pages/SecretGroupRegistration.tsx +++ b/packages/berlin/src/pages/SecretGroupRegistration.tsx @@ -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!`); },