-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuse-page-errors.ts
More file actions
39 lines (33 loc) · 1.08 KB
/
Copy pathuse-page-errors.ts
File metadata and controls
39 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useState, useCallback } from "react";
import { CollectionUtils, ResultRecord } from "andculturecode-javascript-core";
/**
* Hook to bundle common page error handling functionality
*/
export function usePageErrors() {
const [pageErrors, setPageErrors] = useState<Array<string>>([]);
const handlePageLoadError = useCallback((result: any) => {
if (result instanceof ResultRecord) {
setPageErrors((errors: string[]) => [
...errors,
...result.listErrorMessages(),
]);
return;
}
if (typeof result === "string") {
setPageErrors((errors: string[]) => [...errors, result]);
return;
}
setPageErrors((errors: string[]) => [...errors, result.toString()]);
}, []);
const resetPageErrors = useCallback(() => {
setPageErrors((prevState) =>
CollectionUtils.hasValues(prevState) ? [] : prevState
);
}, []);
return {
handlePageLoadError,
pageErrors,
resetPageErrors,
setPageErrors,
};
}