Skip to content

Commit

Permalink
Merge pull request #20 from ourzora/fix-edit-params
Browse files Browse the repository at this point in the history
Fixed bug that when editing and then saving a transaction, resulting url was incorrect
  • Loading branch information
iainnash authored Apr 21, 2024
2 parents 29f1d2f + f11e9b3 commit b2d2e22
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
13 changes: 5 additions & 8 deletions src/app/NewSafeProposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Proposal,
proposalSchema,
} from "../schemas/proposal";
import { useSetParamsFromQuery } from "../hooks/useSetParamsFromQuery";
import { useRedirectToProposalWithNewParams } from "../hooks/useSetParamsFromQuery";
import { useLoadProposalFromQuery } from "../hooks/useLoadProposalFromQuery";
import {
transformValuesFromWei,
Expand Down Expand Up @@ -329,23 +329,20 @@ const ViewProposal = ({

const EditProposal = ({
proposal,
setProposal: setProposal,
setIsEditing,
}: {
proposal: Proposal | undefined;
setProposal: (result: Proposal) => void;
setIsEditing: (editing: boolean) => void;
}) => {
const setProposalParams = useSetParamsFromQuery();
const redirectToEditedProposal = useRedirectToProposalWithNewParams();
const onSubmit = useCallback(
(result: Proposal) => {
setProposal(transformValuesToWei(result));
const proposal = transformValuesToWei(result);
if (proposal) {
setProposalParams(proposal);
redirectToEditedProposal(proposal);
}
setIsEditing(false);
},
[proposal, setIsEditing, setProposal, setProposalParams],
[redirectToEditedProposal]
);

const defaultActions = proposal || DEFAULT_PROPOSAL;
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/useLoadProposalFromQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { Proposal } from "../schemas/proposal";

export const queryKeys = {
targets: "targets",
calldatas: "calldatas",
values: "values"
};

export const useLoadProposalFromQuery = () => {
const [proposal, setProposal] = useState<undefined | Proposal>();
const [params] = useSearchParams();

useEffect(() => {
const targets = params.get("targets")?.split("|");
const calldatas = params.get("calldatas")?.split("|");
const values = params.get("values")?.split("|");
const targets = params.get(queryKeys.targets)?.split("|");
const calldatas = params.get(queryKeys.calldatas)?.split("|");
const values = params.get(queryKeys.values)?.split("|");
if (targets && calldatas) {
// ensure the 3 lengths are the same. check if values also has the same length if its not empty
// check the inverse of the above, if inverse is true, return:
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/useSetParamsFromQuery.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useCallback } from "react";
import { useSearchParams } from "react-router-dom";
import { Proposal } from "../schemas/proposal";
import { queryKeys } from "./useLoadProposalFromQuery";

export const useSetParamsFromQuery = () => {
export const useRedirectToProposalWithNewParams = () => {
const [_, setParams] = useSearchParams();

return useCallback(
Expand All @@ -12,9 +13,9 @@ export const useSetParamsFromQuery = () => {
}
console.log("setting params", proposal.actions);
setParams({
targets: proposal.actions!.map((action) => action.to).join("|"),
data: proposal.actions!.map((action) => action.data).join("|"),
value: proposal.actions!.map((action) => action.value).join("|"),
[queryKeys.targets]: proposal.actions!.map((action) => action.to).join("|"),
[queryKeys.calldatas]: proposal.actions!.map((action) => action.data).join("|"),
[queryKeys.values]: proposal.actions!.map((action) => action.value).join("|"),
});
},
[setParams],
Expand Down

0 comments on commit b2d2e22

Please sign in to comment.