1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-03-18 00:19:49 +01:00

fix: update PR based on feedback

This commit is contained in:
Youssef 2022-02-10 09:23:11 +01:00
parent c10525108e
commit 4d0d39891a
2 changed files with 13 additions and 11 deletions

View File

@ -4,8 +4,9 @@ import { formatApiPath } from '../../../../utils/format-path';
import handleErrorResponses from '../httpErrorResponseHandler';
const useApplication = (name: string, options: SWRConfiguration = {}) => {
const path = formatApiPath(`api/admin/metrics/applications/${name}`);
const fetcher = async () => {
const path = formatApiPath(`api/admin/metrics/applications/${name}`);
return fetch(path, {
method: 'GET',
})
@ -13,16 +14,16 @@ const useApplication = (name: string, options: SWRConfiguration = {}) => {
.then(res => res.json());
};
const FEATURE_CACHE_KEY = `api/admin/metrics/applications/${name}`;
const APPLICATION_CACHE_KEY = `api/admin/metrics/applications/${name}`;
const { data, error } = useSWR(FEATURE_CACHE_KEY, fetcher, {
const { data, error } = useSWR(APPLICATION_CACHE_KEY, fetcher, {
...options,
});
const [loading, setLoading] = useState(!error && !data);
const refetchApplication = () => {
mutate(FEATURE_CACHE_KEY);
mutate(APPLICATION_CACHE_KEY);
};
useEffect(() => {
@ -43,7 +44,7 @@ const useApplication = (name: string, options: SWRConfiguration = {}) => {
error,
loading,
refetchApplication,
FEATURE_CACHE_KEY,
APPLICATION_CACHE_KEY,
};
};

View File

@ -3,26 +3,27 @@ import { useState, useEffect } from 'react';
import { formatApiPath } from '../../../../utils/format-path';
import handleErrorResponses from '../httpErrorResponseHandler';
const path = formatApiPath('api/admin/metrics/applications');
const useApplications = (options: SWRConfiguration = {}) => {
const fetcher = async () => {
const path = formatApiPath('api/admin/metrics/applications');
return fetch(path, {
method: 'GET',
})
.then(handleErrorResponses('Context data'))
.then(handleErrorResponses('Applications data'))
.then(res => res.json());
};
const FEATURE_CACHE_KEY = 'api/admin/metrics/applications';
const APPLICATIONS_CACHE_KEY = 'api/admin/metrics/applications';
const { data, error } = useSWR(FEATURE_CACHE_KEY, fetcher, {
const { data, error } = useSWR(APPLICATIONS_CACHE_KEY, fetcher, {
...options,
});
const [loading, setLoading] = useState(!error && !data);
const refetchApplications = () => {
mutate(FEATURE_CACHE_KEY);
mutate(APPLICATIONS_CACHE_KEY);
};
useEffect(() => {
@ -34,7 +35,7 @@ const useApplications = (options: SWRConfiguration = {}) => {
error,
loading,
refetchApplications,
FEATURE_CACHE_KEY,
APPLICATIONS_CACHE_KEY,
};
};