Compare commits
3 Commits
aa7229eae4
...
1d050f014a
Author | SHA1 | Date | |
---|---|---|---|
1d050f014a | |||
df1dfeaae3 | |||
8ab8cd0684 |
@ -7,8 +7,8 @@ tasks:
|
||||
|
||||
build:
|
||||
cmds:
|
||||
- cd ../types && task build
|
||||
- npm install typescript
|
||||
- typeshare --lang typescript --output-file gen/types.ts ../server/src
|
||||
- npx tsc
|
||||
|
||||
test:
|
||||
|
18
visions/client/package-lock.json
generated
18
visions/client/package-lock.json
generated
@ -8,6 +8,9 @@
|
||||
"name": "visions-client",
|
||||
"version": "0.0.1",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"visions-types": "file:../types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.14",
|
||||
"jest": "^29.7.0",
|
||||
@ -16,6 +19,17 @@
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
},
|
||||
"../types": {
|
||||
"name": "visions-types",
|
||||
"version": "0.0.1",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
},
|
||||
"../types/dist/lib.ts": {
|
||||
"extraneous": true
|
||||
},
|
||||
"node_modules/@ampproject/remapping": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz",
|
||||
@ -3466,6 +3480,10 @@
|
||||
"node": ">=10.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/visions-types": {
|
||||
"resolved": "../types",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/walker": {
|
||||
"version": "1.0.8",
|
||||
"resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz",
|
||||
|
@ -3,6 +3,7 @@
|
||||
"version": "0.0.1",
|
||||
"description": "Shared data types for Visions",
|
||||
"main": "visions.js",
|
||||
"types": "dist/lib.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
@ -19,5 +20,8 @@
|
||||
"prettier": "^3.5.1",
|
||||
"ts-jest": "^29.2.5",
|
||||
"typescript": "^5.7.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"visions-types": "file:../types"
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Connection } from './client'
|
||||
import { Connection } from './lib'
|
||||
|
||||
describe('what happens in an authentication', () => {
|
||||
it('handles a successful response', async () => {
|
@ -1,4 +1,4 @@
|
||||
import { AuthResponse, SessionId, UserOverview } from '../gen/types'
|
||||
import { AuthResponse, SessionId, UserOverview } from 'visions-types'
|
||||
|
||||
export interface Client {
|
||||
auth: (
|
7
visions/types/Taskfile.yml
Normal file
7
visions/types/Taskfile.yml
Normal file
@ -0,0 +1,7 @@
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
build:
|
||||
cmds:
|
||||
- npm install typescript
|
||||
- typeshare --lang typescript --output-file dist/lib.ts ../server/src
|
28
visions/types/package-lock.json
generated
Normal file
28
visions/types/package-lock.json
generated
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "visions-types",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "visions-types",
|
||||
"version": "0.0.1",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "5.7.3",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz",
|
||||
"integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==",
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
"tsserver": "bin/tsserver"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.17"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
visions/types/package.json
Normal file
15
visions/types/package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "visions-types",
|
||||
"version": "0.0.1",
|
||||
"description": "Shared data types for Visions",
|
||||
"main": "visions.js",
|
||||
"types": "dist/lib.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
}
|
15
visions/types/tsconfig.json
Normal file
15
visions/types/tsconfig.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2016",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["./gen/lib.ts"]
|
||||
}
|
35
visions/ui/package-lock.json
generated
35
visions/ui/package-lock.json
generated
@ -9,7 +9,9 @@
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0"
|
||||
"react-dom": "^19.0.0",
|
||||
"visions-client": "file:../client",
|
||||
"visions-types": "file:../types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.19.0",
|
||||
@ -30,6 +32,29 @@
|
||||
"vite": "^6.1.0"
|
||||
}
|
||||
},
|
||||
"../client": {
|
||||
"name": "visions-client",
|
||||
"version": "0.0.1",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"visions-types": "file:../types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.14",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.5.1",
|
||||
"ts-jest": "^29.2.5",
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
},
|
||||
"../types": {
|
||||
"name": "visions-types",
|
||||
"version": "0.0.1",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"typescript": "^5.7.3"
|
||||
}
|
||||
},
|
||||
"node_modules/@ampproject/remapping": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.3.0.tgz",
|
||||
@ -6678,6 +6703,14 @@
|
||||
"node": ">=10.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/visions-client": {
|
||||
"resolved": "../client",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/visions-types": {
|
||||
"resolved": "../types",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/vite": {
|
||||
"version": "6.1.0",
|
||||
"resolved": "https://registry.npmjs.org/vite/-/vite-6.1.0.tgz",
|
||||
|
@ -13,7 +13,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0"
|
||||
"react-dom": "^19.0.0",
|
||||
"visions-client": "file:../client",
|
||||
"visions-types": "file:../types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.19.0",
|
||||
|
@ -1,42 +1,42 @@
|
||||
#root {
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
transition: filter 300ms;
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
transition: filter 300ms;
|
||||
}
|
||||
.logo:hover {
|
||||
filter: drop-shadow(0 0 2em #646cffaa);
|
||||
filter: drop-shadow(0 0 2em #646cffaa);
|
||||
}
|
||||
.logo.react:hover {
|
||||
filter: drop-shadow(0 0 2em #61dafbaa);
|
||||
filter: drop-shadow(0 0 2em #61dafbaa);
|
||||
}
|
||||
|
||||
@keyframes logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
a:nth-of-type(2) .logo {
|
||||
animation: logo-spin infinite 20s linear;
|
||||
}
|
||||
a:nth-of-type(2) .logo {
|
||||
animation: logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 2em;
|
||||
padding: 2em;
|
||||
}
|
||||
|
||||
.read-the-docs {
|
||||
color: #888;
|
||||
color: #888;
|
||||
}
|
||||
|
49
visions/ui/src/App.test.tsx
Normal file
49
visions/ui/src/App.test.tsx
Normal file
@ -0,0 +1,49 @@
|
||||
import { render } from '@testing-library/react'
|
||||
import { State, Action, Controller, initialState, reducer } from './state'
|
||||
import { Client, ClientResponse } from 'visions-client'
|
||||
import { AuthResponse, SessionId, UserOverview } from 'visions-types'
|
||||
import { useReducer } from 'react'
|
||||
import App from './App'
|
||||
|
||||
class MockClient implements Client {
|
||||
constructor() {}
|
||||
|
||||
async auth(
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<ClientResponse<AuthResponse<SessionId>>> {
|
||||
if (username === 'vakarian' && password === 'aoeu') {
|
||||
return {
|
||||
status: 'ok',
|
||||
content: { type: 'success', content: 'vakarian-session-id' },
|
||||
}
|
||||
} else if (username === 'shephard' && password === 'aoeu') {
|
||||
return {
|
||||
status: 'ok',
|
||||
content: {
|
||||
type: 'password-reset',
|
||||
content: 'shephard-session-id',
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return { status: 'unauthorized' }
|
||||
}
|
||||
}
|
||||
|
||||
async listUsers(
|
||||
sessionId: SessionId,
|
||||
): Promise<ClientResponse<UserOverview[]>> {
|
||||
return { status: 'ok', content: [] }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
describe('App tests', async () => {
|
||||
it('shows the login page when the user isn not logged in', () => {
|
||||
const client = new MockClient;
|
||||
const [state, dispatch] = useReducer(reducer, initialState())
|
||||
let controller = new Controller(client, state, dispatch)
|
||||
render(<App state={state} controller={controller} />)
|
||||
expect(screen.getByText(/Login Page/)).toBeInTheDocument();
|
||||
})
|
||||
})
|
@ -1,35 +1,21 @@
|
||||
import { useState } from 'react'
|
||||
import reactLogo from './assets/react.svg'
|
||||
import viteLogo from '/vite.svg'
|
||||
import './App.css'
|
||||
import { State, Controller, sessionId } from "./state"
|
||||
|
||||
function App() {
|
||||
const [count, setCount] = useState(0)
|
||||
const LoginPage = () => (
|
||||
<div> Login Page </div>
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
<a href="https://vite.dev" target="_blank">
|
||||
<img src={viteLogo} className="logo" alt="Vite logo" />
|
||||
</a>
|
||||
<a href="https://react.dev" target="_blank">
|
||||
<img src={reactLogo} className="logo react" alt="React logo" />
|
||||
</a>
|
||||
</div>
|
||||
<h1>Vite + React</h1>
|
||||
<div className="card">
|
||||
<button onClick={() => setCount((count) => count + 1)}>
|
||||
count is {count}
|
||||
</button>
|
||||
<p>
|
||||
Edit <code>src/App.tsx</code> and save to test HMR
|
||||
</p>
|
||||
</div>
|
||||
<p className="read-the-docs">
|
||||
Click on the Vite and React logos to learn more
|
||||
</p>
|
||||
</>
|
||||
)
|
||||
interface AppProps {
|
||||
state: State
|
||||
controller: Controller
|
||||
}
|
||||
|
||||
const App = ({ state, controller }: AppProps) => {
|
||||
if (sessionId(state)) {
|
||||
<div> User is logged in </div>
|
||||
} else {
|
||||
<LoginPage />
|
||||
}
|
||||
}
|
||||
|
||||
export default App
|
||||
|
@ -1,68 +1,68 @@
|
||||
:root {
|
||||
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
|
||||
line-height: 1.5;
|
||||
font-weight: 400;
|
||||
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
|
||||
line-height: 1.5;
|
||||
font-weight: 400;
|
||||
|
||||
color-scheme: light dark;
|
||||
color: rgba(255, 255, 255, 0.87);
|
||||
background-color: #242424;
|
||||
color-scheme: light dark;
|
||||
color: rgba(255, 255, 255, 0.87);
|
||||
background-color: #242424;
|
||||
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
a {
|
||||
font-weight: 500;
|
||||
color: #646cff;
|
||||
text-decoration: inherit;
|
||||
font-weight: 500;
|
||||
color: #646cff;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
a:hover {
|
||||
color: #535bf2;
|
||||
color: #535bf2;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
place-items: center;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
place-items: center;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 3.2em;
|
||||
line-height: 1.1;
|
||||
font-size: 3.2em;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
button {
|
||||
border-radius: 8px;
|
||||
border: 1px solid transparent;
|
||||
padding: 0.6em 1.2em;
|
||||
font-size: 1em;
|
||||
font-weight: 500;
|
||||
font-family: inherit;
|
||||
background-color: #1a1a1a;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.25s;
|
||||
border-radius: 8px;
|
||||
border: 1px solid transparent;
|
||||
padding: 0.6em 1.2em;
|
||||
font-size: 1em;
|
||||
font-weight: 500;
|
||||
font-family: inherit;
|
||||
background-color: #1a1a1a;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.25s;
|
||||
}
|
||||
button:hover {
|
||||
border-color: #646cff;
|
||||
border-color: #646cff;
|
||||
}
|
||||
button:focus,
|
||||
button:focus-visible {
|
||||
outline: 4px auto -webkit-focus-ring-color;
|
||||
outline: 4px auto -webkit-focus-ring-color;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
:root {
|
||||
color: #213547;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
a:hover {
|
||||
color: #747bff;
|
||||
}
|
||||
button {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
:root {
|
||||
color: #213547;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
a:hover {
|
||||
color: #747bff;
|
||||
}
|
||||
button {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import './index.css'
|
||||
import App from './App.tsx'
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
|
72
visions/ui/src/state.ts
Normal file
72
visions/ui/src/state.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import { Client } from 'visions-client'
|
||||
import { ActionDispatch } from 'react'
|
||||
|
||||
export type AuthState =
|
||||
| { type: 'unauthed' }
|
||||
| { type: 'authed'; sessionId: string }
|
||||
|
||||
export type State = {
|
||||
auth: AuthState
|
||||
}
|
||||
|
||||
export const initialState = (): State => ({
|
||||
auth: { type: 'unauthed' },
|
||||
})
|
||||
|
||||
export const sessionId = (state: State) => string | undefined {
|
||||
if (state.type === 'authed') {
|
||||
return state.sessionId
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export type Action = { type: 'set-auth'; content: AuthState }
|
||||
|
||||
export const reducer = (state: State, action: Action) => {
|
||||
switch (action.type) {
|
||||
case 'set-auth': {
|
||||
return { ...state, auth: action.content }
|
||||
}
|
||||
default: {
|
||||
return state
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Controller {
|
||||
client: Client
|
||||
state: State
|
||||
dispatch: ActionDispatch<[action: Action]>
|
||||
|
||||
constructor(
|
||||
client: Client,
|
||||
state: State,
|
||||
dispatch: ActionDispatch<[action: Action]>,
|
||||
) {
|
||||
this.client = client
|
||||
this.state = state
|
||||
this.dispatch = dispatch
|
||||
}
|
||||
|
||||
// On any request, there are four options.
|
||||
// The request succeeds. No problem.
|
||||
// The request succeeds, but the user needs to reset their password.
|
||||
// The action fails.
|
||||
// The HTTP request itself fails.
|
||||
async auth(username: string, password: string) {
|
||||
let response = await this.client.auth(username, password)
|
||||
switch (response.status) {
|
||||
case 'ok': {
|
||||
this.dispatch({
|
||||
type: 'set-auth',
|
||||
content: {
|
||||
type: 'authed',
|
||||
sessionId: response.content.content,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user