Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Query 5 - very rough attempt #6663

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@refinedev/devtools-internal": "1.1.16",
"@tanstack/react-query": "^4.10.1",
"@tanstack/react-query": "^5.64.2",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"papaparse": "^5.3.0",
Expand Down Expand Up @@ -72,7 +72,7 @@
"typescript": "^5.4.2"
},
"peerDependencies": {
"@tanstack/react-query": "^4.10.1",
"@tanstack/react-query": "^5.64.2",
"@types/react": "^17.0.0 || ^18.0.0",
"@types/react-dom": "^17.0.0 || ^18.0.0",
"react": "^17.0.0 || ^18.0.0",
Expand All @@ -81,4 +81,4 @@
"publishConfig": {
"access": "public"
}
}
}
2 changes: 1 addition & 1 deletion packages/core/src/components/autoSaveIndicator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const AutoSaveIndicator: React.FC<AutoSaveIndicatorProps> = ({
return <>{success}</>;
case "error":
return <>{error}</>;
case "loading":
case "pending":
return <>{loading}</>;
default:
return <>{idle}</>;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/components/canAccess/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ describe("CanAccess Component", () => {
legacyRouterProvider: {
...mockLegacyRouterProvider(),
useParams: () =>
({
(({
resource: "posts",
id: undefined,
action: "list",
}) as any,
action: "list"
}) as any),
},
accessControlProvider: {
can: async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/containers/refine/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const Refine: React.FC<RefineProps> = ({
...reactQueryWithDefaults.clientConfig.defaultOptions,
queries: {
refetchOnWindowFocus: false,
keepPreviousData: true,
// keepPreviousData: true,
...reactQueryWithDefaults.clientConfig.defaultOptions?.queries,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const ForgotPasswordPage: React.FC<ForgotPasswordProps> = ({

const [email, setEmail] = useState("");

const { mutate: forgotPassword, isLoading } =
const { mutate: forgotPassword, isPending } =
useForgotPassword<ForgotPasswordFormTypes>();

const renderLink = (link: string, text?: string) => {
Expand Down Expand Up @@ -81,7 +81,7 @@ export const ForgotPasswordPage: React.FC<ForgotPasswordProps> = ({
/>
<input
type="submit"
disabled={isLoading}
disabled={isPending}
value={translate(
"pages.forgotPassword.buttons.submit",
"Send reset instructions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const RegisterPage: React.FC<RegisterProps> = ({
const translate = useTranslate();

const authProvider = useActiveAuthProvider();
const { mutate: register, isLoading } = useRegister({
const { mutate: register, isPending } = useRegister({
v3LegacyAuthProviderCompatible: Boolean(authProvider?.isLegacy),
});

Expand Down Expand Up @@ -136,7 +136,7 @@ export const RegisterPage: React.FC<RegisterProps> = ({
<input
type="submit"
value={translate("pages.register.buttons.submit", "Sign up")}
disabled={isLoading}
disabled={isPending}
/>
{loginLink ?? (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const UpdatePasswordPage: React.FC<UpdatePasswordProps> = ({
const translate = useTranslate();

const authProvider = useActiveAuthProvider();
const { mutate: updatePassword, isLoading } =
const { mutate: updatePassword, isPending } =
useUpdatePassword<UpdatePasswordFormTypes>({
v3LegacyAuthProviderCompatible: Boolean(authProvider?.isLegacy),
});
Expand Down Expand Up @@ -87,7 +87,7 @@ export const UpdatePasswordPage: React.FC<UpdatePasswordProps> = ({
/>
<input
type="submit"
disabled={isLoading}
disabled={isPending}
value={translate("pages.updatePassword.buttons.submit", "Update")}
/>
</div>
Expand Down
32 changes: 16 additions & 16 deletions packages/core/src/contexts/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type BaseOption = {
/**
* @deprecated Use `BaseOption` instead.
*/
export interface Option extends BaseOption {}
export interface Option extends BaseOption { }

export type NestedField = {
operation: string;
Expand All @@ -32,12 +32,12 @@ export type Fields = Array<string | object | NestedField>;

export type VariableOptions =
| {
type?: string;
name?: string;
value: any;
list?: boolean;
required?: boolean;
}
type?: string;
name?: string;
value: any;
list?: boolean;
required?: boolean;
}
| { [k: string]: any };

export interface QueryBuilderOptions {
Expand Down Expand Up @@ -209,11 +209,11 @@ export interface IQueryKeys {
config?:
| UseListConfig
| {
pagination?: Required<Pagination>;
hasPagination?: boolean;
sorters?: CrudSort[];
filters?: CrudFilter[];
}
pagination?: Required<Pagination>;
hasPagination?: boolean;
sorters?: CrudSort[];
filters?: CrudFilter[];
}
| undefined,
) => QueryKey;
many: (ids?: BaseKey[]) => QueryKey;
Expand All @@ -223,10 +223,10 @@ export interface IQueryKeys {

export interface ValidationErrors {
[field: string]:
| string
| string[]
| boolean
| { key: string; message: string };
| string
| string[]
| boolean
| { key: string; message: string };
}

export interface HttpError extends Record<string, any> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/contexts/router/legacy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ILegacyRouterContext } from "./types";
export const defaultProvider: ILegacyRouterContext = {
useHistory: () => false,
useLocation: () => false,
useParams: () => ({}) as any,
useParams: () => (({}) as any),
Prompt: () => null,
Link: () => null,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ export const prepareQueryContext = (
},
});

return queryContext;
Object.defineProperty(queryContext, "direction", {
enumerable: true,
get: () => {
return context.direction;
},
});
return queryContext as QueryFunctionContext<QueryKey, any>;
};
77 changes: 36 additions & 41 deletions packages/core/src/hooks/auditLog/useLog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import type { BaseKey } from "../../../contexts/data/types";

type LogRenameData =
| {
resource?: string;
}
resource?: string;
}
| undefined;

export type UseLogReturnType<TLogData, TLogRenameData> = {
Expand Down Expand Up @@ -81,16 +81,17 @@ export const useLog = <
const {
data: identityData,
refetch,
isLoading,
isPending,
} = useGetIdentity({
v3LegacyAuthProviderCompatible: Boolean(authProvider?.isLegacy),
queryOptions: {
queryKey: keys().auth().get(preferLegacyKeys),
enabled: !!auditLogContext?.create,
},
});

const log = useMutation<TLogData, Error, LogParams, unknown>(
async (params) => {
const log = useMutation({
mutationFn: async (params) => {
const resource = pickResource(params.resource, resources);
const logPermissions = pickNotDeprecated(
resource?.meta?.audit,
Expand All @@ -105,7 +106,7 @@ export const useLog = <
}

let authorData;
if (isLoading && !!auditLogContext?.create) {
if (isPending && !!auditLogContext?.create) {
authorData = await refetch();
}

Expand All @@ -114,45 +115,39 @@ export const useLog = <
author: identityData ?? authorData?.data,
});
},
{
mutationKey: keys().audit().action("log").get(),
...logMutationOptions,
meta: {
...logMutationOptions?.meta,
...getXRay("useLog", preferLegacyKeys),
},
},
);

const rename = useMutation<
TLogRenameData,
Error,
{ id: BaseKey; name: string },
unknown
>(
async (params) => {
...logMutationOptions,

meta: {
...logMutationOptions?.meta,
...getXRay("useLog", preferLegacyKeys),
}
});

const rename = useMutation({
mutationFn: async (params) => {
return await auditLogContext.update?.(params);
},
{
onSuccess: (data) => {
if (data?.resource) {
queryClient.invalidateQueries(
keys()
.audit()
.resource(data?.resource ?? "")
.action("list")
.get(preferLegacyKeys),
);
}
},
mutationKey: keys().audit().action("rename").get(),
...renameMutationOptions,
meta: {
...renameMutationOptions?.meta,
...getXRay("useLog", preferLegacyKeys),
},

onSuccess: (data) => {
if (data?.resource) {
queryClient.invalidateQueries({
queryKey: keys()
.audit()
.resource(data?.resource ?? "")
.action("list")
.get(preferLegacyKeys),
});
}
},
);

...renameMutationOptions,

meta: {
...renameMutationOptions?.meta,
...getXRay("useLog", preferLegacyKeys),
}
});

return { log, rename };
};
2 changes: 1 addition & 1 deletion packages/core/src/hooks/auditLog/useLogList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ export const useLogList = <
},
});

return queryResponse;
return queryResponse as UseQueryResult<TData>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const useInvalidateAuthStore = () => {
const invalidate = async () => {
await Promise.all(
(["check", "identity", "permissions"] as const).map((action) =>
queryClient.invalidateQueries(
keys().auth().action(action).get(preferLegacyKeys),
),
queryClient.invalidateQueries({
queryKey: keys().auth().action(action).get(preferLegacyKeys),
}),
),
);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/auth/useOnError/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function useOnError({
},
}
: {
mutationFn: () => ({}) as Promise<OnErrorResponse>,
mutationFn: () => (({}) as Promise<OnErrorResponse>),
}),
meta: {
...getXRay("useOnError", preferLegacyKeys),
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/hooks/button/button-can-access/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { CanReturnType } from "../../../contexts/accessControl/types";
import type { IResourceItem } from "../../../contexts/resource/types";
import type { Action } from "../../../contexts/router/types";
import type { BaseKey } from "../../../contexts/data/types";

import { useKeys } from "../../useKeys";
type ButtonCanAccessProps = {
action: Action | "delete";
resource?: IResourceItem;
Expand All @@ -31,7 +31,7 @@ export const useButtonCanAccess = (
): ButtonCanAccessValues => {
const translate = useTranslate();
const accessControlContext = React.useContext(AccessControlContext);

const { keys, preferLegacyKeys } = useKeys();
const accessControlEnabled =
props.accessControl?.enabled ??
accessControlContext.options.buttons.enableAccessControl;
Expand All @@ -45,6 +45,7 @@ export const useButtonCanAccess = (
action: props.action === "clone" ? "create" : props.action,
params: { id: props.id, resource: props.resource },
queryOptions: {
queryKey: keys().auth().get(preferLegacyKeys),
enabled: accessControlEnabled,
},
});
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/hooks/button/delete-button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type DeleteButtonValues = {

export function useDeleteButton(props: DeleteButtonProps): DeleteButtonValues {
const translate = useTranslate();
const { mutate, isLoading, variables } = useDelete();
const { mutate, isPending, variables } = useDelete();
const { setWarnWhen } = useWarnAboutChange();
const { mutationMode } = useMutationMode(props.mutationMode);

Expand All @@ -62,7 +62,7 @@ export function useDeleteButton(props: DeleteButtonProps): DeleteButtonValues {

const cancelLabel = translate("buttons.cancel", "Cancel");

const loading = id === variables?.id && isLoading;
const loading = id === variables?.id && isPending;

const onConfirm = () => {
if (id && identifier) {
Expand Down Expand Up @@ -92,7 +92,7 @@ export function useDeleteButton(props: DeleteButtonProps): DeleteButtonValues {
hidden,
disabled,
canAccess,
loading,
loading: isPending,
confirmOkLabel,
cancelLabel,
confirmTitle,
Expand Down
Loading