Skip to content

Commit

Permalink
copy and return typed value instead of messing with original object
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Dec 18, 2024
1 parent 9e01f3d commit 715db03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
15 changes: 12 additions & 3 deletions resources/js/beatmapsets-show/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

import { BeatmapsetJsonForShow } from 'interfaces/beatmapset-extended-json';
import { TagJsonWithCount } from 'interfaces/tag-json';
import TagJson from 'interfaces/tag-json';
import UserJson from 'interfaces/user-json';
import { keyBy } from 'lodash';
import { action, computed, makeObservable, observable, runInAction } from 'mobx';
Expand All @@ -25,6 +25,16 @@ interface State {
showingNsfwWarning: boolean;
}

type TagJsonWithCount = TagJson & { count: number };

function asTagJsonWithCount(tag: TagJson) {
return {
count: 0,
...tag,
};
}


export default class Controller {
@observable hoveredBeatmap: null | BeatmapJsonForBeatmapsetShow = null;
@observable state: State;
Expand Down Expand Up @@ -88,8 +98,7 @@ export default class Controller {
const tagMap = new Map<number, TagJsonWithCount>();

for (const tag of this.beatmapset.related_tags) {
tag.count = 0; // assign 0 and cast
tagMap.set(tag.id, tag as TagJsonWithCount);
tagMap.set(tag.id, asTagJsonWithCount(tag));
}

for (const beatmap of this.beatmapset.beatmaps) {
Expand Down
3 changes: 0 additions & 3 deletions resources/js/interfaces/tag-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

export default interface TagJson {
count?: number;
description: string;
id: number;
name: string;
}

export type TagJsonWithCount = TagJson & Required<Pick<TagJson, 'count'>>;

0 comments on commit 715db03

Please sign in to comment.