Reset error state on valid JSON ABI (#4045)
* Reset error on valid JSON * Cleanup state * setAbi cleanup
This commit is contained in:
parent
20dd61210c
commit
635248b702
@ -10,7 +10,7 @@ import { u8aToString } from '@polkadot/util';
|
||||
|
||||
import store from './store';
|
||||
|
||||
interface UseAbi {
|
||||
interface AbiState {
|
||||
abi: string | null;
|
||||
abiName: string | null;
|
||||
contractAbi: Abi | null;
|
||||
@ -18,19 +18,45 @@ interface UseAbi {
|
||||
isAbiError: boolean;
|
||||
isAbiValid: boolean;
|
||||
isAbiSupplied: boolean;
|
||||
}
|
||||
|
||||
interface UseAbi extends AbiState {
|
||||
onChangeAbi: (u8a: Uint8Array, name: string) => void;
|
||||
onRemoveAbi: () => void;
|
||||
}
|
||||
|
||||
function fromInitial (initialValue: [string | null | undefined, Abi | null | undefined], isRequired: boolean): AbiState {
|
||||
return {
|
||||
abi: initialValue[0] || null,
|
||||
abiName: null,
|
||||
contractAbi: initialValue[1] || null,
|
||||
errorText: null,
|
||||
isAbiError: false,
|
||||
isAbiSupplied: !!initialValue[1],
|
||||
isAbiValid: !isRequired || !!initialValue[1]
|
||||
};
|
||||
}
|
||||
|
||||
const EMPTY: AbiState = {
|
||||
abi: null,
|
||||
abiName: null,
|
||||
contractAbi: null,
|
||||
errorText: null,
|
||||
isAbiError: false,
|
||||
isAbiSupplied: false,
|
||||
isAbiValid: false
|
||||
};
|
||||
|
||||
export default function useAbi (initialValue: [string | null | undefined, Abi | null | undefined] = [null, null], codeHash: StringOrNull = null, isRequired = false): UseAbi {
|
||||
const [[abi, contractAbi, isAbiSupplied, isAbiValid, abiName], setAbi] = useState<[string | null | undefined, Abi | null | undefined, boolean, boolean, string | null]>([initialValue[0], initialValue[1], !!initialValue[1], !isRequired || !!initialValue[1], null]);
|
||||
const [[isAbiError, errorText], setError] = useState<[boolean, string | null]>([false, null]);
|
||||
const [state, setAbi] = useState<AbiState>(fromInitial(initialValue, isRequired));
|
||||
|
||||
useEffect(
|
||||
(): void => {
|
||||
initialValue[0] && abi !== initialValue[0] && setAbi([initialValue[0], initialValue[1], !!initialValue[1], !isRequired || !!initialValue[1], null]);
|
||||
},
|
||||
[abi, initialValue, isRequired]
|
||||
() => setAbi((state) =>
|
||||
initialValue[0] && state.abi !== initialValue[0]
|
||||
? fromInitial(initialValue, isRequired)
|
||||
: state
|
||||
),
|
||||
[initialValue, isRequired]
|
||||
);
|
||||
|
||||
const onChangeAbi = useCallback(
|
||||
@ -38,14 +64,21 @@ export default function useAbi (initialValue: [string | null | undefined, Abi |
|
||||
const json = u8aToString(u8a);
|
||||
|
||||
try {
|
||||
setAbi([json, new Abi(json, api.registry.getChainProperties()), true, true, name.replace('.contract', '').replace('.json', '').replace('_', ' ')]);
|
||||
setAbi({
|
||||
abi: json,
|
||||
abiName: name.replace('.contract', '').replace('.json', '').replace('_', ' '),
|
||||
contractAbi: new Abi(json, api.registry.getChainProperties()),
|
||||
errorText: null,
|
||||
isAbiError: false,
|
||||
isAbiSupplied: true,
|
||||
isAbiValid: true
|
||||
});
|
||||
|
||||
codeHash && store.saveCode(codeHash, { abi: json });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
setAbi([null, null, false, false, null]);
|
||||
setError([true, (error as Error).message]);
|
||||
setAbi({ ...EMPTY, errorText: (error as Error).message });
|
||||
}
|
||||
},
|
||||
[codeHash]
|
||||
@ -53,8 +86,7 @@ export default function useAbi (initialValue: [string | null | undefined, Abi |
|
||||
|
||||
const onRemoveAbi = useCallback(
|
||||
(): void => {
|
||||
setAbi([null, null, false, false, null]);
|
||||
setError([false, null]);
|
||||
setAbi(EMPTY);
|
||||
|
||||
codeHash && store.saveCode(codeHash, { abi: null });
|
||||
},
|
||||
@ -62,13 +94,7 @@ export default function useAbi (initialValue: [string | null | undefined, Abi |
|
||||
);
|
||||
|
||||
return {
|
||||
abi: abi || null,
|
||||
abiName,
|
||||
contractAbi: contractAbi || null,
|
||||
errorText,
|
||||
isAbiError,
|
||||
isAbiSupplied,
|
||||
isAbiValid,
|
||||
...state,
|
||||
onChangeAbi,
|
||||
onRemoveAbi
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user