-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add proper campaign selection to module settings
- Loading branch information
1 parent
8e5b82b
commit 8732493
Showing
10 changed files
with
176 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,3 @@ declare module '*.png' { | |
const content: string; | ||
export default content; | ||
} | ||
|
||
interface App { | ||
|
||
} |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/* eslint-disable no-console */ | ||
import moduleConfig from './module.json'; | ||
|
||
export function info(...args: unknown[]): void { | ||
export function logInfo(...args: unknown[]): void { | ||
console.log(moduleConfig.name, ' | ', ...args); | ||
} | ||
|
||
export function error(...args: unknown[]): void { | ||
export function logError(...args: unknown[]): void { | ||
console.error(moduleConfig.name, ' | ', ...args); | ||
} |
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,91 @@ | ||
import { logError } from '../logger'; | ||
import moduleConfig from '../module.json'; | ||
import KankaSettings from '../types/KankaSettings'; | ||
import getSettings from './getSettings'; | ||
import { getCampaigns } from './kanka'; | ||
|
||
const accessTokenInputName = `${moduleConfig.name}.${KankaSettings.accessToken}`; | ||
const campaignInputName = `${moduleConfig.name}.${KankaSettings.campaign}`; | ||
|
||
async function getCampaignChoices(token?: string): Promise<Record<string, string>> { | ||
if (!token) { | ||
return { | ||
'': game.i18n.localize('KANKA.SettingsCampaignNoToken'), | ||
}; | ||
} | ||
|
||
try { | ||
const campaignChoices: Record<string, string> = { | ||
'': game.i18n.localize('KANKA.SettingsCampaignPleaseChoose'), | ||
}; | ||
const campaignsResult = await getCampaigns(token); | ||
|
||
campaignsResult.data.forEach((campaign) => { | ||
campaignChoices[campaign.id] = campaign.name; | ||
}); | ||
|
||
return campaignChoices; | ||
} catch (error) { | ||
logError(error); | ||
return { | ||
'': game.i18n.localize('KANKA.SettingsCampaignInvalidToken'), | ||
}; | ||
} | ||
} | ||
|
||
async function updateWorldList(event: any): Promise<void> { | ||
const token = event.target.value; | ||
const choices = await getCampaignChoices(token); | ||
|
||
const select = $(`[name="${campaignInputName}"]`); | ||
select.empty(); | ||
|
||
Object.entries(choices).forEach(([value, label]) => { | ||
const option = $(`<option value="${value}">${label}</option>`); | ||
select.append(option); | ||
}); | ||
|
||
select.val(getSettings(KankaSettings.campaign)); | ||
} | ||
|
||
export function clearSettings(): void { | ||
Array | ||
.from<string>(game.settings.settings.keys()) | ||
.filter((key: string) => key.startsWith(moduleConfig.name)) | ||
.forEach(key => game.settings.settings.delete(key)); | ||
|
||
$(document).off('change', `[name="${accessTokenInputName}"]`, updateWorldList); | ||
} | ||
|
||
export async function registerSettings(): Promise<void> { | ||
$(document).on('change', `[name="${accessTokenInputName}"]`, updateWorldList); | ||
|
||
game.settings.register( | ||
moduleConfig.name, | ||
KankaSettings.accessToken, | ||
{ | ||
name: game.i18n.localize('KANKA.SettingsTokenLabel'), | ||
hint: game.i18n.localize('KANKA.SettingsTokenHint'), | ||
scope: 'world', | ||
config: true, | ||
type: String, | ||
default: '', | ||
}, | ||
); | ||
|
||
game.settings.register( | ||
moduleConfig.name, | ||
KankaSettings.campaign, | ||
{ | ||
name: game.i18n.localize('KANKA.SettingsCampaignLabel'), | ||
hint: game.i18n.localize('KANKA.SettingsCampaignHint'), | ||
scope: 'world', | ||
config: true, | ||
type: String, | ||
default: '', | ||
choices: await getCampaignChoices(getSettings(KankaSettings.accessToken)), | ||
}, | ||
); | ||
|
||
game.settings.sheet.render(); // update sheet if it already visible | ||
} |
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,6 @@ | ||
import moduleConfig from '../module.json'; | ||
import KankaSettings from '../types/KankaSettings'; | ||
|
||
export default function getSetting<T = unknown>(setting: KankaSettings): T { | ||
return game.settings.get(moduleConfig.name, setting); | ||
} |
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,54 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import KankaSettings from '../types/KankaSettings'; | ||
import getSetting from './getSettings'; | ||
|
||
interface KankaLinks { | ||
first: string; | ||
last: string; | ||
prev: string | null; | ||
next: string | null | ||
} | ||
|
||
interface KankaMeta { | ||
current_page: number; | ||
from: number; | ||
last_page: number; | ||
path: string; | ||
per_page: number; | ||
to: number; | ||
total: number; | ||
} | ||
|
||
interface KankaResult<T> { | ||
data: T; | ||
links: KankaLinks; | ||
meta: KankaMeta; | ||
} | ||
|
||
interface CampaignData { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
async function fetchKanka<T>(path: string, token?: string): Promise<T> { | ||
const response = await fetch( | ||
`https://kanka.io/api/1.0/${path}`, | ||
{ | ||
mode: 'cors', | ||
headers: { | ||
Authorization: `Bearer ${token ?? getSetting<string>(KankaSettings.accessToken)}`, | ||
'Content-type': 'application/json', | ||
}, | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Kanka request error: ${response.statusText} (${response.status})`); | ||
} | ||
|
||
return response.json(); | ||
} | ||
|
||
export async function getCampaigns(token?: string): Promise<KankaResult<CampaignData[]>> { | ||
return fetchKanka('campaigns', token); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enum KankaSettings { | ||
accessToken = 'access_token', | ||
campaign = 'campaign', | ||
} | ||
|
||
export default KankaSettings; |
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