-
Notifications
You must be signed in to change notification settings - Fork 0
Add dummy backend data fetching #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
4a5cf58
4902577
6e902b2
ffabe2f
30d93dc
2283863
019b956
e42d7eb
38773aa
da7bdd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,40 @@ | ||
| from fastapi import FastAPI | ||
| from fastapi.middleware.cors import CORSMiddleware | ||
| from .models import Condition, Forecast | ||
| from typing import List, Optional | ||
| from datetime import datetime, timedelta | ||
| import random | ||
|
|
||
| app = FastAPI() | ||
|
|
||
| origins = ["http://localhost", "http://localhost:3000"] | ||
|
|
||
| app.add_middleware( | ||
| CORSMiddleware, | ||
| allow_origins=origins, | ||
| allow_credentials=True, | ||
| allow_methods=["*"], | ||
| allow_headers=["*"], | ||
| ) | ||
|
|
||
|
|
||
| @app.get("/") | ||
| async def get_root(): | ||
| return {"data": "I am a WeatherApp"} | ||
|
|
||
|
|
||
| @app.get("/forecast/{location}", response_model=List[Forecast]) | ||
| async def get_forecast_by_location( | ||
| location: str, start_date: Optional[datetime] = None, days: int = 5 | ||
| ): | ||
| if not start_date: | ||
| start_date = datetime.now().date() | ||
| forecasts = [] | ||
| for delta in range(days): | ||
| forecast_date = start_date + timedelta(days=delta) | ||
| # Just get a random condition and temp | ||
| condition = random.choice(list(Condition)) | ||
| temp = round(random.uniform(23, 105), 1) | ||
| forecast = Forecast(date=forecast_date, condition=condition, temp=temp) | ||
| forecasts.append(forecast) | ||
| return forecasts | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from pydantic import BaseModel | ||
| from datetime import date | ||
| from enum import Enum | ||
|
|
||
|
|
||
| class Condition(str, Enum): | ||
| sunny = "sunny" | ||
| cloudy = "cloudy" | ||
| partial_cloudy = "partial-cloudy" | ||
| partial_sunny = "partial-sunny" | ||
| windy = "windy" | ||
| lightning = "lightning" | ||
| snow = "snow" | ||
| rain = "rain" | ||
| light_right = "light-rain" | ||
| heavy_rain = "heavy-rain" | ||
|
|
||
|
|
||
| class Forecast(BaseModel): | ||
| date: date | ||
| condition: Condition | ||
| temp: float | ||
|
|
||
| class Config: | ||
| use_enum_values = True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,30 @@ | ||
| import WeatherWidgetContainer from "./WeatherWidgetContainer"; | ||
| import Header from "./Header"; | ||
| import { useState } from "react"; | ||
| import { useState, useEffect } from "react"; | ||
| import PropTypes from "prop-types"; | ||
|
|
||
| export const WeatherForecast = ({ locationData, defaultExpanded }) => { | ||
| // Placeholder weather data | ||
| const weatherData = [ | ||
| { | ||
| temp: 51, | ||
| cond: "cloudy", | ||
| }, | ||
| { | ||
| temp: 48, | ||
| cond: "rain", | ||
| }, | ||
| { | ||
| temp: 58, | ||
| cond: "windy", | ||
| }, | ||
| { | ||
| temp: 62, | ||
| cond: "partial-cloudy", | ||
| }, | ||
| { | ||
| temp: 71, | ||
| cond: "sunny", | ||
| }, | ||
| ]; | ||
|
|
||
| const [isExpanded, setIsExpanded] = useState(defaultExpanded); | ||
| const [weatherData, setWeatherData] = useState(null); | ||
| const [isLoaded, setIsLoaded] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| fetch( | ||
| `http://localhost:8000/forecast/${locationData.city}, ${locationData.state}` | ||
| ) | ||
| .then((res) => res.json()) | ||
| .then( | ||
| (result) => { | ||
| // console.log(result); | ||
| setWeatherData(result); | ||
| setIsLoaded(true); | ||
| }, | ||
| (error) => { | ||
| console.log(error); | ||
| // TODO: Add isError state handling | ||
| } | ||
| ); | ||
| }, [locationData]); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| const toggleExpanded = () => { | ||
| setIsExpanded(!isExpanded); | ||
|
|
@@ -42,7 +38,11 @@ export const WeatherForecast = ({ locationData, defaultExpanded }) => { | |
| onClick={toggleExpanded} | ||
| > | ||
| <Header city={locationData.city} state={locationData.state} /> | ||
| <WeatherWidgetContainer weatherObjArray={weatherData} /> | ||
| {isLoaded ? ( | ||
| <WeatherWidgetContainer weatherObjArray={weatherData} /> | ||
| ) : ( | ||
| <p>"Loading..."</p> | ||
| )} | ||
| </div> | ||
| ) : ( | ||
| <div | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The frontend is technically located at a different origin so we have to enable CORS so the requests will be allowed. More here.