-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass the file name to the session data key, so that we get separate entries per config. This has the effect of auto-switching the grid when choosing a new config - that's not actually an expected use case but it feels correct that the app should only be able to show a consistent set of config and words. Jump through some hoops to avoid not found resulting in junk localstorage entries: Since we fall back to a default list if the name is not recognised (maybe a bad idea?), the param doesn't always match the list filename, and we don't want eg http://localhost:5173/bingo-frontend/foo to result in storing bingoSession-foo in localstorage. Maybe we can skip all that nonsense and do some kind of redirect or not found instead?
- Loading branch information
Showing
13 changed files
with
93 additions
and
55 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type EmptyObject = Record<string, never>; | ||
type Result<T> = | ||
| { success: true; data: T } | ||
| { success: false; data: EmptyObject }; | ||
|
||
const importJsonData = async <T>(listName: string): Promise<Result<T>> => { | ||
try { | ||
return { | ||
success: true, | ||
data: (await import(`../data/${listName}.json`)).default, | ||
}; | ||
} catch (error) { | ||
return { | ||
success: false, | ||
data: {}, | ||
}; | ||
console.error("Failed to load the data", error); | ||
} | ||
}; | ||
|
||
export default importJsonData; |
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,25 +1,34 @@ | ||
import defaultConfig from "../data/teamLindy.json"; | ||
import nullConfig from "../data/teamLindy.json"; | ||
import JsonDataImporter from "../lib/JsonDataImporter"; | ||
import importJsonData from "../lib/importJsonData"; | ||
import type { Params } from "react-router-dom"; | ||
import type { Config } from "../data/config"; | ||
|
||
export const defaultConfigLoader = (): Config => { | ||
return defaultConfig; | ||
export type KeyedConfig = { id: string } & Config; | ||
|
||
const defaultKeyedConfig: KeyedConfig = { id: "teamLindy", ...defaultConfig }; | ||
|
||
export const defaultConfigLoader = (): KeyedConfig => { | ||
return defaultKeyedConfig; | ||
}; | ||
|
||
export const configLoader = async ({ | ||
params, | ||
}: { | ||
params: Params<string>; | ||
}): Promise<Config> => { | ||
if (params.gameName) { | ||
const importer = new JsonDataImporter({ defaultData: defaultConfig }); | ||
return await importer.import(params.gameName); | ||
}): Promise<KeyedConfig> => { | ||
const id = params.gameName; | ||
if (id) { | ||
const { success, data } = await importJsonData<Config>(id); | ||
if (success) { | ||
return { id, ...data }; | ||
} else { | ||
return defaultKeyedConfig; | ||
} | ||
} else { | ||
console.error( | ||
"expected a param of 'gameName' but didn't find one or it had a falsey value", | ||
); | ||
return nullConfig; | ||
return { id: "null", ...nullConfig }; | ||
} | ||
}; |