Skip to content

Commit

Permalink
Cursus editor
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Oct 4, 2024
1 parent 1831974 commit 086a140
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, {useEffect} from 'react'
import { useHistory } from 'react-router-dom'
import get from 'lodash/get'
import {PropTypes as T} from 'prop-types'

import {trans} from '#/main/app/intl'
import {route} from '#/plugin/cursus/routing'
import {hasPermission} from '#/main/app/security'
import {Editor} from '#/main/app/editor/components/main'
import {Course as CourseTypes} from '#/plugin/cursus/prop-types'

Expand All @@ -20,22 +20,35 @@ import {CourseEditorRegistration} from '#/plugin/cursus/course/editor/components
import {selectors} from '#/plugin/cursus/course/store'

const CourseEditor = (props) => {
const history = useHistory()

useEffect(() => {
props.openForm(props.slug)
}, [props.slug])
if (props.isNew) {
props.openForm(null, CourseTypes.defaultProps, props.course.workspace)
} else {
props.openForm(props.slug)
}
}, [props.isNew, props.slug])

return (
<Editor
path={route(props.course, null, props.path) + '/edit'}
title={get(props.course, 'name')}
path={props.isNew ? (props.contextType === 'workspace' ? props.path + '/new' : props.path + '/course/new') : route(props.course, null, props.path) + '/edit'}
title={get(props.course, 'name', trans('new_course', {}, 'cursus'))}
name={selectors.FORM_NAME}
target={['apiv2_cursus_course_update', {id: props.course.id}]}
canAdministrate={hasPermission('administrate', props.course)}
target={(course, isNew) => isNew ? ['apiv2_cursus_course_create'] : ['apiv2_cursus_course_update', {id: props.course.id}]}
canAdministrate={props.canAdministrate}
onSave={(course, isNew) => {
const newSlug = course.slug

if (!isNew && props.course.slug !== newSlug) {
const newUrl = `${props.path}/course/${newSlug}/edit`
history.push(newUrl)
}
}}
close={props.path}
defaultPage="overview"
historyPage={CourseEditorHistory}
actionsPage={CourseEditorActions}
historyPage={!props.isNew ? CourseEditorHistory : undefined}
actionsPage={!props.isNew ? CourseEditorActions : undefined}
overviewPage={CourseEditorOverview}
appearancePage={CourseEditorAppearance}
permissionsPage={CourseEditorPermissions}
Expand Down Expand Up @@ -65,6 +78,7 @@ const CourseEditor = (props) => {
name: 'canceled',
title: trans('canceled_sessions', {}, 'cursus'),
help: trans('canceled_sessions_help', {}, 'cursus'),
disabled: props.isNew === true,
render: () => (
<CourseEditorCanceledSessions
path={props.path}
Expand All @@ -82,11 +96,13 @@ CourseEditor.propTypes = {
course: T.shape(
CourseTypes.propTypes
),
update: T.func.isRequired,
contextType: T.string,
isNew: T.bool,
pages: T.array,
openForm: T.func.isRequired,
slug: T.string
slug: T.string,
contextType: T.string,
update: T.func.isRequired,
canAdministrate: T.bool,
openForm: T.func.isRequired
}

export {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'

import get from 'lodash/get'
import {trans} from '#/main/app/intl'
import {EditorPage} from '#/main/app/editor'

Expand All @@ -15,6 +15,28 @@ const CourseEditorPermissions = () =>
primary: true,
fields: [
{
name: 'restrictions._restrictUsers',
type: 'boolean',
label: trans('restrict_users_count'),
calculated: (course) => !!get(course, 'restrictions.users') || get(course, 'restrictions._restrictUsers'),
onChange: (value) => {
if (!value) {
props.update(props.name, 'restrictions.users', null)
}
},
linked: [
{
name: 'restrictions.users',
type: 'number',
label: trans('users_count'),
required: true,
displayed: (course) => get(course, 'restrictions.users') || get(course, 'restrictions._restrictUsers'),
options: {
min: 0
}
}
]
},{
name: 'meta.public',
type: 'boolean',
label: trans('make_course_public', {}, 'cursus'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import {connect} from 'react-redux'

import {withReducer} from '#/main/app/store/components/withReducer'

import {selectors as toolSelectors} from '#/main/core/tool'
import {actions as courseActions, reducer, selectors} from '#/plugin/cursus/course/store'
import {hasPermission} from '#/main/app/security'
import {selectors as toolSelectors} from '#/main/core/tool'
import {actions as courseActions, reducer, selectors} from '#/plugin/cursus/course/store'
import {actions as formActions, selectors as formSelectors} from '#/main/app/content/form/store'

import {CourseEditor as CourseEditorComponent} from '#/plugin/cursus/course/editor/components/main'

const CourseEditor = withReducer(selectors.STORE_NAME, reducer)(
connect(
(state) => ({
path: toolSelectors.path(state),
contextType: toolSelectors.contextType(state),
course: formSelectors.data(formSelectors.form(state, selectors.FORM_NAME))
course: formSelectors.data(formSelectors.form(state, selectors.FORM_NAME)),
canAdministrate: hasPermission('administrate', toolSelectors.toolData(state))
}),
(dispatch) => ({
openForm(slug, defaultProps, workspace = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const CreationModal = (props) =>
CreationModal.propTypes = {
path: T.string.isRequired,
contextType: T.string.isRequired,
contextId: T.object.isRequired,
contextId: T.string,
openForm: T.func.isRequired,
reset: T.func.isRequired,
fadeModal: T.func.isRequired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {Tool} from '#/main/core/tool'
import {LINK_BUTTON} from '#/main/app/buttons'
import {Course} from '#/plugin/cursus/course/containers/main'
import {Course as CourseTypes} from '#/plugin/cursus/prop-types'
import {CourseCreation} from '#/plugin/cursus/course/components/creation'
import {CourseEditor} from '#/plugin/cursus/course/editor/containers/main'

import {EmptyCourse} from '#/plugin/cursus/course/components/empty'
Expand All @@ -21,14 +20,14 @@ const EventsTool = (props) =>
<Tool
{...props}
redirect={[
{from: '/', exact: true, to: props.course ? '/course/' + props.course.slug : '/course'}
{from: '/', exact: true, to: (props.course && props.course.slug) ? '/course/' + props.course.slug : '/course'}
]}
menu={[
{
name: 'about',
type: LINK_BUTTON,
label: trans('about', {}, 'platform'),
target: props.course ? props.path + '/course/' + props.course.slug : props.path + '/course'
target: props.course ? `${props.path}/course/${props.course.slug}` : `${props.path}/course`
}, {
name: 'registered',
type: LINK_BUTTON,
Expand Down Expand Up @@ -57,7 +56,7 @@ const EventsTool = (props) =>
path: '/new',
onEnter: () => props.openForm(null, CourseTypes.defaultProps, props.currentContext.data),
disabled: !props.canEdit,
component: CourseCreation
render: () => (<CourseEditor isNew={true}/>)
}, {
path: '/course/:courseSlug/edit',
render: (params = {}) => (
Expand All @@ -69,25 +68,26 @@ const EventsTool = (props) =>
}, {
path: '/course',
onEnter: () => {
if (props.course) {
if (props.course && props.course.slug) {
return props.openCourse(props.course.slug)
}
},
render: (params = {}) => {
if (props.course) {
if (props.course && props.course.slug) {
return (
<Course
path={props.path + '/course/' + props.course.slug}
slug={props.course.slug}
history={params.history}
/>)
/>
)
} else {
return (
<EmptyCourse
path={props.path}
canEdit={props.canEdit}
contextType={props.contextType}
contextId={props.currentContext.data}
contextId={get(props.currentContext, 'data')}
openForm={props.openForm}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {route} from '#/plugin/cursus/routing'
import {Course} from '#/plugin/cursus/course/containers/main'
import {CourseEditor} from '#/plugin/cursus/course/editor/containers/main'
import {CatalogList} from '#/plugin/cursus/tools/trainings/catalog/components/list'
import {CourseCreation} from '#/plugin/cursus/course/components/creation'

const CatalogMain = (props) =>
<Routes
Expand All @@ -27,7 +26,7 @@ const CatalogMain = (props) =>
}, {
path: '/new',
disabled: !props.canEdit,
component: CourseCreation
render: () => (<CourseEditor isNew={true}/>)
}, {
path: '/:slug/edit',
render: (params = {}) => (
Expand Down

0 comments on commit 086a140

Please sign in to comment.