import { useState } from 'react' import fetch from 'isomorphic-unfetch' import {useSocket} from '../../hooks/useSocket' import Layout from '../../components/Layout' const Tally = require('../../domain/Tally') const Log = require('../../domain/Log') const TallyDetails = props => { const [tally, setTally] = useState(Tally.fromValueObject(props.tally)) const [logs, setLogs] = useState(props.logs.map(log => Log.fromValueObject(log))) const socket = useSocket('tally.logged.' + tally.name, log => { const newLogs = logs.slice() newLogs.push(Log.fromValueObject(log)) setLogs(newLogs) }) const format = (log, idx) => { let className = "log " if(log.isWarning()) { className = className + "bg-warning " } else if (log.isError()) { className = className + "bg-danger " } else if (log.isStatus()) { className = className + "bg-primary " } return (
{log.message}
) } return (

{tally.name}'s Logs

{logs.map(format)}
) } TallyDetails.getInitialProps = async (context) => { const { tallyName } = context.query; const baseUrl = context && context.req ? `${context.req.protocol}://${context.req.get('Host')}` : ''; const response = await fetch(baseUrl + '/tally?tallyName=' + tallyName) const data = await response.json() return data } export default TallyDetails;