Skip to content

Commit 489ad78

Browse files
authored
Merge pull request #1892 from kleros/fix/dispute-template-key-check
Fix/dispute template key check
2 parents ce41157 + 35e7031 commit 489ad78

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export * from "./populateTemplate";
22
export * from "./retrieveVariables";
33
export * from "./disputeDetailsTypes";
4+
5+
export const isUndefined = (maybeObject: any): maybeObject is undefined | null =>
6+
typeof maybeObject === "undefined" || maybeObject === null;

kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import mustache from "mustache";
22
import retrieveVariables from "./retrieveVariables";
33
import { ActionMapping } from "./actionTypes";
44
import { InvalidContextError } from "../../errors";
5+
import { isUndefined } from ".";
56

67
export function replacePlaceholdersWithValues(
78
mapping: ActionMapping,
@@ -35,7 +36,8 @@ const validateContext = (template: string, context: Record<string, unknown>) =>
3536
const variables = retrieveVariables(template);
3637

3738
variables.forEach((variable) => {
38-
if (!context[variable]) throw new InvalidContextError(`Expected key "${variable}" to be provided in context.`);
39+
if (isUndefined(context[variable]))
40+
throw new InvalidContextError(`Expected key "${variable}" to be provided in context.`);
3941
});
4042
return true;
4143
};

web-devtools/src/app/(main)/dispute-template/page.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import ReactMarkdown from "components/ReactMarkdown";
2626
import FetchDisputeRequestInput, { DisputeRequest } from "./FetchDisputeRequestInput";
2727
import FetchFromIDInput from "./FetchFromIdInput";
2828
import CustomContextInputs from "./CustomContextInputs";
29+
import { debounceErrorToast } from "utils/debounceErrorToast";
30+
import { isEmpty } from "utils/isEmpty";
2931

3032
const Container = styled.div`
3133
height: auto;
@@ -193,12 +195,14 @@ const DisputeTemplateView = () => {
193195
if (customContext) initialContext = { ...initialContext, ...customContext };
194196

195197
const fetchData = async () => {
198+
if (isEmpty(disputeTemplateInput)) return;
196199
try {
197200
const data = dataMappingsInput ? await executeActions(JSON.parse(dataMappingsInput), initialContext) : {};
198201
const finalDisputeDetails = populateTemplate(disputeTemplateInput, data);
199202
setDisputeDetails(finalDisputeDetails);
200-
} catch (e) {
203+
} catch (e: any) {
201204
console.error(e);
205+
debounceErrorToast(e?.message);
202206
setDisputeDetails(undefined);
203207
} finally {
204208
setLoading(false);

web-devtools/src/hooks/queries/useDisputeTemplateFromId.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isUndefined } from "utils/isUndefined";
55

66
import { graphql } from "src/graphql-generated";
77
import { DisputeTemplateQuery } from "src/graphql-generated/graphql";
8+
import { isEmpty } from "utils/isEmpty";
89

910
const disputeTemplateQuery = graphql(`
1011
query DisputeTemplate($id: ID!) {
@@ -18,7 +19,7 @@ const disputeTemplateQuery = graphql(`
1819
`);
1920

2021
export const useDisputeTemplateFromId = (templateId?: string) => {
21-
const isEnabled = !isUndefined(templateId);
22+
const isEnabled = !isUndefined(templateId) && !isEmpty(templateId);
2223
const { graphqlBatcher } = useGraphqlBatcher();
2324

2425
return useQuery<DisputeTemplateQuery>({

web-devtools/src/utils/isEmpty.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isEmpty = (str: string): boolean => str.trim() === "";

0 commit comments

Comments
 (0)