-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add climate page and reading page
- Loading branch information
Showing
15 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXT_PUBLIC_READING_BASE_URI=https://www.oyvindis.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NEXT_PUBLIC_READING_BASE_URI=https://www.oyvindis.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { validateEnv } from './utils/common'; | ||
|
||
const NAMESPACE = process.env.NAMESPACE! ?? 'dev'; | ||
const READING_BASE_URI = process.env.NEXT_PUBLIC_READING_BASE_URI! ?? 'https://www.oyvindis.com' | ||
|
||
const env = { | ||
NAMESPACE, | ||
READING_BASE_URI | ||
} | ||
|
||
export default validateEnv(env); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { NextPage } from 'next'; | ||
import Link from 'next/link'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import env from '../../env'; | ||
import { Location } from '../../types'; | ||
import {getLocations} from '../../services/api/climate-reading/readings'; | ||
|
||
interface Props {} | ||
|
||
const Climate: NextPage<Props> = () => { | ||
const [locations, setLocations] = useState<Location[]>([]); | ||
const { READING_BASE_URI } = env; | ||
|
||
useEffect(() => { | ||
getLocations().then(locations => setLocations(locations)) | ||
}, []) | ||
|
||
return ( | ||
<div> | ||
<div>Climate Page12</div> | ||
<div> | ||
{READING_BASE_URI} | ||
</div> | ||
<ul> | ||
{locations?.length > 0 && locations.map(({id, name}) => ( | ||
<li key={id} > | ||
<Link href={`/climate/location/${id}`}> | ||
{name} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export default Climate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { NextPage } from 'next'; | ||
import { useRouter } from 'next/router' | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { Reading } from '../../../../types'; | ||
import { getReadings } from '../../../../services/api/climate-reading/readings'; | ||
|
||
interface Props {} | ||
|
||
const Location: NextPage<Props> = () => { | ||
const [readings, setReadings] = useState<Reading[]>([]); | ||
|
||
const router = useRouter() | ||
const { id } = router.query; | ||
|
||
useEffect(() => { | ||
getReadings(Array.isArray(id) ? id[0]: id).then(readings => setReadings(readings)) | ||
}, []) | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
{readings?.length > 0 && readings.map(({id, temperatureReading, humidityReading}) => ( | ||
<li key={id} > | ||
<div> | ||
{temperatureReading} - {humidityReading} | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export default Location; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import axios from 'axios'; | ||
import env from '../../../env'; | ||
|
||
interface Props { | ||
path: string; | ||
method: any; | ||
data?: any; | ||
params?: URLSearchParams; | ||
} | ||
|
||
const { READING_BASE_URI } = env; | ||
|
||
export const climateReading = ({ path, method, data, params }: Props) => | ||
axios({ | ||
url: `${READING_BASE_URI}${path}`, | ||
method, | ||
data, | ||
params | ||
}) | ||
.then(response => response.data) | ||
.catch(() => null); | ||
|
||
export const climateReadingPost = (path: string, body: any) => | ||
climateReading({ path, method: 'POST', data: body }); | ||
|
||
export const climateReadingGet = (path: string, params?: URLSearchParams) => | ||
climateReading({ path, method: 'GET', params }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { climateReadingGet } from './host'; | ||
|
||
export const getReadings = (location: string) => | ||
climateReadingGet(`/climate-api/reading/${location}`) | ||
|
||
export const getLocations = () => | ||
climateReadingGet('/climate-api/location') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Actions<T extends { [key: string]: (...args: any) => any }> = { | ||
[K in keyof T]: ReturnType<T[K]>; | ||
}[keyof T]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface Location { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
export interface Reading { | ||
id: string; | ||
createdDate: string; | ||
temperatureReading: string; | ||
humidityReading: string; | ||
location: Location; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum Namespace { | ||
DEVELOPMENT = 'dev' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface EnvironmentVariables { | ||
NAMESPACE: string; | ||
READING_BASE_URI: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './env'; | ||
export * from './common'; | ||
export * from './domain'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { EnvironmentVariables } from '../../types'; | ||
|
||
function assertIsDefined<T>( | ||
key: string, | ||
value: T | ||
): asserts value is NonNullable<T> { | ||
if (value === undefined || value === null) { | ||
throw new Error(`Expected ${key} to be defined, but received ${value}`); | ||
} | ||
} | ||
|
||
export const validateEnv = ( | ||
env: EnvironmentVariables | ||
): EnvironmentVariables => { | ||
Object.entries(env).forEach(([key, value]) => assertIsDefined(key, value)); | ||
return env; | ||
}; |