-
Notifications
You must be signed in to change notification settings - Fork 34
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
Merged
alesanchezr
merged 10 commits into
breatheco-de:development
from
avanegasp:available_as_saas
Jan 23, 2025
Merged
Available as saas #393
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
597570e
Add alert warnig when available_as_saas is true
avanegasp 6be9905
Resolving when availables is true and show it in input
avanegasp b77b934
Merge branch 'development' into available_as_saas
avanegasp 9d861c6
Add new code to available_as_saas
avanegasp 0729492
Add POST method with logic
avanegasp 77407a1
Add rechange name
avanegasp 657a0f4
Add styles
avanegasp 49d5bf6
Add plan's and submit validations
avanegasp 052d8de
Add data?.data.id and remove console
avanegasp 3a47bfb
Refact code and fix typo
avanegasp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
@@ -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 | ||
}; | ||
|
@@ -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.lenght === 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 | ||
}} | ||
> | ||
|
@@ -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} | ||
|
@@ -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 axios.get(`${config.REACT_APP_API_HOST}/v1/payments/plan?cohort=${selectedCohortSlug}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this request in the payments object of requests located in the |
||
.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"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
length is misspelled