()
| 12 | import { EditUserForm } from "./EditUserForm"; |
| 13 | |
| 14 | const EditUserPage: FC = () => { |
| 15 | const { user: usernameOrId } = useParams() as { user: string }; |
| 16 | const navigate = useNavigate(); |
| 17 | const queryClient = useQueryClient(); |
| 18 | |
| 19 | const userQuery = useQuery(user(usernameOrId)); |
| 20 | const updateProfileMutation = useMutation( |
| 21 | updateProfile(userQuery.data?.id ?? ""), |
| 22 | ); |
| 23 | |
| 24 | if (!userQuery.data) { |
| 25 | return <Loader />; |
| 26 | } |
| 27 | |
| 28 | const userData = userQuery.data; |
| 29 | |
| 30 | const handleSubmit = async (values: UpdateUserProfileRequest) => { |
| 31 | const mutation = updateProfileMutation.mutateAsync(values, { |
| 32 | onSuccess: (updatedUser) => { |
| 33 | // Invalidate the user cache so other parts of the UI reflect the change. |
| 34 | void queryClient.invalidateQueries({ |
| 35 | queryKey: ["user", usernameOrId], |
| 36 | }); |
| 37 | void queryClient.invalidateQueries({ queryKey: ["users"] }); |
| 38 | |
| 39 | // If the URL currently uses the username (not a UUID) and the username |
| 40 | // has changed, rewrite the URL so the page doesn't 404 on refresh. |
| 41 | if (!isUUID(usernameOrId) && updatedUser.username !== usernameOrId) { |
| 42 | navigate(`../${updatedUser.username}`, { |
| 43 | relative: "path", |
| 44 | replace: true, |
| 45 | }); |
| 46 | } |
| 47 | }, |
| 48 | }); |
| 49 | |
| 50 | toast.promise(mutation, { |
| 51 | loading: `Saving user "${values.username}"…`, |
| 52 | success: `User "${values.username}" updated successfully.`, |
| 53 | error: (e) => ({ |
| 54 | message: getErrorMessage( |
| 55 | e, |
| 56 | `Failed to update user "${values.username}".`, |
| 57 | ), |
| 58 | description: getErrorDetail(e), |
| 59 | }), |
| 60 | }); |
| 61 | }; |
| 62 | |
| 63 | return ( |
| 64 | <Margins> |
| 65 | <title>{pageTitle("Edit User", `${userData.username}`)}</title> |
| 66 | |
| 67 | <EditUserForm |
| 68 | error={updateProfileMutation.error} |
| 69 | isLoading={updateProfileMutation.isPending} |
| 70 | initialValues={{ |
| 71 | username: userData.username, |
nothing calls this directly
no test coverage detected