feat(SW-977) check if json is valid
This commit is contained in:
@@ -13,6 +13,7 @@ import { useIntl } from "react-intl"
|
|||||||
|
|
||||||
import SkeletonShimmer from "@/components/SkeletonShimmer"
|
import SkeletonShimmer from "@/components/SkeletonShimmer"
|
||||||
import Caption from "@/components/TempDesignSystem/Text/Caption"
|
import Caption from "@/components/TempDesignSystem/Text/Caption"
|
||||||
|
import isValidJson from "@/utils/isValidJson"
|
||||||
|
|
||||||
import Input from "../Input"
|
import Input from "../Input"
|
||||||
import { init, localStorageKey, reducer, sessionStorageKey } from "./reducer"
|
import { init, localStorageKey, reducer, sessionStorageKey } from "./reducer"
|
||||||
@@ -129,12 +130,17 @@ export default function Search({ locations, handlePressEnter }: SearchProps) {
|
|||||||
typeof window !== "undefined"
|
typeof window !== "undefined"
|
||||||
? localStorage.getItem(localStorageKey)
|
? localStorage.getItem(localStorageKey)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
if (searchData || searchHistory) {
|
if (searchData || searchHistory) {
|
||||||
dispatch({
|
dispatch({
|
||||||
payload: {
|
payload: {
|
||||||
searchData: searchData ? JSON.parse(searchData) : undefined,
|
searchData:
|
||||||
searchHistory: searchHistory ? JSON.parse(searchHistory) : null,
|
isValidJson(searchData) && searchData
|
||||||
|
? JSON.parse(searchData)
|
||||||
|
: undefined,
|
||||||
|
searchHistory:
|
||||||
|
isValidJson(searchHistory) && searchHistory
|
||||||
|
? JSON.parse(searchHistory)
|
||||||
|
: null,
|
||||||
},
|
},
|
||||||
type: ActionType.SET_STORAGE_DATA,
|
type: ActionType.SET_STORAGE_DATA,
|
||||||
})
|
})
|
||||||
|
|||||||
9
utils/isValidJson.ts
Normal file
9
utils/isValidJson.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export default function isValidJson(value: string | null | undefined): boolean {
|
||||||
|
if (!value || value === "undefined") return false
|
||||||
|
try {
|
||||||
|
JSON.parse(value)
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user