This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aiden Keating
committed
Aug 19, 2019
0 parents
commit 5b908b6
Showing
15 changed files
with
11,056 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
Oops, something went wrong.