Skip to content

Commit

Permalink
refact, please ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Apr 23, 2023
1 parent 70d4a50 commit 6f4d50d
Show file tree
Hide file tree
Showing 52 changed files with 341 additions and 1 deletion.
1 change: 1 addition & 0 deletions client/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
22 changes: 22 additions & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.eslintcache

npm-debug.log*
yarn-debug.log*
yarn-error.log*
15 changes: 15 additions & 0 deletions client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# React-admin tutorial

This is the application built while following the [tutorial](https://marmelab.com/react-admin/Tutorial.html).

## How to run

After having cloned the react-admin repository, run the following commands:

```sh
make install

make build

make run-tutorial
```
17 changes: 17 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "tutorial",
"version": "4.9.1",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"ra-data-json-server": "^4.9.1",
"ra-data-simple-rest": "^4.9.1",
"react": "^18.2.0",
"react-admin": "^4.9.1",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.22",
"@types/react-dom": "^18.0.7",
"@vitejs/plugin-react": "^2.2.0",
"typescript": "^4.6.4",
"vite": "^3.2.0"
}
}
Binary file added client/public/favicon.ico
Binary file not shown.
9 changes: 9 additions & 0 deletions client/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"infiniteLoopProtection": true,
"hardReloadOnChange": false,
"view": "browser",
"template": "node",
"container": {
"node": "16"
}
}
47 changes: 47 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Admin, Resource } from "react-admin";
import { fetchUtils } from "ra-core";
import jsonServerProvider from "ra-data-json-server";
import PostIcon from "@mui/icons-material/Book";
import UserIcon from "@mui/icons-material/Group";

import { PostList, PostEdit, PostCreate, PostShow } from "./posts";
import { UserList } from "./users";
import { Dashboard } from "./Dashboard";
import { authProvider } from "./authProvider";

import simpleRestProvider from "ra-data-simple-rest";

// const dataProvider = simpleRestProvider(
// "http://localhost:7001",
// fetchUtils.fetchJson,
// "X-Total-Count"
// );

// const dataProvider = jsonServerProvider("http://127.0.0.1:7001");

const dataProvider = jsonServerProvider("https://jsonplaceholder.typicode.com");

const App = () => (
<Admin
authProvider={authProvider}
dataProvider={dataProvider}
dashboard={Dashboard}
>
<Resource
name="posts"
list={PostList}
edit={PostEdit}
create={PostCreate}
icon={PostIcon}
show={PostShow}
/>
<Resource
name="users"
list={UserList}
icon={UserIcon}
recordRepresentation="name"
/>
</Admin>
);

export default App;
8 changes: 8 additions & 0 deletions client/src/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Card, CardContent, CardHeader } from '@mui/material';

export const Dashboard = () => (
<Card>
<CardHeader title="Welcome to the administration" />
<CardContent>Lorem ipsum sic dolor amet...</CardContent>
</Card>
);
15 changes: 15 additions & 0 deletions client/src/MyUrlField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useRecordContext } from 'react-admin';
import { Link } from '@mui/material';
import LaunchIcon from '@mui/icons-material/Launch';

const MyUrlField = ({ source }: { source: string }) => {
const record = useRecordContext();
return record ? (
<Link href={record[source]} sx={{ textDecoration: 'none' }}>
{record[source]}
<LaunchIcon sx={{ fontSize: 15, ml: 1 }} />
</Link>
) : null;
};

export default MyUrlField;
1 change: 1 addition & 0 deletions client/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions client/src/authProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AuthProvider } from 'react-admin';

export const authProvider: AuthProvider = {
// called when the user attempts to log in
login: ({ username }: { username: string }) => {
localStorage.setItem('username', username);
// accept all username/password combinations
return Promise.resolve();
},
// called when the user clicks on the logout button
logout: () => {
localStorage.removeItem('username');
return Promise.resolve();
},
// called when the API returns an error
checkError: ({ status }: { status: number }) => {
if (status === 401 || status === 403) {
localStorage.removeItem('username');
return Promise.reject();
}
return Promise.resolve();
},
// called when the user navigates to a new location, to check for authentication
checkAuth: () => {
return localStorage.getItem('username')
? Promise.resolve()
: Promise.reject();
},
// called when the user navigates to a new location, to check for permissions / roles
getPermissions: () => Promise.resolve(),
};
3 changes: 3 additions & 0 deletions client/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
margin: 0;
}
10 changes: 10 additions & 0 deletions client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
71 changes: 71 additions & 0 deletions client/src/posts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
List,
Datagrid,
TextField,
ReferenceField,
EditButton,
DeleteButton,
SimpleShowLayout,
Show,
ShowButton,
Edit,
Create,
SimpleForm,
ReferenceInput,
TextInput,
useRecordContext,
} from "react-admin";

const postFilters = [
<TextInput source="q" label="Search" alwaysOn />,
<ReferenceInput source="userId" label="User" reference="users" />,
];

export const PostList = () => (
<List filters={postFilters}>
<Datagrid rowClick="edit">
<TextField source="id" />
<TextField source="title" />
<EditButton label="编辑" />
<ShowButton label="查看" />
<DeleteButton label="删除" />
</Datagrid>
</List>
);

const PostTitle = () => {
const record = useRecordContext();
return <span>Post {record ? `"${record.title}"` : ""}</span>;
};

export const PostEdit = () => (
<Edit title={<PostTitle />}>
<SimpleForm>
<TextInput source="id" disabled />
{/* <ReferenceInput source="userId" reference="users" /> */}
<TextInput source="title" />
{/* <TextInput source="description" multiline rows={5} /> */}
</SimpleForm>
</Edit>
);

export const PostCreate = () => (
<Create>
<SimpleForm>
<ReferenceInput source="userId" reference="users" />
<TextInput source="title" />
<TextInput source="body" multiline rows={5} />
</SimpleForm>
</Create>
);

export const PostShow = () => (
<Show>
<SimpleShowLayout>
<TextField source="title" />
<TextField source="teaser" />
{/* <RichTextField source="body" /> */}
{/* <DateField label="Publication date" source="published_at" /> */}
</SimpleShowLayout>
</Show>
);
27 changes: 27 additions & 0 deletions client/src/users.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useMediaQuery } from '@mui/material';
import { List, SimpleList, Datagrid, TextField, EmailField } from 'react-admin';
import MyUrlField from './MyUrlField';

export const UserList = () => {
const isSmall = useMediaQuery<any>(theme => theme.breakpoints.down('sm'));
return (
<List>
{isSmall ? (
<SimpleList
primaryText={record => record.name}
secondaryText={record => record.username}
tertiaryText={record => record.email}
/>
) : (
<Datagrid rowClick="edit">
<TextField source="id" />
<TextField source="name" />
<EmailField source="email" />
<TextField source="phone" />
<MyUrlField source="website" />
<TextField source="company.name" />
</Datagrid>
)}
</List>
);
};
1 change: 1 addition & 0 deletions client/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
21 changes: 21 additions & 0 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
9 changes: 9 additions & 0 deletions client/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
7 changes: 7 additions & 0 deletions client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json → server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"scripts": {
"start": "NODE_ENV=production node ./bootstrap.js",
"dev": "cross-env NODE_ENV=local midway-bin dev --ts",
"dev": "cross-env NODE_ENV=local midway-bin dev --ts --port 7002",
"test": "midway-bin test --ts",
"cov": "midway-bin cov --ts",
"lint": "mwts check",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 6f4d50d

Please sign in to comment.