import { useState } from 'react'
import { useAuth } from "react-oidc-context"
import { useBrewers, useCoffees, useGrinders, useMethods } from './api';
import { queryClient } from './main';
import { environment } from './environment';
import './App.css'
function Home() {
const auth = useAuth();
const token = auth.user?.access_token
const { isPending: areCoffeesPending, data: coffees } = useCoffees(token!)
const { isPending: areGrindersPending, data: grinders } = useGrinders(token!)
const { isPending: areBrewersPending, data: brewers } = useBrewers(token!)
const { isPending: areMethodsPending, data: methods } = useMethods(token!)
const [addCoffeeName, setAddCoffeeName] = useState('')
const [addCoffeeRoastLevel, setAddCoffeeRoastLevel] = useState(3)
const [addCoffeeNotes, setAddCoffeeNotes] = useState('')
const [addGrinderName, setAddGrinderName] = useState('')
const [addGrinderNotes, setAddGrinderNotes] = useState('')
const [addBrewerName, setAddBrewerName] = useState('')
const [addBrewerNotes, setAddBrewerNotes] = useState('')
const [addMethodCoffeeId, setAddMethodCoffeeId] = useState('')
const [addMethodCoffeeAmountG, setAddMethodCoffeeAmountG] = useState(15)
const [addMethodGrinderId, setAddMethodGrinderId] = useState('')
const [addMethodGrindSetting, setAddMethodGrindSetting] = useState('')
const [addMethodBrewerId, setAddMethodBrewerId] = useState('')
const [addMethodWaterAmountMl, setAddMethodWaterAmountMl] = useState(250)
const [addMethodWaterTemperatureC, setAddMethodWaterTemperatureC] = useState(90)
const [addMethodBloomingTimeSec, setAddMethodBloomingTimeSec] = useState(45)
const [addMethodBrewingTimeSec, setAddMethodBrewingTimeSec] = useState(120)
const [addMethodNotes, setAddMethodNotes] = useState('')
const [randomMix, setRandomMix] = useState(
undefined as {coffeeId: string, grinderId: string, brewerId: string} | undefined
)
const [randomMethodId, setRandomMethodId] = useState(undefined as string | undefined)
if (areCoffeesPending || areGrindersPending || areBrewersPending || areMethodsPending) {
return (
Loading...
)
}
function openDialog(dialogId: string) {
const dialogElement = document.getElementById(dialogId);
(dialogElement as HTMLDialogElement).showModal()
}
function closeDialog(dialogId: string) {
const dialogElement = document.getElementById(dialogId);
(dialogElement as HTMLDialogElement).close()
}
function closeDialogButton(dialogId: string) {
return
}
async function postNewEntry(entryType: 'coffee'|'grinder'|'brewer'|'method', body: any) {
try {
let result = await fetch(
`${environment.platformRoot}/api/${entryType}`,
{
method: 'POST',
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${token}`,
'content-type': 'application/json'
}
}
)
if (result.status !== 200) {
window.alert(`Error: status ${result.status}; ${await result.text()}`)
}
} catch (error) {
window.alert(`Error: ${error}`)
}
}
// ============================================
// COFFEE
// ============================================
const addCoffeeDialogId = 'addCoffeeDialog'
async function postNewCoffee() {
postNewEntry(
'coffee',
{
name: addCoffeeName,
roastLevel: addCoffeeRoastLevel,
notes: addCoffeeNotes,
}
)
queryClient.invalidateQueries({ queryKey: ['coffees'] })
closeDialog(addCoffeeDialogId)
}
function renderAddCoffeeDialog() {
return (
)
}
// ============================================
// GRINDER
// ============================================
const addGrinderDialogId = 'addGrinderDialog'
async function postNewGrinder() {
postNewEntry(
'grinder',
{
name: addGrinderName,
notes: addGrinderNotes,
}
)
queryClient.invalidateQueries({ queryKey: ['grinders'] })
closeDialog(addGrinderDialogId)
}
function renderAddGrinderDialog() {
return (
)
}
// ============================================
// BREWER
// ============================================
const addBrewerDialogId = 'addBrewerDialog'
async function postNewBrewer() {
postNewEntry(
'brewer',
{
name: addBrewerName,
notes: addBrewerNotes,
}
)
queryClient.invalidateQueries({ queryKey: ['brewers'] })
closeDialog(addBrewerDialogId)
}
function renderAddBrewerDialog() {
return (
)
}
// ============================================
// METHOD
// ============================================
const addMethodDialogId = 'addMethodDialog'
async function postNewMethod() {
postNewEntry(
'method',
{
coffeeId: addMethodCoffeeId,
coffeeAmountG: addMethodCoffeeAmountG,
grinderId: addMethodGrinderId,
grindSetting: addMethodGrindSetting,
brewerId: addMethodBrewerId,
waterAmountMl: addMethodWaterAmountMl,
waterTemperatureC: addMethodWaterTemperatureC,
bloomingTimeSec: addMethodBloomingTimeSec,
brewingTimeSec: addMethodBrewingTimeSec,
notes: addMethodNotes,
}
)
queryClient.invalidateQueries({ queryKey: ['methods'] })
closeDialog(addMethodDialogId)
}
function renderAddMethodDialog() {
const coffeeOptions = coffees.map((coffee: any) => )
const grinderOptions = grinders.map((grinder: any) => )
const brewerOptions = brewers.map((brewer: any) => )
return (
)
}
return (
{renderAddCoffeeDialog()}
{coffees?.map((coffee: any) =>
{coffee.name} - Roast {coffee.roast_level}
)}
{renderAddGrinderDialog()}
{grinders?.map((grinder: any) =>
{grinder.name}
)}
{renderAddBrewerDialog()}
{brewers?.map((brewer: any) =>
{brewer.name}
)}
{renderAddMethodDialog()}
{methods?.map((method: any) => {
const coffeeFound = coffees.find((coffee: any) => coffee.id === method.coffee_id)
const grinderFound = grinders.find((grinder: any) => grinder.id === method.grinder_id)
const brewerFound = brewers.find((brewer: any) => brewer.id === method.brewer_id)
const isRandomlySelected = (randomMethodId && method.id === randomMethodId)
|| (randomMix && method.coffee_id === randomMix?.coffeeId && method.grinder_id === randomMix?.grinderId && method.brewer_id === randomMix?.brewerId)
return (
| {coffeeFound.name} |
{method.coffee_amount_g} g |
{grinderFound.name} |
{method.grind_setting} |
{brewerFound.name} |
{method.water_amount_ml} ml |
{method.water_temperature_c} °C |
Bloom {method.blooming_time_sec} sec |
Brew {method.brewing_time_sec} sec |
)
})}
)
}
function App() {
const auth = useAuth();
switch (auth.activeNavigator) {
case "signinSilent":
return Signing you in...
;
case "signoutRedirect":
return Signing you out...
;
}
if (auth.isLoading) {
return Loading...
;
}
if (auth.error) {
return Oops... {auth.error.message}
;
}
if (auth.isAuthenticated) {
if (document.location.search.includes('code=') && document.location.search.includes('state=')) {
document.location.href = '/'
return
}
return (
);
}
return (
)
}
export default App