Fix typescript type checking. Set up application state.

This commit is contained in:
Savanni D'Gerinel 2021-12-29 00:28:02 -05:00
parent 9ea4630f8b
commit 5738ff61e5
6 changed files with 49 additions and 16 deletions

View File

@ -12,14 +12,15 @@ module.exports = {
], ],
plugins: [ plugins: [
'@snowpack/plugin-sass', '@snowpack/plugin-sass',
'@snowpack/plugin-typescript',
], ],
packageOptions: { packageOptions: {
/* ... */ types: true,
}, },
devOptions: { devOptions: {
/* ... */ /* ... */
}, },
buildOptions: { buildOptions: {
/* ... */ sourceMap: true,
}, },
}; };

View File

@ -0,0 +1,28 @@
import React, { ReactNode } from "react";
import { PlayerCharacter } from "./types"
export type AppState = {
playerCharacters: { [name: string]: PlayerCharacter };
}
export const AppContext = React.createContext<AppState>({
playerCharacters: {}
})
const AppProvider = ({ children }: { children: ReactNode }) => {
const [state, setState] = React.useState<AppState>({playerCharacters: {
"priat": {
name: "Priat",
concept: "An Intuitive Jack who Explores Yesterday",
effort: 1,
cypherLimit: 2,
might: { value: 12, max: 12, edge: 0 },
speed: { value: 14, max: 14, edge: 0 },
intellect: { value: 12, max: 12, edge: 1 },
}}})
return (<AppContext.Provider value={{state}}>{children}</AppContext.Provider>)
}
export default AppProvider

View File

@ -1,13 +1,15 @@
import React from "react" import React, { createContext, useState } from "react"
import ReactDOM from "react-dom" import ReactDOM from "react-dom"
import { BrowserRouter, Route, Routes, useParams } from "react-router-dom"; import { BrowserRouter, Route, Routes, useParams } from "react-router-dom";
import Menu from "./components/Menu" import Menu from "./components/Menu"
import PlayerListView from "./views/PlayerListView" import PlayerListView from "./views/PlayerListView"
import "./styles.scss" import "./styles.scss"
import { PlayerCharacter } from "./types"
import AppProvider from "./appContext"
const App = () => ( const App = () => {
<> return (<AppProvider>
<h1>Numenera Datasphere</h1> <h1>Numenera Datasphere</h1>
<div className="columns"> <div className="columns">
<Menu /> <Menu />
@ -21,7 +23,8 @@ const App = () => (
<Route path="battles" element={<div>battles</div>} /> <Route path="battles" element={<div>battles</div>} />
</Routes> </Routes>
</div> </div>
</>) </AppProvider>)
}
const main = () => { const main = () => {
ReactDOM.render( ReactDOM.render(

View File

@ -6,13 +6,12 @@ import { PlayerCharacter } from "../types"
interface PlayerCharacterProps extends PlayerCharacter { } interface PlayerCharacterProps extends PlayerCharacter { }
const PlayerCharacter = ({ name, might, speed, intellect }: PlayerCharacterProps) => { const PlayerCharacter = ({ name, concept, might, speed, intellect }: PlayerCharacterProps) => {
return (<div> return (<div>
<h2>{name}</h2> <h2>{name}</h2>
<div>{concept}</div>
<div className="columns c-2"> <div className="columns c-2">
<div> <div>
<div>{concept}</div>
</div> </div>
<div> <div>

View File

@ -1,11 +1,15 @@
import React from "react" import React, { useContext } from "react"
import ReactDOM from "react-dom" import ReactDOM from "react-dom"
import { Link, Route, Routes, useParams } from "react-router-dom"; import { Link, Route, Routes, useParams } from "react-router-dom"
import PlayerCharacter from "./PlayerCharacter"; import { AppContext } from "../appContext"
import PlayerCharacter from "./PlayerCharacter"
const PlayerListView = () => { const PlayerListView = () => {
let params = useParams(); const params = useParams()
const { state } = useContext(AppContext)
const character = params.name ? state.playerCharacters[params.name] : null
return (<div> return (<div>
<nav> <nav>
<Link to="priat">Priat</Link> | <Link to="priat">Priat</Link> |
@ -13,7 +17,7 @@ const PlayerListView = () => {
<Link to="ember">Ember</Link> | <Link to="ember">Ember</Link> |
<Link to="lise">Lise</Link> <Link to="lise">Lise</Link>
</nav> </nav>
{params.name ? <PlayerCharacter name={params.name} /> : null } {character ? <PlayerCharacter {...character} /> : null }
</div>); </div>);
} }

View File

@ -7,8 +7,6 @@
"esModuleInterop": true, "esModuleInterop": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"skipLibCheck": true,
"noEmit": true, "noEmit": true,
"types": ["snowpack-env"],
} }
} }