accounts-frontend/packages/app/pages/dev/ApplicationsListPage.tsx

88 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { connect } from 'app/functions';
2020-05-24 04:38:24 +05:30
import { fetchAvailableApps, resetApp, deleteApp } from 'app/components/dev/apps/actions';
import ApplicationsIndex from 'app/components/dev/apps/ApplicationsIndex';
import { User } from 'app/components/user';
import { OauthAppResponse } from 'app/services/api/oauth';
2019-12-07 16:58:52 +05:30
interface Props extends RouteComponentProps {
2020-05-24 04:38:24 +05:30
user: User;
apps: OauthAppResponse[];
fetchAvailableApps: () => Promise<void>;
deleteApp: (clientId: string) => Promise<void>;
resetApp: (clientId: string, resetClientSecret: boolean) => Promise<void>;
2019-12-07 16:58:52 +05:30
}
type State = {
2020-05-24 04:38:24 +05:30
isLoading: boolean;
forceUpdate: boolean;
};
2019-12-07 16:58:52 +05:30
class ApplicationsListPage extends React.Component<Props, State> {
2020-05-24 04:38:24 +05:30
state = {
isLoading: false,
forceUpdate: false,
};
2020-05-24 04:38:24 +05:30
componentDidMount() {
!this.props.user.isGuest && this.loadApplicationsList();
}
2020-05-24 04:38:24 +05:30
componentDidUpdate({ user }: Props) {
if (this.props.user !== user) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ forceUpdate: true });
this.loadApplicationsList();
}
}
2020-05-24 04:38:24 +05:30
render() {
const { user, apps, resetApp, deleteApp, location } = this.props;
const { isLoading, forceUpdate } = this.state;
const clientId = location.hash.substr(1) || null;
2020-05-24 04:38:24 +05:30
return (
<ApplicationsIndex
displayForGuest={user.isGuest}
applications={forceUpdate ? [] : apps}
isLoading={isLoading}
deleteApp={deleteApp}
resetApp={resetApp}
clientId={clientId}
resetClientId={this.resetClientId}
/>
);
}
2020-05-24 04:38:24 +05:30
loadApplicationsList = async () => {
this.setState({ isLoading: true });
await this.props.fetchAvailableApps();
this.setState({
isLoading: false,
forceUpdate: false,
});
};
2020-05-24 04:38:24 +05:30
resetClientId = () => {
const { history, location } = this.props;
2020-05-24 04:38:24 +05:30
if (location.hash) {
history.push({ ...location, hash: '' });
}
};
}
2019-12-07 16:58:52 +05:30
export default connect(
(state) => ({
2020-05-24 04:38:24 +05:30
user: state.user,
apps: state.apps.available,
}),
{
fetchAvailableApps,
resetApp,
deleteApp,
},
)(ApplicationsListPage);