-
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.
feat: View of completed operator sign-ins (#51)
- Loading branch information
Showing
22 changed files
with
534 additions
and
4 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,57 @@ | ||
import { back } from "../../browser"; | ||
import { lookupDisplayName, useEmployees } from "../../hooks/useEmployees"; | ||
import { useSignins } from "../../hooks/useSignIns"; | ||
import { HeavyRailLine } from "../../types"; | ||
import { DateTime } from "luxon"; | ||
import { ReactElement } from "react"; | ||
|
||
export const List = ({ line }: { line: HeavyRailLine }): ReactElement => { | ||
const employees = useEmployees(); | ||
const signIns = useSignins(line); | ||
|
||
if (signIns.status === "loading" || employees.status === "loading") { | ||
return <>Loading...</>; | ||
} else if (signIns.status === "error") { | ||
return <>Error retrieving signins: {signIns.error}</>; | ||
} else if (employees.status === "error") { | ||
return <>Error retrieving employees: {employees.error}</>; | ||
} | ||
|
||
return ( | ||
<div className="m-2"> | ||
<button | ||
className="bg-mbta-blue text-gray-100 rounded-md p-2 text-sm m-5" | ||
onClick={() => { | ||
back(); | ||
}} | ||
> | ||
Back | ||
</button> | ||
<u>Today's sign-ins</u> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th className="border">Name</th> | ||
<th className="border">Badge</th> | ||
<th className="border">Time</th> | ||
<th className="border">Official</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{signIns.result.map((si, idx) => ( | ||
<tr key={idx}> | ||
<td className="border"> | ||
{lookupDisplayName(si.signed_in_employee, employees.result)} | ||
</td> | ||
<td className="border">{si.signed_in_employee}</td> | ||
<td className="border"> | ||
{si.signed_in_at.toLocaleString(DateTime.TIME_SIMPLE)} | ||
</td> | ||
<td className="border">{si.signed_in_by_user}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</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
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,22 @@ | ||
import { useApiResult } from "../api"; | ||
import { SignIn, SignInList } from "../models/signin"; | ||
import { HeavyRailLine } from "../types"; | ||
import { DateTime } from "luxon"; | ||
|
||
const SIGN_INS_API_PATH = "/api/signin"; | ||
|
||
const parse = (list: SignInList) => | ||
list.map((si: SignIn) => ({ | ||
...si, | ||
signed_in_at: DateTime.fromISO(si.signed_in_at, { | ||
zone: "America/New_York", | ||
}), | ||
})); | ||
|
||
export const useSignins = (line: HeavyRailLine) => { | ||
return useApiResult({ | ||
RawData: SignInList, | ||
url: `${SIGN_INS_API_PATH}?line=${line}`, | ||
parser: parse, | ||
}); | ||
}; |
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,13 @@ | ||
import { z } from "zod"; | ||
|
||
export const SignIn = z.object({ | ||
rail_line: z.enum(["blue", "orange", "red"]), | ||
signed_in_at: z.string(), | ||
signed_in_by_user: z.string(), | ||
signed_in_employee: z.string(), | ||
}); | ||
|
||
export type SignIn = z.infer<typeof SignIn>; | ||
|
||
export const SignInList = z.array(SignIn); | ||
export type SignInList = z.infer<typeof SignInList>; |
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,13 +1,18 @@ | ||
import { Home } from "../../components/home"; | ||
import { render } from "@testing-library/react"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
|
||
jest.mock("../../components/operatorSignIn/operatorSignInModal", () => ({ | ||
OperatorSignInModal: () => <div>Mock modal</div>, | ||
})); | ||
|
||
describe("home", () => { | ||
test("loads orbit placeholder", () => { | ||
const view = render(<Home />); | ||
const view = render( | ||
<MemoryRouter> | ||
<Home /> | ||
</MemoryRouter>, | ||
); | ||
expect(view.getByText(/🪐/)).toBeInTheDocument(); | ||
}); | ||
}); |
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,45 @@ | ||
import { List } from "../../../components/operatorSignIn/list"; | ||
import { displayName } from "../../../models/employee"; | ||
import { employeeFactory } from "../../helpers/factory"; | ||
import { render } from "@testing-library/react"; | ||
import { DateTime } from "luxon"; | ||
|
||
const EMPLOYEES = [employeeFactory.build()]; | ||
jest.mock("../../../hooks/useEmployees", () => ({ | ||
useEmployees: jest.fn().mockImplementation(() => ({ | ||
status: "ok", | ||
result: EMPLOYEES, | ||
})), | ||
findEmployeeByBadge: jest.fn(() => EMPLOYEES[0]), | ||
findEmployeeByBadgeSerial: jest.fn(() => EMPLOYEES[0]), | ||
lookupDisplayName: jest.fn(() => displayName(EMPLOYEES[0])), | ||
})); | ||
|
||
jest.mock("../../../hooks/useSignIns", () => ({ | ||
useSignins: jest.fn().mockImplementation(() => ({ | ||
status: "ok", | ||
result: [ | ||
{ | ||
rail_line: "blue", | ||
signed_in_at: DateTime.fromISO("2024-07-22T12:45:52.000-04:00", { | ||
zone: "America/New_York", | ||
}), | ||
signed_in_by_user: "[email protected]", | ||
signed_in_employee: EMPLOYEES[0].badge, | ||
}, | ||
], | ||
})), | ||
})); | ||
|
||
describe("List", () => { | ||
test("shows a sign-in", () => { | ||
const view = render(<List line="blue" />); | ||
expect(view.getByText("12:45 PM")).toBeInTheDocument(); | ||
expect( | ||
view.getByText( | ||
`${EMPLOYEES[0].preferred_first} ${EMPLOYEES[0].last_name}`, | ||
), | ||
).toBeInTheDocument(); | ||
expect(view.getByText("[email protected]")).toBeInTheDocument(); | ||
}); | ||
}); |
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,50 @@ | ||
import { fetch } from "../../browser"; | ||
import { useSignins } from "../../hooks/useSignIns"; | ||
import { PromiseWithResolvers } from "../helpers/promiseWithResolvers"; | ||
import { renderHook, waitFor } from "@testing-library/react"; | ||
import { DateTime } from "luxon"; | ||
|
||
jest.mock("../../browser", () => ({ | ||
fetch: jest.fn(), | ||
})); | ||
|
||
const TEST_DATA = { | ||
data: [ | ||
{ | ||
rail_line: "blue", | ||
signed_in_at: "2024-07-22T16:42:32Z", | ||
signed_in_by_user: "[email protected]", | ||
signed_in_employee: "123", | ||
}, | ||
], | ||
}; | ||
|
||
const TEST_PARSED = [ | ||
{ | ||
...TEST_DATA.data[0], | ||
signed_in_at: DateTime.fromISO(TEST_DATA.data[0].signed_in_at, { | ||
zone: "America/New_York", | ||
}), | ||
}, | ||
]; | ||
|
||
describe("useSignins", () => { | ||
test("parses api response", async () => { | ||
const { promise, resolve } = PromiseWithResolvers<Response>(); | ||
jest.mocked(fetch).mockReturnValue(promise); | ||
|
||
const { result } = renderHook(useSignins); | ||
|
||
resolve({ | ||
status: 200, | ||
json: () => | ||
new Promise((resolve) => { | ||
resolve(TEST_DATA); | ||
}), | ||
} as Response); | ||
|
||
await waitFor(() => { | ||
expect(result.current).toEqual({ status: "ok", result: TEST_PARSED }); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
export type HeavyRailLine = "blue" | "orange" | "red"; |
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
Oops, something went wrong.