2020-08-20 15:35:46 +05:00
|
|
|
import { dequal } from 'dequal';
|
2020-05-29 19:31:19 +05:00
|
|
|
import upperFirst from 'lodash/upperFirst';
|
|
|
|
import camelCase from 'lodash/camelCase';
|
2020-05-26 00:46:04 +05:00
|
|
|
import React, { useCallback, useEffect, useRef } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-05-29 19:31:19 +05:00
|
|
|
import classNames from 'classnames';
|
2020-05-26 00:46:04 +05:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2020-05-29 19:31:19 +05:00
|
|
|
import { Button, Image } from 'semantic-ui-react';
|
2020-05-26 00:46:04 +05:00
|
|
|
import { FilePicker, Popup } from '../../../lib/custom-ui';
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
import ProjectBackgroundGradients from '../../../constants/ProjectBackgroundGradients';
|
2020-06-03 22:27:20 +05:00
|
|
|
import { ProjectBackgroundTypes } from '../../../constants/Enums';
|
2020-05-29 19:31:19 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
import styles from './BackgroundEditStep.module.scss';
|
2020-05-29 19:31:19 +05:00
|
|
|
import globalStyles from '../../../styles.module.scss';
|
2020-05-26 00:46:04 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
const BackgroundEditStep = React.memo(
|
2020-05-29 19:31:19 +05:00
|
|
|
({
|
|
|
|
defaultValue,
|
|
|
|
imageCoverUrl,
|
|
|
|
isImageUpdating,
|
|
|
|
onUpdate,
|
|
|
|
onImageUpdate,
|
|
|
|
onImageDelete,
|
|
|
|
onBack,
|
|
|
|
}) => {
|
2020-05-26 00:46:04 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
|
|
|
|
const field = useRef(null);
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
const handleGradientClick = useCallback(
|
|
|
|
(_, { value }) => {
|
|
|
|
const background = {
|
2020-06-03 22:27:20 +05:00
|
|
|
type: ProjectBackgroundTypes.GRADIENT,
|
2020-05-29 19:31:19 +05:00
|
|
|
name: value,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!dequal(background, defaultValue)) {
|
2020-06-03 22:27:20 +05:00
|
|
|
onUpdate(background);
|
2020-05-29 19:31:19 +05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[defaultValue, onUpdate],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleImageClick = useCallback(() => {
|
|
|
|
const background = {
|
2020-06-03 22:27:20 +05:00
|
|
|
type: ProjectBackgroundTypes.IMAGE,
|
2020-05-29 19:31:19 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!dequal(background, defaultValue)) {
|
|
|
|
onUpdate(background);
|
|
|
|
}
|
|
|
|
}, [defaultValue, onUpdate]);
|
|
|
|
|
2020-05-26 00:46:04 +05:00
|
|
|
const handleFileSelect = useCallback(
|
|
|
|
(file) => {
|
|
|
|
onImageUpdate({
|
|
|
|
file,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[onImageUpdate],
|
|
|
|
);
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
const handleDeleteImageClick = useCallback(() => {
|
|
|
|
onImageDelete();
|
|
|
|
}, [onImageDelete]);
|
|
|
|
|
|
|
|
const handleRemoveClick = useCallback(() => {
|
|
|
|
onUpdate(null);
|
|
|
|
}, [onUpdate]);
|
|
|
|
|
2020-05-26 00:46:04 +05:00
|
|
|
useEffect(() => {
|
|
|
|
field.current.focus();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Popup.Header onBack={onBack}>
|
|
|
|
{t('common.editBackground', {
|
|
|
|
context: 'title',
|
|
|
|
})}
|
|
|
|
</Popup.Header>
|
|
|
|
<Popup.Content>
|
2020-05-29 19:31:19 +05:00
|
|
|
<div className={styles.gradientButtons}>
|
|
|
|
{ProjectBackgroundGradients.map((gradient) => (
|
|
|
|
<Button
|
|
|
|
key={gradient}
|
|
|
|
type="button"
|
|
|
|
name="gradient"
|
|
|
|
value={gradient}
|
|
|
|
className={classNames(
|
|
|
|
styles.gradientButton,
|
|
|
|
defaultValue &&
|
2020-06-03 22:27:20 +05:00
|
|
|
defaultValue.type === ProjectBackgroundTypes.GRADIENT &&
|
2020-05-29 19:31:19 +05:00
|
|
|
gradient === defaultValue.name &&
|
|
|
|
styles.gradientButtonActive,
|
|
|
|
globalStyles[`background${upperFirst(camelCase(gradient))}`],
|
|
|
|
)}
|
|
|
|
onClick={handleGradientClick}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
{imageCoverUrl && (
|
|
|
|
/* TODO: wrap in button */
|
|
|
|
<Image
|
|
|
|
src={imageCoverUrl}
|
|
|
|
label={
|
|
|
|
defaultValue &&
|
|
|
|
defaultValue.type === 'image' && {
|
|
|
|
corner: 'left',
|
|
|
|
size: 'small',
|
|
|
|
icon: {
|
|
|
|
name: 'star',
|
|
|
|
color: 'grey',
|
|
|
|
inverted: true,
|
|
|
|
},
|
|
|
|
className: styles.imageLabel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
className={styles.image}
|
|
|
|
onClick={handleImageClick}
|
|
|
|
/>
|
|
|
|
)}
|
2020-05-26 00:46:04 +05:00
|
|
|
<div className={styles.action}>
|
|
|
|
<FilePicker accept="image/*" onSelect={handleFileSelect}>
|
|
|
|
<Button
|
|
|
|
ref={field}
|
2020-05-29 19:31:19 +05:00
|
|
|
content={t('action.uploadNewImage')}
|
2020-05-26 00:46:04 +05:00
|
|
|
loading={isImageUpdating}
|
|
|
|
disabled={isImageUpdating}
|
|
|
|
className={styles.actionButton}
|
|
|
|
/>
|
|
|
|
</FilePicker>
|
|
|
|
</div>
|
2020-05-29 19:31:19 +05:00
|
|
|
{imageCoverUrl && (
|
|
|
|
<div className={styles.action}>
|
|
|
|
<Button
|
|
|
|
content={t('action.deleteImage')}
|
|
|
|
disabled={isImageUpdating}
|
|
|
|
className={styles.actionButton}
|
|
|
|
onClick={handleDeleteImageClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-05-26 00:46:04 +05:00
|
|
|
{defaultValue && (
|
2020-05-29 19:31:19 +05:00
|
|
|
<div className={styles.action}>
|
|
|
|
<Button
|
|
|
|
content={t('action.removeBackground')}
|
|
|
|
disabled={isImageUpdating}
|
|
|
|
className={styles.actionButton}
|
|
|
|
onClick={handleRemoveClick}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-05-26 00:46:04 +05:00
|
|
|
)}
|
|
|
|
</Popup.Content>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
BackgroundEditStep.propTypes = {
|
2020-05-26 00:46:04 +05:00
|
|
|
defaultValue: PropTypes.object, // eslint-disable-line react/forbid-prop-types
|
2020-05-29 19:31:19 +05:00
|
|
|
imageCoverUrl: PropTypes.string,
|
2020-05-26 00:46:04 +05:00
|
|
|
isImageUpdating: PropTypes.bool.isRequired,
|
2020-05-29 19:31:19 +05:00
|
|
|
onUpdate: PropTypes.func.isRequired,
|
2020-05-26 00:46:04 +05:00
|
|
|
onImageUpdate: PropTypes.func.isRequired,
|
|
|
|
onImageDelete: PropTypes.func.isRequired,
|
|
|
|
onBack: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
BackgroundEditStep.defaultProps = {
|
2020-05-26 00:46:04 +05:00
|
|
|
defaultValue: undefined,
|
2020-05-29 19:31:19 +05:00
|
|
|
imageCoverUrl: undefined,
|
2020-05-26 00:46:04 +05:00
|
|
|
};
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
export default BackgroundEditStep;
|