diff --git a/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.create.js b/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.create.js index e56f6d8a4d1cfc6dd273e13e744dfdbd2dd72018..3b2dc318d629e729a796321ae29ea1f9c18f0572 100644 --- a/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.create.js +++ b/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.create.js @@ -9,11 +9,11 @@ import { InputTextarea } from 'primereact/inputtextarea'; import { Button } from 'primereact/button'; import { Dialog } from 'primereact/components/dialog/Dialog'; import Flatpickr from "react-flatpickr"; - import AppLoader from '../../layout/components/AppLoader'; import PageHeader from '../../layout/components/PageHeader'; import UIConstants from '../../utils/ui.constants'; import { CustomDialog } from '../../layout/components/CustomDialog'; + import ProjectService from '../../services/project.service'; import ReservationService from '../../services/reservation.service'; import Jeditor from '../../components/JSONEditor/JEditor'; @@ -44,6 +44,9 @@ export class ReservationCreate extends Component { stop_time: null, project: (props.match?props.match.params.project:null) || null, }, + reservationStrategy: { + id: null, + }, errors: {}, // Validation Errors validFields: {}, // For Validation validForm: false, // To enable Save Button @@ -51,6 +54,7 @@ export class ReservationCreate extends Component { }; this.projects = []; // All projects to load project dropdown this.reservationTemplates = []; + this.reservationStrategies = []; // Validateion Rules this.formRules = { @@ -67,6 +71,8 @@ export class ReservationCreate extends Component { this.checkIsDirty = this.checkIsDirty.bind(this); this.close = this.close.bind(this); this.initReservation = this.initReservation.bind(this); + this.changeStrategy = this.changeStrategy.bind(this); + this.setEditorFunction = this.setEditorFunction.bind(this); } async componentDidMount() { @@ -74,19 +80,20 @@ export class ReservationCreate extends Component { } /** - * Initialized the reservation template + * Initialize the reservation and relevant details */ async initReservation() { const promises = [ ProjectService.getProjectList(), ReservationService.getReservationTemplates(), - UtilService.getUTC() + UtilService.getUTC(), + ReservationService.getReservationStrategyTemplates() ]; let emptyProjects = [{url: null, name: "Select Project"}]; - Promise.all(promises).then(responses => { - let systemTime = moment.utc(responses[2]); + Promise.all(promises).then(responses => { this.projects = emptyProjects.concat(responses[0]); this.reservationTemplates = responses[1]; - + let systemTime = moment.utc(responses[2]); + this.reservationStrategies = responses[3]; let reservationTemplate = this.reservationTemplates.find(reason => reason.name === 'resource reservation'); let schema = { properties: {} @@ -98,12 +105,35 @@ export class ReservationCreate extends Component { paramsSchema: schema, isLoading: false, reservationTemplate: reservationTemplate, - systemTime: systemTime + systemTime: systemTime, }); }); } + /** + * + * @param {Id} strategyId - id value of reservation strategy template + */ + async changeStrategy(strategyId) { + this.setState({isLoading: true}); + const reservationStrategy = _.find(this.reservationStrategies, {'id': strategyId}); + let paramsOutput = {}; + if(reservationStrategy.template.parameters) { + //if reservation strategy has parameter then prepare output parameter + + } else { + paramsOutput = _.cloneDeep(reservationStrategy.template); + delete paramsOutput["$id"]; + } + this.setState({ + isLoading: false, + reservationStrategy: reservationStrategy, + paramsOutput: paramsOutput, + isDirty: true}); + this.initReservation(); + } + /** * Function to set form values to the Reservation object * @param {string} key @@ -123,8 +153,6 @@ export class ReservationCreate extends Component { [key]: true }}); } - - } /** @@ -253,9 +281,7 @@ export class ReservationCreate extends Component { if (reservation && reservation.id){ const dialog = {header: 'Success', detail: 'Reservation is created successfully. Do you want to create another Reservation?'}; this.setState({ dialogVisible: true, dialog: dialog, paramsOutput: {}, showDialog: false, isDirty: false}) - }/* else { - this.growl.show({severity: 'error', summary: 'Error Occured', detail: 'Unable to save Reservation', showDialog: false, isDirty: false}); - }*/ + } } /** @@ -274,6 +300,9 @@ export class ReservationCreate extends Component { dialog: { header: '', detail: ''}, errors: [], reservation: tmpReservation, + reservationStrategy: { + id: null, + }, paramsSchema: null, paramsOutput: null, validEditor: false, @@ -308,6 +337,14 @@ export class ReservationCreate extends Component { this.setState({showDialog: false}); } + /** + * JEditor's function that to be called when parent wants to trigger change in the JSON Editor + * @param {Function} editorFunction + */ + setEditorFunction(editorFunction) { + this.setState({editorFunction: editorFunction}); + } + render() { if (this.state.redirect) { return <Redirect to={ {pathname: this.state.redirect} }></Redirect> @@ -437,6 +474,19 @@ export class ReservationCreate extends Component { {(this.state.errors.project && this.state.touched.project) ? this.state.errors.project : "Select Project"} </label> </div> + <div className="col-lg-1 col-md-1 col-sm-12"></div> + <label htmlFor="strategy" className="col-lg-2 col-md-2 col-sm-12">Reservation Strategy</label> + <div className="col-lg-3 col-md-3 col-sm-12" data-testid="strategy" > + <Dropdown inputId="strategy" optionLabel="name" optionValue="id" + tooltip="Choose Reservation Strategy Template to set default values for create Reservation" tooltipOptions={this.tooltipOptions} + value={this.state.reservationStrategy.id} + options={this.reservationStrategies} + onChange={(e) => {this.changeStrategy(e.value)}} + placeholder="Select Strategy" /> + <label className={(this.state.errors.reservationStrategy && this.state.touched.reservationStrategy) ?"error":"info"}> + {(this.state.errors.reservationStrategy && this.state.touched.reservationStrategy) ? this.state.errors.reservationStrategy : "Select Reservation Strategy Template"} + </label> + </div> </div> <div className="p-grid"> diff --git a/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.list.js b/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.list.js index 979508e47c8880a62d0e368adb8d356620c3d653..a1192925ef42f23a809a60da71488e4d3430e7a9 100644 --- a/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.list.js +++ b/SAS/TMSS/frontend/tmss_webapp/src/routes/Reservation/reservation.list.js @@ -167,7 +167,7 @@ export class ReservationList extends Component{ mergeResourceWithReservation ( reservation, params) { if( params ){ Object.keys(params).map((key, i) => ( - key !== 'description'? reservation[key]= params[key] : '' + ['name', 'description'].indexOf(key)<0? reservation[key]= params[key] : '' )); } return reservation; diff --git a/SAS/TMSS/frontend/tmss_webapp/src/services/reservation.service.js b/SAS/TMSS/frontend/tmss_webapp/src/services/reservation.service.js index d50476d3d0e5faeff74d5b39e2c6fe847a9a5b2b..5811e0e844a453b69b2903d4c3ab51c4927c1742 100644 --- a/SAS/TMSS/frontend/tmss_webapp/src/services/reservation.service.js +++ b/SAS/TMSS/frontend/tmss_webapp/src/services/reservation.service.js @@ -74,6 +74,15 @@ const ReservationService = { console.error(error); } }, + getReservationStrategyTemplates: async function () { + try { + const url = `/api/reservation_strategy_template/?ordering=id`; + const response = await axios.get(url); + return response.data.results; + } catch (error) { + console.error(error); + } + }, } export default ReservationService;