Skip to content

Commit

Permalink
tsx updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AvidDabbler committed Jan 30, 2024
1 parent 154d288 commit d9ea46d
Show file tree
Hide file tree
Showing 13 changed files with 487 additions and 549 deletions.
1 change: 1 addition & 0 deletions app/gtfs/gtfs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error no types
import { importGtfs } from "gtfs";
// @ts-expect-error no types
import gtfsToGeoJSON from "gtfs-to-geojson";
Expand Down
6 changes: 6 additions & 0 deletions app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,22 @@ export const getFiles = (date: number, routeShortName: string) => {
.filter((el) => el.includes("-"))
.map((el) => {
const [startDate, endDate] = el.split("-");
if (!startDate || !endDate) throw Error("Dates are missing in folder");
return { startDate: parseInt(startDate), endDate: parseInt(endDate) };
});
const [currentFolder] = folders.filter(({ startDate, endDate }) => {
return startDate <= date && endDate >= date;
});
if (!currentFolder) throw Error("Current folder not found");
const folder = `html/${currentFolder.startDate}-${currentFolder.endDate}`;
const files = fs.readdirSync(folder);

return files
.map((el) => {
const contents = readFileToString(`${folder}/${el}`);
const [, routeShortName, direction, service] = el.split("_");
if (!routeShortName || !direction || !service)
throw Error("Error with contents");
const obj = {
fileName: el,
routeShortName,
Expand All @@ -100,6 +104,8 @@ export const getGeojson = (routeShortName: string) => {
readFileToString(`${`geojson/${agency}`}/${el}`),
) as FeatureCollection<Point>;
const [, routeShortName, direction] = el.split("_");
if (!direction || !routeShortName)
throw Error("GeoJson not formatted properly");
const obj = {
fileName: el,
routeShortName,
Expand Down
13 changes: 7 additions & 6 deletions app/models/user.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import bcrypt from "bcryptjs";
import { eq } from "drizzle-orm";

import { db } from "drizzle/config";
import { password, user } from "drizzle/schema";
import { db } from "../../drizzle/config";
import { password, user } from "../../drizzle/schema";

export async function getUserById(id: string) {
const [_user] = await db.select().from(user).where(eq(user.id, id)).limit(1);
Expand All @@ -27,10 +27,11 @@ export async function createUser(email: string, _password: string) {
},
])
.returning({ id: user.id, email: user.email });
await tx.insert(password).values({
userId: createdUser.id,
hash: hashedPassword,
});
if (createdUser)
await tx.insert(password).values({
userId: createdUser.id,
hash: hashedPassword,
});
return createdUser;
});
}
Expand Down
10 changes: 6 additions & 4 deletions app/routes/__root.routes.$routeShortName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function loader({ params }: LoaderFunctionArgs) {

const { dow, formattedDate } = getDow(new Date());

if (!_routes[0].routeShortName) return redirect("/");
if (!_routes[0] || !_routes[0].routeShortName) return redirect("/");
const files = getFiles(formattedDate, _routes[0].routeShortName);
const geojson = getGeojson(routeShortName);

Expand All @@ -55,6 +55,9 @@ export async function loader({ params }: LoaderFunctionArgs) {

const serviceIds = Object.keys(groupBy(files, "service"));
const service = getService(serviceIds, dow);
if (!service) throw Error("Service not found");
const routeLongName = _routes[0].routeLongName;
if (!routeLongName) throw Error("Route Long Name not found");

const mapboxAccessToken = process.env.MAPBOX_ACCESS_TOKEN;
if (!mapboxAccessToken) throw Error("Mapbox access token not found");
Expand All @@ -67,6 +70,7 @@ export async function loader({ params }: LoaderFunctionArgs) {
files,
geojson,
mapboxAccessToken,
routeLongName,
directions: groupBy(directions, "directionId"),
});
}
Expand Down Expand Up @@ -119,9 +123,7 @@ export default function RootRouteTemplate() {
console.log(loaderData);
return (
<div className="flex flex-col gap-3 w-[70%] pb-3 pr-10">
<h1 className="text-3xl font-bold">
{loaderData.routes[0].routeLongName}
</h1>
<h1 className="text-3xl font-bold">{loaderData.routeLongName}</h1>
<Map
mapboxAccessToken={loaderData.mapboxAccessToken}
initialViewState={{
Expand Down
2 changes: 1 addition & 1 deletion app/routes/invite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
}

const user = await createUser(email, password);

if (!user) return json(null);
return createUserSession({
redirectTo,
remember: false,
Expand Down
2 changes: 1 addition & 1 deletion app/routes/notes.new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
}

const note = await createNote({ body, title, userId });

if (!note) return json(null);
return redirect(`/notes/${note.id}`);
};

Expand Down
3 changes: 2 additions & 1 deletion app/routes/notes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { json } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { Form, Link, NavLink, Outlet, useLoaderData } from "@remix-run/react";

import { title } from "~/config.server";
Expand All @@ -13,6 +13,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const user = await getUserById(userId);
const noteListItems = await getNoteListItems();
if (!user) throw redirect("/");
return json({ noteListItems, user });
};

Expand Down
15 changes: 8 additions & 7 deletions drizzle/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createClient } from "@libsql/client";
import Database from "better-sqlite3";
import type { Config } from "drizzle-kit";
import { drizzle } from "drizzle-orm/libsql";
import { drizzle } from "drizzle-orm/better-sqlite3";

export default {
schema: "./src/schema/*",
Expand All @@ -11,9 +11,10 @@ export default {
},
} satisfies Config;

export const client = createClient({
url: "file:./drizzle/data.db",
authToken: "DATABASE_AUTH_TOKEN",
});
// export const client = createClient({
// url: "file:./drizzle/data.db",
// authToken: "DATABASE_AUTH_TOKEN",
// });
export const sqlite = new Database("file:./drizzle/data.db");

export const db = drizzle(client);
export const db = drizzle(sqlite);
10 changes: 6 additions & 4 deletions drizzle/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ async function seed() {
},
])
.returning({ id: user.id });
await tx.insert(password).values({
userId: createdUser.id,
hash: hashedPassword,
});
if (createdUser) {
await tx.insert(password).values({
userId: createdUser.id,
hash: hashedPassword,
});
}
});
console.log(`Database has been seeded. 🌱`);
}
Expand Down
Loading

0 comments on commit d9ea46d

Please sign in to comment.