1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource https://esm.sh/react@18.2.0 */
import isPointWithinRadius from "https://esm.sh/geolib/es/isPointWithinRadius";
import { useLoaderData } from "https://esm.sh/react-router-dom@6.23.0?deps=react@18.2.0,react-dom@18.2.0";
import date_me_doc_locations_geo from "https://esm.town/v/stevekrouse/date_me_doc_locations_geo";
import { nominatimSearch } from "https://esm.town/v/stevekrouse/nominatimSearch";
function absoluteURL(url) {
if (url.startsWith("http://") || url.startsWith("https://"))
return url;
else return "https://" + url;
}
let headers = [
"Name",
"Age",
"Gender",
"InterestedIn",
"Style",
"Location",
"LocationFlexibility",
"Contact",
"LastUpdated",
];
let linkClass = "text-sky-600 hover:text-sky-500";
function httpsIfy(url: string) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
return `https://${url}`;
}
return url;
}
// TODO - refactor this into a Cell React component
function renderCell(header, row) {
let data = row[header];
if (header === "Name") {
return <a className={linkClass} href={httpsIfy(row["Profile"])} target="_blank">{data}</a>;
} else if (Array.isArray(data)) {
return data.map(d => <span className="p-1 m-1 border rounded-md">{d}</span>);
} else if (header === "LastUpdated") {
return new Date(data).toISOString().split("T")[0];
}
else {
return data;
}
}
function convertParams(url) {
const params = new URL(url).searchParams;
const search = {
gender: params.get("gender"),
desiredGender: params.get("desired-gender"),
minAge: params.get("min-age")?.length ? params.get("min-age") : 0,
maxAge: params.get("max-age")?.length ? params.get("max-age") : 100,
location: params.get("location"),
radius: params.get("radius"),
};
return search;
}
const metersInMile = 1609.34;
function withinRadius(
profileLocation: string[],
searchLocation: { lat: number; lon: number },
radius: number,
) {
if (!profileLocation) return false;
if (!Array.isArray(profileLocation)) profileLocation = [profileLocation];
const profileLocations = profileLocation.map(l => date_me_doc_locations_geo[l]);
return profileLocations.some(location => {
return location && isPointWithinRadius(location, searchLocation, radius * metersInMile);
});
}
export async function loader({ request }) {
"use server"; // this is not a real feature; just me reminding myself I'm on the server
const { zip } = await import("npm:lodash-es");
const { sqlite } = await import("https://esm.town/v/std/sqlite");
const search = convertParams(request.url);
let { columns, rows } = await sqlite.execute({
sql: `select * from datemedocs
where
Age >= ?
and Age <= ?
and Gender like ?
and InterestedIn like ?
order by LastUpdated desc
`,
args: [
search.minAge,
search.maxAge,
"%" + (search.desiredGender ?? "") + "%",
"%" + (search.gender ?? "") + "%",
],
});
const searchLocation = search["location"]?.length ? await nominatimSearch({ q: search["location"] }) : undefined;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 17, 2024