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

fix/510-file-is-wrongly-reported-as-not-matching #537

Merged
merged 4 commits into from
Mar 22, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch, SetStateAction, useCallback } from "react";
import { Dispatch, SetStateAction, useEffect } from "react";
import { Box } from "@mui/material";
import OpenInNewIcon from "@mui/icons-material/OpenInNew";

Expand All @@ -25,23 +25,25 @@ export const StorageInformation = ({ setStep }: StorageInformationProps) => {
createGovernanceAction,
getValues,
watch,
generateMetadata,
onClickDownloadJson,
isLoading,
} = useCreateGovernanceActionForm(setStep);
const { screenWidth } = useScreenDimension();

// TODO: change on correct file name
const fileName = getValues("governance_action_type");

// TODO: Change link to correct
const openGuideAboutStoringInformation = useCallback(
() => openInNewTab("https://sancho.network/"),
[],
);
const openGuideAboutStoringInformation = () =>
openInNewTab("https://sancho.network/");

const isActionButtonDisabled = !watch("storingURL");

const onClickBack = useCallback(() => setStep(5), []);
const onClickBack = () => setStep(5);

useEffect(() => {
generateMetadata();
}, []);

return (
<BgCard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ export const StoreDataInfo = ({ setStep }: StoreDataInfoProps) => {
const { isMobile } = useScreenDimension();

// TODO: change link when available
const openLink = () => {
openInNewTab("https://docs.sanchogov.tools");
};
const openLink = () => openInNewTab("https://docs.sanchogov.tools");

const isContinueDisabled = !watch("storeData");

const onClickContinue = () => {
setStep(6);
};
const onClickContinue = () => setStep(6);

const onClickBack = () => {
setStep(4);
};
const onClickBack = () => setStep(4);

return (
<BgCard
Expand Down
41 changes: 22 additions & 19 deletions govtool/frontend/src/hooks/forms/useCreateGovernanceActionForm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Dispatch, SetStateAction, useCallback, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useFormContext } from "react-hook-form";
import * as blake from "blakejs";
import * as Sentry from "@sentry/react";
import { blake2bHex } from "blakejs";
import { captureException } from "@sentry/react";
import { useTranslation } from "react-i18next";
import { NodeObject } from "jsonld";

import {
CIP_100,
Expand Down Expand Up @@ -50,6 +51,7 @@ export const useCreateGovernanceActionForm = (
const { t } = useTranslation();
const [isLoading, setIsLoading] = useState<boolean>(false);
const [hash, setHash] = useState<string | null>(null);
const [json, setJson] = useState<NodeObject | null>(null);
const navigate = useNavigate();
const { openModal, closeModal } = useModal();
const {
Expand All @@ -74,10 +76,13 @@ export const useCreateGovernanceActionForm = (
closeModal();
}, []);

const generateMetadata = async (data: CreateGovernanceActionValues) => {
const generateMetadata = useCallback(async () => {
const data = getValues();

if (!govActionType) {
throw new Error("Governance action type is not defined");
}

const acceptedKeys = ["title", "motivation", "abstract", "rationale"];

const filteredData = Object.entries(data)
Expand All @@ -100,20 +105,19 @@ export const useCreateGovernanceActionForm = (
const jsonld = await generateJsonld(body, GOVERNANCE_ACTION_CONTEXT);

const canonizedJson = await canonizeJSON(jsonld);
const generatedHash = blake.blake2bHex(canonizedJson, undefined, 32);
const canonizedJsonHash = blake2bHex(canonizedJson, undefined, 32);

// That allows to validate metadata hash
setHash(generatedHash);
setHash(canonizedJsonHash);
setJson(jsonld);

return jsonld;
};

const onClickDownloadJson = async () => {
const data = getValues();
const json = await generateMetadata(data);
}, [getValues]);

const onClickDownloadJson = useCallback(() => {
if (!json) return;
downloadJson(json, govActionType);
};
}, [govActionType, json]);

const validateHash = useCallback(
async (storingUrl: string, localHash: string | null) => {
Expand Down Expand Up @@ -143,7 +147,7 @@ export const useCreateGovernanceActionForm = (
throw error;
}
},
[hash, backToForm],
[backToForm],
);

const buildTransaction = useCallback(
Expand Down Expand Up @@ -217,9 +221,7 @@ export const useCreateGovernanceActionForm = (
showSuccessModal();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
Sentry.captureException(error);
// eslint-disable-next-line no-console
console.error(error);
captureException(error);
} finally {
setIsLoading(false);
}
Expand All @@ -229,15 +231,16 @@ export const useCreateGovernanceActionForm = (

return {
control,
createGovernanceAction: handleSubmit(onSubmit),
errors,
generateMetadata,
getValues,
isLoading,
isValid,
setValue,
createGovernanceAction: handleSubmit(onSubmit),
watch,
onClickDownloadJson,
register,
reset,
onClickDownloadJson,
setValue,
watch,
};
};
Loading
Loading