Skip to content

Commit

Permalink
cleanup editable table cell
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Sep 25, 2024
1 parent ea41f8d commit 312bb10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,31 @@ import { useChain } from "../../../../hooks/useChain";
type Props = {
name: string;
value: string;
keyTuple: string[];
deployedTable: DeployedTable;
fieldKey: string[];
};

export function EditableTableCell({ name, deployedTable, keyTuple, value: defaultValue }: Props) {
export function EditableTableCell({ name, deployedTable, fieldKey, value: defaultValue }: Props) {
const [value, setValue] = useState<unknown>(defaultValue);
const wagmiConfig = useConfig();
const queryClient = useQueryClient();
const { worldAddress } = useParams();
const { id: chainId } = useChain();
const account = useAccount();
const { tableId, valueSchema } = deployedTable;
const type = valueSchema[name].type;
const fieldType = deployedTable.valueSchema[name].type;

const { mutate, isPending } = useMutation({
mutationFn: async (newValue: unknown) => {
const fieldIndex = getFieldIndex(
Object.fromEntries(Object.entries(valueSchema).map(([key, value]) => [key, value.type])),
Object.fromEntries(Object.entries(deployedTable.valueSchema).map(([key, value]) => [key, value.type])),
name,
);
const encodedField = encodeField(type, newValue);
const encodedFieldValue = encodeField(fieldType, newValue);
const txHash = await writeContract(wagmiConfig, {
abi: IBaseWorldAbi,
address: worldAddress as Hex,
functionName: "setField",
args: [tableId, keyTuple, fieldIndex, encodedField],
args: [deployedTable.tableId, fieldKey, fieldIndex, encodedFieldValue],
chainId,
});

Expand Down Expand Up @@ -86,7 +85,7 @@ export function EditableTableCell({ name, deployedTable, keyTuple, value: defaul
}
};

if (type === "bool") {
if (fieldType === "bool") {
return (
<>
<Checkbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ export function TablesViewer({
}) => {
const namespace = deployedTable?.namespace;
const keysSchema = Object.keys(deployedTable?.keySchema || {});
const keyTuple = keysSchema.map((key) => row.getValue(key));
const value = row.getValue(name)?.toString();

if (!deployedTable || keysSchema.includes(name) || internalNamespaces.includes(namespace)) {
return value;
}
return <EditableTableCell name={name} deployedTable={deployedTable} keyTuple={keyTuple} value={value} />;

const fieldKey = keysSchema.map((key) => row.getValue(key));
return <EditableTableCell name={name} deployedTable={deployedTable} value={value} fieldKey={fieldKey} />;
},
};
});
Expand Down

0 comments on commit 312bb10

Please sign in to comment.