-
Notifications
You must be signed in to change notification settings - Fork 0
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
6991c05
commit 623a8d0
Showing
13 changed files
with
216 additions
and
40 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
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
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
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
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,37 @@ | ||
import { variabilize } from './utils'; | ||
|
||
describe('utils', () => { | ||
describe('variabilize', () => { | ||
it('should return the original text if no variable pass', () => { | ||
const text = 'truc'; | ||
|
||
const variabilizedText = variabilize(text, {}); | ||
|
||
expect(variabilizedText).toBe(text); | ||
}); | ||
|
||
it('should return the variablized text if right variable pass', () => { | ||
const text = 'truc {{count}} machin'; | ||
|
||
const variabilizedText = variabilize(text, { count: 2 }); | ||
|
||
expect(variabilizedText).toBe('truc 2 machin'); | ||
}); | ||
|
||
it('should return the variablized text if right variable passed twice', () => { | ||
const text = 'truc {{count}} machin {{count}}'; | ||
|
||
const variabilizedText = variabilize(text, { count: 2 }); | ||
|
||
expect(variabilizedText).toBe('truc 2 machin 2'); | ||
}); | ||
|
||
it('should return the variablized text if several variables passed', () => { | ||
const text = 'truc {{count}} machin {{double}}'; | ||
|
||
const variabilizedText = variabilize(text, { count: 2, double: 4 }); | ||
|
||
expect(variabilizedText).toBe('truc 2 machin 4'); | ||
}); | ||
}); | ||
}); |
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,9 @@ | ||
function variabilize(text: string, variables: Record<string, string | number>) { | ||
let variabilizedText = text; | ||
for (const [key, value] of Object.entries(variables)) { | ||
const REGEX = new RegExp(`{{${key}}}`, 'g'); | ||
variabilizedText = variabilizedText.replace(REGEX, `${value}`); | ||
} | ||
return variabilizedText; | ||
} | ||
export { variabilize }; |
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,60 @@ | ||
import { | ||
Checkbox, | ||
List, | ||
ListItem, | ||
ListItemButton, | ||
ListItemIcon, | ||
ListItemText, | ||
} from '@mui/material'; | ||
import { appMonitorDtoType } from '../../lib/api/monitorsApi'; | ||
import { useState } from 'react'; | ||
import { variabilize } from '../../locale/utils'; | ||
import { locale } from '../../locale'; | ||
|
||
function AppMonitorList(props: { appMonitorsDtos: appMonitorDtoType[] }) { | ||
const initialSelectedIndexes = props.appMonitorsDtos.map((_, index) => index); | ||
const [selectedIndexes, setSelectedIndexes] = useState<number[]>(initialSelectedIndexes); | ||
return ( | ||
<List dense={true}> | ||
{props.appMonitorsDtos.map((appMonitorDto, index) => ( | ||
<ListItemButton key={appMonitorDto.displayName} onClick={buildOnItemClick(index)}> | ||
<ListItemIcon> | ||
<Checkbox | ||
edge="start" | ||
checked={selectedIndexes.includes(index)} | ||
disableRipple | ||
/> | ||
</ListItemIcon> | ||
<ListItem> | ||
<ListItemText | ||
primary={computeListItemPrimaryText(appMonitorDto)} | ||
secondary={appMonitorDto.url} | ||
/> | ||
</ListItem> | ||
</ListItemButton> | ||
))} | ||
</List> | ||
); | ||
|
||
function buildOnItemClick(index: number) { | ||
return () => { | ||
const indexOfSelectedIndex = selectedIndexes.indexOf(index); | ||
if (indexOfSelectedIndex === -1) { | ||
setSelectedIndexes([...selectedIndexes, index]); | ||
} else { | ||
setSelectedIndexes( | ||
selectedIndexes.filter((selectedIndex) => selectedIndex !== index), | ||
); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
function computeListItemPrimaryText(appMonitorDto: appMonitorDtoType) { | ||
const frequencyText = variabilize(locale.importFrom.uptimeRobot.list.frequencyLabel, { | ||
frequency: appMonitorDto.frequency, | ||
}); | ||
return `${appMonitorDto.displayName} (${frequencyText})`; | ||
} | ||
|
||
export { AppMonitorList }; |
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 |
---|---|---|
@@ -1,21 +1,46 @@ | ||
import { TextField } from '@mui/material'; | ||
import { useState } from 'react'; | ||
import { FormEvent, useState } from 'react'; | ||
import { Button } from '../../components/Button'; | ||
import { locale } from '../../locale'; | ||
import { useApiCall } from '../../lib/useApiCall'; | ||
import { appMonitorDtoType, monitorsApi } from '../../lib/api/monitorsApi'; | ||
import { AppMonitorList } from './AppMonitorList'; | ||
|
||
function ImportFromUptimeRobot() { | ||
const [apiKey, setApiKey] = useState(''); | ||
const [appMonitorsDtos, setAppMonitorDtos] = useState<appMonitorDtoType[] | undefined>(); | ||
const fetchMonitorsFromUptimeRobotApiCall = useApiCall({ | ||
apiCall: monitorsApi.fetchMonitorsFromUptimeRobot, | ||
onSuccess: (data) => { | ||
setAppMonitorDtos(data); | ||
}, | ||
}); | ||
return ( | ||
<div> | ||
<TextField | ||
label="API key" | ||
placeholder="API key" | ||
value={apiKey} | ||
onChange={(event) => setApiKey(event.target.value)} | ||
/> | ||
<Button>{locale.importFrom.uptimeRobot.fetchMonitorsButton}</Button> | ||
<form onSubmit={fetchMonitors}> | ||
<TextField | ||
label="API key" | ||
placeholder="API key" | ||
value={apiKey} | ||
onChange={(event) => setApiKey(event.target.value)} | ||
/> | ||
<Button | ||
type="submit" | ||
disabled={!apiKey} | ||
isLoading={fetchMonitorsFromUptimeRobotApiCall.isLoading} | ||
> | ||
{locale.importFrom.uptimeRobot.fetchMonitorsButton} | ||
</Button> | ||
</form> | ||
{appMonitorsDtos !== undefined && <AppMonitorList appMonitorsDtos={appMonitorsDtos} />} | ||
</div> | ||
); | ||
|
||
function fetchMonitors(event: FormEvent<HTMLFormElement>) { | ||
event.preventDefault(); | ||
|
||
fetchMonitorsFromUptimeRobotApiCall.perform(apiKey); | ||
} | ||
} | ||
|
||
export { ImportFromUptimeRobot }; |
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,30 @@ | ||
type uptimeRobotMonitorApiType = { | ||
id: number; | ||
friendly_name: string; | ||
url: string; | ||
interval: number; | ||
}; | ||
|
||
function buildUptimeRobotApi(uptimeRobotApiKey: string) { | ||
const BASE_URL = `https://api.uptimerobot.com/v2`; | ||
|
||
return { fetchMonitors }; | ||
|
||
async function fetchMonitors() { | ||
const URL = `${BASE_URL}/getMonitors?format=json&api_key=${uptimeRobotApiKey}`; | ||
try { | ||
const response = await fetch(URL, { method: 'POST' }); | ||
const data = (await response.json()) as { monitors: uptimeRobotMonitorApiType[] }; | ||
|
||
return data.monitors.map((monitor) => ({ | ||
url: monitor.url, | ||
frequency: monitor.interval / 60, | ||
displayName: monitor.friendly_name, | ||
})); | ||
} catch (error) { | ||
throw new Error(`Le serveur uptimeRobot a renvoyé l'erreur suivante : ${error}`); | ||
} | ||
} | ||
} | ||
|
||
export { buildUptimeRobotApi }; |
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
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
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
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