feat(project): 修改项目后,界面对应的项目信息也一同自动更新。

This commit is contained in:
Ivan Li 2021-02-14 19:30:58 +08:00
parent 03c5cacec5
commit 54a164ddc6
3 changed files with 76 additions and 37 deletions

View File

@ -339,8 +339,8 @@
"kind": "NON_NULL", "kind": "NON_NULL",
"name": null, "name": null,
"ofType": { "ofType": {
"kind": "SCALAR", "kind": "OBJECT",
"name": "Boolean", "name": "Project",
"ofType": null "ofType": null
} }
}, },
@ -386,16 +386,6 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "SCALAR",
"name": "Boolean",
"description": "The `Boolean` scalar type represents `true` or `false`.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "SCALAR", "kind": "SCALAR",
"name": "Float", "name": "Float",
@ -572,6 +562,16 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "SCALAR",
"name": "Boolean",
"description": "The `Boolean` scalar type represents `true` or `false`.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "__Schema", "name": "__Schema",

View File

@ -1,8 +1,17 @@
import { gql, useQuery } from '@apollo/client'; import { gql, useQuery } from '@apollo/client';
import { useLocalStore } from 'mobx-react'; import {
action,
autorun,
computed,
makeObservable,
observable,
reaction,
when
} from 'mobx';
import { useLocalStore, useObserver } from 'mobx-react';
import { h } from 'preact'; import { h } from 'preact';
import { forwardRef } from 'preact/compat'; import { forwardRef } from 'preact/compat';
import { useImperativeHandle, useRef } from 'preact/hooks'; import { useImperativeHandle, useMemo, useRef } from 'preact/hooks';
import { appStore } from '../../app.store'; import { appStore } from '../../app.store';
import { Project } from '../../generated/graphql'; import { Project } from '../../generated/graphql';
import { createOverlay } from '../commons/overlay/overlay'; import { createOverlay } from '../commons/overlay/overlay';
@ -26,11 +35,42 @@ const FIND_PROJECTS = gql`
interface ListRef { interface ListRef {
refetch: () => void; refetch: () => void;
} }
class Store {
@observable currentProjectId?: string;
@observable projects?: Project[];
constructor() {
makeObservable(this);
reaction(
() => this.details,
() => {
if (this.currentProject) {
appStore.setMain(this.details);
}
}
);
}
@computed
get currentProject() {
return this.projects?.find(it => it.id === this.currentProjectId);
}
@computed
get details() {
return this.currentProject ? (
<ProjectDetails project={this.currentProject} />
) : null;
}
@action
setCurrentProjectId = (id: string) => {
this.currentProjectId = id;
};
}
export function ProjectPanel() { export function ProjectPanel() {
const listRef = useRef<ListRef>(); const listRef = useRef<ListRef>();
const addProject = () => { const addProject = () => {
createOverlay({ createOverlay({
content: <ProjectEditor isCreate={true} /> content: <ProjectEditor />
}).then(() => { }).then(() => {
listRef.current.refetch(); listRef.current.refetch();
}); });
@ -58,30 +98,29 @@ const List = forwardRef<ListRef>((_, ref) => {
projects: Project[]; projects: Project[];
}>(FIND_PROJECTS); }>(FIND_PROJECTS);
const state = useLocalStore<{ currentProject?: Project }>(() => ({ const store = useLocalStore(() => new Store());
currentProject: undefined // store.projects = data?.projects;
})); useMemo(() => {
store.projects = data?.projects;
const setCurrProject = (project: Project) => { }, [data?.projects]);
state.currentProject = project;
appStore.setMain(<ProjectDetails project={project} />);
};
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
refetch refetch
})); }));
const list = data?.projects?.map(item => ( return useObserver(() => {
const list = store.projects?.map(item => (
<li <li
class={`${styles.item} ${ class={`${styles.item} ${
item === state.currentProject ? styles.itemActive : '' item.id === store.currentProject?.id ? styles.itemActive : ''
}`} }`}
key={item.id} key={item.id}
onClick={() => setCurrProject(item)} onClick={() => store.setCurrentProjectId(item.id)}
> >
<h3>{item.name}</h3> <h3>{item.name}</h3>
<small>{item.comment}</small> <small>{item.comment}</small>
</li> </li>
)); ));
return <ol class={styles.list}>{list}</ol>; return <ol class={styles.list}>{list}</ol>;
});
}); });

View File

@ -46,7 +46,7 @@ export type QueryFindProjectArgs = {
export type Mutation = { export type Mutation = {
__typename?: 'Mutation'; __typename?: 'Mutation';
createProject: Project; createProject: Project;
modifyProject: Scalars['Boolean']; modifyProject: Project;
deleteProject: Scalars['Float']; deleteProject: Scalars['Float'];
}; };