Skip to content

Commit

Permalink
areaSearch: refactor into hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
NC-jsAhonen committed Aug 12, 2024
1 parent 40ebb30 commit 3a14750
Show file tree
Hide file tree
Showing 15 changed files with 840 additions and 1,227 deletions.
20 changes: 8 additions & 12 deletions src/areaSearch/components/AreaSearchApplicantInfoCheck.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PureComponent } from "react";
import React from "react";
import { connect } from "react-redux";
import { getFieldOptions } from "util/helpers";
import type { Attributes } from "types";
Expand All @@ -12,17 +12,13 @@ type Props = OwnProps & {
infoCheckAttributes: Attributes;
};

class AreaSearchApplicantInfoCheck extends PureComponent<Props> {
render(): React.ReactNode {
const {
infoCheckAttributes,
infoCheckData
} = this.props;
const infoCheckStateOptions = getFieldOptions(infoCheckAttributes, 'state');
const infoChecks = getApplicantInfoCheckItems(infoCheckData);
return <ApplicantInfoCheck infoChecks={infoChecks} infoCheckStateOptions={infoCheckStateOptions} />;
}

const AreaSearchApplicantInfoCheck = ({
infoCheckAttributes,
infoCheckData
}: Props) => {
const infoCheckStateOptions = getFieldOptions(infoCheckAttributes, 'state');
const infoChecks = getApplicantInfoCheckItems(infoCheckData);
return <ApplicantInfoCheck infoChecks={infoChecks} infoCheckStateOptions={infoCheckStateOptions} />;
}

export default (connect(state => ({
Expand Down
22 changes: 9 additions & 13 deletions src/areaSearch/components/AreaSearchApplicantInfoCheckEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PureComponent } from "react";
import React from "react";
import { connect } from "react-redux";
import { change } from "redux-form";
import PlotApplicationInfoCheckCollapse from "plotApplications/components/infoCheck/PlotApplicationInfoCheckCollapse";
Expand All @@ -20,17 +20,13 @@ type Props = OwnProps & {
}>;
};

class PlotApplicationApplicantInfoCheck extends PureComponent<Props> {
render(): React.ReactNode {
const {
infoCheckIds,
answer,
submissionErrors
} = this.props;
return <ApplicantInfoCheckEdit answer={answer} infoCheckIds={infoCheckIds} submissionErrors={submissionErrors} showMarkAll={false} />;
}

}
const PlotApplicationApplicantInfoCheck = ({
infoCheckIds,
answer,
submissionErrors,
}: Props) => {
return <ApplicantInfoCheckEdit answer={answer} infoCheckIds={infoCheckIds} submissionErrors={submissionErrors} showMarkAll={false} />;
};

export default (connect((state, props) => {
const formName = getApplicantInfoCheckFormName(props.identifier);
Expand All @@ -42,4 +38,4 @@ export default (connect((state, props) => {
};
}, {
change
})(PlotApplicationApplicantInfoCheck) as React.ComponentType<OwnProps>);
})(PlotApplicationApplicantInfoCheck) as React.ComponentType<OwnProps>);
35 changes: 11 additions & 24 deletions src/areaSearch/components/AreaSearchApplication.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $Shape } from "utility-types";
import React, { Component, Fragment } from "react";
import React, { useState, Fragment } from "react";
import { connect } from "react-redux";
import flowRight from "lodash/flowRight";
import orderBy from "lodash/orderBy";
Expand Down Expand Up @@ -37,30 +37,21 @@ type Props = OwnProps & {
formAttributes: Attributes;
areaSearchAttributes: Attributes;
};
type State = {

const AreaSearchApplication = ({
areaSearch,
isFetchingFormAttributes,
formAttributes,
areaSearchAttributes
}: Props) => {
// The Leaflet element doesn't initialize correctly if it's invisible in a collapsed section element,
// only rendering some map tiles and calculating the viewport from given bounds incorrectly, until an
// action that forces it to update its dimensions (like resizing the window) happens. We can
// circumvent this by forcing it to rerender with a key whenever that section is opened; during
// the opening transition, the initialization works properly.
selectedAreaSectionRefreshKey: number;
};
const [selectedAreaSectionRefreshKey, setSelectedAreaSectionRefreshKey] =
useState<number>(0);

class AreaSearchApplication extends Component<Props, State> {
state: any = {
selectedAreaSectionRefreshKey: 0
};

render(): React.ReactNode {
const {
areaSearch,
isFetchingFormAttributes,
formAttributes,
areaSearchAttributes
} = this.props;
const {
selectedAreaSectionRefreshKey
} = this.state;
const fieldTypes = getFieldAttributes(formAttributes, 'sections.child.children.fields.child.children.type.choices');
let form: Form | null | undefined;
let answer: Record<string, any> | null | undefined;
Expand Down Expand Up @@ -121,9 +112,7 @@ class AreaSearchApplication extends Component<Props, State> {
</Collapse>
<Collapse headerTitle={selectedAreaTitle} onToggle={isOpen => {
if (isOpen) {
this.setState(state => ({
selectedAreaSectionRefreshKey: state.selectedAreaSectionRefreshKey + 1
}));
setSelectedAreaSectionRefreshKey(selectedAreaSectionRefreshKey + 1);
}
}} defaultOpen>
<SingleAreaSearchMap geometry={areaSearch.geometry} key={selectedAreaSectionRefreshKey} minimap />
Expand Down Expand Up @@ -234,8 +223,6 @@ class AreaSearchApplication extends Component<Props, State> {
</Collapse>
</>}
</div>;
}

}

export default (flowRight(connect(state => ({
Expand Down
125 changes: 46 additions & 79 deletions src/areaSearch/components/AreaSearchApplicationAuditLog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Fragment, PureComponent } from "react";
import React, { Fragment, useEffect, useState } from "react";
import { connect } from "react-redux";
import isEmpty from "lodash/isEmpty";
import AuditLogTable from "components/auditLog/AuditLogTable";
import Divider from "components/content/Divider";
import Loader from "components/loader/Loader";
Expand All @@ -23,94 +22,62 @@ type Props = {
isFetching: boolean;
areaSearchId: string;
};
type State = {
activePage: number;
auditLogItems: Array<Record<string, any>>;
auditLogList: AuditLogList;
count: number;
maxPage: number;
};

class AreaSearchApplicationAuditLog extends PureComponent<Props, State> {
state = {
activePage: 1,
auditLogItems: [],
auditLogList: {},
count: 0,
maxPage: 0
};
const AreaSearchApplicationAuditLog = ({
isEditMode,
isFetching,
fetchAuditLogByAreaSearch,
areaSearchId,
auditLogList
}: Props) => {
const [activePage, setActivePage] = useState(1);
const [auditLogItems, setAuditLogItems] = useState([]);
const [maxPage, setMaxPage] = useState(0);

componentDidMount() {
const {
fetchAuditLogByAreaSearch,
areaSearchId
} = this.props;
fetchAuditLogByAreaSearch({
const handlePageClick = (page: number) => {
setActivePage(page);

const payload: any = {
id: areaSearchId,
limit: LIST_TABLE_PAGE_SIZE
});
}

static getDerivedStateFromProps(props: Props, state: State) {
const newState: any = {};

if (props.auditLogList !== state.auditLogList) {
newState.auditLogList = props.auditLogList;
newState.auditLogItems = getApiResponseResults(props.auditLogList);
newState.count = getApiResponseCount(props.auditLogList);
newState.maxPage = getApiResponseMaxPage(props.auditLogList, LIST_TABLE_PAGE_SIZE);
limit: LIST_TABLE_PAGE_SIZE,
};

if (page > 1) {
payload.offset = (page - 1) * LIST_TABLE_PAGE_SIZE;
}

fetchAuditLogByAreaSearch(payload);
};

return !isEmpty(newState) ? newState : null;
}

handlePageClick = (page: number) => {
const {
fetchAuditLogByAreaSearch,
areaSearchId
} = this.props;
this.setState({
activePage: page
}, () => {
const payload: any = {
id: areaSearchId,
limit: LIST_TABLE_PAGE_SIZE
};

if (page > 1) {
payload.offset = (page - 1) * LIST_TABLE_PAGE_SIZE;
}
useEffect(() => {
setAuditLogItems(getApiResponseResults(auditLogList))
setMaxPage(getApiResponseMaxPage(auditLogList, LIST_TABLE_PAGE_SIZE))

fetchAuditLogByAreaSearch(payload);
fetchAuditLogByAreaSearch({
id: areaSearchId,
limit: LIST_TABLE_PAGE_SIZE,
});
};
}, []);

render() {
const {
isEditMode,
isFetching
} = this.props;
const {
activePage,
auditLogItems,
maxPage
} = this.state;
return <Fragment>
<Title enableUiDataEdit={isEditMode} uiDataKey={getUiDataLeaseKey(AreaSearchFieldPaths.AUDIT_LOG)}>
{AreaSearchFieldTitles.AUDIT_LOG}
</Title>
<Divider />
return (
<Fragment>
<Title
enableUiDataEdit={isEditMode}
uiDataKey={getUiDataLeaseKey(AreaSearchFieldPaths.AUDIT_LOG)}
>
{AreaSearchFieldTitles.AUDIT_LOG}
</Title>
<Divider />

<TableWrapper>
<TableWrapper>
{isFetching && <LoaderWrapper className='relative-overlay-wrapper'><Loader isLoading={isFetching} /></LoaderWrapper>}

<AuditLogTable items={auditLogItems} />
<Pagination activePage={activePage} maxPage={maxPage} onPageClick={this.handlePageClick} />
</TableWrapper>
</Fragment>;
}

}
<AuditLogTable items={auditLogItems} />
<Pagination activePage={activePage} maxPage={maxPage} onPageClick={handlePageClick} />
</TableWrapper>
</Fragment>
);
};

export default connect((state, props: Props) => {
return {
Expand Down
51 changes: 18 additions & 33 deletions src/areaSearch/components/AreaSearchApplicationCreateForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from "react";
import React, { useEffect } from "react";
import { connect } from "react-redux";
import { flowRight } from "lodash/util";
import { getFormValues, reduxForm } from "redux-form";
Expand All @@ -19,16 +19,15 @@ type Props = OwnProps & {
valid: boolean;
};

class AreaSearchApplicationCreateForm extends Component<Props> {
componentDidMount() {
const {
initialize,
formData,
formValues,
fieldTypeMapping,
receiveFormValidFlags,
valid
} = this.props;
const AreaSearchApplicationCreateForm = ({
initialize,
formData,
formValues,
fieldTypeMapping,
receiveFormValidFlags,
valid,
}: Props) => {
useEffect(() => {
receiveFormValidFlags({
[FormNames.AREA_SEARCH_CREATE_FORM]: valid
});
Expand All @@ -38,35 +37,21 @@ class AreaSearchApplicationCreateForm extends Component<Props> {
form: getInitialApplicationForm(fieldTypeMapping, formData)
});
}
}
}, []);

componentDidUpdate(prevProps) {
const {
receiveFormValidFlags
} = this.props;
useEffect(() => {
receiveFormValidFlags({
[FormNames.AREA_SEARCH_CREATE_FORM]: valid,
});
}, [valid]);

if (prevProps.valid !== this.props.valid) {
receiveFormValidFlags({
[FormNames.AREA_SEARCH_CREATE_FORM]: this.props.valid
});
}
if (!formValues?.form) {
return null;
}

render(): React.ReactNode {
const {
formData,
formValues
} = this.props;

if (!formValues?.form) {
return null;
}

return <div>
{formData.sections.map(section => <ApplicationSubsection path={['form.sections']} section={section} headerTag="h2" key={section.id} formName={FormNames.AREA_SEARCH_CREATE_FORM} formPath='form' sectionTitleTransformers={[]} answerId={null} />)}
</div>;
}

}

export default (flowRight(connect(state => ({
Expand Down
Loading

0 comments on commit 3a14750

Please sign in to comment.