adding next.js app which calls the fastAPI AND displays date in the app.

This commit is contained in:
lucas.oskorep
2023-07-10 21:21:41 -04:00
parent aad7babb2c
commit d24a340c15
6 changed files with 46 additions and 29 deletions

View File

@@ -1,16 +1,17 @@
'use client' 'use client'
import React, {useEffect, useState} from 'react'; import React, {useEffect, useState} from 'react';
import {fetchStationData} from "@/services/api/mta-server"; import {fetchStartDate} from "@/services/mta-api/mta-server";
import {MtaStartTime} from "@/services/mta-api/types";
const TitleBar = () => { const TitleBar = () => {
const [data, setData] = useState(null); const [data, setData] = useState<MtaStartTime|null>(null);
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchData = async () => {
try { try {
console.log("CALLING API") console.log("CALLING API")
const mtaData = await fetchStationData([""]) const mtaData = await fetchStartDate([""])
setData( mtaData) setData( mtaData)
} catch (error) { } catch (error) {

View File

@@ -1,16 +1,16 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
// rewrites: async () => { rewrites: async () => {
// return [ return [
// { {
// source: '/api/:path*', source: '/api/:path*',
// destination: destination:
// process.env.NODE_ENV === 'development' process.env.NODE_ENV === 'development'
// ? 'http://127.0.0.1:8000/api/:path*' ? 'http://127.0.0.1:8000/api/:path*'
// : '/api/', : '/api/',
// }, },
// ] ]
// }, },
} }
module.exports = nextConfig module.exports = nextConfig

View File

@@ -1,11 +0,0 @@
export type MtaData = {
mtaData: any;
};
export const fetchStationData = async (stations:[string]):Promise<MtaData> => {
const res = await fetch("")
const data = await res.json()
return {
mtaData: data
};
};

View File

@@ -0,0 +1,19 @@
import {MtaData, MtaStartTime} from "@/services/mta-api/types";
export const fetchStationData = async (stations: [string]): Promise<MtaData> => {
const res = await fetch("/api/mta_data", {method: "POST"})
const data = await res.json()
return {
mtaData: data
};
};
export const fetchStartDate = async (stations: [string]): Promise<MtaStartTime> => {
const res = await fetch("/api/start_time", {method: "POST"})
const data = await res.text()
const date = new Date(data.replaceAll("\"", ""))
return {
startTime: date
};
};

View File

@@ -0,0 +1,7 @@
export interface MtaData {
mtaData: any;
};
export interface MtaStartTime {
startTime: Date
}

View File

@@ -9,6 +9,7 @@ from fastapi_utils.tasks import repeat_every
# import pandas as pd # import pandas as pd
from dotenv import load_dotenv from dotenv import load_dotenv
from starlette.responses import JSONResponse
from mta_manager import MTA, Feed, Route from mta_manager import MTA, Feed, Route
@@ -26,8 +27,8 @@ app.add_middleware(
logger = logging.getLogger(__name__) # the __name__ resolve to "main" since we are at the root of the project. logger = logging.getLogger(__name__) # the __name__ resolve to "main" since we are at the root of the project.
start_time = datetime.now().strftime("%d/%m/%Y %H:%M:%S") start_time = datetime.now()
last_updated = datetime.now().strftime("%d/%m/%Y %H:%M:%S") last_updated = datetime.now()
mtaController = MTA( mtaController = MTA(
api_key, api_key,
@@ -40,7 +41,7 @@ STATION_STOP_IDs = ["127S", "127N", "A27N", "A27S"]
@app.post("/api/start_time") @app.post("/api/start_time")
def get_start_time(): def get_start_time():
return start_time return start_time.isoformat()
@app.post("/api/mta_data") @app.post("/api/mta_data")
async def get_mta_data(): async def get_mta_data():
@@ -53,7 +54,7 @@ async def get_mta_data():
arrival_times = mtaController.get_arrival_times(route, stop_id) arrival_times = mtaController.get_arrival_times(route, stop_id)
if len(arrival_times) > 0: if len(arrival_times) > 0:
arrival_by_station_and_route[stop_id][route.value] = arrival_times arrival_by_station_and_route[stop_id][route.value] = arrival_times
return arrival_by_station_and_route return JSONResponse(arrival_by_station_and_route)
@app.on_event("startup") @app.on_event("startup")
@repeat_every(seconds=5) @repeat_every(seconds=5)