Skip to content

Commit

Permalink
Merge pull request #29 from unboxed/architecture-cleanup
Browse files Browse the repository at this point in the history
Update architecture to be easier to read for future development
  • Loading branch information
HonTastic2 committed Jul 31, 2024
2 parents ccccf73 + 0966965 commit d19e77a
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 132 deletions.
133 changes: 2 additions & 131 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { SearchBox, ErrorText, HintText } from 'govuk-react';
import './App.css';
import './govuk-styles.scss';
import axios from 'axios';
import LocationMarker from './LocationMarker';
import LocationMarker from './components/LocationMarker';
import {filterTable, sortTable, resetTable} from './components/Table';
// import data from './london-spots.json';
let pageSize = 50;
let zoomSize = 16;
Expand Down Expand Up @@ -54,7 +55,6 @@ const fetchPostCode = async(postcode) => {
}
}

// https://youmightnotneedjquery.com/#ready
function ready(fn) {
if (document.readyState !== 'loading') {
fn();
Expand All @@ -69,15 +69,6 @@ function generateElements(html) {
return template.content;
}

// const fetchApplicationDocs = async(ref) => {
// try {
// const response = await axios.get(apiUrl + ref + "/documents");
// return response.data;
// } catch (e) {
// console.log(e);
// }
// }

// Parsing data acquired from GET request
function parseJSON (data, iter, applicationData) {
for (let i = 0; i < Object.keys(data.data).length; i++) {
Expand Down Expand Up @@ -204,112 +195,6 @@ function App () {
}
}

function filterTable(event) {
var table = document.getElementById("applicationTable");
var tr = table.getElementsByTagName("tr");
var filterSelect = event.target;

for (let i = 0; i < tr.length; i++) {
var row = tr[i];
var td = row.getElementsByTagName("td")[4];
if (td) {
if ("None" === filterSelect.value) {
row.style.display = "";
} else if (td.innerHTML === filterSelect.value) {
row.style.display = "";
} else {
row.style.display = "none";
}
}
}
}

function sortTable() {
var table = document.getElementById("applicationTable");
var selectedVal = document.getElementById("sortSelect").value;
var switching = true;
var i, x, y, shouldSwitch, rows;

if (selectedVal === "date_des") {
while (switching) {
switching = false;
rows = table.rows;

for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[3];
y = rows[i + 1].getElementsByTagName("td")[3];

if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}

if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
else {
while (switching) {
switching = false;
rows = table.rows;

for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;

if (selectedVal === "ref") {
x = rows[i].getElementsByTagName("td")[1];
y = rows[i + 1].getElementsByTagName("td")[1];
}
else if (selectedVal === "date_asc" || selectedVal === "date_des") {
x = rows[i].getElementsByTagName("td")[3];
y = rows[i + 1].getElementsByTagName("td")[3];
}
else { break; }

if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}

if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
}

// function searchTable() {
// var input = document.getElementById("tableSearchInput").value.toUpperCase();
// var tr = document.getElementById("applicationTable").getElementsByTagName("tr");

// for (let i = 0; i < tr.length; i++) {
// var tdAddr = tr[i].getElementsByTagName("td")[0];
// var tdRef = tr[i].getElementsByTagName("td")[1];
// var tdDesc = tr[i].getElementsByTagName("td")[2];
// var tdStat = tr[i].getElementsByTagName("td")[4];

// if (tdAddr) {
// if (tdAddr.innerText.toUpperCase().indexOf(input) > -1 || tdRef.innerText.toUpperCase().indexOf(input) > -1 || tdDesc.innerText.toUpperCase().indexOf(input) > -1 ) {
// if (document.getElementById("filterSelect").value === "None") {
// tr[i].style.display = "";
// }
// else if (document.getElementById("filterSelect").value === tdStat.innerText) {
// tr[i].style.display = "";
// }
// }
// else {
// tr[i].style.display = "none";
// }
// }
// }
// }

const onEachFeature = (feature, layer) => {
if (feature.properties && feature.properties.description && feature.properties.status && feature.properties.publicUrl) {

Expand All @@ -328,12 +213,6 @@ function App () {
<p class="govuk-body" style="font-size: 14px">Reference: ${feature.properties.reference}</p>
<p class="govuk-body" style="font-size: 14px">Planned work: ${feature.properties.description}</p>
<p class="govuk-body" style="font-size: 14px">Current status: ${feature.properties.status}</p>`;

// const button = document.createElement('button');
// button.innerHTML = "More info";
// button.onclick = async function(){console.log(await fetchApplicationDocs(feature.properties.reference))};
// div.appendChild(button);

layer.bindPopup(div);
}
else if (feature.properties && feature.properties.description) {
Expand Down Expand Up @@ -418,14 +297,6 @@ function App () {
});
}

function resetTable() {
var tr = document.getElementById("applicationTable").getElementsByTagName("tr");
for (let i = 0; i < tr.length; i++) {
tr[i].style.display = "";
}
document.getElementById('filterSelect').selectedIndex = 0;
}

const bindSearchToEnter = () => {
var input = document.getElementById("searchInput");
if (input === null) { return }
Expand Down
2 changes: 1 addition & 1 deletion src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jest.mock('react-leaflet', () => {
};
});

jest.mock('./LocationMarker', () => {
jest.mock('./components/LocationMarker', () => {
return jest.fn(() => <div>Mocked LocationMarker</div>);
});

Expand Down
File renamed without changes.
118 changes: 118 additions & 0 deletions src/components/Table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
function filterTable(event) {
var table = document.getElementById("applicationTable");
var tr = table.getElementsByTagName("tr");
var filterSelect = event.target;

for (let i = 0; i < tr.length; i++) {
var row = tr[i];
var td = row.getElementsByTagName("td")[4];
if (td) {
if ("None" === filterSelect.value) {
row.style.display = "";
} else if (td.innerHTML === filterSelect.value) {
row.style.display = "";
} else {
row.style.display = "none";
}
}
}

}


function sortTable() {
var table = document.getElementById("applicationTable");
var selectedVal = document.getElementById("sortSelect").value;
var switching = true;
var i, x, y, shouldSwitch, rows;

if (selectedVal === "date_des") {
while (switching) {
switching = false;
rows = table.rows;

for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[3];
y = rows[i + 1].getElementsByTagName("td")[3];

if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}

if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
else {
while (switching) {
switching = false;
rows = table.rows;

for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;

if (selectedVal === "ref") {
x = rows[i].getElementsByTagName("td")[1];
y = rows[i + 1].getElementsByTagName("td")[1];
}
else if (selectedVal === "date_asc" || selectedVal === "date_des") {
x = rows[i].getElementsByTagName("td")[3];
y = rows[i + 1].getElementsByTagName("td")[3];
}
else { break; }

if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}

if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
}

function resetTable() {
var tr = document.getElementById("applicationTable").getElementsByTagName("tr");
for (let i = 0; i < tr.length; i++) {
tr[i].style.display = "";
}
document.getElementById('filterSelect').selectedIndex = 0;
}

// function searchTable() {
// var input = document.getElementById("tableSearchInput").value.toUpperCase();
// var tr = document.getElementById("applicationTable").getElementsByTagName("tr");

// for (let i = 0; i < tr.length; i++) {
// var tdAddr = tr[i].getElementsByTagName("td")[0];
// var tdRef = tr[i].getElementsByTagName("td")[1];
// var tdDesc = tr[i].getElementsByTagName("td")[2];
// var tdStat = tr[i].getElementsByTagName("td")[4];

// if (tdAddr) {
// if (tdAddr.innerText.toUpperCase().indexOf(input) > -1 || tdRef.innerText.toUpperCase().indexOf(input) > -1 || tdDesc.innerText.toUpperCase().indexOf(input) > -1 ) {
// if (document.getElementById("filterSelect").value === "None") {
// tr[i].style.display = "";
// }
// else if (document.getElementById("filterSelect").value === tdStat.innerText) {
// tr[i].style.display = "";
// }
// }
// else {
// tr[i].style.display = "none";
// }
// }
// }
// }

export { sortTable, filterTable, resetTable };

0 comments on commit d19e77a

Please sign in to comment.