UNPKG

@selfcommunity/react-ui

Version:

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

137 lines (129 loc) 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const styles_1 = require("@mui/material/styles"); const material_1 = require("@mui/material"); const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon")); const react_core_1 = require("@selfcommunity/react-core"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const system_1 = require("@mui/system"); const constants_1 = require("./constants"); const Errors_1 = require("../../constants/Errors"); const api_services_1 = require("@selfcommunity/api-services"); const utils_1 = require("@selfcommunity/utils"); const react_intl_1 = require("react-intl"); const lab_1 = require("@mui/lab"); const messages = (0, react_intl_1.defineMessages)({ errorLoadImage: { id: 'ui.changeGroupPicture.alert', defaultMessage: 'ui.changeGroupPicture.alert' } }); const classes = { root: `${constants_1.PREFIX}-root` }; const Root = (0, styles_1.styled)(lab_1.LoadingButton, { name: constants_1.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 */ function ChangeGroupPicture(inProps) { //PROPS const props = (0, system_1.useThemeProps)({ props: inProps, name: constants_1.PREFIX }); const { groupId, onChange, autoHide, className, isCreationMode = false } = props, rest = tslib_1.__rest(props, ["groupId", "onChange", "autoHide", "className", "isCreationMode"]); //CONTEXT const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext); //STATE let fileInput = (0, react_1.useRef)(null); const [loading, setLoading] = (0, react_1.useState)(false); const [alert, setAlert] = (0, react_1.useState)(null); // INTL const intl = (0, react_intl_1.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); api_services_1.GroupService.changeGroupAvatarOrCover(groupId, formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then((data) => { onChange && onChange(data.image_big); setLoading(false); }) .catch((error) => { utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error); setLoading(false); setAlert(intl.formatMessage(messages.errorLoadImage)); }); } /** * If there is an error */ if (alert) { return ((0, jsx_runtime_1.jsx)(material_1.Alert, Object.assign({ color: "error", onClose: () => setAlert(null) }, { children: alert }))); } /** * Renders the component (if not hidden by autoHide prop) */ if (!autoHide) { return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("input", { type: "file", onChange: handleUpload, ref: fileInput, hidden: true, accept: ".gif,.png,.jpg,.jpeg" }), (0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className), size: "small", variant: "contained", disabled: loading, onClick: () => fileInput.current.click(), loading: loading }, rest, { children: (0, jsx_runtime_1.jsx)(Icon_1.default, { children: "photo_camera" }) }))] })); } return null; } exports.default = ChangeGroupPicture;