diff --git a/packages/cra-template-concent-ts/README.md b/packages/cra-template-concent-ts/README.md new file mode 100644 index 00000000000..db393c4478e --- /dev/null +++ b/packages/cra-template-concent-ts/README.md @@ -0,0 +1,22 @@ +# cra-template-concent-ts + +The official [Concent](https://github.com/concentjs/concent)+TS template for Create React App. + +You can checkout it from [here](https://github.com/concentjs/cra-template-concent-ts) + +To use this template, add `--template concent-ts` when creating a new app. + +For example: + +```sh +npx create-react-app my-app --template concent-ts + +# or + +yarn create react-app my-app --template concent-ts +``` + +For more information, please refer to: + +- [Getting Started](https://create-react-app.dev/docs/getting-started) – How to create a new app. +- [User Guide](https://create-react-app.dev) – How to develop apps bootstrapped with Create React App. diff --git a/packages/cra-template-concent-ts/package.json b/packages/cra-template-concent-ts/package.json new file mode 100644 index 00000000000..7ac9b9de9fb --- /dev/null +++ b/packages/cra-template-concent-ts/package.json @@ -0,0 +1,28 @@ +{ + "name": "cra-template-concent-ts", + "version": "1.0.0", + "keywords": [ + "react", + "create-react-app", + "template", + "typescript", + "concent" + ], + "description": "The base Concent TypeScript template for Create React App.", + "repository": { + "type": "git", + "url": "https://github.com/facebook/create-react-app.git", + "directory": "packages/cra-template-concent-ts" + }, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "bugs": { + "url": "https://github.com/facebook/create-react-app/issues" + }, + "files": [ + "template", + "template.json" + ] +} diff --git a/packages/cra-template-concent-ts/template.json b/packages/cra-template-concent-ts/template.json new file mode 100644 index 00000000000..8e4bb1b55c1 --- /dev/null +++ b/packages/cra-template-concent-ts/template.json @@ -0,0 +1,19 @@ +{ + "package": { + "dependencies": { + "@testing-library/jest-dom": "^5.11.4", + "@testing-library/react": "^11.1.0", + "@testing-library/user-event": "^12.1.10", + "@types/node": "^12.0.0", + "@types/react": "^17.0.0", + "@types/react-dom": "^17.0.0", + "@types/jest": "^26.0.15", + "typescript": "^4.1.2", + "web-vitals": "^1.0.1", + "concent": "^2.10.5" + }, + "eslintConfig": { + "extends": ["react-app", "react-app/jest"] + } + } +} diff --git a/packages/cra-template-concent-ts/template/.gitignore b/packages/cra-template-concent-ts/template/.gitignore new file mode 100644 index 00000000000..4d29575de80 --- /dev/null +++ b/packages/cra-template-concent-ts/template/.gitignore @@ -0,0 +1,23 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/packages/cra-template-concent-ts/template/README.md b/packages/cra-template-concent-ts/template/README.md new file mode 100644 index 00000000000..6070bbc1297 --- /dev/null +++ b/packages/cra-template-concent-ts/template/README.md @@ -0,0 +1,44 @@ +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app), using the [Concent](https://github.com/concentjs/concent). + +## Available Scripts + +In the project directory, you can run: + +### `yarn start` + +Runs the app in the development mode.
+Open [http://localhost:3000](http://localhost:3000) to view it in the browser. + +The page will reload if you make edits.
+You will also see any lint errors in the console. + +### `yarn test` + +Launches the test runner in the interactive watch mode.
+See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `yarn build` + +Builds the app for production to the `build` folder.
+It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.
+Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `yarn eject` + +**Note: this is a one-way operation. Once you `eject`, you can’t go back!** + +If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. + +You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). diff --git a/packages/cra-template-concent-ts/template/public/favicon.ico b/packages/cra-template-concent-ts/template/public/favicon.ico new file mode 100644 index 00000000000..354202a4e6b Binary files /dev/null and b/packages/cra-template-concent-ts/template/public/favicon.ico differ diff --git a/packages/cra-template-concent-ts/template/public/index.html b/packages/cra-template-concent-ts/template/public/index.html new file mode 100644 index 00000000000..1dbdca65e86 --- /dev/null +++ b/packages/cra-template-concent-ts/template/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + React Redux App + + + +
+ + + diff --git a/packages/cra-template-concent-ts/template/public/logo192.png b/packages/cra-template-concent-ts/template/public/logo192.png new file mode 100644 index 00000000000..33624109cdc Binary files /dev/null and b/packages/cra-template-concent-ts/template/public/logo192.png differ diff --git a/packages/cra-template-concent-ts/template/public/logo512.png b/packages/cra-template-concent-ts/template/public/logo512.png new file mode 100644 index 00000000000..b3516222f0b Binary files /dev/null and b/packages/cra-template-concent-ts/template/public/logo512.png differ diff --git a/packages/cra-template-concent-ts/template/public/manifest.json b/packages/cra-template-concent-ts/template/public/manifest.json new file mode 100644 index 00000000000..080d6c77ac2 --- /dev/null +++ b/packages/cra-template-concent-ts/template/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/packages/cra-template-concent-ts/template/public/robots.txt b/packages/cra-template-concent-ts/template/public/robots.txt new file mode 100644 index 00000000000..e9e57dc4d41 --- /dev/null +++ b/packages/cra-template-concent-ts/template/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/packages/cra-template-concent-ts/template/src/App.css b/packages/cra-template-concent-ts/template/src/App.css new file mode 100644 index 00000000000..01cc5867703 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/App.css @@ -0,0 +1,39 @@ +.App { + text-align: center; +} + +.App-logo { + height: 40vmin; + pointer-events: none; +} + +@media (prefers-reduced-motion: no-preference) { + .App-logo { + animation: App-logo-float infinite 3s ease-in-out; + } +} + +.App-header { + min-height: 100vh; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: calc(10px + 2vmin); +} + +.App-link { + color: rgb(112, 76, 182); +} + +@keyframes App-logo-float { + 0% { + transform: translateY(0); + } + 50% { + transform: translateY(10px); + } + 100% { + transform: translateY(0px); + } +} diff --git a/packages/cra-template-concent-ts/template/src/App.test.tsx b/packages/cra-template-concent-ts/template/src/App.test.tsx new file mode 100644 index 00000000000..659cc13d3bb --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/App.test.tsx @@ -0,0 +1,15 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import { Provider } from 'react-redux'; +import { store } from './app/store'; +import App from './App'; + +test('renders learn react link', () => { + const { getByText } = render( + + + + ); + + expect(getByText(/learn/i)).toBeInTheDocument(); +}); diff --git a/packages/cra-template-concent-ts/template/src/App.tsx b/packages/cra-template-concent-ts/template/src/App.tsx new file mode 100644 index 00000000000..70c170a33f4 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/App.tsx @@ -0,0 +1,42 @@ +import React from 'react'; +import logo from './logo.svg'; +import { Counter } from 'features/counter/Counter'; +import { CounterTip } from 'features/counter/CounterTip'; +import './App.css'; + +function App() { + return ( +
+
+ logo + +

+ Edit src/App.tsx and save to reload. +

+ + Learn + + React + + , + + Concent + + + +
+
+ ); +} + +export default App; diff --git a/packages/cra-template-concent-ts/template/src/configs/c2Mods.ts b/packages/cra-template-concent-ts/template/src/configs/c2Mods.ts new file mode 100644 index 00000000000..d0c962897b5 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/configs/c2Mods.ts @@ -0,0 +1,7 @@ +/** + * delare module names in this file + */ + +export const COUNTER = 'Counter' as const; + +export type COUNTER_T = typeof COUNTER; diff --git a/packages/cra-template-concent-ts/template/src/configs/runConcent.ts b/packages/cra-template-concent-ts/template/src/configs/runConcent.ts new file mode 100644 index 00000000000..ccb4dd27daa --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/configs/runConcent.ts @@ -0,0 +1,5 @@ +import { run } from 'concent'; +import models from '../models'; + +// load all models by concent.run api +run(models); diff --git a/packages/cra-template-concent-ts/template/src/configs/useC2Mod.ts b/packages/cra-template-concent-ts/template/src/configs/useC2Mod.ts new file mode 100644 index 00000000000..abd296a417f --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/configs/useC2Mod.ts @@ -0,0 +1,11 @@ +import { useConcent } from 'concent'; +import { CtxM, Modules } from '../types/store'; + +/** + * use the target model context you want by passing a module name + * @param moduleName + */ +export function useC2Mod(moduleName: T) { + type Ctx = CtxM<{}, typeof moduleName>; + return useConcent<{}, Ctx>(moduleName); +} diff --git a/packages/cra-template-concent-ts/template/src/features/counter/Counter.module.css b/packages/cra-template-concent-ts/template/src/features/counter/Counter.module.css new file mode 100644 index 00000000000..6685b780ed6 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/Counter.module.css @@ -0,0 +1,81 @@ +.row { + display: flex; + align-items: center; + justify-content: center; +} + +.row:not(:last-child) { + margin-bottom: 16px; +} + +.value { + font-size: 78px; + padding-left: 16px; + padding-right: 16px; + margin-top: 2px; + font-family: 'Courier New', Courier, monospace; +} + +.button { + appearance: none; + background: none; + font-size: 32px; + padding-left: 12px; + padding-right: 12px; + outline: none; + border: 2px solid transparent; + color: rgb(112, 76, 182); + padding-bottom: 4px; + cursor: pointer; + background-color: rgba(112, 76, 182, 0.1); + border-radius: 2px; + transition: all 0.15s; +} + +.textbox { + font-size: 32px; + padding: 2px; + width: 64px; + text-align: center; + margin-right: 8px; +} + +.button:hover, +.button:focus { + border: 2px solid rgba(112, 76, 182, 0.4); +} + +.button:active { + background-color: rgba(112, 76, 182, 0.2); +} + +.asyncButton { + composes: button; + position: relative; + margin-left: 8px; +} + +.normalButton { + composes: button; + position: relative; + margin-left: 8px; +} + +.asyncButton:after { + content: ''; + background-color: rgba(112, 76, 182, 0.15); + display: block; + position: absolute; + width: 100%; + height: 100%; + left: 0; + top: 0; + opacity: 0; + transition: width 1s linear, opacity 0.5s ease 1s; +} + +.asyncButton:active:after { + width: 0%; + opacity: 1; + transition: 0s; +} diff --git a/packages/cra-template-concent-ts/template/src/features/counter/Counter.tsx b/packages/cra-template-concent-ts/template/src/features/counter/Counter.tsx new file mode 100644 index 00000000000..cc7148e7299 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/Counter.tsx @@ -0,0 +1,59 @@ +import React, { useState } from 'react'; +import { useC2Mod } from 'configs/useC2Mod'; +import { COUNTER } from 'configs/c2Mods'; +import styles from './Counter.module.css'; + +export function Counter() { + const ctx = useC2Mod(COUNTER); + // module state, module computed, module reducer + const { state, moduleComputed, mr } = ctx; + const [incrementAmount, setIncrementAmount] = useState('2'); + + return ( +
+
+ + {state.value} + + doubleCount: {moduleComputed.doubleCount} + + +
+
+ setIncrementAmount(e.target.value)} + /> + + + +
+
+ ); +} diff --git a/packages/cra-template-concent-ts/template/src/features/counter/CounterTip.tsx b/packages/cra-template-concent-ts/template/src/features/counter/CounterTip.tsx new file mode 100644 index 00000000000..a05dc5e5461 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/CounterTip.tsx @@ -0,0 +1,17 @@ +import React, { useState } from 'react'; +import { useC2Mod } from 'configs/useC2Mod'; +import { COUNTER } from 'configs/c2Mods'; + +export const CounterTip = React.memo(function CounterTip() { + const ctx = useC2Mod(COUNTER); + // module state, module computed + const { state, moduleComputed } = ctx; + + return ( +
+ value:{state.value} + bigValue:{state.bigValue} + doubleCount:{moduleComputed.doubleCount} +
+ ); +}); diff --git a/packages/cra-template-concent-ts/template/src/features/counter/model/computed.ts b/packages/cra-template-concent-ts/template/src/features/counter/model/computed.ts new file mode 100644 index 00000000000..a7205406326 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/model/computed.ts @@ -0,0 +1,6 @@ +import { St } from './state'; + +// only value change will triiger this function to execute again +export function doubleCount({ value }: St) { + return value * 2; +} diff --git a/packages/cra-template-concent-ts/template/src/features/counter/model/index.ts b/packages/cra-template-concent-ts/template/src/features/counter/model/index.ts new file mode 100644 index 00000000000..8891fd4effa --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/model/index.ts @@ -0,0 +1,12 @@ +import { COUNTER } from '../../../configs/c2Mods'; +import state from './state'; +import * as computed from './computed'; +import * as reducer from './reducer'; + +export default { + [COUNTER]: { + state, + computed, + reducer, + }, +}; diff --git a/packages/cra-template-concent-ts/template/src/features/counter/model/reducer.ts b/packages/cra-template-concent-ts/template/src/features/counter/model/reducer.ts new file mode 100644 index 00000000000..7ef942459c8 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/model/reducer.ts @@ -0,0 +1,46 @@ +import { St } from './state'; +import { VoidPayloadMev, AC } from 'types/store'; +import { COUNTER_T } from 'configs/c2Mods'; + +type IAC = AC; + +const delay = (ms = 1000) => new Promise(r => setTimeout(r, ms)); + +export function incrementBigValue( + payload: VoidPayloadMev, + moduleState: St +): Partial { + return { bigValue: moduleState.bigValue + 50 }; +} + +export function increment( + payload: VoidPayloadMev, + moduleState: St +): Partial { + return { value: moduleState.value + 1 }; +} + +export function decrement( + payload: VoidPayloadMev, + moduleState: St +): Partial { + return { value: moduleState.value - 1 }; +} + +export function incrementByAmount( + amount: number, + moduleState: St +): Partial { + return { value: moduleState.value + amount }; +} + +export async function incrementAsync( + amount: number, + moduleState: St, + ac: IAC +): Promise> { + await delay(); + // or just write ac.dispatch of return + // await ac.dispatch(incrementByAmount, amount); + return { value: moduleState.value + amount }; +} diff --git a/packages/cra-template-concent-ts/template/src/features/counter/model/state.ts b/packages/cra-template-concent-ts/template/src/features/counter/model/state.ts new file mode 100644 index 00000000000..dfa4246fc37 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/features/counter/model/state.ts @@ -0,0 +1,10 @@ +function getInitialState() { + return { + value: 0, + bigValue: 100, + }; +} + +export type St = ReturnType; + +export default getInitialState; diff --git a/packages/cra-template-concent-ts/template/src/index.css b/packages/cra-template-concent-ts/template/src/index.css new file mode 100644 index 00000000000..ec2585e8c0b --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/index.css @@ -0,0 +1,13 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} diff --git a/packages/cra-template-concent-ts/template/src/index.tsx b/packages/cra-template-concent-ts/template/src/index.tsx new file mode 100644 index 00000000000..b3b227c68cd --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/index.tsx @@ -0,0 +1,18 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import 'configs/runConcent'; +import './index.css'; +import App from './App'; +import * as serviceWorker from './serviceWorker'; + +ReactDOM.render( + + + , + document.getElementById('root') +); + +// If you want your app to work offline and load faster, you can change +// unregister() to register() below. Note this comes with some pitfalls. +// Learn more about service workers: https://bit.ly/CRA-PWA +serviceWorker.unregister(); diff --git a/packages/cra-template-concent-ts/template/src/logo.svg b/packages/cra-template-concent-ts/template/src/logo.svg new file mode 100644 index 00000000000..84667388ceb --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/logo.svg @@ -0,0 +1 @@ + diff --git a/packages/cra-template-concent-ts/template/src/models/global/index.ts b/packages/cra-template-concent-ts/template/src/models/global/index.ts new file mode 100644 index 00000000000..85860fdee5d --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/models/global/index.ts @@ -0,0 +1,8 @@ +import { cst } from 'concent'; +import state from './state'; + +export default { + [cst.MODULE_GLOBAL]: { + state, + }, +}; diff --git a/packages/cra-template-concent-ts/template/src/models/global/state.ts b/packages/cra-template-concent-ts/template/src/models/global/state.ts new file mode 100644 index 00000000000..273aeb54603 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/models/global/state.ts @@ -0,0 +1,9 @@ +function getInitialState() { + return { + someInfo: `overWrite built-in module global's state`, + }; +} + +export type St = ReturnType; + +export default getInitialState; diff --git a/packages/cra-template-concent-ts/template/src/models/index.ts b/packages/cra-template-concent-ts/template/src/models/index.ts new file mode 100644 index 00000000000..5400976e0ff --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/models/index.ts @@ -0,0 +1,10 @@ +// you should declare all models here then expose it +import counterModel from '../features/counter/model'; +import globalModel from './global'; + +const allModels = { + ...globalModel, + ...counterModel, +}; + +export default allModels; diff --git a/packages/cra-template-concent-ts/template/src/react-app-env.d.ts b/packages/cra-template-concent-ts/template/src/react-app-env.d.ts new file mode 100644 index 00000000000..6431bc5fc6b --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/react-app-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/packages/cra-template-concent-ts/template/src/serviceWorker.ts b/packages/cra-template-concent-ts/template/src/serviceWorker.ts new file mode 100644 index 00000000000..109ab0e1de9 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/serviceWorker.ts @@ -0,0 +1,146 @@ +// This optional code is used to register a service worker. +// register() is not called by default. + +// This lets the app load faster on subsequent visits in production, and gives +// it offline capabilities. However, it also means that developers (and users) +// will only see deployed updates on subsequent visits to a page, after all the +// existing tabs open on the page have been closed, since previously cached +// resources are updated in the background. + +// To learn more about the benefits of this model and instructions on how to +// opt-in, read https://bit.ly/CRA-PWA + +const isLocalhost = Boolean( + window.location.hostname === 'localhost' || + // [::1] is the IPv6 localhost address. + window.location.hostname === '[::1]' || + // 127.0.0.0/8 are considered localhost for IPv4. + window.location.hostname.match( + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ) +); + +type Config = { + onSuccess?: (registration: ServiceWorkerRegistration) => void; + onUpdate?: (registration: ServiceWorkerRegistration) => void; +}; + +export function register(config?: Config) { + if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { + // The URL constructor is available in all browsers that support SW. + const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); + if (publicUrl.origin !== window.location.origin) { + // Our service worker won't work if PUBLIC_URL is on a different origin + // from what our page is served on. This might happen if a CDN is used to + // serve assets; see https://github.com/facebook/create-react-app/issues/2374 + return; + } + + window.addEventListener('load', () => { + const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; + + if (isLocalhost) { + // This is running on localhost. Let's check if a service worker still exists or not. + checkValidServiceWorker(swUrl, config); + + // Add some additional logging to localhost, pointing developers to the + // service worker/PWA documentation. + navigator.serviceWorker.ready.then(() => { + console.log( + 'This web app is being served cache-first by a service ' + + 'worker. To learn more, visit https://bit.ly/CRA-PWA' + ); + }); + } else { + // Is not localhost. Just register service worker + registerValidSW(swUrl, config); + } + }); + } +} + +function registerValidSW(swUrl: string, config?: Config) { + navigator.serviceWorker + .register(swUrl) + .then(registration => { + registration.onupdatefound = () => { + const installingWorker = registration.installing; + if (installingWorker == null) { + return; + } + installingWorker.onstatechange = () => { + if (installingWorker.state === 'installed') { + if (navigator.serviceWorker.controller) { + // At this point, the updated precached content has been fetched, + // but the previous service worker will still serve the older + // content until all client tabs are closed. + console.log( + 'New content is available and will be used when all ' + + 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' + ); + + // Execute callback + if (config && config.onUpdate) { + config.onUpdate(registration); + } + } else { + // At this point, everything has been precached. + // It's the perfect time to display a + // "Content is cached for offline use." message. + console.log('Content is cached for offline use.'); + + // Execute callback + if (config && config.onSuccess) { + config.onSuccess(registration); + } + } + } + }; + }; + }) + .catch(error => { + console.error('Error during service worker registration:', error); + }); +} + +function checkValidServiceWorker(swUrl: string, config?: Config) { + // Check if the service worker can be found. If it can't reload the page. + fetch(swUrl, { + headers: { 'Service-Worker': 'script' }, + }) + .then(response => { + // Ensure service worker exists, and that we really are getting a JS file. + const contentType = response.headers.get('content-type'); + if ( + response.status === 404 || + (contentType != null && contentType.indexOf('javascript') === -1) + ) { + // No service worker found. Probably a different app. Reload the page. + navigator.serviceWorker.ready.then(registration => { + registration.unregister().then(() => { + window.location.reload(); + }); + }); + } else { + // Service worker found. Proceed as normal. + registerValidSW(swUrl, config); + } + }) + .catch(() => { + console.log( + 'No internet connection found. App is running in offline mode.' + ); + }); +} + +export function unregister() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.ready + .then(registration => { + registration.unregister(); + }) + .catch(error => { + console.error(error.message); + }); + } +} diff --git a/packages/cra-template-concent-ts/template/src/setupTests.ts b/packages/cra-template-concent-ts/template/src/setupTests.ts new file mode 100644 index 00000000000..74b1a275a0e --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/setupTests.ts @@ -0,0 +1,5 @@ +// jest-dom adds custom jest matchers for asserting on DOM nodes. +// allows you to do things like: +// expect(element).toHaveTextContent(/react/i) +// learn more: https://github.com/testing-library/jest-dom +import '@testing-library/jest-dom/extend-expect'; diff --git a/packages/cra-template-concent-ts/template/src/types/store.ts b/packages/cra-template-concent-ts/template/src/types/store.ts new file mode 100644 index 00000000000..206c8d0b3e1 --- /dev/null +++ b/packages/cra-template-concent-ts/template/src/types/store.ts @@ -0,0 +1,146 @@ +import { + ICtx, + IActionCtx, + IAnyObj, + MODULE_VOID, + MODULE_DEFAULT, + MODULE_GLOBAL, + GetRootState, + GetRootReducer, + GetRootComputed, +} from 'concent'; +import models from '../models'; + +const allModels = models; +type Models = typeof allModels; + +export type RootState = GetRootState; + +export type RootRd = GetRootReducer; + +export type RootCu = GetRootComputed; + +export type TGlobalSt = RootState[MODULE_GLOBAL]; +export type Modules = keyof RootState; + +/** ultil type based on actionCtx */ +export type AC< + M extends Modules, + FullState extends IAnyObj = RootState[M] +> = IActionCtx, FullState>; +// ******************************** +// ultil types based on Ctx +// ******************************** + +/** belong one module. CtxM */ +export type CtxM< + P = {}, + M extends Modules = MODULE_DEFAULT, + Se = {}, + RefCu = {}, + Mp = {} +> = ICtx; + +/** belong one module,expand private state. CtxMS*/ +export type CtxMS< + P = {}, + M extends Modules = MODULE_DEFAULT, + St = {}, + Se = {}, + RefCu = {}, + Mp = {} +> = ICtx; + +/** belong one module, connect other modules. CtxMConn */ +export type CtxMConn< + P = {}, + M extends Modules = MODULE_DEFAULT, + Conn extends Modules = MODULE_VOID, + Se = {}, + RefCu = {}, + Mp = {} +> = ICtx; + +/** belong one module,expand private state, connect other modules. CtxMSConn */ +export type CtxMSConn< + P = {}, + M extends Modules = MODULE_DEFAULT, + St = {}, + Conn extends Modules = MODULE_VOID, + Se = {}, + RefCu = {}, + Mp = {} +> = ICtx; + +/** expand private state, connect other modules. CtxMSConn */ +export type CtxSConn< + P = {}, + St = {}, + Conn extends Modules = MODULE_VOID, + Se = {}, + RefCu = {}, + Mp = {} +> = ICtx; + +/** expand private state. CtxMSConn */ +export type CtxS

