UNPKG

@selfcommunity/react-ui

Version:

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

134 lines (126 loc) 4.93 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 Icon from '@mui/material/Icon'; import { SCUserContext } from '@selfcommunity/react-core'; import classNames from 'classnames'; import { useThemeProps } from '@mui/system'; import { PREFIX } from './constants'; import { SCOPE_SC_UI } from '../../constants/Errors'; import { GroupService } from '@selfcommunity/api-services'; import { Logger } from '@selfcommunity/utils'; import { defineMessages, useIntl } from 'react-intl'; import { LoadingButton } from '@mui/lab'; const messages = defineMessages({ errorLoadImage: { id: 'ui.changeGroupPicture.alert', defaultMessage: 'ui.changeGroupPicture.alert' } }); const classes = { root: `${PREFIX}-root` }; const Root = styled(LoadingButton, { name: PREFIX, slot: 'Root' })(() => ({})); /** * > API documentation for the Community-JS Change Group Picture component. Learn about the available props and the CSS API. * * * This component renders a button that allows admins to manage their group pictures. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/ChangeGroupPicture) #### Import ```jsx import {ChangeGroupPicture} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCChangeGroupPictureButton` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCChangeGroupPictureButton-root|Styles applied to the root element.| * @param inProps */ export default function ChangeGroupPicture(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 avatar 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 < 600 && img.height < 600) { // setAlert(intl.formatMessage(messages.errorLoadImage)); // } 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); } }; /** * Performs save avatar after upload */ function handleSave() { setLoading(true); const formData = new FormData(); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore formData.append('image_original', fileInput); GroupService.changeGroupAvatarOrCover(groupId, formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then((data) => { onChange && onChange(data.image_big); 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 the component (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: "small", variant: "contained", disabled: loading, onClick: () => fileInput.current.click(), loading: loading }, rest, { children: _jsx(Icon, { children: "photo_camera" }) }))] })); } return null; }