-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support unifying export views #4102 (#312)
- Loading branch information
1 parent
0375d48
commit cbeb6e5
Showing
6 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
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
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,16 @@ | ||
import { ExportConfig } from "../config/entities"; | ||
import { useConfig } from "./useConfig"; | ||
|
||
/** | ||
* Returns the export configuration for the given entity. | ||
* @returns export configuration. | ||
*/ | ||
export const useEntityExportConfig = (): ExportConfig => { | ||
const { entityConfig } = useConfig(); | ||
|
||
if (!entityConfig.export) { | ||
throw new Error("This entity config does not have an export field set"); | ||
} | ||
|
||
return entityConfig.export; | ||
}; |
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
67 changes: 67 additions & 0 deletions
67
src/views/EntityExportMethodView/entityExportMethodView.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,67 @@ | ||
import { useRouter } from "next/router"; | ||
import type { ParsedUrlQuery } from "querystring"; | ||
import React from "react"; | ||
import { EntityDetailViewProps } from "views/EntityDetailView/entityDetailView"; | ||
import { PARAMS_INDEX_EXPORT_METHOD } from "../../common/constants"; | ||
import { ComponentCreator } from "../../components/ComponentCreator/ComponentCreator"; | ||
import { BackPageView } from "../../components/Layout/components/BackPage/backPageView"; | ||
import { ExportMethodConfig } from "../../config/entities"; | ||
import { useEntityExportConfig } from "../../hooks/useEntityExportConfig"; | ||
import { useFetchEntity } from "../../hooks/useFetchEntity"; | ||
import { useUpdateURLCatalogParams } from "../../hooks/useUpdateURLCatalogParam"; | ||
|
||
export const EntityExportMethodView = ( | ||
props: EntityDetailViewProps | ||
): JSX.Element => { | ||
// Update the catalog param if necessary. | ||
useUpdateURLCatalogParams(); | ||
|
||
// Grab the entity to be exported. | ||
const { response } = useFetchEntity(props); | ||
|
||
// Get the column definitions for the entity export. | ||
const { query } = useRouter(); | ||
const { exportMethods, tabs } = useEntityExportConfig(); | ||
const { sideColumn } = tabs[0]; | ||
const { mainColumn, top } = getExportMethodConfig(exportMethods, query) || {}; | ||
|
||
// Wait for the entity to be fetched. | ||
if (!response) { | ||
return <span></span>; | ||
} | ||
|
||
return ( | ||
<BackPageView | ||
mainColumn={ | ||
<ComponentCreator components={mainColumn || []} response={response} /> | ||
} | ||
sideColumn={ | ||
sideColumn ? ( | ||
<ComponentCreator components={sideColumn} response={response} /> | ||
) : undefined | ||
} | ||
top={<ComponentCreator components={top || []} response={response} />} | ||
/> | ||
); | ||
}; | ||
|
||
/** | ||
* Returns the export method configuration for the given pathname. | ||
* @param exportMethods - Export methods config. | ||
* @param query - Router query object. | ||
* @returns export method configuration. | ||
*/ | ||
function getExportMethodConfig( | ||
exportMethods: ExportMethodConfig[], | ||
query: ParsedUrlQuery | ||
): ExportMethodConfig | undefined { | ||
// Determine the selected export method from the URL. | ||
const exportMethodRoute = query.params?.[PARAMS_INDEX_EXPORT_METHOD]; | ||
if (!exportMethodRoute) { | ||
return; | ||
} | ||
// Find the config for the selected export method. | ||
return exportMethods.find(({ route }) => { | ||
return route.includes(exportMethodRoute); | ||
}); | ||
} |
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,39 @@ | ||
import React from "react"; | ||
import { EntityDetailViewProps } from "views/EntityDetailView/entityDetailView"; | ||
import { ComponentCreator } from "../../components/ComponentCreator/ComponentCreator"; | ||
import { BackPageView } from "../../components/Layout/components/BackPage/backPageView"; | ||
import { useEntityExportConfig } from "../../hooks/useEntityExportConfig"; | ||
import { useFetchEntity } from "../../hooks/useFetchEntity"; | ||
import { useUpdateURLCatalogParams } from "../../hooks/useUpdateURLCatalogParam"; | ||
|
||
export const EntityExportView = (props: EntityDetailViewProps): JSX.Element => { | ||
// Update the catalog param if necessary. | ||
useUpdateURLCatalogParams(); | ||
|
||
// Grab the entity to be exported. | ||
const { response } = useFetchEntity(props); | ||
|
||
// Get the column definitions for the entity export. | ||
const { tabs, top } = useEntityExportConfig(); | ||
const currentTab = tabs[0]; | ||
const { mainColumn, sideColumn } = currentTab; | ||
|
||
// Wait for the entity to be fetched. | ||
if (!response) { | ||
return <span></span>; | ||
} | ||
|
||
return ( | ||
<BackPageView | ||
mainColumn={ | ||
<ComponentCreator components={mainColumn} response={response} /> | ||
} | ||
sideColumn={ | ||
sideColumn ? ( | ||
<ComponentCreator components={sideColumn} response={response} /> | ||
) : undefined | ||
} | ||
top={<ComponentCreator components={top} response={response} />} | ||
/> | ||
); | ||
}; |