Skip to content

Commit

Permalink
feat: store configured season in user config
Browse files Browse the repository at this point in the history
  • Loading branch information
voliva committed Dec 8, 2022
1 parent 49a3729 commit 1ea8212
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
43 changes: 41 additions & 2 deletions src/App/playerDetailsState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
merge,
Observable,
switchMap,
take,
tap,
withLatestFrom,
} from "rxjs";
Expand Down Expand Up @@ -117,9 +118,47 @@ export const playerDetails$ = state<PlayerDetails | null>(
null
);

export const selectedType$ = state(selectedTypeChange$, "2v2");
export const selectedType$ = state(
merge(
initialConfig$.pipe(map((v) => v.season.type)),
selectedTypeChange$.pipe(
tap((type) => {
initialConfig$
.pipe(
map((v) => v.season.end),
take(1),
switchMap((end) =>
writeConfig$({
season: { type, end },
})
)
)
.subscribe();
})
)
),
"2v2"
);
export const endDate$ = state(
endDateChange$.pipe(map((v) => (v ? v : null))),
merge(
initialConfig$.pipe(map((v) => v.season.end ?? null)),
endDateChange$.pipe(
map((v) => (v ? v : null)),
tap((end) => {
initialConfig$
.pipe(
map((v) => v.season.type),
take(1),
switchMap((type) =>
writeConfig$({
season: { type, end: end ?? undefined },
})
)
)
.subscribe();
})
)
),
null
);

Expand Down
20 changes: 17 additions & 3 deletions src/service/localData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shareLatest } from "@react-rxjs/core";
import { shareLatest, state } from "@react-rxjs/core";
import {
BaseDirectory,
createDir,
Expand All @@ -12,17 +12,27 @@ export interface Goal {
value: number;
}

export interface ManualSeason {
type: "2v2" | "3v3" | "5v5";
end: string | undefined;
}

export interface Config {
version: 1;
apiKey: string;
holidays: number[]; // day0 = start of season
season: ManualSeason;
goals: Goal[];
}

const defaultConfig: Config = {
version: 1,
apiKey: "",
holidays: [],
season: {
type: "2v2",
end: undefined,
},
goals: [
{
title: "All chests",
Expand All @@ -38,14 +48,18 @@ const readConfig$ = () =>
dir: BaseDirectory.App,
});

return JSON.parse(content) as Config;
const parsed = JSON.parse(content) as Partial<Config>;
return {
...defaultConfig,
...parsed,
};
} catch (ex) {
console.error(ex);
return defaultConfig;
}
});

export const initialConfig$ = readConfig$().pipe(shareLatest());
export const initialConfig$ = state(readConfig$());

export const writeConfig$ = (config: Partial<Config>) =>
readConfig$().pipe(
Expand Down

0 comments on commit 1ea8212

Please sign in to comment.