UNPKG

@selfcommunity/react-ui

Version:

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

141 lines (140 loc) • 8.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const material_1 = require("@mui/material"); const react_intl_1 = require("react-intl"); const AddUsersButton_1 = tslib_1.__importDefault(require("../../shared/AddUsersButton")); const react_1 = require("react"); const types_1 = require("@selfcommunity/types"); const utils_1 = require("@selfcommunity/utils"); const Errors_1 = require("../../constants/Errors"); const notistack_1 = require("notistack"); const Status_1 = tslib_1.__importDefault(require("./Status")); const constants_1 = require("./constants"); const CourseUsersTable_1 = tslib_1.__importDefault(require("../../shared/CourseUsersTable")); const Pagination_1 = require("../../constants/Pagination"); const react_core_1 = require("@selfcommunity/react-core"); const widget_1 = require("../../utils/widget"); const api_services_1 = require("@selfcommunity/api-services"); const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js")); const PubSub_1 = require("../../constants/PubSub"); const course_1 = require("../../types/course"); const classes = { usersStatusWrapper: `${constants_1.PREFIX}-users-status-wrapper`, contrastColor: `${constants_1.PREFIX}-contrast-color` }; const headerCells = [ { id: 'ui.editCourse.tab.users.table.header.name' }, { id: 'ui.editCourse.tab.users.table.header.role' }, { id: 'ui.editCourse.tab.users.table.header.registration' }, { id: 'ui.editCourse.tab.users.table.header.latestActivity' }, {} ]; function Users(props) { // PROPS const { course, endpointQueryParams = { limit: 6, offset: Pagination_1.DEFAULT_PAGINATION_OFFSET, statuses: JSON.stringify([types_1.SCCourseJoinStatusType.JOINED, types_1.SCCourseJoinStatusType.MANAGER, types_1.SCCourseJoinStatusType.CREATOR]) }, handleTabChange } = props; // STATES const [state, dispatch] = (0, react_1.useReducer)(widget_1.dataWidgetReducer, { isLoadingNext: false, next: null, cacheKey: react_core_1.SCCache.getWidgetStateCacheKey(react_core_1.SCCache.USERS_PARTECIPANTS_COURSES_STATE_CACHE_PREFIX_KEY, course.id), cacheStrategy: utils_1.CacheStrategies.NETWORK_ONLY, visibleItems: endpointQueryParams.limit }, widget_1.stateWidgetInitializer); // CONTEXTS const scUserContext = (0, react_core_1.useSCUser)(); // HOOKS const { enqueueSnackbar } = (0, notistack_1.useSnackbar)(); // REFS const updatedUsers = (0, react_1.useRef)(null); const removedUsers = (0, react_1.useRef)(null); // CALLBACKS const _init = (0, react_1.useCallback)(() => { if (!state.initialized && !state.isLoadingNext) { dispatch({ type: widget_1.actionWidgetTypes.LOADING_NEXT }); api_services_1.CourseService.getCourseDashboardUsers(course.id, Object.assign({}, endpointQueryParams)) .then((payload) => { dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_SUCCESS, payload: Object.assign(Object.assign({}, payload), { initialized: true }) }); }) .catch((error) => { dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_FAILURE, payload: { errorLoadNext: error } }); utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error); }); } }, [state.isLoadingNext, state.initialized, course, dispatch, endpointQueryParams]); // HANDLERS const handleAddUser = (0, react_1.useCallback)((_msg, user) => { dispatch({ type: widget_1.actionWidgetTypes.LOAD_PREVIOUS_SUCCESS, payload: { count: state.count + 1, results: [user], initialized: true } }); }, [state.count, dispatch]); const handleRemoveUser = (0, react_1.useCallback)((_msg, user) => { dispatch({ type: widget_1.actionWidgetTypes.SET_RESULTS, payload: { count: state.count - 1, results: state.results.filter((result) => result.id !== user.id), initialized: true } }); }, [state.count, state.results, dispatch]); // EFFECTS (0, react_1.useEffect)(() => { let _t; if (scUserContext.user) { _t = setTimeout(_init); return () => { clearTimeout(_t); }; } }, [scUserContext.user, _init]); (0, react_1.useEffect)(() => { updatedUsers.current = pubsub_js_1.default.subscribe(`${PubSub_1.SCTopicType.COURSE}.${PubSub_1.SCCourseEventType.ADD_MEMBER}`, handleAddUser); removedUsers.current = pubsub_js_1.default.subscribe(`${PubSub_1.SCTopicType.COURSE}.${PubSub_1.SCCourseEventType.REMOVE_MEMBER}`, handleRemoveUser); return () => { updatedUsers.current && pubsub_js_1.default.unsubscribe(updatedUsers.current); removedUsers.current && pubsub_js_1.default.unsubscribe(removedUsers.current); }; }, [handleAddUser, handleRemoveUser]); const handleConfirm = (0, react_1.useCallback)((newUsers) => { const data = { joined: newUsers.map((user) => user.id) }; api_services_1.CourseService.changeCourseUserRole(course.id, data) .then(() => { dispatch({ type: widget_1.actionWidgetTypes.LOAD_PREVIOUS_SUCCESS, payload: { count: state.count + newUsers.length, results: newUsers, initialized: true } }); enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.editCourse.tab.users.table.snackbar.success", defaultMessage: "ui.editCourse.tab.users.table.snackbar.success" }), { variant: 'success', autoHideDuration: 3000 }); }) .catch((error) => { dispatch({ type: widget_1.actionWidgetTypes.LOAD_NEXT_FAILURE, payload: { errorLoadNext: error } }); utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error); enqueueSnackbar((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.common.error.action", defaultMessage: "ui.common.error.action" }), { variant: 'error', autoHideDuration: 3000 }); }); }, [course, dispatch]); return ((0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h6", className: classes.contrastColor }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.editCourse.tab.users.title", defaultMessage: "ui.editCourse.tab.users.title", values: { usersNumber: state.count } }) })), (0, jsx_runtime_1.jsxs)(material_1.Stack, Object.assign({ className: classes.usersStatusWrapper }, { children: [(0, jsx_runtime_1.jsx)(Status_1.default, { course: course, handleTabChange: handleTabChange }), (0, jsx_runtime_1.jsx)(AddUsersButton_1.default, { label: "ui.editCourse.tab.users.addUsersButton.label", endpoint: { url: () => api_services_1.Endpoints.GetCourseSuggestedUsers.url({ id: course.id }), method: api_services_1.Endpoints.GetCourseSuggestedUsers.method }, onConfirm: handleConfirm })] })), (0, jsx_runtime_1.jsx)(CourseUsersTable_1.default, { state: state, dispatch: dispatch, course: course, endpointSearch: { url: () => api_services_1.Endpoints.GetCourseDashboardUsers.url({ id: course.id }), method: api_services_1.Endpoints.GetCourseDashboardUsers.method }, endpointQueryParamsSearch: { statuses: JSON.stringify([types_1.SCCourseJoinStatusType.JOINED, types_1.SCCourseJoinStatusType.MANAGER, types_1.SCCourseJoinStatusType.CREATOR]) }, headerCells: headerCells, mode: course_1.SCCourseUsersTableModeType.EDIT, emptyStatusTitle: "ui.courseUsersTable.empty.users.title", emptyStatusDescription: "ui.courseUsersTable.empty.users.description" })] })); } exports.default = (0, react_1.memo)(Users);