Skip to content

Commit

Permalink
feat: store contract interaction form values
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 committed Aug 8, 2024
1 parent 35fc6e1 commit 8496dd3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/components/workspace/ABIUi/TactABIUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Button, Form, Input, Popover, Select, Switch } from 'antd';
import { Rule, RuleObject } from 'antd/es/form';
import { useForm } from 'antd/lib/form/Form';
import { useRouter } from 'next/router';
import { FC, Fragment, useState } from 'react';
import { FC, Fragment, useEffect, useState } from 'react';
import { ABIUiProps } from './ABIUi';
import s from './ABIUi.module.scss';

Expand Down Expand Up @@ -139,7 +139,7 @@ export const renderField = (
return (
<div key={key} className={s.dictItem}>
{field.fields?.map((subField, _i) => (
<Fragment key={subField.name}>
<Fragment key={`${key}-${_i}-${subField.name}`}>
{renderField(
subField as TactABIField,
files,
Expand Down Expand Up @@ -310,9 +310,16 @@ const TactABIUi: FC<TactABI> = ({
const { callGetter, callSetter } = useContractAction();
const { createLog } = useLogActivity();
const [form] = useForm();
const { projectFiles, getAllFilesWithContent } = useWorkspaceActions();
const {
projectFiles,
getAllFilesWithContent,
updateABIInputValues,
getABIInputValues,
project,
} = useWorkspaceActions();
const router = useRouter();
const { id: projectId } = router.query;
const activeProject = project(projectId as string);

const getItemHeading = (item: TactType) => {
if (item.type?.kind === 'simple') {
Expand Down Expand Up @@ -372,6 +379,10 @@ const TactABIUi: FC<TactABI> = ({
} else {
createLog(JSON.stringify(response));
}
updateABIInputValues(
{ key: abiType.name, value: formValues, type: type },
projectId as string,
);
} catch (error) {
if ((error as Error).message.includes('no healthy nodes for')) {
createLog(
Expand All @@ -389,6 +400,17 @@ const TactABIUi: FC<TactABI> = ({
}
};

useEffect(() => {
if (!activeProject) return;
const abiFields = getABIInputValues(
projectId as string,
abiType.name,
type,
);
if (!abiFields) return;
form.setFieldsValue(abiFields);
}, []);

return (
<div className={`${s.root} ${s.tact} ${s[type]}`}>
<Form
Expand Down
19 changes: 19 additions & 0 deletions src/components/workspace/BuildProject/BuildProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
NetworkEnvironment,
Project,
TactABIField,
TactInputFields,
} from '@/interfaces/workspace.interface';
import { Analytics } from '@/utility/analytics';
import { buildTs } from '@/utility/typescriptHelper';
Expand Down Expand Up @@ -91,6 +92,8 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
project,
activeFile,
getAllFilesWithContent,
updateABIInputValues,
getABIInputValues,
} = useWorkspaceActions();

const currentActiveFile = activeFile(projectId as string);
Expand Down Expand Up @@ -224,6 +227,15 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
if (activeProject?.language === 'tact') {
delete tempFormValues.contract;

updateABIInputValues(
{
key: 'init',
value: tempFormValues as TactInputFields,
type: 'Init',
},
projectId as string,
);

let tsProjectFiles = {};
if (isIncludesTypeCellOrSlice(tempFormValues)) {
tsProjectFiles = await getAllFilesWithContent(
Expand Down Expand Up @@ -649,6 +661,13 @@ const BuildProject: FC<Props> = ({ projectId, contract, updateContract }) => {
});
};

if (activeProject?.language === 'tact') {
const abiFields = getABIInputValues(projectId as string, 'init', 'Init');
if (abiFields) {
deployForm.setFieldsValue(abiFields);
}
}

window.addEventListener('message', handler);
return () => {
window.removeEventListener('message', handler);
Expand Down
40 changes: 40 additions & 0 deletions src/hooks/workspace.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ABIFormInputValues,
Project,
Tree,
WorkspaceState,
Expand Down Expand Up @@ -45,6 +46,8 @@ function useWorkspaceActions() {
getAllFilesWithContent,
compileTsFile,
isProjectEditable,
updateABIInputValues,
getABIInputValues,
clearWorkSpace,
};

Expand Down Expand Up @@ -528,6 +531,43 @@ function useWorkspaceActions() {
return true;
}

function updateABIInputValues(
inputValues: ABIFormInputValues,
projectId: string,
) {
const projectItem = project(projectId);
if (!projectItem) {
return;
}
const formInputValues = cloneDeep(inputValues);
const abiFormInputValues = cloneDeep(projectItem.abiFormInputValues) ?? [];
const index = abiFormInputValues.findIndex(
(item) =>
item.key === formInputValues.key && item.type === formInputValues.type,
);
if (index < 0) {
abiFormInputValues.push(formInputValues);
} else {
abiFormInputValues[index] = formInputValues;
}
updateProjectById(
{
abiFormInputValues,
} as Project,
projectId,
);
}

function getABIInputValues(projectId: string, key: string, type: string) {
const projectItem = project(projectId);
if (!projectItem) {
return [];
}
return projectItem.abiFormInputValues?.find(
(item) => item.type === type && item.key === key,
)?.value;
}

function clearWorkSpace() {
updateStateByKey({ openFiles: {}, projectFiles: null, projects: [] });
}
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/workspace.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export interface CellABI {
deploy?: InitParams;
setter?: InitParams;
}

export interface ABIFormInputValues {
key: string;
value: TactInputFields;
type: 'Init' | 'Getter' | 'Setter';
}

export interface Project {
id: string;
userId?: string;
Expand All @@ -50,6 +57,7 @@ export interface Project {
createdAt?: Date;
updatedAt?: Date;
cellABI?: CellABI;
abiFormInputValues?: ABIFormInputValues[];
}

export type WorkspaceState = {
Expand Down

0 comments on commit 8496dd3

Please sign in to comment.