moved views logic to types
This commit is contained in:
@@ -14,54 +14,7 @@ import Register from './components/Register';
|
|||||||
import Profile from './components/Profile';
|
import Profile from './components/Profile';
|
||||||
import { useArticles } from './providers/Article';
|
import { useArticles } from './providers/Article';
|
||||||
import { useAuth } from './providers/Author';
|
import { useAuth } from './providers/Author';
|
||||||
|
import { View, useViewRouter } from "./types/views";
|
||||||
type View =
|
|
||||||
| "home"
|
|
||||||
| "login"
|
|
||||||
| "register"
|
|
||||||
| "article"
|
|
||||||
| "editor"
|
|
||||||
| "profile"
|
|
||||||
| "create";
|
|
||||||
|
|
||||||
type ViewNode = {
|
|
||||||
parent: View | null;
|
|
||||||
children?: View[];
|
|
||||||
};
|
|
||||||
|
|
||||||
const VIEW_TREE: Record<View, ViewNode> = {
|
|
||||||
home: {
|
|
||||||
parent: null,
|
|
||||||
children: ["login", "article", "profile", "create"],
|
|
||||||
},
|
|
||||||
|
|
||||||
login: {
|
|
||||||
parent: "home",
|
|
||||||
children: ["register"],
|
|
||||||
},
|
|
||||||
|
|
||||||
register: {
|
|
||||||
parent: "login",
|
|
||||||
},
|
|
||||||
|
|
||||||
article: {
|
|
||||||
parent: "home",
|
|
||||||
children: ["editor"],
|
|
||||||
},
|
|
||||||
|
|
||||||
editor: {
|
|
||||||
parent: "article",
|
|
||||||
},
|
|
||||||
|
|
||||||
profile: {
|
|
||||||
parent: "home",
|
|
||||||
},
|
|
||||||
|
|
||||||
create: {
|
|
||||||
parent: "home",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
export default function Blog(props: { disableCustomTheme?: boolean }) {
|
export default function Blog(props: { disableCustomTheme?: boolean }) {
|
||||||
const { articles, loading, error } = useArticles();
|
const { articles, loading, error } = useArticles();
|
||||||
@@ -72,42 +25,32 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
|
|||||||
view: "home" as View,
|
view: "home" as View,
|
||||||
});
|
});
|
||||||
|
|
||||||
const goBack = () => {
|
const {
|
||||||
const parent = VIEW_TREE[ui.view].parent;
|
goBack,
|
||||||
if (parent) setUI(prev => ({ ...prev, view: parent }));
|
openLogin,
|
||||||
};
|
openRegister,
|
||||||
|
openProfile,
|
||||||
const navigate = (view: View) => {
|
openCreate,
|
||||||
setUI(prev => ({ ...prev, view }));
|
openEditor,
|
||||||
window.scrollTo({ top: 0, behavior: "smooth" });
|
openArticle,
|
||||||
};
|
} = useViewRouter(setUI);
|
||||||
const openLogin = () => navigate("login");
|
|
||||||
const openRegister = () => navigate("register");
|
|
||||||
const openProfile = () => navigate("profile");
|
|
||||||
const openCreate = () => navigate("create");
|
|
||||||
const openEditor = () => navigate("editor");
|
|
||||||
|
|
||||||
const openArticle = (i: number) => {
|
|
||||||
setUI({ selectedArticle: i, view: "article" });
|
|
||||||
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
||||||
};
|
|
||||||
|
|
||||||
const renderView = () => {
|
const renderView = () => {
|
||||||
switch (ui.view) {
|
switch (ui.view) {
|
||||||
case "login":
|
case "login":
|
||||||
return <Login onBack={goBack} onRegister={openRegister} />;
|
return <Login onBack={() => goBack(ui.view)} onRegister={openRegister} />;
|
||||||
|
|
||||||
case "register":
|
case "register":
|
||||||
return <Register onBack={goBack} />;
|
return <Register onBack={() => goBack(ui.view)} />;
|
||||||
|
|
||||||
case "profile":
|
case "profile":
|
||||||
return <Profile onBack={goBack} />;
|
return <Profile onBack={() => goBack(ui.view)} />;
|
||||||
|
|
||||||
case "article":
|
case "article":
|
||||||
return (
|
return (
|
||||||
<ArticleView
|
<ArticleView
|
||||||
article={articles[ui.selectedArticle!]}
|
article={articles[ui.selectedArticle!]}
|
||||||
onBack={goBack}
|
onBack={() => goBack(ui.view)}
|
||||||
onEdit={openEditor}
|
onEdit={openEditor}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@@ -116,7 +59,7 @@ export default function Blog(props: { disableCustomTheme?: boolean }) {
|
|||||||
return (
|
return (
|
||||||
<ArticleEditor
|
<ArticleEditor
|
||||||
article={ui.selectedArticle !== null ? articles[ui.selectedArticle] : null}
|
article={ui.selectedArticle !== null ? articles[ui.selectedArticle] : null}
|
||||||
onBack={goBack}
|
onBack={() => goBack(ui.view)}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
71
src/blog/types/views.ts
Normal file
71
src/blog/types/views.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// types.ts
|
||||||
|
export type View =
|
||||||
|
| "home"
|
||||||
|
| "login"
|
||||||
|
| "register"
|
||||||
|
| "article"
|
||||||
|
| "editor"
|
||||||
|
| "profile"
|
||||||
|
| "create";
|
||||||
|
|
||||||
|
export type ViewNode = {
|
||||||
|
parent: View | null;
|
||||||
|
children?: View[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const VIEW_TREE: Record<View, ViewNode> = {
|
||||||
|
home: {
|
||||||
|
parent: null,
|
||||||
|
children: ["login", "article", "profile", "create"],
|
||||||
|
},
|
||||||
|
login: {
|
||||||
|
parent: "home",
|
||||||
|
children: ["register"],
|
||||||
|
},
|
||||||
|
register: {
|
||||||
|
parent: "login",
|
||||||
|
},
|
||||||
|
article: {
|
||||||
|
parent: "home",
|
||||||
|
children: ["editor"],
|
||||||
|
},
|
||||||
|
editor: {
|
||||||
|
parent: "article",
|
||||||
|
},
|
||||||
|
profile: {
|
||||||
|
parent: "home",
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
parent: "home",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useViewRouter(setUI: React.Dispatch<any>) {
|
||||||
|
const navigate = (view: View) => {
|
||||||
|
setUI((prev: any) => ({ ...prev, view }));
|
||||||
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||||
|
};
|
||||||
|
|
||||||
|
const goBack = (currentView: View) => {
|
||||||
|
const parent = VIEW_TREE[currentView].parent;
|
||||||
|
if (parent) {
|
||||||
|
setUI((prev: any) => ({ ...prev, view: parent }));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const openArticle = (i: number) => {
|
||||||
|
setUI({ selectedArticle: i, view: "article" });
|
||||||
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
navigate,
|
||||||
|
goBack,
|
||||||
|
openLogin: () => navigate("login"),
|
||||||
|
openRegister: () => navigate("register"),
|
||||||
|
openProfile: () => navigate("profile"),
|
||||||
|
openCreate: () => navigate("create"),
|
||||||
|
openEditor: () => navigate("editor"),
|
||||||
|
openArticle,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user