hooks#useChangeEffect TypeScript Examples
The following examples show how to use
hooks#useChangeEffect.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnumItem.tsx From gear-js with GNU General Public License v3.0 | 6 votes |
EnumItem = ({ title, levelName, typeStructure, renderNextItem }: PayloadItemProps) => {
const { name, value } = typeStructure;
const options = useMemo(() => Object.keys(value).map((key) => ({ value: key, label: key })), [value]);
const [, , helpers] = useField(levelName);
const [selected, setSelected] = useState(options[0].value);
const handleChange = (event: ChangeEvent<HTMLSelectElement>) => setSelected(event.target.value);
const itemLabel = getItemLabel(name, title);
const nextLevelName = getNextLevelName(levelName, selected);
useChangeEffect(() => {
//@ts-ignore
const parsedStructure = getPayloadValue(value[selected]);
helpers.setValue({ [selected]: parsedStructure }, false);
}, [selected]);
return (
<Fieldset legend={itemLabel} className={styles.fieldset}>
<Select options={options} className={styles.select} onChange={handleChange} />
{renderNextItem({
levelName: nextLevelName,
//@ts-ignore
typeStructure: value[selected],
})}
</Fieldset>
);
}
Example #2
Source File: OptionItem.tsx From gear-js with GNU General Public License v3.0 | 6 votes |
OptionItem = ({ title, levelName, typeStructure, renderNextItem }: PayloadItemProps) => {
const { name, value } = typeStructure;
const [, , helpers] = useField(levelName);
const [selected, setSelected] = useState(DEFAULT_VALUE);
const handleChange = (event: ChangeEvent<HTMLSelectElement>) => setSelected(event.target.value);
const parsedPayload = useMemo(() => getPayloadValue(value as any), [value]);
const isNone = selected === DEFAULT_VALUE;
const itemLabel = getItemLabel(name, title);
useChangeEffect(() => {
const fieldValue = isNone ? null : parsedPayload;
helpers.setValue(fieldValue, false);
}, [isNone]);
return (
<Fieldset legend={itemLabel} className={styles.fieldset}>
<Select options={OPTION_OPTIONS} className={styles.select} onChange={handleChange} />
{renderNextItem({
levelName,
//@ts-ignore
typeStructure: isNone ? null : value,
})}
</Fieldset>
);
}
Example #3
Source File: Messages.tsx From gear-js with GNU General Public License v3.0 | 5 votes |
Messages = () => {
const [searchParams, setSearchParams] = useSearchParams();
const { account } = useAccount();
const page = searchParams.has(URL_PARAMS.PAGE) ? Number(searchParams.get(URL_PARAMS.PAGE)) : 1;
const query = searchParams.has(URL_PARAMS.QUERY) ? String(searchParams.get(URL_PARAMS.QUERY)) : '';
const [messages, setMessages] = useState<MessageModel[]>([]);
const [messagesCount, setMessagesCount] = useState(0);
useChangeEffect(() => {
searchParams.set(URL_PARAMS.PAGE, String(1));
searchParams.set(URL_PARAMS.QUERY, '');
setSearchParams(searchParams);
}, [account]);
useEffect(() => {
if (account) {
const messageParams = {
destination: GearKeyring.decodeAddress(account.address),
limit: INITIAL_LIMIT_BY_PAGE,
offset: (page - 1) * INITIAL_LIMIT_BY_PAGE,
query,
};
getMessages(messageParams).then(({ result }) => {
setMessages(result.messages);
setMessagesCount(result.count);
});
}
}, [page, query, account]);
return (
<div className="messages">
<div className="pagination__wrapper">
<span className="pagination__wrapper-caption">Total results: {messagesCount || 0}</span>
<Pagination page={page} pagesAmount={messagesCount || 1} />
</div>
<SearchForm placeholder="Find message by ID" />
<MessagesList messages={messages} />
{messagesCount > 0 && (
<div className="pagination_bottom">
<Pagination page={page} pagesAmount={messagesCount || 1} />
</div>
)}
</div>
);
}
Example #4
Source File: Recent.tsx From gear-js with GNU General Public License v3.0 | 5 votes |
Recent = () => {
const [searchParams, setSearchParams] = useSearchParams();
const { account } = useAccount();
const page = searchParams.has(URL_PARAMS.PAGE) ? Number(searchParams.get(URL_PARAMS.PAGE)) : 1;
const query = searchParams.has(URL_PARAMS.QUERY) ? String(searchParams.get(URL_PARAMS.QUERY)) : '';
const [programs, setPrograms] = useState<ProgramModel[]>([]);
const [programsCount, setProgramsCount] = useState(0);
useChangeEffect(() => {
searchParams.set(URL_PARAMS.PAGE, String(1));
searchParams.set(URL_PARAMS.QUERY, '');
setSearchParams(searchParams);
}, [account]);
useEffect(() => {
if (account) {
const params = {
query,
owner: GearKeyring.decodeAddress(account.address),
limit: INITIAL_LIMIT_BY_PAGE,
offset: (page - 1) * INITIAL_LIMIT_BY_PAGE,
};
getUserPrograms(params).then(({ result }) => {
setPrograms(result.programs);
setProgramsCount(result.count);
});
}
}, [page, query, account]);
return (
<div className={styles.blockList}>
<div className={styles.paginationWrapper}>
<span>Total results: {programsCount || 0}</span>
<Pagination page={page} pagesAmount={programsCount || 1} />
</div>
<SearchForm placeholder="Find program" />
<ProgramsLegend />
<div>
{programs.map((program: ProgramModel) => (
<UserProgram key={program.id} program={program} />
))}
</div>
{programsCount > 0 && (
<div className={styles.paginationBottom}>
<Pagination page={page} pagesAmount={programsCount || 1} />
</div>
)}
</div>
);
}
Example #5
Source File: FormPayload.tsx From gear-js with GNU General Public License v3.0 | 4 votes |
FormPayload = (props: Props) => {
const { name, values } = props;
const alert = useAlert();
const [field, meta, { setValue }] = useField<PayloadValue>(name);
const jsonManualPayload = useRef<string>();
const [isManualView, setIsManualView] = useState(!values);
const [manualPayloadFile, setManualPayloadFile] = useState<File>();
const handleViewChange = () => setIsManualView((prevState) => !prevState);
const resetFileData = () => {
setManualPayloadFile(void 0);
jsonManualPayload.current = void 0;
};
const dropManualPayloadFile = () => {
resetFileData();
if (values) {
setValue(values.manualPayload, false);
}
};
const handleUploadManualPayload = async (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) {
return dropManualPayloadFile();
}
try {
if (!checkFileFormat(file, FILE_TYPES.JSON)) {
throw new Error('Wrong file format');
}
setManualPayloadFile(file);
const fileText = (await readTextFileAsync(file)) ?? '';
setValue(fileText);
jsonManualPayload.current = fileText;
} catch (error: unknown) {
alert.error((error as Error).message);
}
};
useEffect(() => {
if (!values) {
return;
}
const payloadValue = isManualView ? jsonManualPayload.current ?? values.manualPayload : values.payload;
setValue(payloadValue, false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isManualView]);
useChangeEffect(() => {
if (!values && manualPayloadFile) {
resetFileData();
}
setIsManualView(!values);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values]);
return (
<>
<div className={styles.formPayload}>
{values && (
<Checkbox
type="switch"
label="Manual input"
checked={isManualView}
className={styles.viewCheckbox}
onChange={handleViewChange}
/>
)}
{!isManualView && values ? (
<PayloadStructure levelName={name} typeStructure={values.typeStructure} />
) : (
<>
<Textarea
{...field}
id={name}
rows={15}
value={field.value as string}
placeholder="// Enter your payload here"
/>
{values && (
<FileInput
value={manualPayloadFile}
accept={FILE_TYPES.JSON}
className={styles.fileInput}
onChange={handleUploadManualPayload}
/>
)}
</>
)}
</div>
{meta.error && <div className={styles.error}>{meta.error}</div>}
</>
);
}