Skip to content

Commit

Permalink
revamping the approach a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Pratap Singh <[email protected]>
  • Loading branch information
harsh-ps-2003 committed Jan 12, 2024
1 parent de9385d commit 22bbb40
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
17 changes: 15 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ func Respond(w http.ResponseWriter, data interface{}, warnings []error) {
w.WriteHeader(http.StatusOK)

resp := &response{
Status: StatusSuccess,
Data: data,
Status: StatusSuccess,
Warnings: warningsToString(warnings),
Data: data,
}
for _, warn := range warnings {
resp.Warnings = append(resp.Warnings, warn.Error())
Expand Down Expand Up @@ -287,3 +288,15 @@ func RespondError(w http.ResponseWriter, apiErr *ApiError, data interface{}) {
Data: data,
})
}

func warningsToString(errors []error) []string {
warningStrings := make([]string, len(errors))

for i, err := range errors {
if err != nil {
warningStrings[i] = err.Error()
}
}

return warningStrings
}
34 changes: 17 additions & 17 deletions pkg/ui/react-app/src/pages/targets/ScrapePoolList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useLocalStorage } from '../../hooks/useLocalStorage';
import { ToggleMoreLess } from '../../components/ToggleMoreLess';
import { KVSearch } from '@nexucis/kvsearch';
import styles from './ScrapePoolPanel.module.css';
import { Col, Collapse, Row } from 'reactstrap';
import { Col, Collapse, Row, UncontrolledAlert } from 'reactstrap';
import SearchBar from '../../components/SearchBar';
import { ScrapePoolContent } from './ScrapePoolContent';

Expand Down Expand Up @@ -111,27 +111,27 @@ ScrapePoolListContent.displayName = 'ScrapePoolListContent';

const ScrapePoolListWithStatusIndicator = withStatusIndicator(ScrapePoolListContent);

const ScrapePoolList: FC<PathPrefixProps & { setWarnings: (warnings: string[]) => void }> = ({
pathPrefix,
setWarnings,
}) => {
const ScrapePoolList: FC<PathPrefixProps> = ({ pathPrefix }) => {
const { response, error, isLoading } = useFetch<ScrapePoolListProps>(`${pathPrefix}/api/v1/targets?state=active`);
const { status: responseStatus, warnings: responseWarnings } = response;
const badResponse = responseStatus !== 'success' && responseStatus !== 'start fetching';

useEffect(() => {
if (responseWarnings?.length > 0) {
setWarnings(responseWarnings);
}
}, [responseWarnings, setWarnings]);

return (
<ScrapePoolListWithStatusIndicator
{...response.data}
error={badResponse ? new Error(responseStatus) : error}
isLoading={isLoading}
componentTitle="Targets information"
/>
<>
<div>
{responseWarnings?.map((warning, index) => (
<UncontrolledAlert key={index} color="warning">
{warning}
</UncontrolledAlert>
))}
</div>
<ScrapePoolListWithStatusIndicator
{...response.data}
error={badResponse ? new Error(responseStatus) : error}
isLoading={isLoading}
componentTitle="Targets information"
/>
</>
);
};

Expand Down
19 changes: 3 additions & 16 deletions pkg/ui/react-app/src/pages/targets/Targets.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import React, { FC, useState } from 'react';
import React, { FC } from 'react';
import { RouteComponentProps } from '@reach/router';
import Filter from './Filter';
import ScrapePoolList from './ScrapePoolList';
import PathPrefixProps from '../../types/PathPrefixProps';
import { useLocalStorage } from '../../hooks/useLocalStorage';
import { UncontrolledAlert } from 'reactstrap';

const Targets: FC<RouteComponentProps & PathPrefixProps> = ({ pathPrefix }) => {
const [warnings, setWarnings] = useState<string[]>([]);
return (
<>
{
<>
<div>
{warnings.map((warning, index) => (
<UncontrolledAlert key={index} color="warning">
{warning}
</UncontrolledAlert>
))}
</div>
<h2>Targets</h2>
<ScrapePoolList pathPrefix={pathPrefix} setWarnings={setWarnings} />
</>
}
<h2>Targets</h2>
<ScrapePoolList pathPrefix={pathPrefix} />
</>
);
};
Expand Down

0 comments on commit 22bbb40

Please sign in to comment.