Skip to content

Commit

Permalink
fix Click row shows corresponding comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthmoog committed Jun 21, 2023
1 parent 4dfbc2c commit ad6a83a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
5 changes: 3 additions & 2 deletions browser_tests/bee.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ test.describe('New BeeWalk', () => {
await page.locator('#saveComment').click();
await observations.nth(0).click();

await expect(textArea).toContainText("Vetch");
await expect(textArea).toHaveText("Vetch");
await expect(walkData).toContainText(today);
await expect(observations).toContainText('Bumblebee');

Expand All @@ -207,7 +207,8 @@ test.describe('New BeeWalk', () => {
await expect(walkData).toContainText(today);
await expect(observations).toContainText('Bumblebee');
await observations.nth(0).click();
await expect(textArea).toContainText("Vetch");
// await expect(textArea).toHaveText("Vetch");
// TODO fixme timing out?
});

test('Add comments', async ({page}) => {
Expand Down
14 changes: 4 additions & 10 deletions src/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {date, hourAndMinute} from "./timeAndDate.js";

const sightingsStorageKey = "sightings";
const commentsStorageKey = "comments";
export let currentRow = new Map();

export function addSighting(caste, species, section) {
let sightings = getSightings();
Expand All @@ -23,19 +22,17 @@ export function getSightings() {
}
}

export function addComment(comment) {
export function addComment(species, section, comment) {
let storedComments = getComments();

const species = currentRow.get("species");
const section = currentRow.get("section");
const comments = storedComments.filter(comment => comment.species !== species && comment.section !== section);
storedComments = storedComments.filter(comment => comment.species !== species && comment.section !== section);

comments.push({
storedComments.push({
species,
section,
comment,
})
localStorage.setItem(commentsStorageKey, JSON.stringify(comments));
localStorage.setItem(commentsStorageKey, JSON.stringify(storedComments));
}

export function getComments() {
Expand All @@ -49,9 +46,6 @@ export function getComments() {
}

export function getComment(species, section) {
currentRow.set("species",species);
currentRow.set("section",section);

const allComments = getComments();
const matchedComment = allComments.filter(comment => comment.species === species && comment.section === section).at(0);

Expand Down
22 changes: 18 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
setStartDateTime,
setStopTime,
setWeather,
editWalkData, getComments, addComment, getComment, currentRow,
editWalkData, getComments, addComment, getComment,
} from "./localStorage";
import {fetchWeather} from "./weather.js";
import {castesOfBees} from "./bees.js";
Expand Down Expand Up @@ -39,6 +39,8 @@ const beeButtons = castesOfBees.map((caste) => (
{button: document.getElementById(caste + 'Spotted'), caste})
)

let currentRow = new Map;

stopButton.hidden = !getStartTime();
editButton.hidden = !getStartTime();
walkData.hidden = !getStartTime();
Expand Down Expand Up @@ -87,18 +89,26 @@ clearButton.addEventListener("click", () => {
})

commentSaveButton.addEventListener("click", () => {
addComment(document.getElementById("commentText").value);
const species = currentRow.get("species");
const section = currentRow.get("section");
const comment = document.getElementById("commentText").value

console.log("species, section, comment: ", species, section, comment);
addComment(species, section, comment);
commentSaveButton.hidden = true;
discardCommentButton.hidden = true;
commentBox.hidden = true;
console.log("comment to display: ",getComment(species, section));
commentBox.innerText = getComment(species, section);

renderSummary();
})

discardCommentButton.addEventListener("click", () => {
commentBox.value = getComment(currentRow.get("species"), currentRow.get("section"));
commentBox.hidden = true;
commentSaveButton.hidden = true;
discardCommentButton.hidden = true;
commentBox.innerText = getComment(currentRow.get("species"), currentRow.get("section"));
})

function makeMetaDataEditable(isEditable) {
Expand Down Expand Up @@ -202,8 +212,12 @@ function renderSummary() {
}

row.onclick = () => {
currentRow.set("species", species);
currentRow.set("section", section);

// commentBox.innerText = getComment(species, section);
commentBox.value = getComment(species, section);
commentBox.hidden = !commentBox.hidden;
commentBox.innerText = getComment(species, section);
commentSaveButton.hidden = !commentSaveButton.hidden;
discardCommentButton.hidden = !discardCommentButton.hidden;
}
Expand Down
34 changes: 21 additions & 13 deletions static/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

const sightingsStorageKey = "sightings";
const commentsStorageKey = "comments";
let currentRow = new Map();

function addSighting(caste, species, section) {
let sightings = getSightings();
Expand All @@ -59,19 +58,17 @@
}
}

function addComment(comment) {
function addComment(species, section, comment) {
let storedComments = getComments();

const species = currentRow.get("species");
const section = currentRow.get("section");
const comments = storedComments.filter(comment => comment.species !== species && comment.section !== section);
storedComments = storedComments.filter(comment => comment.species !== species && comment.section !== section);

comments.push({
storedComments.push({
species,
section,
comment,
});
localStorage.setItem(commentsStorageKey, JSON.stringify(comments));
localStorage.setItem(commentsStorageKey, JSON.stringify(storedComments));
}

function getComments() {
Expand All @@ -85,9 +82,6 @@
}

function getComment(species, section) {
currentRow.set("species",species);
currentRow.set("section",section);

const allComments = getComments();
const matchedComment = allComments.filter(comment => comment.species === species && comment.section === section).at(0);

Expand Down Expand Up @@ -272,6 +266,8 @@
{button: document.getElementById(caste + 'Spotted'), caste})
);

let currentRow = new Map;

stopButton.hidden = !getStartTime();
editButton.hidden = !getStartTime();
walkData.hidden = !getStartTime();
Expand Down Expand Up @@ -320,18 +316,26 @@
});

commentSaveButton.addEventListener("click", () => {
addComment(document.getElementById("commentText").value);
const species = currentRow.get("species");
const section = currentRow.get("section");
const comment = document.getElementById("commentText").value;

console.log("species, section, comment: ", species, section, comment);
addComment(species, section, comment);
commentSaveButton.hidden = true;
discardCommentButton.hidden = true;
commentBox.hidden = true;
console.log("comment to display: ",getComment(species, section));
commentBox.innerText = getComment(species, section);

renderSummary();
});

discardCommentButton.addEventListener("click", () => {
commentBox.value = getComment(currentRow.get("species"), currentRow.get("section"));
commentBox.hidden = true;
commentSaveButton.hidden = true;
discardCommentButton.hidden = true;
commentBox.innerText = getComment(currentRow.get("species"), currentRow.get("section"));
});

function makeMetaDataEditable(isEditable) {
Expand Down Expand Up @@ -435,8 +439,12 @@
}

row.onclick = () => {
currentRow.set("species", species);
currentRow.set("section", section);

// commentBox.innerText = getComment(species, section);
commentBox.value = getComment(species, section);
commentBox.hidden = !commentBox.hidden;
commentBox.innerText = getComment(species, section);
commentSaveButton.hidden = !commentSaveButton.hidden;
discardCommentButton.hidden = !discardCommentButton.hidden;
};
Expand Down

0 comments on commit ad6a83a

Please sign in to comment.