@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
72 lines (71 loc) • 3.43 kB
JavaScript
;
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 Errors_1 = require("../../../constants/Errors");
const utils_1 = require("@selfcommunity/utils");
const CourseUsersTable_1 = tslib_1.__importDefault(require("../../../shared/CourseUsersTable"));
const api_services_1 = require("@selfcommunity/api-services");
const react_core_1 = require("@selfcommunity/react-core");
const widget_1 = require("../../../utils/widget");
const Pagination_1 = require("../../../constants/Pagination");
const course_1 = require("../../../types/course");
const headerCells = [
{
id: 'ui.course.dashboard.teacher.tab.students.table.header.name'
},
{
id: 'ui.course.dashboard.teacher.tab.students.table.header.progress'
},
{
id: 'ui.course.dashboard.teacher.tab.students.table.header.registration'
},
{
id: 'ui.course.dashboard.teacher.tab.students.table.header.latestActivity'
},
{}
];
function Students(props) {
// PROPS
const { course, endpointQueryParams = { limit: 6, offset: Pagination_1.DEFAULT_PAGINATION_OFFSET } } = 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.STUDENTS_PARTECIPANTS_COURSES_STATE_CACHE_PREFIX_KEY, course.id),
cacheStrategy: utils_1.CacheStrategies.NETWORK_ONLY,
visibleItems: endpointQueryParams.limit
}, widget_1.stateWidgetInitializer);
// HOOKS
const scUserContext = (0, react_core_1.useSCUser)();
// 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]);
// EFFECTS
(0, react_1.useEffect)(() => {
let _t;
if (scUserContext.user) {
_t = setTimeout(_init);
return () => {
clearTimeout(_t);
};
}
}, [scUserContext.user, _init]);
return ((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
}, headerCells: headerCells, mode: course_1.SCCourseUsersTableModeType.DASHBOARD, emptyStatusTitle: "ui.courseUsersTable.empty.users.title", emptyStatusDescription: "ui.courseUsersTable.empty.users.description" }));
}
exports.default = (0, react_1.memo)(Students);