-
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.
Merge pull request #2 from numerique-gouv/add-event-system
Add event system
- Loading branch information
Showing
36 changed files
with
581 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
import { pathHandler } from './pathHandler'; | ||
|
||
describe('pathHandler', () => { | ||
describe('getRoutePath', () => { | ||
it('should return the generic route path if no parameters provided', () => { | ||
const path = pathHandler.getRoutePath('CLIENTS'); | ||
|
||
expect(path).toBe('/clients'); | ||
}); | ||
|
||
it('should return the route path with parameter', () => { | ||
const path = pathHandler.getRoutePath('CLIENT_SUMMARY', { | ||
clientId: '219a36c4-a04e-4877-b300-000a27c0830f', | ||
}); | ||
|
||
expect(path).toBe('/clients/219a36c4-a04e-4877-b300-000a27c0830f'); | ||
}); | ||
}); | ||
|
||
describe('extractParameters', () => { | ||
it('should return path with no parameter if path has no parameter', () => { | ||
const path = pathHandler.getRoutePath('CLIENTS'); | ||
|
||
const parsedPath = pathHandler.parsePath(path); | ||
|
||
expect(parsedPath?.routeKey).toEqual('CLIENTS'); | ||
expect(parsedPath?.parameters).toEqual({}); | ||
}); | ||
|
||
it('should return home if path is home', () => { | ||
const path = pathHandler.getRoutePath('HOME'); | ||
|
||
const parsedPath = pathHandler.parsePath(path); | ||
|
||
expect(parsedPath?.routeKey).toEqual('HOME'); | ||
expect(parsedPath?.parameters).toEqual({}); | ||
}); | ||
|
||
it('should return path with parameter if path has one parameter', () => { | ||
const clientId = `${Math.floor(Math.random() * 10000) + 1}`; | ||
const path = pathHandler.getRoutePath('CLIENT_SUMMARY', { clientId }); | ||
|
||
const parsedPath = pathHandler.parsePath(path); | ||
|
||
expect(parsedPath?.routeKey).toEqual('CLIENT_SUMMARY'); | ||
expect(parsedPath?.parameters).toEqual({ clientId }); | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
import { ROUTE_KEYS } from '../routes/routeKeys'; | ||
import { ROUTE_PATHS } from '../routes/routePaths'; | ||
|
||
const pathHandler = { | ||
getRoutePath, | ||
parsePath, | ||
}; | ||
|
||
function getRoutePath<paramsT extends Record<string, string>>( | ||
routeKey: (typeof ROUTE_KEYS)[number], | ||
parameters?: paramsT, | ||
queryParameters?: Record<string, string>, | ||
) { | ||
let path = ROUTE_PATHS[routeKey].path; | ||
if (parameters) { | ||
Object.keys(parameters).forEach((key) => { | ||
path = path.replace(new RegExp(':' + key), parameters[key]); | ||
}); | ||
} | ||
if (queryParameters) { | ||
path = path + '?'; | ||
const queryParameterKeys = Object.keys(queryParameters); | ||
for (let i = 0; i < queryParameterKeys.length; i++) { | ||
const key = queryParameterKeys[i]; | ||
const value = queryParameters[key]; | ||
if (i > 0) { | ||
path += '&'; | ||
} | ||
path += `${key}=${value}`; | ||
} | ||
} | ||
return path; | ||
} | ||
|
||
function parsePath( | ||
path: string, | ||
): { parameters: Record<string, string>; routeKey: (typeof ROUTE_KEYS)[number] } | undefined { | ||
const splitActualPath = path.split('/'); | ||
|
||
routeKeysLoop: for (const ROUTE_KEY of ROUTE_KEYS) { | ||
const parameters: any = {}; | ||
|
||
const ROUTE_PATH = ROUTE_PATHS[ROUTE_KEY].path; | ||
const splitCanonicalPath = ROUTE_PATH.split('/'); | ||
if (splitActualPath.length !== splitCanonicalPath.length) { | ||
continue; | ||
} | ||
const chunkCount = splitActualPath.length; | ||
for (let i = 0; i < chunkCount; i++) { | ||
const actualPathChunk = splitActualPath[i]; | ||
const canonicalPathChunk = splitCanonicalPath[i]; | ||
if (canonicalPathChunk.length > 0 && canonicalPathChunk[0] === ':') { | ||
const parameterKey = canonicalPathChunk.substring(1); | ||
const parameterValue = actualPathChunk; | ||
parameters[parameterKey] = parameterValue; | ||
} else if (canonicalPathChunk !== actualPathChunk) { | ||
continue routeKeysLoop; | ||
} | ||
} | ||
return { parameters, routeKey: ROUTE_KEY }; | ||
} | ||
return undefined; | ||
} | ||
|
||
export { pathHandler }; |
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,42 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useParams } from 'react-router-dom'; | ||
import { api } from '../lib/api'; | ||
|
||
type statusValueType = 'up' | 'down'; | ||
|
||
// type elementaryStatusType = { timestamp: number; statusValue: statusValueType }; | ||
|
||
type eventType = { timestamp: number; kind: statusValueType; title: string }; | ||
|
||
type clientSummaryType = { | ||
id: string; | ||
name: string; | ||
currentStatusValue: statusValueType; | ||
// uptime: { | ||
// last24Hours: elementaryStatusType[]; | ||
// last90Days: elementaryStatusType[]; | ||
// }; | ||
// overallUptime: { | ||
// last24Hours: number; | ||
// last7Days: number; | ||
// last30Days: number; | ||
// last90Days: number; | ||
// }; | ||
events: eventType[]; | ||
}; | ||
|
||
function ClientSummary() { | ||
const params = useParams<{ clientId: string }>(); | ||
const clientId = params.clientId as string; | ||
const query = useQuery<clientSummaryType>({ | ||
queryFn: () => api.getClientSummary(clientId), | ||
queryKey: ['clients', clientId, 'summary'], | ||
}); | ||
|
||
if (!query.data) { | ||
return <div>Loading...</div>; | ||
} | ||
return <div></div>; | ||
} | ||
|
||
export { ClientSummary }; |
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,34 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { api } from '../lib/api'; | ||
import { Link } from 'react-router-dom'; | ||
import { pathHandler } from '../lib/pathHandler'; | ||
|
||
type clientType = { | ||
id: string; | ||
name: string; | ||
}; | ||
|
||
function Clients() { | ||
const query = useQuery<clientType[]>({ queryFn: api.getClients, queryKey: ['clients'] }); | ||
|
||
if (!query.data) { | ||
return <div>Loading...</div>; | ||
} | ||
return ( | ||
<div> | ||
<ul> | ||
{query.data.map((client) => ( | ||
<li key={client.id}> | ||
<Link | ||
to={pathHandler.getRoutePath('CLIENT_SUMMARY', { clientId: client.id })} | ||
> | ||
{client.name} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
export { Clients }; |
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,8 +1,12 @@ | ||
import { ClientSummary } from '../pages/ClientSummary'; | ||
import { Clients } from '../pages/Clients'; | ||
import { Home } from '../pages/Home'; | ||
import { ROUTE_KEYS } from './routeKeys'; | ||
|
||
const ROUTE_ELEMENTS: Record<(typeof ROUTE_KEYS)[number], { element: JSX.Element }> = { | ||
HOME: { element: <Home /> }, | ||
CLIENTS: { element: <Clients /> }, | ||
CLIENT_SUMMARY: { element: <ClientSummary /> }, | ||
}; | ||
|
||
export { ROUTE_ELEMENTS }; |
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,3 +1,3 @@ | ||
const ROUTE_KEYS = ['HOME'] as const; | ||
const ROUTE_KEYS = ['HOME', 'CLIENTS', 'CLIENT_SUMMARY'] as const; | ||
|
||
export { ROUTE_KEYS }; |
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,26 @@ | ||
import { Client } from '../modules/client'; | ||
import { Event } from '../modules/event'; | ||
|
||
const api = { | ||
fetchAllClients, | ||
fetchAllEvents, | ||
}; | ||
const BASE_URL = 'https://ping-storage.osc-fr1.scalingo.io'; | ||
|
||
async function fetchAllClients(): Promise<Client[]> { | ||
const URL = `${BASE_URL}/api/all-clients`; | ||
|
||
const response = await fetch(URL); | ||
const parsedData = await response.json(); | ||
return parsedData; | ||
} | ||
|
||
async function fetchAllEvents(): Promise<Event[]> { | ||
const URL = `${BASE_URL}/api/all-events`; | ||
|
||
const response = await fetch(URL); | ||
const parsedData = await response.json(); | ||
return parsedData; | ||
} | ||
|
||
export { api }; |
Oops, something went wrong.