Skip to content

Commit

Permalink
Add query cache updates for mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcretu committed Feb 4, 2025
1 parent 3d6e5ed commit 9f19b23
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 21 deletions.
4 changes: 4 additions & 0 deletions server/lib/orcasite/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ defmodule Orcasite.Accounts.User do
bypass AshAuthentication.Checks.AshAuthenticationInteraction do
authorize_if always()
end

policy action_type(:read) do
authorize_if expr(id == ^actor(:id))
end

policy action(:current_user) do
authorize_if always()
Expand Down
56 changes: 46 additions & 10 deletions ui/src/graphql/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2251,11 +2251,17 @@ export type RegisterWithPasswordMutation = {
result?: {
__typename?: "User";
id: string;
email: string;
username?: string | null;
admin: boolean;
firstName?: string | null;
lastName?: string | null;
username?: string | null;
email: string;
admin: boolean;
moderator: boolean;
isScientist: boolean;
organization?: string | null;
newsletter: boolean;
volunteering: boolean;
userTesting: boolean;
} | null;
errors: Array<{
__typename?: "MutationError";
Expand Down Expand Up @@ -2343,10 +2349,17 @@ export type SignInWithPasswordMutation = {
user?: {
__typename?: "User";
id: string;
email: string;
admin: boolean;
firstName?: string | null;
lastName?: string | null;
username?: string | null;
email: string;
admin: boolean;
moderator: boolean;
isScientist: boolean;
organization?: string | null;
newsletter: boolean;
volunteering: boolean;
userTesting: boolean;
} | null;
errors: Array<{
__typename?: "MutationError";
Expand Down Expand Up @@ -2499,6 +2512,11 @@ export type GetCurrentUserQuery = {
email: string;
admin: boolean;
moderator: boolean;
isScientist: boolean;
organization?: string | null;
newsletter: boolean;
volunteering: boolean;
userTesting: boolean;
} | null;
};

Expand Down Expand Up @@ -2832,11 +2850,17 @@ export const RegisterWithPasswordDocument = `
) {
result {
id
email
username
admin
firstName
lastName
username
email
admin
moderator
isScientist
organization
newsletter
volunteering
userTesting
}
errors {
message
Expand Down Expand Up @@ -3054,10 +3078,17 @@ export const SignInWithPasswordDocument = `
signInWithPassword(input: {email: $email, password: $password}) {
user {
id
email
admin
firstName
lastName
username
email
admin
moderator
isScientist
organization
newsletter
volunteering
userTesting
}
errors {
message
Expand Down Expand Up @@ -3404,6 +3435,11 @@ export const GetCurrentUserDocument = `
email
admin
moderator
isScientist
organization
newsletter
volunteering
userTesting
}
}
`;
Expand Down
12 changes: 9 additions & 3 deletions ui/src/graphql/mutations/registerWithPassword.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ mutation registerWithPassword(
) {
result {
id
email
username
admin
firstName
lastName
username
email
admin
moderator
isScientist
organization
newsletter
volunteering
userTesting
}

errors {
Expand Down
11 changes: 9 additions & 2 deletions ui/src/graphql/mutations/signInWithPassword.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ mutation signInWithPassword($email: String!, $password: String!) {
signInWithPassword(input: { email: $email, password: $password }) {
user {
id
email
admin
firstName
lastName
username
email
admin
moderator
isScientist
organization
newsletter
volunteering
userTesting
}
errors {
message
Expand Down
5 changes: 5 additions & 0 deletions ui/src/graphql/queries/getCurrentUser.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ query getCurrentUser {
email
admin
moderator
isScientist
organization
newsletter
volunteering
userTesting
}
}
22 changes: 16 additions & 6 deletions ui/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQueryClient } from "@tanstack/react-query";

import {
GetCurrentUserQuery,
useGetCurrentUserQuery,
useRegisterWithPasswordMutation,
useSignInWithPasswordMutation,
Expand All @@ -12,23 +13,32 @@ export function useAuth() {

const { data: { currentUser } = {}, isLoading: isLoadingUser } =
useGetCurrentUserQuery();

const { mutate: signIn } = useSignInWithPasswordMutation({
onSuccess: ({ signInWithPassword }) => {
queryClient.setQueryData(useGetCurrentUserQuery.getKey(), {
currentUser: signInWithPassword?.user,
});
queryClient.setQueryData<GetCurrentUserQuery>(
useGetCurrentUserQuery.getKey(),
{
currentUser: signInWithPassword?.user,
},
);
},
});

const { mutate: signOut } = useSignOutMutation({
onSuccess: () => {
queryClient.removeQueries({ queryKey: useGetCurrentUserQuery.getKey() });
},
});

const { mutate: register } = useRegisterWithPasswordMutation({
onSuccess: ({ registerWithPassword }) => {
queryClient.setQueryData(useGetCurrentUserQuery.getKey(), {
currentUser: registerWithPassword?.result,
});
queryClient.setQueryData<GetCurrentUserQuery>(
useGetCurrentUserQuery.getKey(),
{
currentUser: registerWithPassword?.result,
},
);
},
});

Expand Down
13 changes: 13 additions & 0 deletions ui/src/modules/join/components/PreferencesStep.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { Alert, FormControlLabel, Switch } from "@mui/material";
import { useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";

import {
GetCurrentUserQuery,
MutationError,
useGetCurrentUserQuery,
useUpdateUserPreferencesMutation,
} from "@/graphql/generated";
import { useAuth } from "@/hooks/useAuth";
Expand All @@ -26,6 +29,7 @@ type PreferencesFormInputs = z.infer<typeof preferencesSchema>;
const usePreferencesForm = (onSuccess: () => void) => {
const [errors, setErrors] = useState<MutationError[]>([]);
const { user } = useAuth();
const queryClient = useQueryClient();

const form = useForm<PreferencesFormInputs>({
resolver: zodResolver(preferencesSchema),
Expand All @@ -52,6 +56,15 @@ const usePreferencesForm = (onSuccess: () => void) => {
onSuccess: (data) => {
const { updateUserPreferences } = data;
if (updateUserPreferences?.result) {
queryClient.setQueryData<GetCurrentUserQuery>(
useGetCurrentUserQuery.getKey(),
{
currentUser: {
...user,
...updateUserPreferences.result,
},
},
);
onSuccess();
} else if (updateUserPreferences?.errors?.length) {
setErrors(updateUserPreferences.errors);
Expand Down
13 changes: 13 additions & 0 deletions ui/src/modules/join/components/ProfileStep.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { Checkbox, FormControlLabel, TextField } from "@mui/material";
import { useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";

import {
GetCurrentUserQuery,
MutationError,
useGetCurrentUserQuery,
useUpdateUserProfileMutation,
} from "@/graphql/generated";
import { useAuth } from "@/hooks/useAuth";
Expand All @@ -31,6 +34,7 @@ type ProfileFormInputs = z.infer<typeof profileSchema>;
const useProfileForm = (onSuccess: () => void) => {
const [errors, setErrors] = useState<MutationError[]>([]);
const { user } = useAuth();
const queryClient = useQueryClient();

const form = useForm<ProfileFormInputs>({
resolver: zodResolver(profileSchema),
Expand All @@ -54,6 +58,15 @@ const useProfileForm = (onSuccess: () => void) => {
onSuccess: (data) => {
const { updateUserProfile } = data;
if (updateUserProfile?.result) {
queryClient.setQueryData<GetCurrentUserQuery>(
useGetCurrentUserQuery.getKey(),
{
currentUser: {
...user,
...updateUserProfile.result,
},
},
);
onSuccess();
} else if (updateUserProfile?.errors?.length) {
setErrors(updateUserProfile.errors);
Expand Down

0 comments on commit 9f19b23

Please sign in to comment.