Admin template working now to update status. Not sure why I had to resort to SetTimeout in order to refresh page after form submission, but nothing else worked.
This commit is contained in:
		
							parent
							
								
									0277ae81ee
								
							
						
					
					
						commit
						5631fb216e
					
				
							
								
								
									
										26
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								server.js
									
									
									
									
									
								
							| @ -118,18 +118,11 @@ app.get('/', function(req, res){ | ||||
|     }); | ||||
| }); | ||||
| 
 | ||||
| app.post(':loc/admin', isAuthenticated, function(req, res){ | ||||
|     winston.info('POST by admin at ' + loc); | ||||
|     // possible to forward req to POST to '/'?
 | ||||
|     var loc = req.params.loc; | ||||
| }); | ||||
| 
 | ||||
| app.post('/', function(req, res){ | ||||
|     var postEvent = req.body.postEvent; | ||||
|     var source = req.body.source; | ||||
|     winston.info(req.body); | ||||
|     // If it's stripped down JSON sent by cell modem, there won't be a req.body.data key w/full-nested JSON as value!!!
 | ||||
|     // ???how to fix???
 | ||||
|     // If it's stripped down JSON sent by cell modem, there won't be a req.body.data key w/full-nested JSON as value.
 | ||||
|     // If post body includes coreid and published_at, assume it's from a particle devcie and parse accordingly.
 | ||||
|     if (req.body.coreid !== undefined && req.body.published_at !== undefined) { | ||||
|         var innerDataJSON = JSON.parse(req.body.data); | ||||
| @ -144,6 +137,20 @@ app.post('/', function(req, res){ | ||||
|             ); | ||||
|             done(); | ||||
|         }); | ||||
|     } else if (req.body.loc !== undefined) {  // Assume POST from /admin template
 | ||||
|         var coreid = '2a002b000947343432313031'  // Hard-code TT coreid for now, otherwise need to look up coreid from location value.
 | ||||
|         var received_at = new Date(Date.now()); | ||||
|         received_at = received_at.toISOString(); | ||||
|         var status = req.body.status; | ||||
|         winston.info(coreid + '; ' + received_at + '; ' + status + '; ' + JSON.stringify(req.body, null, 4)); | ||||
|         pg.connect(conString, function(err, client, done) { | ||||
|             if(err) { return winston.error('error fetching client from pool', err); } | ||||
|             client.query( | ||||
|                 "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);", | ||||
|                 [JSON.stringify(req.body, null, 4), coreid, received_at, status] | ||||
|             ); | ||||
|             done(); | ||||
|         }); | ||||
|     } else { // parse for minimal data from cell modem.
 | ||||
|         var deviceid = JSON.stringify(req.body.data, null, 4).slice(1,4); | ||||
|         var statusCode = JSON.stringify(req.body.data, null, 4).slice(5,7); | ||||
| @ -197,14 +204,17 @@ app.get('/:loc/admin', isAuthenticated,  function(req, res) { | ||||
|                 } | ||||
|                 // Loop over elements in rows array, convert ugly UTC times to pretty local times.
 | ||||
|                 result.rows.forEach(function(row){ | ||||
|                     row.loc = loc; | ||||
|                     row.pubdate = courtsopenUtils.getLocDateFromUTC(row.published_at); | ||||
|                     row.pubtime = courtsopenUtils.getLocTimeFromUTC(row.published_at); | ||||
|                     if(row.status.toLowerCase().indexOf('closed') > -1){ | ||||
|                         row.statusclass = 'closed'; | ||||
|                         row.oppstatus = 'open'; | ||||
|                         row.prettystatus = 'Open'; | ||||
|                     } else {  | ||||
|                         row.statusclass = 'open'; | ||||
|                         row.oppstatus = 'closed'; | ||||
|                         row.prettystatus = 'Closed'; | ||||
|                     } | ||||
|                 }); | ||||
|                 res.render('admin', {values: result.rows}, function(err, html) { | ||||
|  | ||||
| @ -19,8 +19,9 @@ | ||||
| <hr></hr> | ||||
| 
 | ||||
| <div> | ||||
|     <form action="/" method="post"> | ||||
|         <button type="submit" class="jumbotron {{values.0.oppstatus}} col-xs-4 col-xs-offset-4 btn btn-primary"><h2>Change Status to: {{values.0.oppstatus}}</h2></button> | ||||
|         <input type="text" class="hidden" value="{{values.0.oppstatus}}"/> | ||||
|     <form action="/" method="post" onsubmit="setTimeout(function () { window.location.reload(); }, 1000)"> | ||||
|         <button type="submit" class="jumbotron {{values.0.oppstatus}} col-xs-4 col-xs-offset-4 btn btn-primary"><h2>Change Status to: {{values.0.prettystatus}}</h2></button> | ||||
|         <input name="status" class="hidden" value="{{values.0.prettystatus}}"/> | ||||
|         <input name="loc" class="hidden" value="{{values.0.loc}}"/> | ||||
|     </form> | ||||
| </div> | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user