1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/common/FormTemplate/FormTemplate.tsx
olav 3959e846e8 refactor: fix misc TS errors (#729)
* refactor: update test deps

* refactor: remove unused ts-expect-error annotations

* refactor: add missing arg and return types

* refactor: the loading prop is optional

* refactor: add missing arg and return types

* reafactor: fix value arg type

* refactor: fix missing array type

* refactor: the parameters field is an array

* refactor: use undefined instead of null in state

* refactor: add missing params type

* refactor: add missing children prop

* refactor: add missing array type

* refactor: add missing React imports

* refactor: use correct IProjectEnvironment type

* refactor: type errors as unknown

* refactor: the index prop is required

* refactor: fix date prop type

* refactor: fix tooltip placement prop type

* refactor: fix environments state type

* refactor: add missing arg types

* refactor: add guard for undefined field

* refactor: fix ChangePassword prop types

* refactor: fix MUI import paths

* refactor: add missing arg type

* refactor: fix showDialog prop type

* refactor: remove unused openUpdateDialog prop

* refactor: add missing non-null assertion

* refactor: remove unused types prop

* refactor: stricten API error handler types

* refactor: add missing undefined check

* refactor: add missing IProject id field

* refactor: fix ConditionallyRender condition prop types

* refactor: remove unused args

* refactor: add AddVariant prop types

* refactor: add types to UIContext

* refactor: fix event arg type

* refactor: add missing default impressionData field

* refactor: fix handleDeleteEnvironment prop args

* refactor: fix IFeatureMetrics field requirements

* refactor: add missing element types to ConditionallyRender

* refactor: remove unused ProjectAccess projectId prop

* refactor: add missing undefined check

* refactor: fix getCreateTogglePath arg type

* refactor: add missing IStrategyPayload import

* refactor: remove unused user arg

* refactor: add missing event arg type

* refactor: add missing style object types

* refactor: improve userApiErrors prop type

* refactor: the Dialogue onClose prop is optional

* refactor: fix the AddonEvents setEventValue prop type
2022-02-25 10:55:39 +01:00

96 lines
3.1 KiB
TypeScript

import { useStyles } from './FormTemplate.styles';
import MenuBookIcon from '@material-ui/icons/MenuBook';
import Codebox from '../Codebox/Codebox';
import { IconButton, useMediaQuery } from '@material-ui/core';
import { FileCopy } from '@material-ui/icons';
import ConditionallyRender from '../ConditionallyRender';
import Loader from '../Loader/Loader';
import copy from 'copy-to-clipboard';
import useToast from '../../../hooks/useToast';
import React from 'react';
interface ICreateProps {
title: string;
description: string;
documentationLink: string;
loading?: boolean;
formatApiCode: () => string;
}
const FormTemplate: React.FC<ICreateProps> = ({
title,
description,
children,
documentationLink,
loading,
formatApiCode,
}) => {
const { setToastData } = useToast();
const styles = useStyles();
const smallScreen = useMediaQuery(`(max-width:${900}px)`);
const copyCommand = () => {
if (copy(formatApiCode())) {
setToastData({
title: 'Successfully copied the command',
text: 'The command should now be automatically copied to your clipboard',
autoHideDuration: 6000,
type: 'success',
show: true,
});
} else {
setToastData({
title: 'Could not copy the command',
text: 'Sorry, but we could not copy the command.',
autoHideDuration: 6000,
type: 'error',
show: true,
});
}
};
return (
<section className={styles.container}>
<aside className={styles.sidebar}>
<h2 className={styles.title}>{title}</h2>
<p className={styles.description}>{description}</p>
<div className={styles.linkContainer}>
<MenuBookIcon className={styles.linkIcon} />
<a
className={styles.documentationLink}
href={documentationLink}
rel="noopener noreferrer"
target="_blank"
>
Learn more
</a>
</div>
<ConditionallyRender
condition={!smallScreen}
show={
<>
<h3 className={styles.subtitle}>
API Command{' '}
<IconButton onClick={copyCommand}>
<FileCopy className={styles.icon} />
</IconButton>
</h3>
<Codebox text={formatApiCode()} />
</>
}
/>
</aside>
<div className={styles.formContent}>
<ConditionallyRender
condition={loading || false}
show={<Loader />}
elseShow={<>{children}</>}
/>{' '}
</div>
</section>
);
};
export default FormTemplate;