-
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.
Merge pull request #23 from dgmstuart/keyed-session-data
Separate session data per config
- 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 }; | ||
} | ||
}; |