forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3ef553
commit a0353ac
Showing
3 changed files
with
95 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...p/percona/add-instance/components/AddRemoteInstance/FormParts/NodesAgents/NodesAgents.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { FormApi } from "final-form"; | ||
import React, { FC, useCallback, useEffect, useState } from "react"; | ||
|
||
import { useStyles2 } from '@grafana/ui'; | ||
import { getStyles } from "app/percona/add-instance/components/AddRemoteInstance/FormParts/FormParts.styles"; | ||
import { GET_NODES_CANCEL_TOKEN } from "app/percona/inventory/Inventory.constants"; | ||
import { AgentsOptions, NodesOptions } from "app/percona/inventory/Inventory.types"; | ||
import { SelectField } from "app/percona/shared/components/Form/SelectFieldCore"; | ||
import { useCancelToken } from "app/percona/shared/components/hooks/cancelToken.hook"; | ||
import { fetchNodesOptionsAction } from "app/percona/shared/core/reducers/nodes/nodes"; | ||
import { isApiCancelError } from "app/percona/shared/helpers/api"; | ||
import { logger } from "app/percona/shared/helpers/logger"; | ||
import { useAppDispatch } from "app/store/store"; | ||
|
||
export interface NodesAgentsProps { | ||
form?: FormApi; | ||
} | ||
|
||
export const NodesAgents: FC<NodesAgentsProps> = ({ form }) => { | ||
const styles = useStyles2(getStyles); | ||
const dispatch = useAppDispatch(); | ||
const [generateToken] = useCancelToken(); | ||
const [selectedNode, setSelectedNode] = useState<NodesOptions| undefined>(undefined); | ||
const [selectedAgent, setSelectedAgent] = useState<AgentsOptions | undefined>(undefined); | ||
const [nodes, setNodes] = useState<NodesOptions[]>([]); | ||
|
||
const loadData = useCallback(async () => { | ||
try { | ||
setNodes(await dispatch(fetchNodesOptionsAction({ token: generateToken(GET_NODES_CANCEL_TOKEN) })).unwrap()); | ||
} catch (e) { | ||
if (isApiCancelError(e)) { | ||
return; | ||
} | ||
logger.error(e); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const setNodeAndAgent = (value: NodesOptions) => { | ||
setSelectedNode(value); | ||
if (value.agents && value.agents.length > 1) { | ||
form?.change('agent', value.agents[1]); | ||
} else if (value.agents && value.agents.length === 1) { | ||
form?.change('agent', value.agents[0]); | ||
} | ||
|
||
if(selectedAgent && selectedAgent.value !== "pmm-server") { | ||
form?.change('address', 'localhost'); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if(nodes.length === 0) { | ||
loadData(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [nodes]); | ||
|
||
return ( | ||
<div className={styles.group}> | ||
<div className={styles.selectFieldWrapper}> | ||
<SelectField | ||
label="Nodes" | ||
isSearchable={false} | ||
disabled={false} | ||
options={nodes} | ||
name="node" | ||
data-testid="nodes-label" | ||
onChange={ (event) => { setNodeAndAgent(event as NodesOptions) }} | ||
className={styles.selectField} | ||
value={selectedNode} | ||
/> | ||
</div> | ||
<div className={styles.selectFieldWrapper}> | ||
<SelectField | ||
label="Agents" | ||
isSearchable={false} | ||
disabled={!selectedNode || !selectedNode.agents || selectedNode.agents.length === 1} | ||
options={selectedNode?.agents || []} | ||
name="agent" | ||
data-testid="nodes-label" | ||
onChange={ (event) => { setSelectedAgent(event as AgentsOptions); }} | ||
className={styles.selectField} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters