Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
b263 committed Jan 4, 2024
1 parent 22abc36 commit f1146d2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/js/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ $SD.onConnected(() => {
store.patchState({
[StateKey.globalSettings]: settings,
});
KimaiApi.config(settings.backendProviderConfig["kimai"]);
// Initial settings are undefined
if (settings?.backendProviderConfig?.["kimai"]) {
KimaiApi.config(settings.backendProviderConfig["kimai"]);
}
}
);
});
Expand Down
10 changes: 4 additions & 6 deletions src/js/src/lib/action/tracker-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ export function initTrackerAction(store: Store<AppState>) {
});

async function getApi() {
const {
backendProviderConfig: {
["kimai"]: { url, user, token },
},
} = await store.once(StateKey.globalSettings);
return KimaiApi.config({ url, user, token }).get();
const globalSettings = await store.once(StateKey.globalSettings);
const apiConfig =
globalSettings?.backendProviderConfig?.["kimai"] ?? KimaiApi.emptyConfig;
return KimaiApi.config(apiConfig).get();
}
}
18 changes: 13 additions & 5 deletions src/js/src/lib/api/kimai-api-tracker-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,19 @@ export class KimaiApiTrackerConnector implements ApiTrackerConnector {
}

async onRequestWorkedToday() {
this.#tracker.workedToday = await this.getWorkedToday({
projectId: this.settings(this.#tracker)!.projectId,
activityId: this.settings(this.#tracker)!.activityId,
});
this.#tracker.render();
const projectId = this.settings(this.#tracker)?.projectId;
const activityId = this.settings(this.#tracker)?.activityId;
if (projectId && activityId) {
try {
this.#tracker.workedToday = await this.getWorkedToday({
projectId,
activityId,
});
this.#tracker.render();
} catch (e) {
/* empty */
}
}
}

connect(tracker: Tracker) {
Expand Down
25 changes: 24 additions & 1 deletion src/js/src/lib/api/kimai-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ type ApiConfig = {
export class KimaiApi {
static instance: KimaiApi | null = null;

static emptyConfig = {
url: "",
user: "",
token: "",
};

static config(c: ApiConfig) {
KimaiApi.get().config = c;
KimaiApi.get().config = c ?? KimaiApi.emptyConfig;
return KimaiApi;
}

Expand Down Expand Up @@ -62,10 +68,23 @@ export class KimaiApi {
} as RequestInit; // eslint-disable-line
}

assertValidConfig() {
if (!this.#user) {
throw new Error("Invalid config. User must not be empty.");
}
if (!this.#baseUrl) {
throw new Error("Invalid config. BaseUrl must not be empty.");
}
if (!this.#token) {
throw new Error("Invalid config. Token must not be empty.");
}
}

async startTracking({
projectId,
activityId,
}: KimaiBackendProviderPluginConfig): Promise<ApiResponse<TrackingItem>> {
this.assertValidConfig();
const url = `${this.#baseUrl}api/timesheets`;
const body = {
begin: format(new Date(), "yyyy-MM-dd'T'HH:mm:ss"),
Expand All @@ -82,6 +101,7 @@ export class KimaiApi {
}

async stopTracking(id: number) {
this.assertValidConfig();
const url = `${this.#baseUrl}api/timesheets/${id}/stop`;
const options = {
...this.fetchOptions,
Expand All @@ -91,11 +111,13 @@ export class KimaiApi {
}

async getProjects() {
this.assertValidConfig();
const url = `${this.#baseUrl}api/projects`;
return tryFetch<Category[]>(url, this.fetchOptions);
}

async getActivities(projectId: number) {
this.assertValidConfig();
const params = new URLSearchParams({
project: String(projectId),
});
Expand All @@ -107,6 +129,7 @@ export class KimaiApi {
projectId: number,
activityId: number
): Promise<ApiResponse<TimeEntry[]>> {
this.assertValidConfig();
if (!projectId || !activityId) {
return {
success: false,
Expand Down

0 comments on commit f1146d2

Please sign in to comment.