Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 user get consumables if having ♻️ refactor of mentorshipService #1587

Merged
Merged
3 changes: 3 additions & 0 deletions public/locales/en/dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"get-more-mentorships": "Get more mentorships",
"schedule-button": "Schedule a mentoring session",
"mentors-available": "+{{count}} Mentors available",
"for": "for",
"service-not-found": "couldn't find service",
"mentor-not-found": "couldn't find mentor",
"actionButtons": [
{
"name": "mentoring",
Expand Down
5 changes: 5 additions & 0 deletions public/locales/en/workshops.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"in-person-confirm": "This is an in-person event that will take place in {{address}}. Do you still want to confirm your attendance?",
"confirm-attendance": "Yes, I will be there",
"deny-attendance" : "No, I will not attend",
"denny-access":{
"description": "This is a private event, only students with access to",
"button": "You don't have access to this event",
"can-join": "can join"
},
"form": {
"title": "Join this event",
"description": "Sign in to join other coders live solving technical or career challenges.",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/es/dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"get-more-mentorships": "Consigue más mentorías",
"schedule-button": "Programar una sesión de tutoría",
"mentors-available": "+{{count}} Mentores disponibles",
"for": "para",
"service-not-found": "no se ha encontrado servicio",
"mentor-not-found": "no se ha encontrado el mentor",
"actionButtons": [
{
"name": "mentoring",
Expand Down
5 changes: 5 additions & 0 deletions public/locales/es/workshops.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"in-person-confirm": "Este es un evento presencial que se llevará a cabo en {{address}}. ¿Aún quieres confirmar tu asistencia?",
"confirm-attendance": "Si, si asistiré",
"deny-attendance" : "No, no asistiré",
"denny-access":{
"description": "Esto es un evento privado, solo estudiantes con acceso a",
"button": "No tienes acceso a este evento.",
"can-join": "pueden entrar"
},
"form": {
"title": "Únete a este evento",
"description": "Inicia sesión para unirte a otros programadores en vivo resolviendo desafíos técnicos o profesionales.",
Expand Down
155 changes: 47 additions & 108 deletions src/common/components/SupportSidebar/Mentoring.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,29 @@ import {
// useToast,
} from '@chakra-ui/react';
import PropTypes from 'prop-types';
import { format } from 'date-fns';
import { es } from 'date-fns/locale';
import { useRouter } from 'next/router';
import useTranslation from 'next-translate/useTranslation';
import bc from '../../services/breathecode';
import MentoringFree from './MentoringFree';
import MentoringConsumables from './MentoringConsumables';
import useAuth from '../../hooks/useAuth';
import useCohortHandler from '../../hooks/useCohortHandler';

function Mentoring({
width, allCohorts, allSyllabus, programServices, subscriptions, subscriptionData,
}) {
// const toast = useToast();
const { t } = useTranslation('dashboard');
const [savedChanges, setSavedChanges] = useState({});
const router = useRouter();
const { isLoading, user } = useAuth();
const { slug } = router.query;
const { state } = useCohortHandler();
const { cohortSession } = state;
const router = useRouter();
const [consumables, setConsumables] = useState({});
const [consumables, setConsumables] = useState([]);
const [mentoryProps, setMentoryProps] = useState({});
const [allMentorsAvailable, setAllMentorsAvailable] = useState([]);
const [programMentors, setProgramMentors] = useState([]);
const [isAvailableForConsumables, setIsAvailableForConsumables] = useState(true);
const { isLoading, user } = useAuth();
// const toast = useToast();
const { slug } = router.query;

const [searchProps, setSearchProps] = useState({
serviceSearch: '',
mentorSearch: '',
});

const servicesFiltered = programServices.list.filter(
(l) => l.name.toLowerCase().includes(searchProps.serviceSearch),
);
const [cohortSessionIsSaaS, setCohortSessionIsSaaS] = useState(true);
const [searchProps, setSearchProps] = useState({ serviceSearch: '', mentorSearch: '' });

const filterServices = () => {
if (subscriptionData?.selected_mentorship_service_set?.mentorship_services?.length > 0) {
Expand All @@ -62,38 +50,13 @@ function Mentoring({
const mentorsFiltered = programMentors.filter(
(mentor) => {
const fullName = `${mentor.user.first_name} ${mentor.user.last_name}`.toLowerCase();
const mentorServices = fullName.includes(searchProps.mentorSearch) && mentor.services.some((sv) => sv.status === 'ACTIVE'
&& sv.slug === mentoryProps?.service?.slug);
return mentorServices;
return (
fullName.includes(searchProps.mentorSearch)
&& mentor.services.some((sv) => sv.status === 'ACTIVE' && sv.slug === mentoryProps?.service?.slug)
);
},
);

const dateFormated = {
en: mentoryProps?.date && format(new Date(mentoryProps.date), 'MMMM dd'),
es: mentoryProps?.date && format(new Date(mentoryProps.date), "dd 'de' MMMM", { locale: es }),
};

const dateFormated2 = {
en: mentoryProps?.date && format(new Date(mentoryProps.date), 'MMMM dd, yyyy'),
es: mentoryProps?.date && format(new Date(mentoryProps.date), "dd 'de' MMMM, yyyy", { locale: es }),
};

useEffect(() => {
if (mentoryProps?.time) {
const [hours, minutes] = mentoryProps.time.split(':');

const nDate = mentoryProps?.date
&& new Date(mentoryProps.date);

nDate.setHours(+hours, +minutes, 0, 0); // set hours/minute;
setMentoryProps({ ...mentoryProps, date: nDate });
setSavedChanges({ ...mentoryProps, date: nDate });
}
}, [mentoryProps?.time]);

const step1 = !mentoryProps?.service;
const step2 = mentoryProps?.service && !mentoryProps?.date;

const getAllMentorsAvailable = async () => {
const servicesSlugs = programServices.list.map((service) => service?.slug);

Expand All @@ -106,7 +69,6 @@ function Mentoring({
academies[academy.id].services.push(restOfService);
});

// Convert the object to an array of academies with their services
const academyData = Object.entries(academies).map(([academy, values]) => ({
id: Number(academy),
services: values.services,
Expand All @@ -130,6 +92,21 @@ function Mentoring({
return [];
};

const sortByConsumptionAvailability = (allConsumables) => allConsumables.sort((a, b) => {
lumi-tip marked this conversation as resolved.
Show resolved Hide resolved
const balanceA = a?.balance?.unit;
const balanceB = b?.balance?.unit;

if (balanceA === -1 && balanceB !== -1) return -1;
if (balanceA !== -1 && balanceB === -1) return 1;

if (balanceA > 0 && balanceB <= 0) return -1;
if (balanceA <= 0 && balanceB > 0) return 1;

if (balanceA > 0 && balanceB > 0) return balanceB - balanceA;

return 0;
});

const getMentorsAndConsumables = async () => {
const mentors = await getAllMentorsAvailable();
const reqConsumables = await bc.payment().service().consumable()
Expand All @@ -141,7 +118,8 @@ function Mentoring({
}))));

const allConsumables = await Promise.all(reqConsumables);
setConsumables(allConsumables);
const sortedConsumables = sortByConsumptionAvailability(allConsumables);
setConsumables(sortedConsumables);
setAllMentorsAvailable(mentors);
};

Expand All @@ -153,73 +131,34 @@ function Mentoring({

useEffect(() => {
const existsCohortSession = typeof cohortSession?.available_as_saas === 'boolean';

if (existsCohortSession) {
setIsAvailableForConsumables(cohortSession?.available_as_saas);
}
if (!existsCohortSession) {
if (allCohorts.length > 0) {
setIsAvailableForConsumables(allCohorts?.some((c) => c.cohort?.available_as_saas === true));
}
setCohortSessionIsSaaS(cohortSession?.available_as_saas);
}
}, [allCohorts]);

const mentorshipService = consumables?.mentorship_service_sets?.find(
(c) => c?.slug.toLowerCase() === subscriptionData?.selected_mentorship_service_set?.slug.toLowerCase(),
);

return !isLoading && user?.id && (
<Box>
<Box fontSize="16px" padding="6px 8px" color="black" background="yellow.light" textAlign="center" borderRadius="17px" fontWeight={700}>
{t('supportSideBar.mentoring-label')}
</Box>
{isAvailableForConsumables ? (
<MentoringConsumables
{...{
mentoryProps,
width,
consumables,
mentorshipService,
setMentoryProps,
programServices: programServices.list?.length > 0 ? programServices.list : subscriptionData?.selected_mentorship_service_set?.mentorship_services,
servicesFiltered: suscriptionServicesFiltered,
dateFormated,
searchProps,
setSearchProps,
setProgramMentors,
savedChanges,
setSavedChanges,
mentorsFiltered,
step1,
step2,
dateFormated2,
allMentorsAvailable,
subscriptionData,
allSubscriptions: subscriptions,
}}
/>
) : (
<MentoringFree
{...{
mentoryProps,
width,
setMentoryProps,
programServices: programServices.list,
dateFormated,
servicesFiltered,
searchProps,
setSearchProps,
setProgramMentors,
savedChanges,
setSavedChanges,
mentorsFiltered,
step1,
step2,
dateFormated2,
allMentorsAvailable,
}}
/>
)}
<MentoringConsumables
{...{
mentoryProps,
width,
consumables,
setMentoryProps,
programServices: programServices.list?.length > 0 ? programServices.list : subscriptionData?.selected_mentorship_service_set?.mentorship_services,
servicesFiltered: suscriptionServicesFiltered,
searchProps,
setSearchProps,
setProgramMentors,
mentorsFiltered,
allMentorsAvailable,
subscriptionData,
cohortSessionIsSaaS,
allSubscriptions: subscriptions,
}}
/>
</Box>
);
}
Expand Down
Loading