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

Available as saas #393

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
15 changes: 14 additions & 1 deletion src/app/services/breathecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class BreatheCodeClient {
"Academy student",
`${this.host}/auth/academy/student`,
payload
),
),
getAcademyMembers: async (query) => {
const qs = serializeQuerystring(query);
return await axios.bcGet(
Expand Down Expand Up @@ -1182,6 +1182,19 @@ class BreatheCodeClient {
};
}

payments() {
return {
addAcademyPlanSlugSubscription: (planSlug, payload) =>
axios.bcPost(
"Academy Plan Subscription",
`${this.host}/payments/academy/plan/${planSlug}/subscription`,
payload
),
getPlanByCohort: (cohortSlug) =>
axios.get(`${config.REACT_APP_API_HOST}/v1/payments/plan?cohort=${cohortSlug}`),
};
}

getItem(key) {
const value = this.ls.getItem(key);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useState } from 'react';
import { Formik } from 'formik';
import { Grid, TextField, Button } from '@material-ui/core';
import { Grid, TextField, Button, Divider } from '@material-ui/core';
import { useHistory } from 'react-router-dom';
import PropTypes from 'prop-types';
import bc from '../../../../services/breathecode';
import axios from '../../../../../axios';
import { AsyncAutocomplete } from '../../../../components/Autocomplete';
import { ToastContainer, toast, Zoom, Bounce } from 'react-toastify';
import config from '../../../../../config.js';
import { Alert, AlertTitle } from '@material-ui/lab';

const propTypes = {
initialValues: PropTypes.objectOf(PropTypes.object).isRequired,
Expand All @@ -16,14 +17,15 @@ const propTypes = {
export const ProfileForm = ({ initialValues }) => {
const [cohort, setCohort] = useState([]);
const history = useHistory();
const [availableAsSaas, setAvailableAsSaas] = useState(false)
const [selectedPlans, setSelectedPlans] = useState(null)

const postAcademyStudentProfile = (values) => {
if (typeof (values.invite) === 'undefined' || !values.invite) values.user = values.id;

let cohortId = cohort.map(c => {
return c.id
});

let requestValues = { ...values,
cohort: cohort.length > 0 ? cohortId : undefined
};
Expand All @@ -47,28 +49,54 @@ export const ProfileForm = ({ initialValues }) => {
.then((data) => {
console.log("addAcademyStudent", data, data.ok)
if (data !== undefined && data.ok) {
history.push('/admissions/students');
const userId = data.data?.id;
if (availableAsSaas && selectedPlans?.slug) {
const planSlug = selectedPlans?.slug;
const payload = {
provided_payment_details: "Added on admin",
reference: "Added on admin",
user: userId,
payment_method: 5,
};
bc.payments().addAcademyPlanSlugSubscription(planSlug, payload)
.then((response) => {
console.log("Subscription created", response.data);
})
.catch((error) => {
console.error("Error creating subscription", error);
});
}
history.push('/admissions/students');
}
})
.catch((error) => console.error(error));
}

};

return (

<Formik

initialValues={initialValues}
onSubmit={(values) => postAcademyStudentProfile(values)}
onSubmit={(values) => {
if (!selectedPlans || selectedPlans.length === 0){
console.error("You must select at leats one plan before submitting")
toast.error("You must select at leats one plan before submitting")
return;
}
if (selectedPlans.lenght > 1){
console.error("You can only select one plan")
toast.error("You can only select one plan")
return;
}
postAcademyStudentProfile(values)}
}
enableReinitialize
validate={(values)=>{
let errors = {}

if (cohort.length === 0) {
errors.cohort = 'You must select at least one cohort'
}

return errors
}}
>
Expand Down Expand Up @@ -152,7 +180,12 @@ export const ProfileForm = ({ initialValues }) => {
</Grid>
<Grid item md={10} sm={8} xs={12}>
<AsyncAutocomplete
onChange={(newCohort) => setCohort(newCohort)}
onChange={(newCohort) => {
setCohort(newCohort)
const isAvailableAsSaas = newCohort.some(cohort => cohort.available_as_saas);
setAvailableAsSaas(isAvailableAsSaas)
}}

// name="cohort"
error={errors.cohort && touched.cohort}
helperText={touched.cohort && errors.cohort}
Expand All @@ -168,6 +201,50 @@ export const ProfileForm = ({ initialValues }) => {
/>
<small>Only cohorts with stage PREWORK or STARTED will be shown here</small>
</Grid>
{availableAsSaas === true && (
<>
<Divider className="mb-2" />
<Grid item md={12} sm={12} xs={12}>
<Alert severity='warning'>
<AlertTitle> On adding a new cohort</AlertTitle>
You are selecting a cohort that is available as saas, in order to add him/her to this cohort, you should select the plan that you want to add him to
</Alert>
</Grid>
<Grid item md={2} sm={4} xs={12}>
Plan
</Grid>
<Grid item md={10} sm={8} xs={12}>
<AsyncAutocomplete
onChange={(newPlan) => {
setSelectedPlans(newPlan);
}}
width="30%"
size="small"
label="Select a plan"
debounced={false}
isOptionEqualToValue={(option, value) => option.id === value.id}
getOptionLabel={(option) => `${option.slug}`}
multiple={false}
asyncSearch={() => {
const selectedCohortSlug = cohort.length > 0 ? cohort[0].slug : null;
if (selectedCohortSlug) {
return bc.payments().getPlanByCohort(selectedCohortSlug)
.then((response) => {
console.log("Plans:", response.data);
return response.data
})
.catch((error) => {
console.error("Error fetching plans:", error);
return [];
});
} else {
return [];
}
}}
/>
</Grid>
</>
)}
</Grid>
<div className="mt-6">
<Button color="primary" variant="contained" type="submit">
Expand Down
1 change: 0 additions & 1 deletion src/app/views/leads/leads-form/NewLead.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ const NewLead = () => {
bc.marketing().getAcademySingleLead(id)
.then(({ data }) => {
if (!availableCourses || availableCourses.length === 0){
console.error("AvailableCourses is empty")
return
}

Expand Down