Skip to content

Commit e6a7024

Browse files
committed
Add api call to send data to server. Introduce data state var.
1 parent 9e91f11 commit e6a7024

File tree

7 files changed

+61
-33
lines changed

7 files changed

+61
-33
lines changed

api.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

backend-aws/handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ def format_response(status_code, body):
1818

1919
def get_data(event, context):
2020
response = table.get_item(Key={"dataId": "1"})
21-
print(response)
2221
data = response.get("Item")
23-
print(data)
2422
return format_response(200, data)
2523

2624
def store_data(event, context):
27-
print(event)
2825
item = json.loads(event.get('body'))
29-
item["dataId"] = "2"
26+
item["dataId"] = "1"
3027
response = table.put_item(Item=item)
3128
return format_response(200, "Stored the data")

backend-aws/serverless.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@ functions:
2323
- http:
2424
path: data
2525
method: get
26+
cors: true
2627
store_data:
2728
handler: handler.store_data
2829
events:
2930
- http:
3031
path: data
3132
method: post
33+
cors:
34+
origin: '*'
35+
headers:
36+
- Content-Type
37+
- Access-Control-Allow-Origin
38+
- Access-Control-Allow-Credentials
3239

3340
resources:
3441
Resources:

frontend/api.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const BACKEND_URL = "https://syar2tbuna.execute-api.us-east-1.amazonaws.com/"
2+
const DATA_ENDPOINT = "dev/data"
3+
4+
function getData() {
5+
return fetch(`${BACKEND_URL}${DATA_ENDPOINT}`)
6+
.then(response => {
7+
if (response.ok) {
8+
return response.json()
9+
} throw new Error(response.statusText)
10+
})
11+
.catch(error => { throw new Error(error.message) });
12+
}
13+
14+
function storeData(data) {
15+
return fetch(`${BACKEND_URL}${DATA_ENDPOINT}`,
16+
{
17+
method: 'POST',
18+
body: JSON.stringify(data),
19+
headers: {
20+
'Content-Type': 'application/json',
21+
"Access-Control-Allow-Origin": "*",
22+
"Access-Control-Allow-Credentials": true
23+
}
24+
})
25+
.then(response => {
26+
if (response.ok) {
27+
return response.json()
28+
} throw new Error(response.statusText)
29+
})
30+
.catch(error => { throw new Error(error.message) });
31+
}

graph.js renamed to frontend/graph.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const graphElem = document.getElementById("idea-graph");
22
const Graph = ForceGraph3D()(graphElem)
3+
var dataState = {}
34
displayGraph();
45

56
window.onclick = function (event) {
@@ -9,11 +10,9 @@ window.onclick = function (event) {
910
}
1011

1112
async function displayGraph() {
12-
let newData
1313
try {
14-
newData = await getData();
15-
newData.nodes = newData.nodes.map(node => ({...node, tags: new Set(node.tags)}));
16-
Graph.graphData(newData)
14+
dataState = await getData();
15+
Graph.graphData(JSON.parse(JSON.stringify(dataState)))
1716
.nodeLabel("name")
1817
.onNodeHover(node => graphElem.style.cursor = node ? "pointer" : null)
1918
.onNodeClick(node => {
@@ -30,7 +29,7 @@ async function displayGraph() {
3029
});
3130
}
3231
catch (error) {
33-
console.log(error)
32+
console.log(error);
3433
}
3534
}
3635

@@ -42,7 +41,7 @@ function displayDetail(node) {
4241
const detailDescription = document.getElementById("detail-description");
4342

4443
detailName.textContent = node.name;
45-
detailTags.textContent = [...node.tags].join(', ');
44+
detailTags.textContent = node.tags.filter(Boolean).toString();
4645
detailCreator.textContent = node.creator;
4746
detailDescription.textContent = node.description;
4847

@@ -66,24 +65,30 @@ function onModalSubmitClick(event, form) {
6665
const ideaDescription = form["description"];
6766
const creatorName = form["creator-name"];
6867

69-
const { nodes, links } = Graph.graphData();
70-
const id = nodes.length;
68+
const id = dataState.nodes.length;
69+
const newTags = new Set(ideaTags.value.split(",").map(tag => tag.trim().toLowerCase()));
7170

7271
const newIdea = {
73-
id: id,
72+
id: id.toString(),
7473
name: ideaName.value,
75-
tags: new Set(ideaTags.value.split(",").map(tag => tag.trim().toLowerCase())),
74+
tags: Array.from(newTags),
7675
creator: creatorName.value,
7776
description: ideaDescription.value,
7877
};
79-
const newLinks = matchTags(newIdea, nodes)
78+
const newLinks = matchTags(newIdea, dataState.nodes)
8079

81-
Graph.graphData({
82-
nodes: [...nodes, { ...newIdea }],
83-
links: [...links, ...newLinks]
84-
});
80+
dataState.nodes = [...dataState.nodes, {...newIdea}]
81+
dataState.links = [...dataState.links, ...newLinks]
8582

83+
Graph.graphData(JSON.parse(JSON.stringify(dataState)))
8684
form.reset();
85+
86+
try {
87+
storeData(dataState);
88+
}
89+
catch (error) {
90+
console.log(error);
91+
}
8792
}
8893

8994
function onModalCloseClick(modal) {
@@ -93,8 +98,8 @@ function onModalCloseClick(modal) {
9398
function matchTags(newIdea, nodes) {
9499
var newLinks = [];
95100
for (idea of nodes) {
96-
var intersection = new Set([...idea.tags].filter(x => newIdea.tags.has(x)));
97-
if (intersection.size != 0) {
101+
var intersection = idea.tags.filter(x => newIdea.tags.includes(x));
102+
if (intersection.length != 0) {
98103
newLinks = [...newLinks, { source: newIdea.id, target: idea.id }]
99104
}
100105
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)