diff --git a/src/common/components/LiveEvent/MainEvent.jsx b/src/common/components/LiveEvent/MainEvent.jsx index b798934a2..98bc85eec 100644 --- a/src/common/components/LiveEvent/MainEvent.jsx +++ b/src/common/components/LiveEvent/MainEvent.jsx @@ -1,3 +1,4 @@ +/* eslint-disable camelcase */ /* eslint-disable react/jsx-no-useless-fragment */ import { Box, Divider, Tag, TagLabel } from '@chakra-ui/react'; import PropTypes from 'prop-types'; @@ -26,7 +27,7 @@ function MainEvent({ const liveStartsAtDate = new Date(event?.starting_at); const liveEndsAtDate = new Date(event?.ended_at || event?.ending_at); - const isTeacher = cohorts.some(({ cohort, role }) => cohort.slug === event.cohort?.slug && ['TEACHER', 'ASSISTANT'].includes(role)); + const isTeacher = cohorts.some(({ slug, cohort_user }) => slug === event.cohort?.slug && ['TEACHER', 'ASSISTANT'].includes(cohort_user.role)); const joinMessage = () => (isTeacher ? t('start-class') : event?.cohort?.name); return ( diff --git a/src/common/components/SupportSidebar/index.jsx b/src/common/components/SupportSidebar/index.jsx index 678088533..58585d237 100644 --- a/src/common/components/SupportSidebar/index.jsx +++ b/src/common/components/SupportSidebar/index.jsx @@ -19,7 +19,7 @@ function SupportSidebar({ allCohorts, allSyllabus, services, subscriptions, subs const filterByFinantialStatus = (list) => list.filter((service) => { if (allCohorts.length > 0) { return allCohorts.some((elem) => { - if (elem?.cohort?.academy?.id === service?.academy?.id && (elem?.finantial_status === 'LATE' || elem?.educational_status === 'SUSPENDED')) { + if (elem?.academy?.id === service?.academy?.id && (elem?.cohort_user.finantial_status === 'LATE' || elem?.cohort_user.educational_status === 'SUSPENDED')) { return false; } return true; diff --git a/src/common/handlers/index.js b/src/common/handlers/index.js index d9cf65fa5..54bde8b9b 100644 --- a/src/common/handlers/index.js +++ b/src/common/handlers/index.js @@ -168,24 +168,24 @@ const handlers = { date: formatedDate, }; }, - getCohortsFinished: (cohorts) => cohorts.filter((program) => { - const educationalStatus = program?.educational_status?.toUpperCase(); - const programCohortStage = program?.cohort?.stage?.toUpperCase(); + getCohortsFinished: (cohorts) => cohorts.filter((cohort) => { + const educationalStatus = cohort.cohort_user.educational_status.toUpperCase(); + const programCohortStage = cohort.stage.toUpperCase(); const hasEnded = ['ENDED'].includes(programCohortStage); const isGraduated = educationalStatus === 'GRADUATED'; const showStudent = ['GRADUATED', 'POSTPONED', 'ACTIVE'].includes(educationalStatus); const isNotHiddenOnPrework = programCohortStage === 'PREWORK' - && program?.cohort?.is_hidden_on_prework === false + && cohort.is_hidden_on_prework === false && hasEnded; return (isGraduated || hasEnded || isNotHiddenOnPrework) && showStudent; }), - getActiveCohorts: (cohorts) => cohorts.filter((program) => { - const educationalStatus = program?.educational_status?.toUpperCase(); - const programRole = program?.role?.toUpperCase(); - const programCohortStage = program?.cohort?.stage?.toUpperCase(); + getActiveCohorts: (cohorts) => cohorts.filter((cohort) => { + const educationalStatus = cohort.cohort_user.educational_status?.toUpperCase(); + const programRole = cohort.cohort_user.role?.toUpperCase(); + const programCohortStage = cohort.stage.toUpperCase(); const isGraduated = educationalStatus === 'GRADUATED'; const visibleForTeacher = programRole !== 'STUDENT'; @@ -201,7 +201,7 @@ const handlers = { const cohortIsAvailable = showCohort && !hasEnded; const isNotHiddenOnPrework = programCohortStage === 'PREWORK' - && program?.cohort?.is_hidden_on_prework === false + && cohort.is_hidden_on_prework === false && !hasEnded; const showStudent = ['ACTIVE'].includes(educationalStatus) && programRole === 'STUDENT'; diff --git a/src/js_modules/chooseProgram/Programs.jsx b/src/js_modules/chooseProgram/Programs.jsx index 69c084d28..56ca85753 100644 --- a/src/js_modules/chooseProgram/Programs.jsx +++ b/src/js_modules/chooseProgram/Programs.jsx @@ -1,3 +1,4 @@ +/* eslint-disable camelcase */ import PropTypes from 'prop-types'; import { useRouter } from 'next/router'; import { subMinutes } from 'date-fns'; @@ -11,7 +12,7 @@ function Programs({ item, onOpenModal, setLateModalProps }) { const { setCohortSession } = useCohortHandler(); const [isLoadingPageContent, setIsLoadingPageContent] = useState(false); const { programsList } = useProgramList(); - const { cohort, ...cohortUser } = item; + const { cohort_user: cohortUser, ...cohort } = item; const signInDate = item.created_at; const { version, slug } = cohort.syllabus_version; const currentCohortProps = programsList[cohort.slug]; @@ -97,8 +98,8 @@ function Programs({ item, onOpenModal, setLateModalProps }) { // isBought={!isFreeTrial} isLoadingPageContent={isLoadingPageContent} isLoading={currentCohortProps === undefined} - startsIn={item?.cohort?.kickoff_date} - endsAt={item?.cohort?.ending_date} + startsIn={item?.kickoff_date} + endsAt={item?.ending_date} signInDate={signInDate} icon="coding" subscription={subscription || {}} diff --git a/src/js_modules/chooseProgram/index.jsx b/src/js_modules/chooseProgram/index.jsx index 4eba9cac1..f990484ff 100644 --- a/src/js_modules/chooseProgram/index.jsx +++ b/src/js_modules/chooseProgram/index.jsx @@ -28,15 +28,13 @@ function ChooseProgram({ chooseList, handleChoose, setLateModalProps }) { const cardColumnSize = 'repeat(auto-fill, minmax(17rem, 1fr))'; const finishedCohorts = handlers.getCohortsFinished(chooseList); - const activeCohorts = handlers.getActiveCohorts(chooseList).map((item) => { - const cohort = item?.cohort; + const activeCohorts = handlers.getActiveCohorts(chooseList).map((cohort) => { + const { cohort_user: cohortUser } = cohort; const currentCohortProps = programsList[cohort.slug]; return ({ - ...item, - cohort: { - ...cohort, - available_as_saas: item?.role === 'TEACHER' ? false : cohort?.available_as_saas, - }, + ...cohort, + available_as_saas: cohortUser?.role === 'TEACHER' ? false : cohort.available_as_saas, + cohort_user: { ...cohortUser }, subscription: currentCohortProps?.subscription, plan_financing: currentCohortProps?.plan_financing, all_subscriptions: currentCohortProps?.all_subscriptions, @@ -44,11 +42,11 @@ function ChooseProgram({ chooseList, handleChoose, setLateModalProps }) { }); }); - const hasNonSaasCourse = chooseList.some(({ cohort }) => !cohort.available_as_saas); + const hasNonSaasCourse = chooseList.some((cohort) => !cohort.available_as_saas); const marketingCourses = marketingCursesList.filter( (item) => !activeCohorts.some( - ({ cohort }) => cohort.slug === item?.cohort?.slug, + (cohort) => cohort.slug === item?.cohort?.slug, ) && item?.course_translation?.title, ); diff --git a/src/pages/choose-program/index.jsx b/src/pages/choose-program/index.jsx index 7e56fbaec..a86cec99a 100644 --- a/src/pages/choose-program/index.jsx +++ b/src/pages/choose-program/index.jsx @@ -72,7 +72,7 @@ function chooseProgram() { isLoading: true, data: [], }); - const { isAuthenticated, user, updateProfile } = useAuth(); + const { user, updateProfile } = useAuth(); const toast = useToast(); const commonStartColor = useColorModeValue('gray.300', 'gray.light'); const commonEndColor = useColorModeValue('gray.400', 'gray.400'); @@ -85,7 +85,19 @@ function chooseProgram() { en: 'https://www.youtube.com/embed/ijEp5XHm7qo', }; - const fetchAdmissions = () => bc.admissions().me(); + const fetchAdmissions = async () => { + try { + const resp = await bc.admissions().me(); + + const { cohorts } = resp.data; + if (cohorts) setMyCohorts(cohorts.map(parseCohort)); + + return resp; + } catch (e) { + console.log(e); + return e; + } + }; const options = { cacheTime: 1000 * 60 * 60, // cache 1 hour @@ -95,14 +107,14 @@ function chooseProgram() { const getStudentAndTeachers = async (item) => { const users = await bc.cohort({ role: 'TEACHER,ASSISTANT', - cohorts: item?.cohort?.slug, - academy: item?.cohort?.academy?.id, + cohorts: item?.slug, + academy: item?.academy?.id, }).getMembers(); return users || []; }; - const { isLoading, data: dataQuery, setData: setDataQuery, refetch } = useLocalStorageQuery('admissions', fetchAdmissions, { ...options }); + const { isLoading, refetch } = useLocalStorageQuery('admissions', fetchAdmissions, { ...options }); const getMembers = async (cohortSubscription) => { const members = await getStudentAndTeachers(cohortSubscription); @@ -111,15 +123,14 @@ function chooseProgram() { const getAllSyllabus = () => { const syllabus = []; - const allCohorts = dataQuery?.cohorts || []; - allCohorts.forEach(({ cohort }) => { - if (!syllabus.includes(cohort.syllabus_version.slug)) syllabus.push(cohort.syllabus_version.slug); + myCohorts.forEach(({ syllabus_version: syllabusVersion }) => { + if (!syllabus.includes(syllabusVersion.slug)) syllabus.push(syllabusVersion.slug); }); return syllabus; }; - const allSyllabus = useMemo(getAllSyllabus, [dataQuery]); + const allSyllabus = useMemo(getAllSyllabus, [myCohorts]); const getServices = async (userRoles) => { if (userRoles?.length > 0) { @@ -149,57 +160,58 @@ function chooseProgram() { }; useEffect(() => { - const cohorts = dataQuery?.cohorts; - if (cohorts) setMyCohorts(cohorts.map(parseCohort)); - const cohortSubscription = cohorts?.find((item) => item?.cohort?.slug === subscriptionProcess?.slug); - const members = cohortSubscription?.cohort?.slug ? getMembers(cohortSubscription) : []; - - getServices(dataQuery?.roles); - const cohortIsReady = cohorts?.length > 0 && cohorts?.some((item) => { - const cohort = item?.cohort; - const academy = cohort?.academy; - if (cohort?.id === subscriptionProcess?.id + let revalidate; + if (user) { + const cohortSubscription = myCohorts?.find((item) => item?.slug === subscriptionProcess?.slug); + const members = cohortSubscription ? getMembers(cohortSubscription) : []; + + getServices(user.roles); + const cohortIsReady = myCohorts?.length > 0 && myCohorts?.some((cohort) => { + // const cohort = item?.cohort; + const academy = cohort?.academy; + if (cohort?.id === subscriptionProcess?.id && cohort?.slug === subscriptionProcess?.slug && academy?.id === subscriptionProcess?.academy_info?.id) return true; - return false; - }); - if (cohorts?.length > 0) { - const hasAvailableAsSaas = cohorts.some((elem) => elem.cohort.available_as_saas === true); - const cohortsSlugs = cohorts.map((elem) => elem.cohort.slug).join(','); - const cohortsAcademies = cohorts.map((elem) => elem.cohort.academy.slug).join(','); - const cohortWithFinantialStatusLate = cohorts.filter((elem) => elem.finantial_status === 'LATE' || elem.educational_status === 'SUSPENDED'); - setLateModalProps({ - isOpen: cohortWithFinantialStatusLate?.length > 0 && !isClosedLateModal, - data: cohortWithFinantialStatusLate, + return false; }); + if (myCohorts?.length > 0) { + const hasAvailableAsSaas = myCohorts.some((elem) => elem.available_as_saas === true); + const cohortsSlugs = myCohorts.map((elem) => elem.slug).join(','); + const cohortsAcademies = myCohorts.map((elem) => elem.academy.slug).join(','); + const cohortWithFinantialStatusLate = myCohorts.filter((elem) => elem.cohort_user.finantial_status === 'LATE' || elem.educational_status === 'SUSPENDED'); + setLateModalProps({ + isOpen: cohortWithFinantialStatusLate?.length > 0 && !isClosedLateModal, + data: cohortWithFinantialStatusLate, + }); - reportDatalayer({ - dataLayer: { - available_as_saas: hasAvailableAsSaas, - cohorts: cohortsSlugs, - academies: cohortsAcademies, - }, - }); - } + reportDatalayer({ + dataLayer: { + available_as_saas: hasAvailableAsSaas, + cohorts: cohortsSlugs, + academies: cohortsAcademies, + }, + }); + } - const revalidate = setTimeout(() => { - if (subscriptionProcess?.status === PREPARING_FOR_COHORT) { - setIsRevalidating(true); - if (!cohortIsReady && members.length === 0) { - refetch(); - console.log('revalidated on:', new Date().toLocaleString()); - setIsRevalidating(false); - } else { - setIsRevalidating(false); - console.log('Start learning!'); - removeStorageItem('subscription-process'); + revalidate = setTimeout(() => { + if (subscriptionProcess?.status === PREPARING_FOR_COHORT) { + setIsRevalidating(true); + if (!cohortIsReady && members.length === 0) { + refetch(); + console.log('revalidated on:', new Date().toLocaleString()); + setIsRevalidating(false); + } else { + setIsRevalidating(false); + console.log('Start learning!'); + removeStorageItem('subscription-process'); + } } - } - }, 2000); + }, 2000); + } return () => clearTimeout(revalidate); - }, [isAuthenticated, dataQuery?.cohorts, dataQuery?.roles]); + }, [user, myCohorts]); useEffect(() => { setSubscriptionLoading(true); @@ -225,40 +237,40 @@ function chooseProgram() { // .filter((subscription) => subscription?.plans?.[0]?.slug !== undefined); useEffect(() => { - if (subscriptionLoading === false && dataQuery && Object.values(cohortTasks)?.length > 0) { - updateProgramList(dataQuery?.cohorts?.reduce((acc, value) => { - acc[value.cohort.slug] = { - ...state[value.cohort.slug], - ...programsList[value.cohort.slug], - ...cohortTasks[value.cohort.slug], - name: value.cohort.name, + if (subscriptionLoading === false && myCohorts.length > 0 && Object.values(cohortTasks)?.length > 0) { + updateProgramList(myCohorts?.reduce((acc, value) => { + acc[value.slug] = { + ...state[value.slug], + ...programsList[value.slug], + ...cohortTasks[value.slug], + name: value.name, plan_financing: subscriptionData?.plan_financings?.find( - (sub) => sub?.selected_cohort_set?.cohorts.some((cohort) => cohort?.slug === value.cohort.slug), + (sub) => sub?.selected_cohort_set?.cohorts.some((cohort) => cohort?.slug === value.slug), ) || null, subscription: subscriptionData?.subscriptions?.find( - (sub) => sub?.selected_cohort_set?.cohorts.some((cohort) => cohort?.slug === value.cohort.slug), + (sub) => sub?.selected_cohort_set?.cohorts.some((cohort) => cohort?.slug === value.slug), ) || null, all_subscriptions: allSubscriptions, - slug: value.cohort.slug, + slug: value.slug, }; return acc; }, {})); } - }, [dataQuery, cohortTasks, subscriptionLoading]); + }, [myCohorts, cohortTasks, subscriptionLoading]); - const processCohort = async (item) => { - if (item?.cohort?.slug) { - const isFinantialStatusLate = item?.finantial_status === 'LATE' || item?.educational_status === 'SUSPENDED'; - const { slug } = item.cohort; + const processCohort = async (cohort) => { + if (cohort?.slug) { + const isFinantialStatusLate = cohort.cohort_user.finantial_status === 'LATE' || cohort.cohort_user.educational_status === 'SUSPENDED'; + const { slug } = cohort; const studentAndTeachers = isFinantialStatusLate ? {} : await bc.cohort({ role: 'TEACHER,ASSISTANT', - cohorts: item?.cohort?.slug, - academy: item?.cohort?.academy?.id, + cohorts: slug, + academy: cohort.academy?.id, }).getMembers(); const teacher = studentAndTeachers?.data?.filter((st) => st.role === 'TEACHER') || []; const assistant = studentAndTeachers?.data?.filter((st) => st.role === 'ASSISTANT') || []; const { tasks, syllabus } = cohortsAssignments[slug]; - const assignmentData = await handlers.getAssignmentsCount({ data: syllabus, taskTodo: tasks, cohortId: item?.cohort?.id }); + const assignmentData = await handlers.getAssignmentsCount({ data: syllabus, taskTodo: tasks, cohortId: cohort.id }); setCohortTasks((prev) => ({ ...prev, @@ -278,10 +290,10 @@ function chooseProgram() { }, [myCohorts, isLoading]); useEffect(() => { - if (dataQuery?.cohorts?.length > 0 && Object.keys(cohortsAssignments).length > 0) { - dataQuery.cohorts.map(processCohort); + if (myCohorts.length > 0 && Object.keys(cohortsAssignments).length > 0) { + myCohorts.map(processCohort); } - }, [dataQuery?.id, isLoading, cohortsAssignments]); + }, [myCohorts, isLoading, cohortsAssignments]); const userID = user?.id; @@ -373,12 +385,7 @@ function chooseProgram() { const inv = invites[invitationIndex]; const { name: cohortName } = inv.cohort; - const { data: refetchData } = await refetch(); - - setDataQuery(refetchData.data); - - const cohorts = refetchData.data; - if (cohorts) setMyCohorts(cohorts.map(parseCohort)); + await refetch(); const invList = [...invites]; invList.splice(invitationIndex, 1); @@ -419,7 +426,7 @@ function chooseProgram() { if (status >= 200 && status < 400) { const invitationIndex = invites.findIndex((invite) => invite.id === id); - const [meRefetch, admissionsRefetch] = await Promise.all([ + const [meRefetch] = await Promise.all([ bc.auth().me(), refetch(), ]); @@ -427,9 +434,6 @@ function chooseProgram() { const { data: userData } = meRefetch; updateProfile(userData); - const { data: refetchData } = admissionsRefetch; - setDataQuery(refetchData.data); - const invList = [...invites]; invList.splice(invitationIndex, 1); setInvites(invList); @@ -468,7 +472,7 @@ function chooseProgram() { return t('invite.singular-word', { invitesLength: invites?.length }); }; - const isMainCohort = ({ cohort }) => !dataQuery.cohorts.some((elem) => elem.cohort.micro_cohorts.some((micro) => micro.slug === cohort.slug)); + const isMainCohort = (cohort) => !myCohorts.some((elem) => elem.micro_cohorts.some((micro) => micro.slug === cohort.slug)); return ( @@ -623,7 +627,7 @@ function chooseProgram() { {!isLoading && ( - + )} {isRevalidating && ( @@ -681,13 +685,13 @@ function chooseProgram() { mainClasses={liveClasses?.length > 0 ? liveClasses : []} otherEvents={events} margin="0 auto" - cohorts={dataQuery?.cohorts || []} + cohorts={myCohorts} /> {!mentorshipServices.isLoading && mentorshipServices?.data?.length > 0 && ( - {dataQuery?.cohorts?.every((elem) => elem.cohort.available_as_saas) && ( + {myCohorts.every((elem) => elem.available_as_saas) && (