Ref EditableCell signature
Signed-off-by: Jonathan <contact@grafikart.fr>
This commit is contained in:
@@ -121,13 +121,17 @@ export const deleteAssetMutation = graphql`
|
||||
`;
|
||||
|
||||
export const useDeleteAsset = (
|
||||
asset: { id?: string; name?: string },
|
||||
connectionId: string
|
||||
asset?: { id?: string; name?: string },
|
||||
connectionId?: string,
|
||||
) => {
|
||||
const [mutate] = useMutation(deleteAssetMutation);
|
||||
const confirm = useConfirm();
|
||||
const { __ } = useTranslate();
|
||||
|
||||
if (!asset) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (!asset.id || !asset.name) {
|
||||
return alert(__("Failed to delete asset: missing id or name"));
|
||||
@@ -145,56 +149,61 @@ export const useDeleteAsset = (
|
||||
{
|
||||
message: sprintf(
|
||||
__(
|
||||
'This will permanently delete "%s". This action cannot be undone.'
|
||||
'This will permanently delete "%s". This action cannot be undone.',
|
||||
),
|
||||
asset.name
|
||||
asset.name,
|
||||
),
|
||||
}
|
||||
},
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export const useCreateAsset = (connectionId: string) => {
|
||||
const [mutate] = useMutation(createAssetMutation);
|
||||
const [mutate, isMutating] = useMutation(createAssetMutation);
|
||||
const { __ } = useTranslate();
|
||||
|
||||
return (input: {
|
||||
name: string;
|
||||
amount: number;
|
||||
assetType: string;
|
||||
ownerId: string;
|
||||
organizationId: string;
|
||||
vendorIds?: string[];
|
||||
dataTypesStored: string;
|
||||
}) => {
|
||||
if (!input.name?.trim()) {
|
||||
return alert(__("Failed to create asset: name is required"));
|
||||
}
|
||||
if (!input.ownerId) {
|
||||
return alert(__("Failed to create asset: owner is required"));
|
||||
}
|
||||
if (!input.organizationId) {
|
||||
return alert(__("Failed to create asset: organization is required"));
|
||||
}
|
||||
if (!input.dataTypesStored) {
|
||||
return alert(__("Failed to create asset: data types stored is required"));
|
||||
}
|
||||
return [
|
||||
(input: {
|
||||
name: string;
|
||||
amount: number;
|
||||
assetType: string;
|
||||
ownerId: string;
|
||||
organizationId: string;
|
||||
vendorIds?: string[];
|
||||
dataTypesStored: string;
|
||||
}) => {
|
||||
if (!input.name?.trim()) {
|
||||
return alert(__("Failed to create asset: name is required"));
|
||||
}
|
||||
if (!input.ownerId) {
|
||||
return alert(__("Failed to create asset: owner is required"));
|
||||
}
|
||||
if (!input.organizationId) {
|
||||
return alert(__("Failed to create asset: organization is required"));
|
||||
}
|
||||
if (!input.dataTypesStored) {
|
||||
return alert(
|
||||
__("Failed to create asset: data types stored is required"),
|
||||
);
|
||||
}
|
||||
|
||||
return promisifyMutation(mutate)({
|
||||
variables: {
|
||||
input: {
|
||||
name: input.name,
|
||||
amount: input.amount,
|
||||
assetType: input.assetType,
|
||||
dataTypesStored: input.dataTypesStored || "",
|
||||
ownerId: input.ownerId,
|
||||
organizationId: input.organizationId,
|
||||
vendorIds: input.vendorIds || [],
|
||||
return promisifyMutation(mutate)({
|
||||
variables: {
|
||||
input: {
|
||||
name: input.name,
|
||||
amount: input.amount,
|
||||
assetType: input.assetType,
|
||||
dataTypesStored: input.dataTypesStored || "",
|
||||
ownerId: input.ownerId,
|
||||
organizationId: input.organizationId,
|
||||
vendorIds: input.vendorIds || [],
|
||||
},
|
||||
connections: [connectionId],
|
||||
},
|
||||
connections: [connectionId],
|
||||
},
|
||||
});
|
||||
};
|
||||
});
|
||||
},
|
||||
isMutating,
|
||||
] as const;
|
||||
};
|
||||
|
||||
export const useUpdateAsset = () => {
|
||||
|
||||
33
apps/console/src/hooks/useMutateField.tsx
Normal file
33
apps/console/src/hooks/useMutateField.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { GraphQLTaggedNode } from "relay-runtime";
|
||||
import { useMutation } from "react-relay";
|
||||
|
||||
export type MutationFieldUpdate<T> = (
|
||||
field: keyof T,
|
||||
value: T[typeof field],
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Mutate a single field from a graphql mutation
|
||||
*/
|
||||
export function useMutateField<Input extends Record<string, unknown>>(
|
||||
mutation: GraphQLTaggedNode,
|
||||
) {
|
||||
const [mutate, isUpdating] = useMutation(mutation);
|
||||
|
||||
return {
|
||||
update<T extends keyof Input>(id: string, fieldName: T, value: Input[T]) {
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
mutate({
|
||||
variables: {
|
||||
input: {
|
||||
id: id,
|
||||
[fieldName]: value,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
isUpdating,
|
||||
};
|
||||
}
|
||||
31
apps/console/src/hooks/useStateWithSchema.ts
Normal file
31
apps/console/src/hooks/useStateWithSchema.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { z, ZodError, type ZodTypeAny } from "zod";
|
||||
import { useMemo, useState } from "react";
|
||||
|
||||
export function useStateWithSchema<T extends ZodTypeAny>(
|
||||
schema: T,
|
||||
initialValue: z.infer<T>,
|
||||
) {
|
||||
const [state, setState] = useState(initialValue);
|
||||
const errors = useMemo(() => {
|
||||
try {
|
||||
schema.parse(state);
|
||||
return {};
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) {
|
||||
return Object.fromEntries(
|
||||
error.issues.map((issue) => [issue.path.join("."), issue.message]) ??
|
||||
[],
|
||||
);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}, [state, schema]);
|
||||
|
||||
return [
|
||||
state,
|
||||
(key: keyof z.infer<T>, value: z.infer<T>[typeof key]) => {
|
||||
setState((prevState) => ({ ...prevState, [key]: value }));
|
||||
},
|
||||
errors,
|
||||
] as const;
|
||||
}
|
||||
Reference in New Issue
Block a user