Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reatom #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: yarn install && yarn run build
command: yarn run start


69 changes: 2 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,3 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app), using the [Redux](https://redux.js.org/) and [Redux Toolkit](https://redux-toolkit.js.org/) template.
This is a port of [rtk-entities-basic-example](https://codesandbox.io/s/rtk-entities-basic-example-4jg0m) to [Reatom](https://www.reatom.dev/packages/npm-react).

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.<br />
Open [https://localhost:3000](https://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
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.<br />
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.<br />
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/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `yarn build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
The bundle size is a similar. LoC for model (slice) reduced dramaticaly.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.3.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.1.3",
"react-scripts": "3.4.0"
"@reatom/framework": "^3.1.5",
"@reatom/npm-react": "^3.1.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1"
},
"scripts": {
"start": "react-scripts start",
Expand All @@ -35,4 +35,4 @@
},
"keywords": [],
"description": ""
}
}
34 changes: 14 additions & 20 deletions src/features/users/UsersList.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import {
fetchUsers,
removeUser,
selectTotalUsers,
selectAllUsers
} from "./usersSlice";
import { useAtom, useAction } from "@reatom/npm-react";
import { fetchUsers, removeUser } from "./usersSlice";
import styles from "./UsersList.module.css";

export function UsersList() {
const count = useSelector(selectTotalUsers);
const users = useSelector(selectAllUsers);
const usersLoading = useSelector(state => state.users.loading);
const dispatch = useDispatch();
const [users] = useAtom(fetchUsers.dataAtom);
const [count] = useAtom((ctx) => ctx.spy(fetchUsers.dataAtom).length);
const [isLoading] = useAtom((ctx) => ctx.spy(fetchUsers.pendingAtom) > 0);
const handleFetchUsers = useAction(fetchUsers);
const handleRemoveUser = useAction(removeUser);

return (
<div>
<div className={styles.row}>
<button
className={styles.button}
aria-label="Fetch Users"
onClick={() => dispatch(fetchUsers())}
disabled={usersLoading}
onClick={handleFetchUsers}
disabled={isLoading}
>
Fetch Users
</button>
Expand All @@ -30,15 +26,13 @@ export function UsersList() {
There are <span className={styles.value}>{count}</span> users.{" "}
{count === 0 && `Why don't you fetch some more?`}
</div>
{users.map(user => (
{users.map((user) => (
<div className={styles.row} key={user.id}>
<div style={{ width: "80%" }}>{`${user.first_name} ${
user.last_name
}`}</div>
<div
style={{ width: "80%" }}
>{`${user.first_name} ${user.last_name}`}</div>
<div style={{ width: "20%" }}>
<button onClick={() => dispatch(removeUser(user.id))}>
remove
</button>
<button onClick={() => handleRemoveUser(user.id)}>remove</button>
</div>
</div>
))}
Expand Down
46 changes: 6 additions & 40 deletions src/features/users/usersSlice.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,11 @@
import {
createSlice,
createAsyncThunk,
createEntityAdapter
} from "@reduxjs/toolkit";
import { action, reatomAsync, withDataAtom } from "@reatom/framework";
import userAPI from "./userAPI";

export const fetchUsers = createAsyncThunk("users/fetchAll", async () => {
export const fetchUsers = reatomAsync(async (ctx) => {
const response = await userAPI.fetchAll();
return response.data;
});
}, "fetchUsers").pipe(withDataAtom([]));

export const usersAdapter = createEntityAdapter();

const initialState = usersAdapter.getInitialState({ loading: false });

export const slice = createSlice({
name: "users",
initialState,
reducers: {
removeUser: usersAdapter.removeOne
},
extraReducers: builder => {
builder.addCase(fetchUsers.pending, (state, action) => {
state.loading = true;
});
builder.addCase(fetchUsers.fulfilled, (state, action) => {
usersAdapter.upsertMany(state, action.payload);
state.loading = false;
});
}
});

const reducer = slice.reducer;
export default reducer;

export const { removeUser } = slice.actions;

export const {
selectById: selectUserById,
selectIds: selectUserIds,
selectEntities: selectUserEntities,
selectAll: selectAllUsers,
selectTotal: selectTotalUsers
} = usersAdapter.getSelectors(state => state.users);
export const removeUser = action((ctx, id) => {
fetchUsers.dataAtom(ctx, (users) => users.filter((user) => user.id !== id));
}, "removeUser");
20 changes: 10 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import store from './store';
import { Provider } from 'react-redux';
import * as serviceWorker from './serviceWorker';
import React from "react";
import ReactDOM from "react-dom";
import { reatomContext } from "@reatom/npm-react";
import "./index.css";
import App from "./App";
import { ctx } from "./store";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(
<Provider store={store}>
<reatomContext.Provider value={ctx}>
<App />
</Provider>,
document.getElementById('root')
</reatomContext.Provider>,
document.getElementById("root")
);

// If you want your app to work offline and load faster, you can change
Expand Down
11 changes: 4 additions & 7 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { configureStore } from "@reduxjs/toolkit";
import usersReducer from "./features/users/usersSlice";
import { createCtx, connectLogger } from "@reatom/framework";

export default configureStore({
reducer: {
users: usersReducer
}
});
export const ctx = createCtx();

connectLogger(ctx);
Loading