-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: clean up production-line component
- Loading branch information
1 parent
21701d9
commit d7a9271
Showing
4 changed files
with
121 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Dispatch, useEffect } from "react"; | ||
import { TJoinProductionOptions } from "./types.ts"; | ||
import { TGlobalStateAction } from "../../global-state/global-state-actions.ts"; | ||
|
||
type TProps = { | ||
joinProductionOptions: TJoinProductionOptions | null; | ||
paramProductionId: string | undefined; | ||
paramLineId: string | undefined; | ||
dispatch: Dispatch<TGlobalStateAction>; | ||
}; | ||
|
||
export const useCheckBadLineData = ({ | ||
joinProductionOptions, | ||
paramProductionId, | ||
paramLineId, | ||
dispatch, | ||
}: TProps) => { | ||
useEffect(() => { | ||
if (!joinProductionOptions) { | ||
const pidIsNan = Number.isNaN( | ||
paramProductionId && parseInt(paramProductionId, 10) | ||
); | ||
|
||
const lidIsNan = Number.isNaN(paramLineId && parseInt(paramLineId, 10)); | ||
|
||
if (pidIsNan || lidIsNan) { | ||
// Someone entered a production id in the URL that's not a number | ||
|
||
const errorString = `Bad URL. ${pidIsNan ? "Production ID is not a number." : ""} ${lidIsNan ? "Line ID is not a number." : ""}`; | ||
|
||
dispatch({ | ||
type: "ERROR", | ||
payload: new Error(errorString), | ||
}); | ||
} | ||
} | ||
}, [paramProductionId, paramLineId, joinProductionOptions, dispatch]); | ||
}; |
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,18 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
type TProps = { | ||
connectionState: string | null; | ||
}; | ||
|
||
export const useIsLoading = ({ connectionState }: TProps) => { | ||
const [loading, setLoading] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
if (connectionState === "connected") { | ||
setLoading(false); | ||
} | ||
// TODO add handling for `connectionState === "failed"` | ||
}, [connectionState]); | ||
|
||
return loading; | ||
}; |
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 @@ | ||
import { useEffect, useState } from "react"; | ||
import { noop } from "../../helpers.ts"; | ||
import { API } from "../../api/api.ts"; | ||
import { TJoinProductionOptions, TLine } from "./types.ts"; | ||
|
||
type TProps = { | ||
joinProductionOptions: TJoinProductionOptions | null; | ||
}; | ||
|
||
export const useLinePolling = ({ joinProductionOptions }: TProps) => { | ||
const [line, setLine] = useState<TLine | null>(null); | ||
useEffect(() => { | ||
if (!joinProductionOptions) return noop; | ||
|
||
const productionId = parseInt(joinProductionOptions.productionId, 10); | ||
const lineId = parseInt(joinProductionOptions.lineId, 10); | ||
|
||
const interval = window.setInterval(() => { | ||
API.fetchProductionLine(productionId, lineId) | ||
.then((l) => setLine(l)) | ||
.catch(console.error); | ||
}, 1000); | ||
|
||
return () => { | ||
window.clearInterval(interval); | ||
}; | ||
}, [joinProductionOptions]); | ||
|
||
return line; | ||
}; |