Make /_config/*.jst use ES6 superclass calls, fix failure to await the superclass...
[olly_site.git] / calculate_button_trapezium.html.jst
1 let querystring = require('querystring')
2 let stream_buffers = require('stream-buffers')
3
4 return async env => {
5   if (env.request.method == 'POST') {
6     let write_stream = new stream_buffers.WritableStreamBuffer()
7     let data = new Promise(
8       (resolve, reject) => {
9         write_stream.
10         on('finish', () => {resolve(write_stream.getContents())}).
11         on('error', () => {reject()})
12       }
13     )
14     env.request.pipe(write_stream)
15     let query = querystring.parse((await data).toString())
16     let a_length = parseFloat(query.a_length)
17     let b_length = parseFloat(query.b_length)
18     let height = parseFloat(query.height)
19     let area = (a_length + b_length) / 2 * height
20
21     let _out = []
22     _out.push(
23       `<!DOCTYPE html>
24 <html>
25 <head>
26 <link rel="stylesheet" type="text/css" href="olly.css">
27 <title>Calculation</title>
28 </head>
29 <body class="olly">
30 <h1>The Height is: ${height} units</h1>
31 <h1>The A length is: ${a_length} units</h1>
32 <h1>The B length is: ${b_length} units</h1>
33 <h1>The area is: ${area} units<sup>2</sup></h1>
34 </body>
35 </html>
36 `
37     )
38     env.site.serve(
39       env,
40       200,
41       Buffer.from(_out.join('')),
42       'calculate_button_trapezium.html.jst'
43     )
44   }
45   else {
46     let _out = []
47     _out.push(
48       `<!DOCTYPE html>
49 <html>
50 <head>
51 <link rel="stylesheet" type="text/css" href="olly.css">
52 <title>Calculation</title>
53 </head>
54 <body class="olly">
55 <h1>Error - Unable to Calculate</h1>
56 </body>
57 </html>
58 `
59     )
60     env.site.serve(
61       env,
62       200,
63       Buffer.from(_out.join('')),
64       'calculatebutton.html.jst'
65     )
66   }
67 }
68