PromucFlow_constructor/app/client/src/pages/Applications/index.tsx

126 lines
3.8 KiB
TypeScript
Raw Normal View History

import React, { Component } from "react";
import styled from "styled-components";
import { connect } from "react-redux";
2019-11-25 05:07:27 +00:00
import { AppState } from "reducers";
import {
getApplicationList,
getIsFetchingApplications,
getIsCreatingApplication,
2019-11-21 10:52:49 +00:00
getCreateApplicationError,
2019-11-25 05:07:27 +00:00
} from "selectors/applicationSelectors";
import {
ReduxActionTypes,
ApplicationPayload,
2019-11-25 05:07:27 +00:00
} from "constants/ReduxActionConstants";
import PageWrapper from "pages/common/PageWrapper";
2019-11-21 10:52:49 +00:00
import SubHeader from "pages/common/SubHeader";
import PageSectionDivider from "pages/common/PageSectionDivider";
2019-11-21 10:52:49 +00:00
import { getApplicationPayloads } from "mockComponentProps/ApplicationPayloads";
import ApplicationCard from "./ApplicationCard";
import CreateApplicationForm from "./CreateApplicationForm";
2019-11-21 10:52:49 +00:00
import { CREATE_APPLICATION_FORM_NAME } from "constants/forms";
const ApplicationCardsWrapper = styled.div`
display: flex;
flex-flow: row wrap;
2019-11-21 10:52:49 +00:00
justify-content: flex-start;
align-items: space-evenly;
`;
type ApplicationProps = {
applicationList: ApplicationPayload[];
fetchApplications: () => void;
createApplication: (appName: string) => void;
isCreatingApplication: boolean;
2019-11-21 10:52:49 +00:00
isFetchingApplications: boolean;
createApplicationError?: string;
2020-01-27 08:24:58 +00:00
searchApplications: (keyword: string) => void;
deleteApplication: (id: string) => void;
};
class Applications extends Component<ApplicationProps> {
componentDidMount() {
this.props.fetchApplications();
}
public render() {
2019-11-21 10:52:49 +00:00
const applicationList = this.props.isFetchingApplications
? getApplicationPayloads(8)
2019-11-21 10:52:49 +00:00
: this.props.applicationList;
return (
<PageWrapper displayName="Applications">
<SubHeader
add={{
form: <CreateApplicationForm />,
title: "Create New App",
formName: CREATE_APPLICATION_FORM_NAME,
formSubmitIntent: "primary",
isAdding:
this.props.isCreatingApplication ||
!!this.props.createApplicationError,
errorAdding: this.props.createApplicationError,
}}
search={{
placeholder: "Search",
queryFn: this.props.searchApplications,
}}
/>
<PageSectionDivider />
<ApplicationCardsWrapper>
{applicationList.map((application: ApplicationPayload) => {
return (
application.pageCount > 0 && (
<ApplicationCard
key={application.id}
loading={this.props.isFetchingApplications}
application={application}
2020-01-27 08:24:58 +00:00
delete={this.props.deleteApplication}
/>
)
);
})}
</ApplicationCardsWrapper>
</PageWrapper>
);
}
}
const mapStateToProps = (state: AppState) => ({
applicationList: getApplicationList(state),
isFetchingApplications: getIsFetchingApplications(state),
isCreatingApplication: getIsCreatingApplication(state),
2019-11-21 10:52:49 +00:00
createApplicationError: getCreateApplicationError(state),
});
const mapDispatchToProps = (dispatch: any) => ({
fetchApplications: () =>
dispatch({ type: ReduxActionTypes.FETCH_APPLICATION_LIST_INIT }),
createApplication: (appName: string) => {
dispatch({
type: ReduxActionTypes.CREATE_APPLICATION_INIT,
payload: {
name: appName,
},
});
},
searchApplications: (keyword: string) => {
dispatch({
type: ReduxActionTypes.SEARCH_APPLICATIONS,
payload: {
keyword,
},
});
},
2020-01-27 08:24:58 +00:00
deleteApplication: (applicationId: string) => {
if (applicationId && applicationId.length > 0) {
dispatch({
type: ReduxActionTypes.DELETE_APPLICATION_INIT,
payload: {
applicationId,
},
});
}
},
});
export default connect(mapStateToProps, mapDispatchToProps)(Applications);