Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiden Keating committed Aug 19, 2019
0 parents commit 5b908b6
Show file tree
Hide file tree
Showing 15 changed files with 11,056 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Integreatly Walkthrough Applications

Repo containing any custom Integreatly applications used in walkthroughs

- [Basic CRUD Application](./fruit-app)
13 changes: 13 additions & 0 deletions fruit-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:12-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm ci --only=production

COPY . .

EXPOSE 8080

ENTRYPOINT [ "node", "server.js" ]
10 changes: 10 additions & 0 deletions fruit-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Fruit CRUD Application

Very basic CRUD application for fruit

## Running locally

- Install dependencies, `npm install`
- Run the server, `npm run`

When `client.js` has been modified, run `npm run client:build`
69 changes: 69 additions & 0 deletions fruit-app/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const io = require('socket.io-client');

fetch('/api/fruits')
.then(resp => resp.json())
.then(fruit => refreshList(fruit))
.then(() => {
const socket = io();

socket.on('fruit', (fruitList) => {
refreshList(fruitList);
});
});

function handleCreateFruit(e) {
e.preventDefault();

const fruitNameElem = document.getElementById('fruitInput');
const fruitName = fruitNameElem.value;

fetch('/api/fruits', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: fruitName })
});

fruitNameElem.value = '';

return false;
}

const fruitForm = document.getElementById('fruitForm');
fruitForm.addEventListener('submit', handleCreateFruit);

function buildFruitElem(fruit) {
const fruitElem = document.createElement('tr');
const fruitIDElem = document.createElement('td');
fruitIDElem.textContent = fruit.id;
const fruitNameElem = document.createElement('td');
fruitNameElem.textContent = fruit.name;
const fruitActionElem = document.createElement('button');
fruitActionElem.className = 'pure-button';
fruitActionElem.style = 'margin: 5px;'
fruitActionElem.textContent = 'Delete';
fruitActionElem.onclick = deleteFruit.bind(null, fruit.id);

fruitElem.appendChild(fruitIDElem);
fruitElem.appendChild(fruitNameElem);
fruitElem.appendChild(fruitActionElem);

return fruitElem;
}

function deleteFruit(fruitID) {
fetch(`/api/fruits/${fruitID}`, {
method: 'DELETE'
});
}

function refreshList(fruitList) {
const fruitListElem = document.getElementById('fruitList');
const fruitElems = fruitList.map(f => buildFruitElem(f));
// remove existing children
while (!!fruitListElem.firstChild) {
fruitListElem.removeChild(fruitListElem.firstChild);
}
fruitElems.forEach(e => fruitListElem.appendChild(e));
}
Loading

0 comments on commit 5b908b6

Please sign in to comment.