UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

136 lines (130 loc) 5.12 kB
import { __rest } from "tslib"; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { useContext, useRef, useState } from 'react'; import { styled } from '@mui/material/styles'; import { Alert } from '@mui/material'; import { SCUserContext } from '@selfcommunity/react-core'; import Icon from '@mui/material/Icon'; import { defineMessages, useIntl } from 'react-intl'; import classNames from 'classnames'; import { useThemeProps } from '@mui/system'; import { PREFIX } from './constants'; import { LoadingButton } from '@mui/lab'; import { GroupService } from '@selfcommunity/api-services'; import { SCOPE_SC_UI } from '../../constants/Errors'; import { Logger } from '@selfcommunity/utils'; const classes = { root: `${PREFIX}-root` }; const Root = styled(LoadingButton, { name: PREFIX, slot: 'Root' })(() => ({})); const messages = defineMessages({ errorLoadImage: { id: 'ui.changeGroupCover.button.change.alertErrorImage', defaultMessage: 'ui.changeGroupCover.button.change.alertErrorImage' }, errorImageSize: { id: 'ui.changeGroupCover.alert', defaultMessage: 'ui.changeGroupCover.alert' } }); /** * > API documentation for the Community-JS Change Group Cover component. Learn about the available props and the CSS API. * * * This component renders a button that allows admins to edit the group cover and a popover that specifies format and sizes allowed. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/ChangeGroupCover) #### Import ```jsx import {ChangeGroupCover} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCChangeGroupCoverButton` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCChangeGroupCoverButton-root|Styles applied to the root element.| * @param inProps */ export default function ChangeGroupCover(inProps) { //PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { groupId, onChange, autoHide, className, isCreationMode = false } = props, rest = __rest(props, ["groupId", "onChange", "autoHide", "className", "isCreationMode"]); //CONTEXT const scUserContext = useContext(SCUserContext); //STATE let fileInput = useRef(null); const [loading, setLoading] = useState(false); const [alert, setAlert] = useState(null); // INTL const intl = useIntl(); // Anonymous if (!scUserContext.user) { return null; } /** * Handles file upload * @param event */ const handleUpload = (event) => { fileInput = event.target.files[0]; if (fileInput) { const reader = new FileReader(); reader.onload = (e) => { const img = new Image(); img.onload = () => { isCreationMode ? onChange && onChange(fileInput) : handleSave(); // if (img.width < 1920) { // setAlert(intl.formatMessage(messages.errorImageSize)); // } else { // isCreationMode ? onChange && onChange(fileInput) : handleSave(); // } }; // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore img.src = e.target.result; }; // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore reader.readAsDataURL(fileInput); } }; /** * Handles cover saving after upload action */ function handleSave() { setLoading(true); const formData = new FormData(); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore formData.append('emotional_image_original', fileInput); GroupService.changeGroupAvatarOrCover(groupId, formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then((data) => { onChange && onChange(data.emotional_image); setLoading(false); }) .catch((error) => { Logger.error(SCOPE_SC_UI, error); setLoading(false); setAlert(intl.formatMessage(messages.errorLoadImage)); }); } /** * If there is an error */ if (alert) { return (_jsx(Alert, Object.assign({ color: "error", onClose: () => setAlert(null) }, { children: alert }))); } /** * Renders root object (if not hidden by autoHide prop) */ if (!autoHide) { return (_jsxs(_Fragment, { children: [_jsx("input", { type: "file", onChange: handleUpload, ref: fileInput, hidden: true, accept: ".gif,.png,.jpg,.jpeg" }), _jsx(Root, Object.assign({ className: classNames(classes.root, className), size: "medium", variant: "contained", disabled: loading, onClick: () => fileInput.current.click(), loading: loading }, rest, { children: _jsx(Icon, { children: "image" }) }))] })); } return null; }