fix: onApply data is updated (#1655)

This commit is contained in:
Palash Gupta 2022-11-23 16:25:02 +05:30 committed by GitHub
parent 4c0d573760
commit d5bd991417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 15 deletions

View File

@ -1,15 +1,30 @@
import { Button, Input, notification } from 'antd';
import FormItem from 'antd/lib/form/FormItem';
import getFeaturesFlags from 'api/features/getFeatureFlags';
import apply from 'api/licenses/apply';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { QueryObserverResult, RefetchOptions, useQuery } from 'react-query';
import { useDispatch } from 'react-redux';
import { Dispatch } from 'redux';
import { AppAction, UPDATE_FEATURE_FLAGS } from 'types/actions/app';
import { ErrorResponse, SuccessResponse } from 'types/api';
import { PayloadProps } from 'types/api/licenses/getAll';
import { ApplyForm, ApplyFormContainer, LicenseInput } from './applyFormStyles';
import { ApplyForm, ApplyFormContainer, LicenseInput } from './styles';
function ApplyLicenseForm(): JSX.Element {
function ApplyLicenseForm({
licenseRefetch,
}: ApplyLicenseFormProps): JSX.Element {
const { t } = useTranslation(['licenses']);
const [key, setKey] = useState('');
const [loading, setLoading] = useState(false);
const dispatch = useDispatch<Dispatch<AppAction>>();
const { refetch } = useQuery({
queryFn: getFeaturesFlags,
queryKey: 'getFeatureFlags',
enabled: false,
});
const onFinish = async (values: unknown | { key: string }): Promise<void> => {
const params = values as { key: string };
@ -28,6 +43,16 @@ function ApplyLicenseForm(): JSX.Element {
});
if (response.statusCode === 200) {
const [featureFlagsResponse] = await Promise.all([
refetch(),
licenseRefetch(),
]);
if (featureFlagsResponse.data?.payload) {
dispatch({
type: UPDATE_FEATURE_FLAGS,
payload: featureFlagsResponse.data.payload,
});
}
notification.success({
message: 'Success',
description: t('license_applied'),
@ -74,4 +99,12 @@ function ApplyLicenseForm(): JSX.Element {
);
}
interface ApplyLicenseFormProps {
licenseRefetch: (
options?: RefetchOptions,
) => Promise<
QueryObserverResult<SuccessResponse<PayloadProps> | ErrorResponse, unknown>
>;
}
export default ApplyLicenseForm;

View File

@ -1,4 +1,3 @@
/* eslint-disable react/display-name */
import { Table } from 'antd';
import { ColumnsType } from 'antd/lib/table';
import React from 'react';

View File

@ -1,9 +1,9 @@
import { Tabs, Typography } from 'antd';
import getAll from 'api/licenses/getAll';
import Spinner from 'components/Spinner';
import useFetch from 'hooks/useFetch';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useQuery } from 'react-query';
import ApplyLicenseForm from './ApplyLicenseForm';
import ListLicenses from './ListLicenses';
@ -12,29 +12,31 @@ const { TabPane } = Tabs;
function Licenses(): JSX.Element {
const { t } = useTranslation(['licenses']);
const { loading, payload, error, errorMessage } = useFetch(getAll);
const { data, isError, isLoading, refetch } = useQuery({
queryFn: getAll,
queryKey: 'getAllLicenses',
});
if (error) {
return <Typography>{errorMessage}</Typography>;
if (isError || data?.error) {
return <Typography>{data?.error}</Typography>;
}
if (loading || payload === undefined) {
if (isLoading || data?.payload === undefined) {
return <Spinner tip={t('loading_licenses')} height="90vh" />;
}
const allValidLicense =
data?.payload?.filter((license) => license.isCurrent) || [];
return (
<Tabs destroyInactiveTabPane defaultActiveKey="licenses">
<TabPane tabKey="licenses" tab={t('tab_current_license')} key="licenses">
<ApplyLicenseForm />
<ListLicenses
licenses={payload ? payload.filter((l) => l.isCurrent === true) : []}
/>
<ApplyLicenseForm licenseRefetch={refetch} />
<ListLicenses licenses={allValidLicense} />
</TabPane>
<TabPane tabKey="history" tab={t('tab_license_history')} key="history">
<ListLicenses
licenses={payload ? payload.filter((l) => l.isCurrent === false) : []}
/>
<ListLicenses licenses={allValidLicense} />
</TabPane>
</Tabs>
);