Skip to content

Commit 311a743

Browse files
authored
Merge pull request #59 from UW-Macrostrat/checkins
Checkin and photo error handling
2 parents efc02d1 + 12c79b9 commit 311a743

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

pages/checkin/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function Map(props) {
2222
}
2323

2424
export function Checkins({checkin, comments}) {
25+
console.log("Checkin data:", checkin);
2526
const center = {
2627
lat: checkin.lat,
2728
lng: checkin.lng
@@ -56,20 +57,18 @@ export function Checkins({checkin, comments}) {
5657

5758
// add observations
5859
checkin.observations.forEach(observation => {
59-
if(Object.keys(observation.rocks).length != 0) {
60-
// if photo exists
61-
const imageSrc = getImageUrl(checkin.person_id, observation.photo);
62-
let observationBody = observationFooter(observation);
63-
64-
observations.push(
65-
h('div', {className: 'observation'}, [
66-
h('a', {href: "/photo/" + observation.photo},
67-
h(BlankImage, { className: 'observation-image', src: imageSrc })
68-
),
69-
observationBody,
70-
])
71-
);
72-
}
60+
// if photo exists
61+
const imageSrc = getImageUrl(checkin.person_id, observation.photo);
62+
let observationBody = observationFooter(observation);
63+
64+
observations.push(
65+
h('div', {className: 'observation'}, [
66+
h('a', {href: "/photo/" + observation.photo},
67+
h(BlankImage, { className: 'observation-image', src: imageSrc })
68+
),
69+
observationBody,
70+
])
71+
);
7372
});
7473

7574
let LngLatProps = {
@@ -151,7 +150,7 @@ function observationFooter(observation) {
151150

152151
// get liths
153152
let liths = [];
154-
for(var j = 0; j < rocks.liths.length; j++) {
153+
for(var j = 0; j < rocks?.liths?.length; j++) {
155154
liths.push(h('p', { className: "observation-detail liths"}, rocks.liths[j].name));
156155
}
157156

@@ -176,7 +175,7 @@ function observationFooter(observation) {
176175
let obsAge = observation.age_est ? observation.age_est.name + " (" + observation.age_est.b_age + " - " + observation?.age_est?.t_age + ")" : null;
177176

178177
let lithologies = [];
179-
rocks.liths.forEach(lith => {
178+
rocks.liths?.forEach(lith => {
180179
if(!lith.color.includes("#")) {
181180
lithologies.push({
182181
name: lith.name,
@@ -203,7 +202,7 @@ function observationFooter(observation) {
203202
name: obsAge
204203
})
205204
}
206-
if(rocks.interval.name) {
205+
if(rocks.interval?.name) {
207206
lithologies.push({
208207
name: rocks.interval.name
209208
})
@@ -214,8 +213,10 @@ function observationFooter(observation) {
214213
})
215214
}
216215

216+
const show = lithologies.length > 0 || rocks?.notes?.length > 0 && observation.lat && rocks.strat_name?.strat_name_long;
217+
217218
// observation body
218-
return h('div', {className: 'observation-body'}, [
219+
return h.if(show)("div", {className: 'observation-body'}, [
219220
observation.lat && rocks.strat_name?.strat_name_long ? h('h4', {className: 'observation-header'}, [
220221
rocks.strat_name?.strat_name_long,
221222
observation.lat ? LngLatCoords(LngLatProps) : null,

pages/photo/@id/+Page.client.ts

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { LngLatCoords } from "@macrostrat/map-interface";
2-
import { useState } from "react";
2+
import { useState, useEffect } from "react";
33
import "mapbox-gl/dist/mapbox-gl.css";
44
import {
55
BlankImage,
66
getImageUrl,
7+
TestImage,
78
Footer,
89
} from "~/components/general";
910
import { Icon } from "@blueprintjs/core";
@@ -23,14 +24,9 @@ export function Page() {
2324
.replace(/%40/g, "@")
2425
.replace(/%2B/g, "+"));
2526

26-
let photoIDArr = [checkin.photo];
27-
if (checkin.observations) {
28-
checkin.observations.forEach((obs) => {
29-
if (obs.photo && !photoIDArr.includes(obs.photo)) {
30-
photoIDArr.push(obs.photo);
31-
}
32-
});
33-
}
27+
28+
const TestObject = PhotoIDCollector({ checkin })
29+
const photoIDArr = TestObject.props.photoIDArr.sort((a, b) => a - b);
3430

3531
const photoIndex = photoIDArr.indexOf(photoID);
3632

@@ -68,7 +64,9 @@ export function Page() {
6864
h(Icon, { icon: "symbol-circle", style: { color: "white" } }),
6965
]);
7066
}
71-
})), h(Footer)
67+
})),
68+
h('div.hide', TestObject),
69+
h(Footer)
7270
]);
7371
}
7472

@@ -84,7 +82,7 @@ function ObservationContent({ observation, setBody }) {
8482
}
8583

8684
let lithologies = [];
87-
rocks.liths.forEach((lith) => {
85+
rocks.liths?.forEach((lith) => {
8886
if (!lith.color.includes("#")) {
8987
lithologies.push({ name: lith.name, color: rgbaStringToHex(lith.color) });
9088
} else {
@@ -119,7 +117,9 @@ function ObservationContent({ observation, setBody }) {
119117
zoom: 10,
120118
};
121119

122-
return h("div", { className: "observation-body" }, [
120+
const show = lithologies.length > 0 || rocks?.notes?.length > 0 && observation.lat && rocks.strat_name?.strat_name_long
121+
122+
return h.if(show)("div", { className: "observation-body" }, [
123123
h(Icon, {
124124
className: "close-body",
125125
icon: "ban-circle",
@@ -131,7 +131,7 @@ function ObservationContent({ observation, setBody }) {
131131
LngLatCoords(LngLatProps),
132132
])
133133
: null,
134-
h("div", { className: "observation-details" }, [
134+
h.if(lithologies || rocks)("div", { className: "observation-details" }, [
135135
h(LithologyList, { lithologies }),
136136
h("p", { className: "notes" }, rocks.notes),
137137
]),
@@ -166,4 +166,53 @@ function Item({ checkin, photoID }) {
166166
onClick: () => setBody(true),
167167
})
168168
]);
169+
}
170+
171+
function PhotoIDCollector({ checkin }) {
172+
const [photoIDArr, setPhotoIDArr] = useState(() =>
173+
checkin.photo ? [checkin.photo] : []
174+
);
175+
const [testedPhotos, setTestedPhotos] = useState(() => {
176+
return checkin.observations
177+
? checkin.observations.map(obs => obs.photo).filter(Boolean)
178+
: [];
179+
});
180+
181+
useEffect(() => {
182+
// Only update if checkin changes
183+
if (checkin.photo && !photoIDArr.includes(checkin.photo)) {
184+
setPhotoIDArr(prev => [...prev, checkin.photo]);
185+
}
186+
187+
const newPhotos = (checkin.observations || [])
188+
.map(obs => obs.photo)
189+
.filter(photo => photo && !testedPhotos.includes(photo));
190+
191+
if (newPhotos.length > 0) {
192+
setTestedPhotos(prev => [...prev, ...newPhotos]);
193+
}
194+
}, [checkin]);
195+
196+
return h('div', { photoIDArr }, [
197+
...testedPhotos.map((photo) => {
198+
return h(HideImage, {
199+
key: photo,
200+
checkin,
201+
photo,
202+
setPhotoIDArr,
203+
});
204+
})
205+
]);
206+
}
207+
208+
209+
function HideImage({ checkin, photo, setPhotoIDArr }) {
210+
return h(TestImage, {
211+
key: photo,
212+
src: getImageUrl(checkin.person_id, photo),
213+
onLoad: () => {
214+
setPhotoIDArr((prev) => (prev.includes(photo) ? prev : [...prev, photo]));
215+
},
216+
onError: () => console.warn("Image failed to load:", photo),
217+
});
169218
}

src/components/general/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ interface ImageProps {
140140
onError?: () => void;
141141
}
142142

143-
function TestImage({src, onError}) {
143+
export function TestImage({ src, onError, onLoad }) {
144144
return h("img", {
145145
src,
146146
onError,
147+
onLoad,
147148
});
148149
}
149150

0 commit comments

Comments
 (0)