|
| 1 | +import h from "@macrostrat/hyper"; |
| 2 | +import { LngLatCoords } from "@macrostrat/map-interface"; |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { usePageContext } from 'vike-react/usePageContext'; |
| 5 | +import 'mapbox-gl/dist/mapbox-gl.css'; |
| 6 | +import { BlankImage, Image } from "../index"; |
| 7 | + |
| 8 | +function imageExists(image_url){ |
| 9 | + var http = new XMLHttpRequest(); |
| 10 | + |
| 11 | + http.open('HEAD', image_url, false); |
| 12 | + http.send(); |
| 13 | + |
| 14 | + return http.status != 404; |
| 15 | +} |
| 16 | + |
| 17 | +export function App() { |
| 18 | + const pageContext = usePageContext(); |
| 19 | + const [userData, setUserData] = useState(null); |
| 20 | + const [loading, setLoading] = useState(true); |
| 21 | + const [error, setError] = useState(null); |
| 22 | + const [checkinNum, setCheckin] = useState(null); |
| 23 | + |
| 24 | + let stop; |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (pageContext.urlParsed) { |
| 28 | + stop = parseInt(pageContext.urlParsed.search.checkin); |
| 29 | + setCheckin(stop); |
| 30 | + } else { |
| 31 | + setCheckin(0); |
| 32 | + } |
| 33 | + console.log(`Fetching data for checkin ID: ` + stop); |
| 34 | + |
| 35 | + // Ensure trip ID is valid |
| 36 | + if (isNaN(stop)) { |
| 37 | + setLoading(false); |
| 38 | + setError('Invalid checkin ID.'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + fetch("https://rockd.org/api/v2/protected/checkins?checkin_id=" + stop) |
| 43 | + .then(response => { |
| 44 | + if (!response.ok) { |
| 45 | + throw new Error('Network response was not ok'); |
| 46 | + } |
| 47 | + return response.json(); |
| 48 | + }) |
| 49 | + .then(data => { |
| 50 | + console.log('Fetched data:', data); // Log fetched data for debugging |
| 51 | + if (data.success && data.success.data.length > 0) { |
| 52 | + setUserData(data.success.data[0]); |
| 53 | + } else { |
| 54 | + setUserData(null); |
| 55 | + } |
| 56 | + }) |
| 57 | + .catch(error => { |
| 58 | + console.error('Fetch error:', error); |
| 59 | + setError(error.message); |
| 60 | + }) |
| 61 | + .finally(() => { |
| 62 | + setLoading(false); |
| 63 | + }); |
| 64 | + }, []); |
| 65 | + |
| 66 | + if (loading) { |
| 67 | + if(checkinNum == null) { |
| 68 | + return h("div", { className: 'loading' }, [ |
| 69 | + h("h1", "Loading checkin..."), |
| 70 | + ]); |
| 71 | + } else { |
| 72 | + return h("div", { className: 'loading' }, [ |
| 73 | + h("h1", "Loading checkin " + checkinNum + "..."), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + if (error) { |
| 80 | + return h("div", { className: 'error' }, [ |
| 81 | + h("h1", "Error"), |
| 82 | + h("p", error) |
| 83 | + ]); |
| 84 | + } |
| 85 | + |
| 86 | + if (!userData) { |
| 87 | + return h("div", { className: 'error' }, [ |
| 88 | + h("h1", "Trip " + checkinNum + " not found"), |
| 89 | + ]); |
| 90 | + } |
| 91 | + |
| 92 | + let checkin = userData; |
| 93 | + console.log(checkin) |
| 94 | + |
| 95 | + let profile_pic = h(BlankImage, {src: "https://rockd.org/api/v2/protected/gravatar/" + checkin.person_id, className: "profile-pic"}); |
| 96 | + |
| 97 | + // format rating |
| 98 | + let ratingArr = []; |
| 99 | + for(var i = 0; i < checkin.rating; i++) { |
| 100 | + ratingArr.push(h(Image, {className: "star", src: "blackstar.png"})); |
| 101 | + } |
| 102 | + |
| 103 | + // get observations |
| 104 | + let observations = []; |
| 105 | + |
| 106 | + // add checkin photo and notes |
| 107 | + console.log("Checkin photo: ", checkin.photo != null); |
| 108 | + let headerImg; |
| 109 | + if(imageExists("https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + checkin.photo) && checkin.photo != null) { |
| 110 | + headerImg = h(BlankImage, {className: 'observation-img', src: "https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + checkin.photo}) |
| 111 | + } else { |
| 112 | + headerImg = h(BlankImage, { className: 'observation-img', src: "https://storage.macrostrat.org/assets/rockd/rockd.jpg"}) |
| 113 | + } |
| 114 | + |
| 115 | + observations.push( |
| 116 | + h('div', {className: 'observation'}, [ |
| 117 | + headerImg, |
| 118 | + h('h4', {className: 'observation-header'}, checkin.notes), |
| 119 | + ]) |
| 120 | + ); |
| 121 | + |
| 122 | + // add observations |
| 123 | + for(var i = 0; i < checkin.observations.length; i++) { |
| 124 | + let observation = checkin.observations[i]; |
| 125 | + console.log("Observation " + i); |
| 126 | + console.log(observation); |
| 127 | + |
| 128 | + if(Object.keys(observation.rocks).length != 0) { |
| 129 | + // get liths |
| 130 | + let liths = []; |
| 131 | + for(var j = 0; j < observation.rocks.liths.length; j++) { |
| 132 | + liths.push(h('p', observation.rocks.liths[j].name)); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + let LngLatProps = { |
| 137 | + position: { |
| 138 | + lat: observation.lat, |
| 139 | + lng: observation.lng |
| 140 | + }, |
| 141 | + precision: 3, |
| 142 | + zoom: 10 |
| 143 | + }; |
| 144 | + |
| 145 | + // LngLatCoords(LngLatProps); |
| 146 | + |
| 147 | + |
| 148 | + // if photo exists |
| 149 | + if (imageExists("https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + observation.photo)) { |
| 150 | + observations.push( |
| 151 | + h('div', {className: 'observation'}, [ |
| 152 | + h(BlankImage, { className: 'observation-img', src: "https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + observation.photo}), |
| 153 | + h('h4', {className: 'observation-header'}, observation.rocks.strat_name?.strat_name_long), |
| 154 | + h('div', {className: 'observation-details'}, [ |
| 155 | + h('p', {className: 'observation-detail'}, observation.rocks.strat_name?.strat_name_long), |
| 156 | + h('p', {className: 'observation-detail'}, observation.rocks.map_unit?.unit_name), |
| 157 | + h('p', {className: 'observation-detail'}, observation.age_est.name + " (" + observation.age_est.b_age + " - " + observation.age_est.t_age + ")"), |
| 158 | + h('p', {className: 'observation-detail'}, liths), |
| 159 | + h('p', {className: 'observation-detail'}, observation.orientation.feature?.name), |
| 160 | + h('p', {className: 'observation-detail'}, "Coords: " + observation.lat + ", " + observation.lng), |
| 161 | + ]), |
| 162 | + ]) |
| 163 | + ); |
| 164 | + } else { |
| 165 | + observations.push( |
| 166 | + h('div', {className: 'observation'}, [ |
| 167 | + h(BlankImage, { className: 'observation-img', src: "https://storage.macrostrat.org/assets/rockd/rockd.jpg"}), |
| 168 | + h('h4', {className: 'observation-header'}, observation.rocks.strat_name?.strat_name_long), |
| 169 | + h('div', {className: 'observation-details'}, [ |
| 170 | + h('p', {className: 'observation-detail'}, observation.rocks.strat_name?.strat_name_long), |
| 171 | + h('p', {className: 'observation-detail'}, observation.rocks.map_unit?.unit_name), |
| 172 | + h('p', {className: 'observation-detail'}, observation.age_est.name + " (" + observation.age_est.b_age + " - " + observation.age_est.t_age + ")"), |
| 173 | + h('p', {className: 'observation-detail'}, liths), |
| 174 | + h('p', {className: 'observation-detail'}, observation.orientation.feature?.name), |
| 175 | + ]), |
| 176 | + ]) |
| 177 | + ); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return h('div', { className: 'main'}, [ |
| 183 | + h('h1', { className: "checkin-header" }, checkin.description), |
| 184 | + h(BlankImage, { className: "location-img", src: "https://api.mapbox.com/styles/v1/jczaplewski/cje04mr9l3mo82spihpralr4i/static/geojson(%7B%22type%22%3A%22Point%22%2C%22coordinates%22%3A%5B" + checkin.lng + "%2C" + checkin.lat + "%5D%7D)/" + checkin.lng + "," + checkin.lat + ",5,0/1200x400?access_token=" + import.meta.env.VITE_MAPBOX_API_TOKEN }), |
| 185 | + h('div', { className: 'stop-header' }, [ |
| 186 | + h('h3', {className: 'profile-pic'}, profile_pic), |
| 187 | + h('div', {className: 'stop-main-info'}, [ |
| 188 | + h('h3', {className: 'name'}, checkin.first_name + " " + checkin.last_name), |
| 189 | + h('h4', {className: 'edited'}, checkin.created), |
| 190 | + h('p', {className: 'location'}, [ |
| 191 | + h('p', "Near " + checkin.near), |
| 192 | + ]), |
| 193 | + h('h3', {className: 'rating'}, ratingArr), |
| 194 | + ]), |
| 195 | + ]), |
| 196 | + h('div', { className: 'observations' }, observations), |
| 197 | + ]); |
| 198 | +} |
0 commit comments