private static ReactCode DrawSelect(RequestDesign design, int grid) { ReactCode codeGen = new ReactCode(); StringBuilder sbHtml = new StringBuilder(); sbHtml.AppendLine("<Grid item xs={" + grid.ToString() + "}>"); sbHtml.AppendLineFormat("<InputLabel id='{0}'>{1}</InputLabel>", design.ObjectName, design.Level); sbHtml.AppendLineFormat("<Select labelId = '{0}' {1} fullWidth {2}>", design.ObjectName, design.Required ? "required" : "", "onChange={ e=>Set_" + design.ObjectName + "(e.target.value)}"); foreach (var value in design.Values) { sbHtml.AppendLineFormat("<MenuItem value={0}>{1}</MenuItem>", "{'" + value + "'}", value); } sbHtml.AppendLine("</Select>"); sbHtml.AppendLineFormat("<FormHelperText>{0}</FormHelperText>", design.Description); sbHtml.AppendLine("</Grid>"); codeGen.Html = sbHtml.ToString(); codeGen.States.Add(design.ObjectName, string.Format("const [{0}, {1}] = useState('{2}')", design.ObjectName, "Set_" + design.ObjectName, design.Value)); codeGen.Imports.Add("import Select from '@material-ui/core/Select';"); codeGen.Imports.Add("import MenuItem from '@material-ui/core/MenuItem';"); codeGen.Imports.Add("import InputLabel from '@material-ui/core/InputLabel';"); codeGen.Imports.Add("import FormHelperText from '@material-ui/core/FormHelperText';"); return(codeGen); }
private static ReactCode DrawTextBox(RequestDesign design, string type, int grid) { ReactCode codeGen = new ReactCode(); StringBuilder sbHtml = new StringBuilder(); sbHtml.AppendLine("<Grid item xs={" + grid.ToString() + "}>"); sbHtml.AppendLineFormat("<TextField label='{0}' helperText='{1}' type='{2}' {3} {4} fullWidth />", design.Level, design.Description, type, design.Required ? "required" : "", "onInput={ e=>Set_" + design.ObjectName + "(e.target.value)}"); sbHtml.AppendLine("</Grid>"); codeGen.Html = sbHtml.ToString(); codeGen.States.Add(design.ObjectName, string.Format("const [{0}, {1}] = useState('{2}')", design.ObjectName, "Set_" + design.ObjectName, design.Value)); codeGen.Imports.Add("import TextField from '@material-ui/core/TextField';"); return(codeGen); }
private static ReactCode DrawSubmitModel(Request request, List <ReactCode> gens) { ReactCode codeGen = new ReactCode(); Dictionary <string, string> states = new Dictionary <string, string>(); string postModel = GeneratePostModel(request.Operation, ref states); string url = "'" + request.Server + request.Operation.Name + "'"; foreach (var param in request.Operation.Params) { url = url.Replace("{" + param.Name + "}", "'+query_" + param.Name + "+'"); states.Add("query_" + param.Name, param.Type); } url = url.Replace("+''", ""); var usedState = new List <string>(); foreach (var gen in gens) { foreach (var state in gen.States) { usedState.Add(state.Key); } } foreach (var state in states) { if (!usedState.Contains(state.Key)) { codeGen.States.Add(state.Key, string.Format("const [{0}, {1}] = useState({2})", state.Key, "Set_" + state.Key, state.Value == "integer" ? "0" : (state.Value == "array" ? "[]" : "''"))); } } StringBuilder sbFunc = new StringBuilder(); sbFunc.AppendLine("const onSubmit = useCallback((event) => {"); sbFunc.AppendLine("event.preventDefault();"); sbFunc.AppendLine("const requestOptions = {"); sbFunc.AppendLineFormat("method: '{0}',", request.Operation.Verb.ToUpper()); sbFunc.AppendLine("headers: { 'Content-Type': 'application/json' },"); if (request.Operation.Verb == "Post" || request.Operation.Verb == "Put") { sbFunc.AppendLineFormat("body: JSON.stringify({0})", postModel); } sbFunc.AppendLine("};"); sbFunc.AppendLineFormat("fetch({0}, requestOptions)", url); sbFunc.AppendLine(".then(response => response.json())"); sbFunc.AppendLine(".then(data => console.log(data))"); sbFunc.AppendLine(".catch (err => console.log(err));"); sbFunc.AppendLine("})"); codeGen.Function.Add(sbFunc.ToString()); codeGen.Html = "<Grid item xs={12}><Button variant='contained' color='primary' type='submit'>Submit</Button></Grid>"; codeGen.Imports.Add("import Button from '@material-ui/core/Button';"); return(codeGen); }
private static ReactCode DrawArray(RequestDesign design, int grid) { ReactCode codeGen = new ReactCode(); StringBuilder sbHtml = new StringBuilder(); sbHtml.AppendLine("<Grid item xs={" + grid.ToString() + "}>"); sbHtml.AppendLine("<Grid item xs={12}>"); sbHtml.AppendLine("<Grid item xs={12} className={classes.rightMargin}>"); sbHtml.AppendLineFormat("<TextField label='{0}' helperText='{1}' type='text' {2} {3} fullWidth />", design.Level, design.Description, design.Required ? "required" : "", "onInput={ e=>Set_" + design.ObjectName + "(e.target.value, 0)}"); sbHtml.AppendLine("</Grid>"); sbHtml.AppendLineFormat("<Button variant='contained' className={0} onClick={1}>Add</Button>", "{classes.rightFloat}", "{Add_" + design.ObjectName + "}"); sbHtml.AppendLine("</Grid>"); sbHtml.AppendLine("{" + design.ObjectName + ".map((value, index) => {"); sbHtml.AppendLine("if (index > 0) {"); sbHtml.AppendLine("return ("); sbHtml.AppendLine("<Grid key={`tag-${index}`} item xs={12}>"); sbHtml.AppendLine("<Grid item xs={12} className={classes.rightMargin}>"); sbHtml.AppendLineFormat("<TextField label='{0}' type='text' {1} fullWidth />", design.Level, "onInput={ e=>Set_" + design.ObjectName + "(e.target.value, index)}"); sbHtml.AppendLine("</Grid>"); sbHtml.AppendLineFormat("<Button variant='contained' className={0} onClick={1}>Remove</Button>", "{classes.rightFloat}", "{e=> Remove_" + design.ObjectName + "(index)}"); sbHtml.AppendLine("</Grid>"); sbHtml.AppendLine(")"); sbHtml.AppendLine("}"); sbHtml.AppendLine("})}"); sbHtml.AppendLine("</Grid>"); codeGen.Html = sbHtml.ToString(); codeGen.States.Add(design.ObjectName, string.Format("const [{0}, {1}] = useState(['{2}'])", design.ObjectName, "Set_Array_" + design.ObjectName, design.Value)); StringBuilder sbFunc = new StringBuilder(); //Set Array value function sbFunc.AppendLineFormat("function Set_{0}(val, index)", design.ObjectName); sbFunc.AppendLine("{"); sbFunc.AppendLineFormat("{0}[index]=val;", design.ObjectName); sbFunc.AppendLineFormat("Set_Array_{0}([...{0}]);", design.ObjectName); sbFunc.AppendLine("}"); // Add Array sbFunc.AppendLineFormat("function Add_{0}()", design.ObjectName); sbFunc.AppendLine("{"); sbFunc.AppendLineFormat("Set_Array_{0}([...{0}, '']);", design.ObjectName); sbFunc.AppendLine("}"); //Remove Array sbFunc.AppendLineFormat("function Remove_{0}(index)", design.ObjectName); sbFunc.AppendLine("{"); sbFunc.AppendLineFormat("{0}.splice(index,1);", design.ObjectName); sbFunc.AppendLineFormat("Set_Array_{0}([...{0}]);", design.ObjectName); sbFunc.AppendLine("}"); codeGen.Function.Add(sbFunc.ToString()); codeGen.Imports.Add("import TextField from '@material-ui/core/TextField';"); return(codeGen); }