= ICtx< + RootState, + RootRd, + RootCu, + P, + St, + MODULE_DEFAULT, + MODULE_VOID, + Se, + RefCu, + Mp +>; + +/** connect other modules. CtxConn */ +export type CtxConn< + P = {}, + Conn extends Modules = MODULE_VOID, + Se = {}, + RefCu = {}, + Mp = {} +> = ICtx; + +/** default series, when no module specified, the component belong to $$default module by default */ +export type CtxDe

= CtxM< + P, + MODULE_DEFAULT, + Se, + RefCu, + Mp +>; +export type CtxDeS

= CtxMS< + P, + MODULE_DEFAULT, + St, + Se, + RefCu, + Mp +>; +export type CtxDeSConn< + P = {}, + St = {}, + Conn extends Modules = MODULE_VOID, + Se = {}, + RefCu = {}, + Mp = {} +> = CtxMSConn; +export type CtxDeConn< + P = {}, + Conn extends Modules = MODULE_VOID, + Se = {}, + RefCu = {}, + Mp = {} +> = CtxSConn; + +export type ItemsType = Arr extends ReadonlyArray ? E : never; + +export type VoidPayload = null | void; + +export type VoidPayloadMev = + | VoidPayload + | React.MouseEvent; diff --git a/packages/cra-template-concent-ts/template/tsconfig.json b/packages/cra-template-concent-ts/template/tsconfig.json new file mode 100644 index 00000000000..126ba4399cc --- /dev/null +++ b/packages/cra-template-concent-ts/template/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "baseUrl": "src", + "target": "es5", + "lib": ["dom", "dom.iterable", "esnext"], + "allowJs": true, + "skipLibCheck": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, + "module": "esnext", + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react" + }, + "include": ["src"] +}