-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add content table hook (#266)
- Loading branch information
1 parent
3dbe920
commit e78bc50
Showing
10 changed files
with
617 additions
and
255 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
web/src/components/molecules/Content/ContentListMolecule/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import styled from "@emotion/styled"; | ||
|
||
import Content from "@reearth-cms/components/atoms/Content"; | ||
import PageHeader from "@reearth-cms/components/atoms/PageHeader"; | ||
import { ProColumns } from "@reearth-cms/components/atoms/ProTable"; | ||
import ContentTable from "@reearth-cms/components/molecules/Content/ContentTable"; | ||
import { ContentTableField } from "@reearth-cms/components/molecules/Content/types"; | ||
|
||
export type Props = { | ||
modelName?: string; | ||
contentTableFields?: ContentTableField[]; | ||
contentTableColumns?: ProColumns<ContentTableField>[]; | ||
modelsMenu: React.ReactNode; | ||
}; | ||
|
||
const ContentListMolecule: React.FC<Props> = ({ | ||
modelName, | ||
contentTableFields, | ||
contentTableColumns, | ||
modelsMenu: ModelsMenu, | ||
}) => ( | ||
<PaddedContent> | ||
<LeftPaneWrapper>{ModelsMenu}</LeftPaneWrapper> | ||
<ContentChild> | ||
<PageHeader title={modelName} /> | ||
<StyledContentTable | ||
contentTableFields={contentTableFields} | ||
contentTableColumns={contentTableColumns} | ||
/> | ||
</ContentChild> | ||
</PaddedContent> | ||
); | ||
|
||
const LeftPaneWrapper = styled.div` | ||
width: 200px; | ||
`; | ||
|
||
const PaddedContent = styled(Content)` | ||
margin: 16px; | ||
background-color: #fff; | ||
display: flex; | ||
min-height: 100%; | ||
`; | ||
|
||
const StyledContentTable = styled(ContentTable)` | ||
flex: 1; | ||
background-color: #fff; | ||
padding: 24px; | ||
`; | ||
|
||
const ContentChild = styled.div` | ||
flex: 1; | ||
background-color: #fff; | ||
padding: 24px; | ||
`; | ||
|
||
export default ContentListMolecule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { FieldType } from "@reearth-cms/components/molecules/Schema/types"; | ||
|
||
export type ItemField = { | ||
schemaFieldID: string; | ||
type: FieldType; | ||
value: any; | ||
}; | ||
|
||
export type Item = { | ||
id: string; | ||
schemaID: string; | ||
fields: ItemField[] | undefined | null; | ||
}; | ||
|
||
export type ContentTableField = { | ||
id: string; | ||
schemaID: string; | ||
fields: { [key: string]: any }; | ||
}; |
44 changes: 44 additions & 0 deletions
44
web/src/components/organisms/Project/Content/ContentList/hooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useMemo } from "react"; | ||
|
||
import { ProColumns } from "@reearth-cms/components/atoms/ProTable"; | ||
import { ContentTableField } from "@reearth-cms/components/molecules/Content/types"; | ||
import { useGetItemsQuery } from "@reearth-cms/gql/graphql-client-api"; | ||
import { useModel } from "@reearth-cms/state"; | ||
|
||
export default () => { | ||
const [currentModel] = useModel(); | ||
const { data } = useGetItemsQuery({ | ||
variables: { schemaID: currentModel?.schema.id ?? "", first: 100 }, | ||
skip: !currentModel?.schema.id, | ||
}); | ||
|
||
const contentTableFields: ContentTableField[] | undefined = useMemo(() => { | ||
return data?.items.nodes | ||
?.map(item => | ||
item | ||
? { | ||
...item, | ||
fields: item?.fields?.reduce( | ||
(obj, field) => Object.assign(obj, { [field.schemaFieldID]: field.value }), | ||
{}, | ||
), | ||
} | ||
: undefined, | ||
) | ||
.filter((contentTableField): contentTableField is ContentTableField => !!contentTableField); | ||
}, [data?.items.nodes]); | ||
|
||
const contentTableColumns: ProColumns<ContentTableField>[] | undefined = useMemo(() => { | ||
return currentModel?.schema.fields.map(field => ({ | ||
title: field.title, | ||
dataIndex: ["fields", field.id], | ||
key: field.id, | ||
})); | ||
}, [currentModel?.schema.fields]); | ||
|
||
return { | ||
currentModel, | ||
contentTableFields, | ||
contentTableColumns, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.