@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
98 lines (97 loc) • 5.14 kB
JavaScript
"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 react_1 = require("react");
const utils_1 = require("@selfcommunity/utils");
const Errors_1 = require("../../constants/Errors");
const Status_1 = tslib_1.__importDefault(require("./Status"));
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 constants_1 = require("./constants");
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.registration'
},
{
id: 'ui.editCourse.tab.users.table.header.latestActivity'
},
{}
];
function Requests(props) {
// PROPS
const { course, endpointQueryParams = {
limit: 6,
offset: Pagination_1.DEFAULT_PAGINATION_OFFSET
}, handleTabChange } = props;
// STATES
const [state, dispatch] = (0, react_1.useReducer)(widget_1.dataWidgetReducer, {
isLoadingPrevious: false,
isLoadingNext: false,
next: null,
cacheKey: react_core_1.SCCache.getWidgetStateCacheKey(react_core_1.SCCache.USERS_REQUESTS_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)();
// REFS
const updatedUsers = (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.getCourseWaitingApproval(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 handleRejectUser = (0, react_1.useCallback)((_msg, user) => {
dispatch({
type: widget_1.actionWidgetTypes.SET_RESULTS,
payload: { count: state.results.length - 1, results: state.results.filter((result) => result.id !== user.id) }
});
}, [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.REJECT_MEMBER}`, handleRejectUser);
return () => {
updatedUsers.current && pubsub_js_1.default.unsubscribe(updatedUsers.current);
};
}, [handleRejectUser]);
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.requests.title", defaultMessage: "ui.editCourse.tab.requests.title", values: { requestsNumber: state.results.length } }) })), (0, jsx_runtime_1.jsx)(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)(CourseUsersTable_1.default, { state: state, dispatch: dispatch, course: course, endpointSearch: {
url: () => api_services_1.Endpoints.GetCourseWaitingApproval.url({ id: course.id }),
method: api_services_1.Endpoints.GetCourseWaitingApproval.method
}, headerCells: headerCells, mode: course_1.SCCourseUsersTableModeType.REQUESTS, emptyStatusTitle: "ui.courseUsersTable.empty.requests.title" })] }));
}
exports.default = (0, react_1.memo)(Requests);