From cf01670ee681d104d0aaba8ef1a9a5146666e66f Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 3 Sep 2022 14:26:41 +1000 Subject: [PATCH] unistore wip --- client/.babelrc.js | 1 + client/package-lock.json | 25 ++++++ client/package.json | 1 + client/src/app.tsx | 66 +++++++--------- client/src/components/acp.offer.tsx | 2 +- client/src/components/bundle.tsx | 2 +- client/src/components/header.tsx | 14 +++- client/src/components/offer.tsx | 4 +- client/src/{actions.ts => http.ts} | 0 client/src/routes/acp.tsx | 4 +- client/src/routes/bundles.tsx | 4 +- client/src/routes/home.tsx | 16 ++-- client/src/routes/list.tsx | 4 +- client/src/routes/login.tsx | 34 +++++--- client/src/routes/search.tsx | 8 +- client/src/store.tsx | 32 ++++++-- client/src/ws.ts | 115 ++++++++++++++++++++++++++++ 17 files changed, 253 insertions(+), 79 deletions(-) rename client/src/{actions.ts => http.ts} (100%) create mode 100644 client/src/ws.ts diff --git a/client/.babelrc.js b/client/.babelrc.js index 1931bed..abbe6f3 100644 --- a/client/.babelrc.js +++ b/client/.babelrc.js @@ -1,3 +1,4 @@ module.exports = { parserOpts: { strictMode: false }, + plugins: ['@babel/plugin-transform-react-jsx-source'] }; diff --git a/client/package-lock.json b/client/package-lock.json index 4ef0604..34d06c5 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -19,6 +19,7 @@ "unistore": "^3.5.2" }, "devDependencies": { + "@babel/plugin-transform-react-jsx-source": "^7.18.6", "@typescript-eslint/eslint-plugin": "^5.30.6", "@typescript-eslint/parser": "^5.30.6", "eslint": "^8.20.0", @@ -1598,6 +1599,21 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/plugin-transform-react-jsx-source": { + "version": "7.18.6", + "resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-jsx-source/-/plugin-transform-react-jsx-source-7.18.6.tgz", + "integrity": "sha512-utZmlASneDfdaMh0m/WausbjUjEdGrQJz0vFK93d7wD3xf5wBtX219+q6IlCNZeguIcxS2f/CvLZrlLSvSHQXw==", + "dev": true, + "dependencies": { + "@babel/helper-plugin-utils": "^7.18.6" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/plugin-transform-regenerator": { "version": "7.18.6", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-regenerator/-/plugin-transform-regenerator-7.18.6.tgz", @@ -22250,6 +22266,15 @@ "@babel/types": "^7.18.6" } }, + "@babel/plugin-transform-react-jsx-source": { + "version": "7.18.6", + "resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-jsx-source/-/plugin-transform-react-jsx-source-7.18.6.tgz", + "integrity": "sha512-utZmlASneDfdaMh0m/WausbjUjEdGrQJz0vFK93d7wD3xf5wBtX219+q6IlCNZeguIcxS2f/CvLZrlLSvSHQXw==", + "dev": true, + "requires": { + "@babel/helper-plugin-utils": "^7.18.6" + } + }, "@babel/plugin-transform-regenerator": { "version": "7.18.6", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-regenerator/-/plugin-transform-regenerator-7.18.6.tgz", diff --git a/client/package.json b/client/package.json index bd7d133..97236b9 100644 --- a/client/package.json +++ b/client/package.json @@ -29,6 +29,7 @@ "unistore": "^3.5.2" }, "devDependencies": { + "@babel/plugin-transform-react-jsx-source": "^7.18.6", "@typescript-eslint/eslint-plugin": "^5.30.6", "@typescript-eslint/parser": "^5.30.6", "eslint": "^8.20.0", diff --git a/client/src/app.tsx b/client/src/app.tsx index 5ce7c96..f545e2a 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -1,8 +1,11 @@ import { h } from 'preact'; import { useState, useEffect } from 'preact/hooks'; import { Route, Router, route } from 'preact-router'; +import { Provider, connect } from 'unistore/preact' -import actions from './actions'; +import { createStore, actions } from './store'; +import createWs from './ws'; +import http from './http'; import Header from './components/header'; @@ -13,47 +16,36 @@ import Login from './routes/login'; import Search from './routes/search'; import List from './routes/list'; -function App() { - const [user, setUser] = useState(null); +const store = createStore(); +const ws = createWs(store); - useEffect(() => { - async function getLogin() { - const [error, user] = await actions.getLogin(); - if (error) return route('/login'); - setUser(user); - route('/bundles'); - } +async function init() { + const [error, user] = await http.getLogin(); + if (error) return route('/login'); + route('/bundles'); + store.setState({ user }); +} - getLogin(); - }, []); - - function loginSuccess(user) { - setUser(user); - route('/search'); - } - - async function logout(e) { - e.preventDefault(); - await actions.postLogout(); - route('/login'); - setUser(null); - } +init(); +const App = () =>{ return ( -
-
- - - - - - + +
+
+ + + + + + - {/* ACP */} - - - -
+ {/* ACP */} + + +
+
+ ); } diff --git a/client/src/components/acp.offer.tsx b/client/src/components/acp.offer.tsx index d44d39d..3cfcacc 100644 --- a/client/src/components/acp.offer.tsx +++ b/client/src/components/acp.offer.tsx @@ -10,7 +10,7 @@ export default function Offer(offer) { const [accepted, setAccepted] = useState(offer.status !== 'pending'); async function onClick() { - const [error] = await actions.postOfferAccept(offer.id); + const [error] = await http.postOfferAccept(offer.id); if (error) return false; setAccepted(true); } diff --git a/client/src/components/bundle.tsx b/client/src/components/bundle.tsx index 277944a..1d2ad6d 100644 --- a/client/src/components/bundle.tsx +++ b/client/src/components/bundle.tsx @@ -48,7 +48,7 @@ export default function Bundle(bundle) { async function submit(e) { e.preventDefault(); - const [error, offers] = await actions.postOffers(state.checkedIds, shipping, baseShipping); + const [error, offers] = await http.postOffers(state.checkedIds, shipping, baseShipping); console.log(error, offers); } diff --git a/client/src/components/header.tsx b/client/src/components/header.tsx index 5f50532..1ec63fb 100644 --- a/client/src/components/header.tsx +++ b/client/src/components/header.tsx @@ -1,9 +1,17 @@ import { h } from 'preact'; import { route } from 'preact-router'; +import { connect } from 'unistore/preact' -import actions from './../actions'; +import http from './../http'; +import { actions } from './../store'; const Header = ({ user, logout }) => { + + function logoutClick(e) { + e.preventDefault(); + logout(); + } + const nav = () => (