-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
psp-6988 replace state based routing with router.
- Loading branch information
1 parent
53fa762
commit 2ab12ec
Showing
23 changed files
with
236 additions
and
242 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
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
134 changes: 134 additions & 0 deletions
134
source/frontend/src/features/mapSideBar/property/PropertyRouter.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,134 @@ | ||
import { FormikProps } from 'formik'; | ||
import React from 'react'; | ||
import { Redirect, Switch, useHistory, useRouteMatch } from 'react-router-dom'; | ||
|
||
import { UpdatePropertyDetailsContainer } from '@/features/mapSideBar/property/tabs/propertyDetails/update/UpdatePropertyDetailsContainer'; | ||
import ComposedPropertyState from '@/hooks/repositories/useComposedProperties'; | ||
import { useQuery } from '@/hooks/use-query'; | ||
import { stripTrailingSlash } from '@/utils'; | ||
import AppRoute from '@/utils/AppRoute'; | ||
|
||
import { InventoryTabNames } from './InventoryTabs'; | ||
import PropertyContainer from './PropertyContainer'; | ||
import { PropertyContactEditContainer } from './tabs/propertyDetailsManagement/update/PropertyContactEditContainer'; | ||
import { PropertyContactEditForm } from './tabs/propertyDetailsManagement/update/PropertyContactEditForm'; | ||
import { PropertyManagementUpdateContainer } from './tabs/propertyDetailsManagement/update/summary/PropertyManagementUpdateContainer'; | ||
import { PropertyManagementUpdateForm } from './tabs/propertyDetailsManagement/update/summary/PropertyManagementUpdateForm'; | ||
|
||
export enum PropertyEditForms { | ||
UpdatePropertyDetailsContainer = 'UpdatePropertyDetailsContainer', | ||
UpdateManagementSummaryContainer = 'UpdateManagementSummaryContainer', | ||
UpdateContactContainer = 'UpdateContactContainer', | ||
} | ||
|
||
export interface EditManagementState { | ||
form: PropertyEditForms; | ||
childId: number | null; | ||
} | ||
|
||
export interface IPropertyRouterProps { | ||
onSuccess: () => void; | ||
composedPropertyState: ComposedPropertyState; | ||
} | ||
|
||
const PropertyRouter = React.forwardRef<FormikProps<any>, IPropertyRouterProps>( | ||
(props, formikRef) => { | ||
const query = useQuery(); | ||
const isEditing = query.get('edit') === 'true'; | ||
const { path } = useRouteMatch(); | ||
const history = useHistory(); | ||
|
||
const setIsEditing = (value: boolean) => { | ||
if (value) { | ||
query.set('edit', value.toString()); | ||
} else { | ||
query.delete('edit'); | ||
} | ||
history.push({ search: query.toString() }); | ||
}; | ||
|
||
if (isEditing && !!props?.composedPropertyState?.apiWrapper?.response?.id) { | ||
return ( | ||
<Switch> | ||
<AppRoute | ||
exact | ||
path={`${stripTrailingSlash(path)}/${InventoryTabNames.property}`} | ||
customRender={({ match }) => ( | ||
<UpdatePropertyDetailsContainer | ||
id={match.params.propertyId} | ||
onSuccess={() => { | ||
setIsEditing(false); | ||
props.onSuccess(); | ||
}} | ||
ref={formikRef} | ||
/> | ||
)} | ||
key={PropertyEditForms.UpdatePropertyDetailsContainer} | ||
title={'Update Property Details'} | ||
></AppRoute> | ||
<AppRoute | ||
exact | ||
path={`${stripTrailingSlash(path)}/${InventoryTabNames.management}/${ | ||
PropertyEditForms.UpdateContactContainer | ||
}/:contactId?`} | ||
customRender={({ match }) => ( | ||
<PropertyContactEditContainer | ||
propertyId={match.params.propertyId} | ||
contactId={+match.params.contactId ?? 0} | ||
View={PropertyContactEditForm} | ||
onSuccess={() => { | ||
setIsEditing(false); | ||
props.onSuccess(); | ||
}} | ||
ref={formikRef} | ||
/> | ||
)} | ||
key={PropertyEditForms.UpdateContactContainer} | ||
title="Update Contact" | ||
></AppRoute> | ||
<AppRoute | ||
exact | ||
path={`${stripTrailingSlash(path)}/${InventoryTabNames.management}`} | ||
customRender={({ match }) => ( | ||
<PropertyManagementUpdateContainer | ||
propertyId={match.params.propertyId} | ||
View={PropertyManagementUpdateForm} | ||
onSuccess={() => { | ||
setIsEditing(false); | ||
props.onSuccess(); | ||
}} | ||
ref={formikRef} | ||
/> | ||
)} | ||
key={PropertyEditForms.UpdateManagementSummaryContainer} | ||
title="Update Management Summary" | ||
></AppRoute> | ||
</Switch> | ||
); | ||
} else { | ||
return ( | ||
<Switch> | ||
<AppRoute | ||
path={`${stripTrailingSlash(path)}/:tab`} | ||
customRender={() => ( | ||
<PropertyContainer composedPropertyState={props.composedPropertyState} /> | ||
)} | ||
key={'property_tabs'} | ||
title={'Property Tabs'} | ||
></AppRoute> | ||
|
||
<Redirect | ||
from={`${path}`} | ||
to={`${stripTrailingSlash(path)}/${ | ||
path.includes('non-inventory-property') | ||
? InventoryTabNames.title | ||
: InventoryTabNames.property | ||
}`} | ||
/> | ||
</Switch> | ||
); | ||
} | ||
}, | ||
); | ||
|
||
export default PropertyRouter; |
Oops, something went wrong.