UNPKG

@selfcommunity/react-ui

Version:

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

192 lines (191 loc) • 14.8 kB
import { __awaiter } from "tslib"; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { styled } from '@mui/material/styles'; import Box from '@mui/material/Box'; import { useThemeProps } from '@mui/system'; import classNames from 'classnames'; import { Preferences } from '@selfcommunity/react-core'; import { PREFIX } from '../../constants'; import { Button, Drawer, IconButton, Tab, Tabs, TextField, Typography } from '@mui/material'; import { MuiColorInput } from 'mui-color-input'; import { PreferenceService } from '@selfcommunity/api-services'; import { SCPreferenceSection } from '@selfcommunity/types'; import { formatColorLabel, formatLogoLabel } from '../../../../utils/onBoarding'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import Icon from '@mui/material/Icon'; import { SCOPE_SC_UI } from '../../../../constants/Errors'; import { Logger } from '@selfcommunity/utils'; import { LoadingButton } from '@mui/lab'; import ScrollContainer from '../../../../shared/ScrollContainer'; const messages = defineMessages({ titleField: { id: 'ui.onBoardingWidget.step.appearance.titleSlogan.field.title', defaultMessage: 'ui.onBoardingWidget.step.appearance.titleSlogan.field.title' }, sloganField: { id: 'ui.onBoardingWidget.step.appearance.titleSlogan.field.slogan', defaultMessage: 'ui.onBoardingWidget.step.appearance.titleSlogan.field.slogan' } }); const classes = { root: `${PREFIX}-appearance-root`, title: `${PREFIX}-appearance-title`, summary: `${PREFIX}-appearance-summary`, colorContainer: `${PREFIX}-appearance-color-container`, color: `${PREFIX}-appearance-color`, colorProgress: `${PREFIX}-appearance-color-progress`, logoContainer: `${PREFIX}-appearance-logo-container`, logo: `${PREFIX}-appearance-logo`, uploadButton: `${PREFIX}-appearance-upload-button`, drawerRoot: `${PREFIX}-appearance-drawer-root`, drawerHeader: `${PREFIX}-appearance-drawer-header`, drawerHeaderAction: `${PREFIX}-appearance-drawer-header-action`, drawerContent: `${PREFIX}-appearance-drawer-content` }; var AppearanceTabType; (function (AppearanceTabType) { AppearanceTabType["COLOR"] = "color"; AppearanceTabType["LOGO"] = "logo"; AppearanceTabType["SLOGAN"] = "slogan"; })(AppearanceTabType || (AppearanceTabType = {})); const Root = styled(Box, { name: PREFIX, slot: 'AppearanceRoot' })(() => ({})); const DrawerRoot = styled(Drawer, { name: PREFIX, slot: 'AppearanceDrawerRoot', overridesResolver: (props, styles) => styles.appearanceDrawerRoot })(({ theme }) => ({})); export default function Appearance(inProps) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; // PROPS const props = useThemeProps({ props: inProps, name: PREFIX }); const { className, onCompleteAction } = props; // STATE const [preferences, setPreferences] = useState([]); const [data, setData] = useState({}); const [loading, setLoading] = useState(true); const [loadingLogo, setLoadingLogo] = useState(''); const [anchorEl, setAnchorEl] = useState(null); const [tab, setTab] = useState(AppearanceTabType.COLOR); const [updating, setUpdating] = useState(false); const colorRef = useRef(null); // INTL const intl = useIntl(); // HANDLERS const handleTabChange = (event, newValue) => { setTab(newValue); setData({}); }; const handleOpen = useCallback((event) => { setAnchorEl(event.currentTarget); }, []); const handleClose = useCallback(() => { setAnchorEl(null); }, []); const handleClosePopover = () => { if (colorRef.current) { colorRef.current.blur(); } }; const handleChange = (event) => { const { name, value } = event.target; setPreferences((prev) => { return prev.map((p) => Object.assign({}, p, { value: p.name === name ? value : p.value })); }); handleDataUpdate(name, value); }; const handleColorChange = (newColor, name) => { setPreferences((prev) => { return prev.map((p) => Object.assign({}, p, { value: p.name === name ? newColor : p.value })); }); handleDataUpdate(name, newColor); }; const handleDataUpdate = (key, value) => { const elementInDict = preferences.filter((p) => p.name === key && p.value === value); if (elementInDict.length) { const newData = Object.assign({}, data); delete newData[key]; setData(newData); } else { setData((prevData) => (Object.assign(Object.assign({}, prevData), { [key]: value }))); } }; const fetchPreferences = () => { PreferenceService.searchPreferences('', '', `${Preferences.COLORS_COLORBACK},${Preferences.COLORS_COLORPRIMARY},${Preferences.COLORS_COLORSECONDARY},${Preferences.COLORS_NAVBARBACK},${Preferences.COLORS_COLORFONT},${Preferences.COLORS_COLORFONTSECONDARY}, ${Preferences.LOGO_NAVBAR_LOGO},${Preferences.LOGO_NAVBAR_LOGO_MOBILE}, ${Preferences.TEXT_APPLICATION_SLOGAN1},${Preferences.TEXT_APPLICATION_SLOGAN2}`) .then((res) => { setPreferences(res.results); setLoading(false); }) .catch((e) => { Logger.error(SCOPE_SC_UI, e); setLoading(false); }); }; const updatePreference = () => __awaiter(this, void 0, void 0, function* () { setUpdating(true); try { yield PreferenceService.updatePreferences(data); } catch (e) { Logger.error(SCOPE_SC_UI, e); } finally { setUpdating(false); setData({}); onCompleteAction(); } }); const updateLogoPreference = (name, file) => __awaiter(this, void 0, void 0, function* () { setLoadingLogo(name); const formData = new FormData(); formData.append(name, file); yield PreferenceService.updatePreferences(formData) .then((preference) => { setLoadingLogo(''); setData({}); setPreferences((prev) => { return prev.map((p) => Object.assign({}, p, { value: p.name === name ? preference[name].value : p.value })); }); onCompleteAction(); }) .catch((e) => { setLoadingLogo(''); setData({}); Logger.error(SCOPE_SC_UI, e); }); }); /** * Handles logo upload * @param event * @param name */ const handleUpload = (event, name) => { const file = event.target.files[0]; if (file) { updateLogoPreference(name, file); } }; useEffect(() => { fetchPreferences(); }, []); return (_jsxs(Root, Object.assign({ className: classNames(classes.root, className) }, { children: [_jsx(Typography, Object.assign({ variant: "h4", className: classes.title, alignSelf: "self-start" }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.appearance", defaultMessage: "ui.onBoardingWidget.appearance" }) })), _jsx(Typography, Object.assign({ className: classes.summary }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.summary", defaultMessage: "ui.onBoardingWidget.step.appearance.summary" }) })), _jsx(Button, Object.assign({ variant: "outlined", size: "small", color: "primary", onClick: handleOpen }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.button", defaultMessage: "ui.onBoardingWidget.step.appearance.button" }) })), _jsxs(DrawerRoot, Object.assign({ className: classes.drawerRoot, anchor: "right", open: Boolean(anchorEl), onClose: handleClose }, { children: [_jsxs(Box, Object.assign({ className: classes.drawerHeader }, { children: [_jsx(Typography, Object.assign({ variant: "h4", color: "primary" }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.header.title", defaultMessage: "ui.onBoardingWidget.step.appearance.header.title" }) })), _jsx(IconButton, Object.assign({ className: classes.drawerHeaderAction, onClick: handleClose }, { children: _jsx(Icon, { children: "close" }) }))] })), _jsxs(Tabs, Object.assign({ value: tab, onChange: handleTabChange, variant: "scrollable", scrollButtons: "auto", "aria-label": "scrollable-tabs" }, { children: [_jsx(Tab, { value: AppearanceTabType.COLOR, label: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.colors.title", defaultMessage: "ui.onBoardingWidget.step.appearance.colors.title" }) }), _jsx(Tab, { value: AppearanceTabType.LOGO, label: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.logo.title", defaultMessage: "ui.onBoardingWidget.step.appearance.logo.title" }) }), _jsx(Tab, { value: AppearanceTabType.SLOGAN, label: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.titleSlogan.title", defaultMessage: "ui.onBoardingWidget.step.appearance.titleSlogan.title" }) })] })), _jsx(ScrollContainer, { children: _jsxs(Box, Object.assign({ className: classes.drawerContent }, { children: [tab === AppearanceTabType.COLOR && (_jsxs(Box, Object.assign({ className: classes.colorContainer }, { children: [preferences .filter((item) => item.section === SCPreferenceSection.COLORS) .map((color) => (_jsxs(React.Fragment, { children: [_jsx(Typography, Object.assign({ variant: "h6" }, { children: formatColorLabel(color) })), _jsx(Box, { children: _jsx(MuiColorInput, { inputRef: colorRef, className: classes.color, format: "hex", value: color.value, onChange: (value) => handleColorChange(value, color.name), isAlphaHidden: true, PopoverProps: { onClose: handleClosePopover } }) })] }, color.id))), _jsx(LoadingButton, Object.assign({ loading: loading || updating, disabled: loading || updating || Object.keys(data).length === 0, variant: "outlined", size: "small", color: "primary", onClick: updatePreference }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.titleSlogan.button", defaultMessage: "ui.onBoardingWidget.step.appearance.titleSlogan.button" }) }))] }))), tab === AppearanceTabType.LOGO && (_jsx(_Fragment, { children: preferences .filter((item) => item.section === SCPreferenceSection.LOGO) .map((logo) => (_jsxs(React.Fragment, { children: [_jsx(Typography, Object.assign({ variant: "h6" }, { children: formatLogoLabel(logo.name) })), _jsxs(Box, Object.assign({ className: classes.logoContainer }, { children: [_jsx("img", { src: logo.value, className: classes.logo }), _jsx("input", { type: "file", onChange: (event) => handleUpload(event, logo.name), hidden: true, accept: ".gif,.png,.jpg,.jpeg", id: logo.name }), _jsx(LoadingButton, Object.assign({ className: classes.uploadButton, onClick: () => document.getElementById(`${logo.name}`).click(), loading: Boolean(loadingLogo) && Boolean(logo.name === loadingLogo), disabled: Boolean(loadingLogo) && Boolean(logo.name !== loadingLogo) }, { children: _jsx(Icon, { children: "upload" }) }))] }))] }, logo.id))) })), tab === AppearanceTabType.SLOGAN && (_jsxs(Box, { children: [_jsx(TextField, { multiline: true, fullWidth: true, label: `${intl.formatMessage(messages.titleField)}`, margin: "normal", value: ((_a = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan1')) === null || _a === void 0 ? void 0 : _a.value) || '', name: "application_slogan1", onChange: handleChange, InputProps: { endAdornment: (_jsx(Typography, Object.assign({ variant: "body2" }, { children: ((_c = (_b = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan1')) === null || _b === void 0 ? void 0 : _b.value) === null || _c === void 0 ? void 0 : _c.length) ? 50 - ((_d = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan1')) === null || _d === void 0 ? void 0 : _d.value.length) : 50 }))) }, error: Boolean(((_f = (_e = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan1')) === null || _e === void 0 ? void 0 : _e.value) === null || _f === void 0 ? void 0 : _f.length) > 50) }), _jsx(TextField, { multiline: true, fullWidth: true, label: `${intl.formatMessage(messages.sloganField)}`, margin: "normal", value: ((_g = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan2')) === null || _g === void 0 ? void 0 : _g.value) || '', name: "application_slogan2", onChange: handleChange, InputProps: { endAdornment: (_jsx(Typography, Object.assign({ variant: "body2" }, { children: ((_j = (_h = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan2')) === null || _h === void 0 ? void 0 : _h.value) === null || _j === void 0 ? void 0 : _j.length) ? 150 - ((_k = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan2')) === null || _k === void 0 ? void 0 : _k.value.length) : 150 }))) }, error: Boolean(((_m = (_l = preferences === null || preferences === void 0 ? void 0 : preferences.find((item) => item.section === 'text' && item.name === 'application_slogan2')) === null || _l === void 0 ? void 0 : _l.value) === null || _m === void 0 ? void 0 : _m.length) > 150) }), _jsx(LoadingButton, Object.assign({ loading: updating, disabled: updating || Object.keys(data).length === 0, variant: "outlined", size: "small", color: "primary", onClick: updatePreference }, { children: _jsx(FormattedMessage, { id: "ui.onBoardingWidget.step.appearance.titleSlogan.button", defaultMessage: "ui.onBoardingWidget.step.appearance.titleSlogan.button" }) }))] }))] })) })] }))] }))); }