feat: Article 模块

This commit is contained in:
Ivan Li 2021-05-01 15:13:38 +08:00
parent 0b5fa5b643
commit 183564aed8
12 changed files with 367 additions and 189 deletions

12
.vscode/launch.json vendored
View File

@ -5,19 +5,13 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "chrome",
"type": "chrome", "type": "chrome",
"request": "launch", "request": "launch",
"name": "Launch Chrome against blog.localhost",
"url": "http://localhost:8090/",
"webRoot": "${workspaceFolder}"
},
{
"name": "Launch blog.localhost",
"type": "firefox",
"request": "launch",
"reAttach": true, "reAttach": true,
"url": "http://blog.localhost/", "url": "http://blog.localhost/",
"webRoot": "${workspaceFolder}" "webRoot": "${workspaceFolder}",
"userDataDir": "/Users/ivan/Projects/.chrome"
} }
] ]
} }

View File

@ -2,7 +2,7 @@ module.exports = {
client: { client: {
service: { service: {
name: 'blog-be', name: 'blog-be',
url: 'http://localhost:7132/graphql' url: 'http://api.blog.localhost/graphql'
} }
} }
}; };

136
package-lock.json generated
View File

@ -14,6 +14,7 @@
"@hickory/browser": "^2.1.0", "@hickory/browser": "^2.1.0",
"@material-ui/core": "^4.11.3", "@material-ui/core": "^4.11.3",
"@material-ui/icons": "^4.11.2", "@material-ui/icons": "^4.11.2",
"@material-ui/lab": "*",
"@material-ui/pickers": "^3.3.10", "@material-ui/pickers": "^3.3.10",
"@testing-library/jest-dom": "^5.11.10", "@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6", "@testing-library/react": "^11.2.6",
@ -28,6 +29,7 @@
"formik-material-ui": "^3.0.1", "formik-material-ui": "^3.0.1",
"formik-material-ui-pickers": "^0.0.12", "formik-material-ui-pickers": "^0.0.12",
"graphql": "^15.5.0", "graphql": "^15.5.0",
"ramda": "^0.27.1",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-scripts": "4.0.3", "react-scripts": "4.0.3",
@ -44,7 +46,7 @@
"@graphql-codegen/typescript-react-apollo": "2.2.3", "@graphql-codegen/typescript-react-apollo": "2.2.3",
"@types/date-fns": "^2.6.0", "@types/date-fns": "^2.6.0",
"@types/graphql": "^14.5.0", "@types/graphql": "^14.5.0",
"@types/react-router-dom": "^5.1.7", "@types/ramda": "^0.27.40",
"@types/sass": "^1.16.0", "@types/sass": "^1.16.0",
"@types/yup": "^0.29.11", "@types/yup": "^0.29.11",
"sass": "^1.32.11" "sass": "^1.32.11"
@ -3148,6 +3150,32 @@
"node": ">=8.0.0" "node": ">=8.0.0"
} }
}, },
"node_modules/@material-ui/lab": {
"version": "4.0.0-alpha.58",
"resolved": "https://registry.npmjs.org/@material-ui/lab/-/lab-4.0.0-alpha.58.tgz",
"integrity": "sha512-GKHlJqLxUeHH3L3dGQ48ZavYrqGOTXkFkiEiuYMAnAvXAZP4rhMIqeHOPXSUQan4Bd8QnafDcpovOSLnadDmKw==",
"dependencies": {
"@babel/runtime": "^7.4.4",
"@material-ui/utils": "^4.11.2",
"clsx": "^1.0.4",
"prop-types": "^15.7.2",
"react-is": "^16.8.0 || ^17.0.0"
},
"engines": {
"node": ">=8.0.0"
},
"peerDependencies": {
"@material-ui/core": "^4.9.10",
"@types/react": "^16.8.6 || ^17.0.0",
"react": "^16.8.0 || ^17.0.0",
"react-dom": "^16.8.0 || ^17.0.0"
},
"peerDependenciesMeta": {
"@types/react": {
"optional": true
}
}
},
"node_modules/@material-ui/pickers": { "node_modules/@material-ui/pickers": {
"version": "3.3.10", "version": "3.3.10",
"resolved": "https://registry.npmjs.org/@material-ui/pickers/-/pickers-3.3.10.tgz", "resolved": "https://registry.npmjs.org/@material-ui/pickers/-/pickers-3.3.10.tgz",
@ -3877,12 +3905,6 @@
"graphql": "*" "graphql": "*"
} }
}, },
"node_modules/@types/history": {
"version": "4.7.8",
"resolved": "https://registry.npmjs.org/@types/history/-/history-4.7.8.tgz",
"integrity": "sha512-S78QIYirQcUoo6UJZx9CSP0O2ix9IaeAXwQi26Rhr/+mg7qqPy8TzaxHSUut7eGjL8WmLccT7/MXf304WjqHcA==",
"dev": true
},
"node_modules/@types/html-minifier-terser": { "node_modules/@types/html-minifier-terser": {
"version": "5.1.1", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz", "resolved": "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz",
@ -3998,6 +4020,15 @@
"resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.4.tgz", "resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.4.tgz",
"integrity": "sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==" "integrity": "sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug=="
}, },
"node_modules/@types/ramda": {
"version": "0.27.40",
"resolved": "https://registry.npmjs.org/@types/ramda/-/ramda-0.27.40.tgz",
"integrity": "sha512-V99ZfTH2tqVYdLDAlgh2uT+N074HPgqnAsMjALKSBqogYd0HbuuGMqNukJ6fk9Ml/Htaus76fsc4Yh3p7q1VdQ==",
"dev": true,
"dependencies": {
"ts-toolbelt": "^6.15.1"
}
},
"node_modules/@types/react": { "node_modules/@types/react": {
"version": "17.0.3", "version": "17.0.3",
"resolved": "https://registry.npmjs.org/@types/react/-/react-17.0.3.tgz", "resolved": "https://registry.npmjs.org/@types/react/-/react-17.0.3.tgz",
@ -4016,27 +4047,6 @@
"@types/react": "*" "@types/react": "*"
} }
}, },
"node_modules/@types/react-router": {
"version": "5.1.13",
"resolved": "https://registry.npmjs.org/@types/react-router/-/react-router-5.1.13.tgz",
"integrity": "sha512-ZIuaO9Yrln54X6elg8q2Ivp6iK6p4syPsefEYAhRDAoqNh48C8VYUmB9RkXjKSQAJSJV0mbIFCX7I4vZDcHrjg==",
"dev": true,
"dependencies": {
"@types/history": "*",
"@types/react": "*"
}
},
"node_modules/@types/react-router-dom": {
"version": "5.1.7",
"resolved": "https://registry.npmjs.org/@types/react-router-dom/-/react-router-dom-5.1.7.tgz",
"integrity": "sha512-D5mHD6TbdV/DNHYsnwBTv+y73ei+mMjrkGrla86HthE4/PVvL1J94Bu3qABU+COXzpL23T1EZapVVpwHuBXiUg==",
"dev": true,
"dependencies": {
"@types/history": "*",
"@types/react": "*",
"@types/react-router": "*"
}
},
"node_modules/@types/react-transition-group": { "node_modules/@types/react-transition-group": {
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.1.tgz", "resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.1.tgz",
@ -18148,6 +18158,11 @@
"performance-now": "^2.1.0" "performance-now": "^2.1.0"
} }
}, },
"node_modules/ramda": {
"version": "0.27.1",
"resolved": "https://registry.npmjs.org/ramda/-/ramda-0.27.1.tgz",
"integrity": "sha512-PgIdVpn5y5Yns8vqb8FzBUEYn98V3xcPgawAkkgj0YJ0qDsnHCiNmZYfOGMgOvoB0eWFLpYbhxUR3mxfDIMvpw=="
},
"node_modules/randombytes": { "node_modules/randombytes": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
@ -21463,6 +21478,12 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/ts-toolbelt": {
"version": "6.15.5",
"resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz",
"integrity": "sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==",
"dev": true
},
"node_modules/tsconfig-paths": { "node_modules/tsconfig-paths": {
"version": "3.9.0", "version": "3.9.0",
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz",
@ -26787,6 +26808,18 @@
"@babel/runtime": "^7.4.4" "@babel/runtime": "^7.4.4"
} }
}, },
"@material-ui/lab": {
"version": "4.0.0-alpha.58",
"resolved": "https://registry.npmjs.org/@material-ui/lab/-/lab-4.0.0-alpha.58.tgz",
"integrity": "sha512-GKHlJqLxUeHH3L3dGQ48ZavYrqGOTXkFkiEiuYMAnAvXAZP4rhMIqeHOPXSUQan4Bd8QnafDcpovOSLnadDmKw==",
"requires": {
"@babel/runtime": "^7.4.4",
"@material-ui/utils": "^4.11.2",
"clsx": "^1.0.4",
"prop-types": "^15.7.2",
"react-is": "^16.8.0 || ^17.0.0"
}
},
"@material-ui/pickers": { "@material-ui/pickers": {
"version": "3.3.10", "version": "3.3.10",
"resolved": "https://registry.npmjs.org/@material-ui/pickers/-/pickers-3.3.10.tgz", "resolved": "https://registry.npmjs.org/@material-ui/pickers/-/pickers-3.3.10.tgz",
@ -27380,12 +27413,6 @@
"graphql": "*" "graphql": "*"
} }
}, },
"@types/history": {
"version": "4.7.8",
"resolved": "https://registry.npmjs.org/@types/history/-/history-4.7.8.tgz",
"integrity": "sha512-S78QIYirQcUoo6UJZx9CSP0O2ix9IaeAXwQi26Rhr/+mg7qqPy8TzaxHSUut7eGjL8WmLccT7/MXf304WjqHcA==",
"dev": true
},
"@types/html-minifier-terser": { "@types/html-minifier-terser": {
"version": "5.1.1", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz", "resolved": "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz",
@ -27501,6 +27528,15 @@
"resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.4.tgz", "resolved": "https://registry.npmjs.org/@types/q/-/q-1.5.4.tgz",
"integrity": "sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==" "integrity": "sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug=="
}, },
"@types/ramda": {
"version": "0.27.40",
"resolved": "https://registry.npmjs.org/@types/ramda/-/ramda-0.27.40.tgz",
"integrity": "sha512-V99ZfTH2tqVYdLDAlgh2uT+N074HPgqnAsMjALKSBqogYd0HbuuGMqNukJ6fk9Ml/Htaus76fsc4Yh3p7q1VdQ==",
"dev": true,
"requires": {
"ts-toolbelt": "^6.15.1"
}
},
"@types/react": { "@types/react": {
"version": "17.0.3", "version": "17.0.3",
"resolved": "https://registry.npmjs.org/@types/react/-/react-17.0.3.tgz", "resolved": "https://registry.npmjs.org/@types/react/-/react-17.0.3.tgz",
@ -27519,27 +27555,6 @@
"@types/react": "*" "@types/react": "*"
} }
}, },
"@types/react-router": {
"version": "5.1.13",
"resolved": "https://registry.npmjs.org/@types/react-router/-/react-router-5.1.13.tgz",
"integrity": "sha512-ZIuaO9Yrln54X6elg8q2Ivp6iK6p4syPsefEYAhRDAoqNh48C8VYUmB9RkXjKSQAJSJV0mbIFCX7I4vZDcHrjg==",
"dev": true,
"requires": {
"@types/history": "*",
"@types/react": "*"
}
},
"@types/react-router-dom": {
"version": "5.1.7",
"resolved": "https://registry.npmjs.org/@types/react-router-dom/-/react-router-dom-5.1.7.tgz",
"integrity": "sha512-D5mHD6TbdV/DNHYsnwBTv+y73ei+mMjrkGrla86HthE4/PVvL1J94Bu3qABU+COXzpL23T1EZapVVpwHuBXiUg==",
"dev": true,
"requires": {
"@types/history": "*",
"@types/react": "*",
"@types/react-router": "*"
}
},
"@types/react-transition-group": { "@types/react-transition-group": {
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.1.tgz", "resolved": "https://registry.npmjs.org/@types/react-transition-group/-/react-transition-group-4.4.1.tgz",
@ -39296,6 +39311,11 @@
"performance-now": "^2.1.0" "performance-now": "^2.1.0"
} }
}, },
"ramda": {
"version": "0.27.1",
"resolved": "https://registry.npmjs.org/ramda/-/ramda-0.27.1.tgz",
"integrity": "sha512-PgIdVpn5y5Yns8vqb8FzBUEYn98V3xcPgawAkkgj0YJ0qDsnHCiNmZYfOGMgOvoB0eWFLpYbhxUR3mxfDIMvpw=="
},
"randombytes": { "randombytes": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
@ -42088,6 +42108,12 @@
"resolved": "https://registry.npmjs.org/ts-pnp/-/ts-pnp-1.2.0.tgz", "resolved": "https://registry.npmjs.org/ts-pnp/-/ts-pnp-1.2.0.tgz",
"integrity": "sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw==" "integrity": "sha512-csd+vJOb/gkzvcCHgTGSChYpy5f1/XKNsmvBGO4JXS+z1v2HobugDz4s1IeFXM3wZB44uczs+eazB5Q/ccdhQw=="
}, },
"ts-toolbelt": {
"version": "6.15.5",
"resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz",
"integrity": "sha512-FZIXf1ksVyLcfr7M317jbB67XFJhOO1YqdTcuGaq9q5jLUoTikukZ+98TPjKiP2jC5CgmYdWWYs0s2nLSU0/1A==",
"dev": true
},
"tsconfig-paths": { "tsconfig-paths": {
"version": "3.9.0", "version": "3.9.0",
"resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz",

View File

@ -10,6 +10,7 @@
"@hickory/browser": "^2.1.0", "@hickory/browser": "^2.1.0",
"@material-ui/core": "^4.11.3", "@material-ui/core": "^4.11.3",
"@material-ui/icons": "^4.11.2", "@material-ui/icons": "^4.11.2",
"@material-ui/lab": "*",
"@material-ui/pickers": "^3.3.10", "@material-ui/pickers": "^3.3.10",
"@testing-library/jest-dom": "^5.11.10", "@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6", "@testing-library/react": "^11.2.6",
@ -24,6 +25,7 @@
"formik-material-ui": "^3.0.1", "formik-material-ui": "^3.0.1",
"formik-material-ui-pickers": "^0.0.12", "formik-material-ui-pickers": "^0.0.12",
"graphql": "^15.5.0", "graphql": "^15.5.0",
"ramda": "^0.27.1",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-scripts": "4.0.3", "react-scripts": "4.0.3",
@ -65,7 +67,7 @@
"@graphql-codegen/typescript-react-apollo": "2.2.3", "@graphql-codegen/typescript-react-apollo": "2.2.3",
"@types/date-fns": "^2.6.0", "@types/date-fns": "^2.6.0",
"@types/graphql": "^14.5.0", "@types/graphql": "^14.5.0",
"@types/react-router-dom": "^5.1.7", "@types/ramda": "^0.27.40",
"@types/sass": "^1.16.0", "@types/sass": "^1.16.0",
"@types/yup": "^0.29.11", "@types/yup": "^0.29.11",
"sass": "^1.32.11" "sass": "^1.32.11"

View File

@ -9,20 +9,32 @@ import { Field, Form, Formik, FormikHelpers } from "formik";
import { FC } from "react"; import { FC } from "react";
import { Editor } from "../commons/editor/vditor"; import { Editor } from "../commons/editor/vditor";
import * as Yup from "yup"; import * as Yup from "yup";
import { import { Article, CreateArticleInput, UpdateArticleInput } from "../generated/graphql";
CreateArticleInput, import { DateTimePicker } from "formik-material-ui-pickers";
UpdateArticleInput, import { gql, useMutation } from "@apollo/client";
} from "../generated/graphql"; import { useRouter } from "@curi/react-universal";
import { DateTimePicker } from 'formik-material-ui-pickers';
import { gql, useMutation } from '@apollo/client';
const CREATE_ARTICLE = gql` const CREATE_ARTICLE = gql`
mutation createArticle($input: CreateArticleInput!) { mutation createArticle($createArticleInput: CreateArticleInput!) {
createArticle(input: $input) { createArticle(createArticleInput: $createArticleInput) {
id, id
title
content
publishedAt
} }
} }
` `;
const UPDATE_ARTICLE = gql`
mutation updateArticle($updateArticleInput: UpdateArticleInput!) {
updateArticle(updateArticleInput: $updateArticleInput) {
id
title
content
publishedAt
}
}
`;
const useStyles = makeStyles((theme) => const useStyles = makeStyles((theme) =>
createStyles({ createStyles({
@ -47,30 +59,88 @@ export const ArticleEditor: FC<Props> = ({ article }) => {
content: Yup.string() content: Yup.string()
.max(65535, "文章内容不得超过 65535 个字符") .max(65535, "文章内容不得超过 65535 个字符")
.required("文章内容不得为空"), .required("文章内容不得为空"),
publishedAt: Yup.date() publishedAt: Yup.date(),
}); });
const router = useRouter();
const [createArticle] = useMutation(CREATE_ARTICLE); const [createArticle] = useMutation<{createArticle: Article} ,{
createArticleInput: CreateArticleInput;
}>(CREATE_ARTICLE, {
update(cache, { data }) {
cache.modify({
fields: {
articles(existingArticles = []) {
const newArticleRef = cache.writeFragment({
data: data!.createArticle,
fragment: gql`
fragment NewArticle on Article {
id
title
content
publishedAt
}
`,
});
return [newArticleRef, ...existingArticles];
},
},
});
},
});
const [updateArticle] = useMutation<{
updateArticleInput: UpdateArticleInput;
}>(UPDATE_ARTICLE);
const SubmitForm = ( const SubmitForm = (
values: Values, values: Values,
{ setSubmitting }: FormikHelpers<Values> { setSubmitting }: FormikHelpers<Values>
) => { ) => {
if ('id' in article!) { if ("id" in article!) {
console.log('update', article) const title = /# (.+)$/m.exec(values.content ?? "")?.[1];
} else {
const title = /# (.+)$/.exec(values.content ?? '')?.[1];
if (!title) { if (!title) {
console.log('no title') console.log("no title");
setSubmitting(false);
return;
}
updateArticle({
variables: {
updateArticleInput: {
...values,
title,
},
},
})
.then(() => {
router.navigate({
url: router.url({ name: "articles" }),
method: "replace",
});
})
.finally(() => {
setSubmitting(false);
});
} else {
const title = /# (.+)$/m.exec(values.content ?? "")?.[1];
if (!title) {
console.log("no title");
setSubmitting(false); setSubmitting(false);
return; return;
} }
createArticle({ createArticle({
variables: { variables: {
...values, createArticleInput: {
...values as CreateArticleInput,
title, title,
}, },
}).finally(() => { },
})
.then(() => {
router.navigate({
url: router.url({ name: "articles" }),
method: "replace",
});
})
.finally(() => {
setSubmitting(false); setSubmitting(false);
}); });
} }

View File

@ -0,0 +1,82 @@
import { useMutation, useQuery } from "@apollo/client";
import {
IconButton,
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
} from "@material-ui/core";
import React, { FC } from "react";
import { Article } from "../generated/graphql";
import EditIcon from "@material-ui/icons/Edit";
import { useRouter } from "@curi/react-dom";
import { ARTICLES, REMOVE_ARTICLE } from './articles.constants';
import { Delete } from '@material-ui/icons';
export const ArticleIndex: FC = () => {
const { data } = useQuery<{
articles: Article[];
}>(ARTICLES, {});
const [removeArticle] = useMutation<any, { id: string }>(REMOVE_ARTICLE);
const router = useRouter();
return (
<section>
<TableContainer component={Paper}>
<Table aria-label="articles table">
<TableHead>
<TableRow>
<TableCell>Article Title</TableCell>
<TableCell>Published At</TableCell>
<TableCell>Views</TableCell>
<TableCell>Comments</TableCell>
<TableCell>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data?.articles.map((article) => (
<TableRow key={article.id}>
<TableCell component="th" scope="row">
{article.title}
</TableCell>
<TableCell>{article.publishedAt}</TableCell>
<TableCell align="right"> -- </TableCell>
<TableCell align="right"> -- </TableCell>
<TableCell>
<IconButton
aria-label="edit"
onClick={() =>
router.navigate({
url: router.url({
name: "modify-article",
params: article,
}),
})
}
>
<EditIcon />
</IconButton>
<IconButton
aria-label="delete"
onClick={() =>
removeArticle({
variables: article,
})
}
>
<Delete />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</section>
);
};

View File

@ -0,0 +1,26 @@
import { gql } from "@apollo/client";
export const ARTICLE = gql`
query Article($id: String!) {
article(id: $id) {
id
title
content
publishedAt
}
}
`;
export const ARTICLES = gql`
query Articles {
articles {
id
title
publishedAt
}
}
`;
export const REMOVE_ARTICLE = gql`
mutation RemoveArticle($id: String!) {
removeArticle(id: $id)
}
`;

3
src/articles/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './article-editor';
export * from './articles-index';
export * from './articles.constants'

View File

@ -1,56 +0,0 @@
import { gql, useQuery } from "@apollo/client";
import {
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
} from "@material-ui/core";
import React, { FC } from "react";
import { Article } from '../generated/graphql';
const articles = gql`
query Articles {
articles {
id
title
publishedAt
}
}`;
export const ArticleIndex: FC = () => {
const { data } = useQuery<{
articles: Article[];
}>(articles, {});
return (
<section>
<TableContainer component={Paper}>
<Table aria-label="articles table">
<TableHead>
<TableRow>
<TableCell>Article Title</TableCell>
<TableCell>Published At</TableCell>
<TableCell>Views</TableCell>
<TableCell>Comments</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data?.articles.map((article) => (
<TableRow key={article.id}>
<TableCell component="th" scope="row">
{article.title}
</TableCell>
<TableCell align="right">{article.publishedAt}</TableCell>
<TableCell align="right"> -- </TableCell>
<TableCell align="right"> -- </TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</section>
);
};

View File

@ -1,4 +1,4 @@
import { FormControl, FormHelperText, FormLabel } from '@material-ui/core'; import { createStyles, FormControl, FormHelperText, FormLabel, makeStyles } from '@material-ui/core';
import { FieldHookConfig, useField } from 'formik'; import { FieldHookConfig, useField } from 'formik';
import React, { FC, useEffect, useRef, useState } from 'react'; import React, { FC, useEffect, useRef, useState } from 'react';
import Vditor from 'vditor'; import Vditor from 'vditor';
@ -9,6 +9,14 @@ type Props = (FieldHookConfig<string>) & {
className?: string; className?: string;
}; };
const useStyles = makeStyles((theme) =>
createStyles({
formControl: {
width: '100%',
},
})
);
export const Editor: FC<Props> = ({ label, className, ...props }) => { export const Editor: FC<Props> = ({ label, className, ...props }) => {
const [field, meta, helpers] = useField(props); const [field, meta, helpers] = useField(props);
const editor = useRef<HTMLDivElement>(null); const editor = useRef<HTMLDivElement>(null);
@ -25,6 +33,7 @@ export const Editor: FC<Props> = ({ label, className, ...props }) => {
fullscreen: { fullscreen: {
index: 1500, index: 1500,
}, },
value: meta.initialValue,
input: (val) => helpers.setValue(val), input: (val) => helpers.setValue(val),
blur: () => helpers.setTouched(true), blur: () => helpers.setTouched(true),
}); });
@ -33,8 +42,10 @@ export const Editor: FC<Props> = ({ label, className, ...props }) => {
instance?.destroy(); instance?.destroy();
}; };
}, []); }, []);
const classes = useStyles();
return ( return (
<FormControl> <FormControl className={classes.formControl}>
<FormLabel>{label}</FormLabel> <FormLabel>{label}</FormLabel>
<div className={className} ref={editor} key={containerKey}></div> <div className={className} ref={editor} key={containerKey}></div>
{meta.error && <FormHelperText error={true}>{meta.error}</FormHelperText>} {meta.error && <FormHelperText error={true}>{meta.error}</FormHelperText>}

View File

@ -1,25 +1,26 @@
import React from 'react'; import React from "react";
import ReactDOM from 'react-dom'; import ReactDOM from "react-dom";
import './index.css'; import "./index.css";
import "fontsource-roboto"; import "fontsource-roboto";
import App from './App'; import App from "./App";
import reportWebVitals from './reportWebVitals'; import reportWebVitals from "./reportWebVitals";
import { client } from './commons/graphql/client'; import { client } from "./commons/graphql/client";
import { ApolloProvider } from '@apollo/client'; import { ApolloProvider } from "@apollo/client";
import { MuiPickersUtilsProvider } from '@material-ui/pickers'; import { MuiPickersUtilsProvider } from "@material-ui/pickers";
import DateFnsUtils from "@date-io/date-fns"; import DateFnsUtils from "@date-io/date-fns";
import zhLocale from "date-fns/locale/zh-CN"; import zhLocale from "date-fns/locale/zh-CN";
import { createRouterComponent } from '@curi/react-dom'; import { createRouterComponent } from "@curi/react-dom";
import { createRouter, announce } from '@curi/router'; import { createRouter, announce } from "@curi/router";
import { browser } from '@hickory/browser'; import { browser } from "@hickory/browser";
import routes from './routes'; import routes from "./routes";
const router = createRouter(browser, routes, { const router = createRouter(browser, routes, {
sideEffects: [ sideEffects: [
announce(({ response }) => { announce(({ response }) => {
return `Navigated to ${response.location.pathname}`; return `Navigated to ${response.location.pathname}`;
}) }),
] ],
external: { client }
}); });
const Router = createRouterComponent(router); const Router = createRouterComponent(router);
@ -36,7 +37,7 @@ router.once(() => {
</React.StrictMode>, </React.StrictMode>,
document.getElementById("root") document.getElementById("root")
); );
}) });
// If you want to start measuring performance in your app, pass a function // If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log)) // to log results (for example: reportWebVitals(console.log))

View File

@ -1,50 +1,69 @@
import { prepareRoutes } from '@curi/router' import { ApolloClient } from "@apollo/client";
import { prepareRoutes } from "@curi/router";
import { omit } from 'ramda';
import { ARTICLE } from "./articles";
import { Article } from './generated/graphql';
export default prepareRoutes([ export default prepareRoutes([
{ {
name: 'dashboard', name: "dashboard",
path: '', path: "",
respond() { respond() {
return { body: () => (<div>DashBoard</div>) } return { body: () => <div>DashBoard</div> };
} },
}, },
{ {
name: 'create-article', name: "create-article",
path: 'articles/create', path: "articles/create",
resolve() { resolve() {
const body = import(/* webpackChunkName: "create-article" */ './articles/article-editor').then(m => m.ArticleEditor); const body = import(
/* webpackChunkName: "article-editor" */ "./articles"
).then((m) => m.ArticleEditor);
return body; return body;
}, },
respond({ resolved }) { respond({ resolved }) {
return { body: resolved } return { body: resolved };
} },
}, },
{ {
name: 'modify-article', name: "modify-article",
path: 'articles/:id', path: "articles/:id",
resolve() { async resolve(matched, { client }: { client: ApolloClient<any> }) {
return import(/* webpackChunkName: "modify-article" */ './articles/article-editor').then(m => m.ArticleEditor); const [ArticleEditor, result] = await Promise.all([
import(/* webpackChunkName: "article-editor" */ "./articles").then(
(m) => m.ArticleEditor
),
client.query<{article: Article}, { id: string }>({
query: ARTICLE,
variables: { id: matched!.params.id },
}),
]);
console.log(ArticleEditor, result);
return () => (
<ArticleEditor article={omit(["__typename"], result.data.article)} />
);
}, },
respond({ resolved }) { respond({ resolved }) {
const ModifyArticle = () => resolved(); return { body: resolved };
return { body: <ModifyArticle /> } },
}
}, },
{ {
name: 'articles', name: "articles",
path: 'articles', path: "articles",
resolve() { resolve() {
return import(/* webpackChunkName: "articles" */ './articles/index').then(m => m.ArticleIndex); return import(/* webpackChunkName: "articles" */ "./articles").then(
(m) => m.ArticleIndex
);
}, },
respond({ resolved }) { respond({ resolved }) {
return { body: resolved } return { body: resolved };
} },
}, },
{ {
name: 'tags', name: "tags",
path: 'tags', path: "tags",
respond() { respond() {
return { body: () => (<div>Tags</div>) } return { body: () => <div>Tags</div> };
}
}, },
]) },
]);