import { useCourse, useCourseDispatch } from '@components/Contexts/CourseContext'
import LinkToUserGroup from '@components/Objects/Modals/Dash/EditCourseAccess/LinkToUserGroup'
import ConfirmationModal from '@components/StyledElements/ConfirmationModal/ConfirmationModal'
import Modal from '@components/StyledElements/Modal/Modal'
import { getAPIUrl } from '@services/config/config'
import { unLinkResourcesToUserGroup } from '@services/usergroups/usergroups'
import { swrFetcher } from '@services/utils/ts/requests'
import { Globe, SquareUserRound, Users, UsersRound, X } from 'lucide-react'
import React from 'react'
import toast from 'react-hot-toast'
import useSWR, { mutate } from 'swr'
type EditCourseAccessProps = {
orgslug: string
course_uuid?: string
}
function EditCourseAccess(props: EditCourseAccessProps) {
const [error, setError] = React.useState('')
const course = useCourse() as any
const dispatchCourse = useCourseDispatch() as any
const courseStructure = course.courseStructure
const { data: usergroups } = useSWR(
courseStructure ? `${getAPIUrl()}usergroups/resource/${courseStructure.course_uuid}` : null,
swrFetcher
)
const [isPublic, setIsPublic] = React.useState(courseStructure.public)
React.useEffect(() => {
// This code will run whenever form values are updated
if (isPublic !== courseStructure.public) {
dispatchCourse({ type: 'setIsNotSaved' })
const updatedCourse = {
...courseStructure,
public: isPublic,
}
dispatchCourse({ type: 'setCourseStructure', payload: updatedCourse })
}
}, [course, isPublic])
return (
{' '}
Access to the course
{' '}
Choose if want your course to be publicly available on the internet or only accessible to signed in users{' '}
{isPublic ? (
Active
) : null}
Public
The Course is publicly available on the internet, it is indexed by search engines and can be accessed by anyone
}
functionToExecute={() => {
setIsPublic(true)
}}
status="info"
>
{!isPublic ? (
Active
) : null}
Users Only
The Course is only accessible to signed in users, additionaly you can choose which UserGroups can access this course
}
functionToExecute={() => {
setIsPublic(false)
}}
status="info"
>
)
}
function UserGroupsSection({ usergroups }: { usergroups: any[] }) {
const course = useCourse() as any
const [userGroupModal, setUserGroupModal] = React.useState(false)
const removeUserGroupLink = async (usergroup_id: number) => {
const res = await unLinkResourcesToUserGroup(usergroup_id, course.courseStructure.course_uuid)
if (res.status === 200) {
toast.success('Successfully unliked from usergroup')
mutate(`${getAPIUrl()}usergroups/resource/${course.courseStructure.course_uuid}`)
}
else {
toast.error('Error ' + res.status + ': ' + res.data.detail)
}
}
return (
<>
UserGroups
{' '}
Choose which UserGroups can access this course{' '}
Name
Actions
<>
{usergroups?.map((usergroup: any) => (
{usergroup.name}
Delete link
}
functionToExecute={() => {
removeUserGroupLink(usergroup.id)
}}
status="warning"
>
))}
>
setUserGroupModal(!userGroupModal)
}
minHeight="no-min"
dialogContent={
}
dialogTitle="Link Course to a UserGroup"
dialogDescription={
'Choose which UserGroups can access this course'
}
dialogTrigger={
Link to a UserGroup
}
/>
>
)
}
export default EditCourseAccess