Skip to content

Commit

Permalink
wiring of api call
Browse files Browse the repository at this point in the history
  • Loading branch information
jzakotnik committed Aug 2, 2021
1 parent b84ed3d commit dd45331
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 71 deletions.
32 changes: 0 additions & 32 deletions nextjs/mwe/pages/[id].js

This file was deleted.

13 changes: 7 additions & 6 deletions nextjs/mwe/pages/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,26 @@ export default function Admin(props) {
const handleCreate = (event) => {
console.log("Create links..");
event.preventDefault();
const adminID = uuidv4();
const readerID = uuidv4();
const authid = { admin: uuidv4(), reader: uuidv4() };
const defaultLunchProfile = {
mon: true,
tue: true,
wed: true,
wed: false,
thu: true,
fri: true,
};
setAuthurl({ admin: adminID, reader: readerID });

fetch("http://localhost:3000/api/insertData", {
method: "POST",
body: JSON.stringify({ authurl, defaultLunchProfile }),
body: JSON.stringify({ authid, defaultLunchProfile }),
headers: {
"Content-Type": "application/json",
},
})
.then((response) => console.log("Success:", JSON.stringify(response)))
.then((response) => {
console.log("Success:", JSON.stringify(response));
setAuthurl(authid);
})
.catch((error) => console.error("Error:", error));
};

Expand Down
26 changes: 7 additions & 19 deletions nextjs/mwe/pages/api/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@ var level = require("level-rocksdb");

export default function handler(req, res) {
var db = level("./lunchdb");
//console.log(req.body);
/*db.get("juresUUID", function (err, value) {
if (err) return console.log("Ooops!", err); // likely the key was not found
console.log("Got request for the database");
console.log(req.query.id);
db.get(req.query.id, function (err, value) {
if (err) return console.log("DB request failed", err); // likely the key was not found
console.log(value);
});*/
db.createReadStream()
.on("data", function (data) {
console.log(data.key, "=", data.value);
})
.on("error", function (err) {
console.log("Oh my!", err);
})
.on("close", function () {
console.log("Stream closed");
})
.on("end", function () {
console.log("Stream ended");
});
db.close();
res.end().status(200);
db.close();
res.end(value);
});
}
24 changes: 16 additions & 8 deletions nextjs/mwe/pages/api/insertData.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ export default function handler(req, res) {
var db = level("./lunchdb");
console.log("Inserting data into DB");
console.log(req.body);
const authurl = req.body.authurl;
const lunchprofile = req.body.lunchprofile;
console.log(req.body.authid);
console.log(req.body.defaultLunchProfile);
const authid = req.body.authid;
const lunchprofile = req.body.defaultLunchProfile;

db.put(
authurl.admin,
{ lunchprofile, reader: authurl.reader, admin: true },
authid.admin,
JSON.stringify({ lunchprofile, reader: authid.reader, admin: true }),
function (err) {
if (err) return console.log("Ooops!", err); // some kind of I/O error
if (err)
return console.log("Inserting of new admin URL did not work", err); // some kind of I/O error
}
);
db.put(
authid.reader,
JSON.stringify({ lunchprofile, admin: false }),
function (err) {
if (err)
return console.log("Inserting of new reader URL did not work", err); // some kind of I/O error
}
);
db.put(authurl.reader, { lunchprofile, admin: false }, function (err) {
if (err) return console.log("Ooops!", err); // some kind of I/O error
});
db.close();

res.end().status(200);
Expand Down
8 changes: 7 additions & 1 deletion nextjs/mwe/pages/lunchprofile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { useRouter } from "next/router";
import Avatar from "@material-ui/core/Avatar";
import Button from "@material-ui/core/Button";
Expand Down Expand Up @@ -45,6 +45,12 @@ export default function LunchProfile(props) {
fri: true,
});

useEffect(() => {
console.log("Use Effect with props:");
console.log(props);
setDayselection(props.lunchdata);
}, [props]);

const handleSubmit = (event) => {
event.preventDefault();
//console.log(event);
Expand Down
10 changes: 5 additions & 5 deletions nextjs/mwe/pages/profile/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const Profile = (data) => {
};

// This gets called on every request
export async function getServerSideProps() {
// Fetch data from external API
const res = await fetch(
"http://localhost:3000/api/getData?id=48bf4e6a-c92e-4d7c-9fad-6e141685fb31"
);
export async function getServerSideProps(context) {
const { id } = context.query;
console.log("Page get server side props for page: " + id);

const res = await fetch("http://localhost:3000/api/getData?id=" + id);
const data = await res.json();
console.log("Received response from API");
console.log(data);
Expand Down

0 comments on commit dd45331

Please sign in to comment.