-
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.
refactor(competitions): remove usage of Vuex ORM for competition model
- Loading branch information
1 parent
48431ca
commit b6c0f75
Showing
9 changed files
with
66 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,36 @@ | ||
import { Model } from '@vuex-orm/core'; | ||
import CompetitionEdition from '~/models/competition-edition.model'; | ||
import { PartialItem } from '@directus/sdk'; | ||
import { NationalCompetitionEdition } from '~/plugins/cms-service'; | ||
import { DirectusNationalCompetition, getTranslatedFields } from '~/plugins/directus'; | ||
|
||
export default class Competition extends Model { | ||
export default class Competition { | ||
static entity = 'competitions'; | ||
|
||
id!: string; | ||
name!: string; | ||
slug!: string; | ||
editions!: CompetitionEdition[]; | ||
|
||
static fields() { | ||
return { | ||
id: this.string(null), | ||
name: this.string(null), | ||
slug: this.string(null), | ||
editions: this.hasMany(CompetitionEdition, 'competition_id'), | ||
}; | ||
readonly id: number; | ||
name: string; | ||
slug: string; | ||
editions?: NationalCompetitionEdition[]; | ||
|
||
constructor(rawCompetition: PartialItem<DirectusNationalCompetition>) { | ||
const translatedFields = getTranslatedFields(rawCompetition); | ||
|
||
if (!rawCompetition.id || !translatedFields?.name || !translatedFields?.slug) { | ||
throw new Error('Competition is missing requested fields'); | ||
} | ||
|
||
// Fallback for mandatory fields should not happen as we requested those fields | ||
|
||
this.id = rawCompetition.id; | ||
this.name = translatedFields.name; | ||
this.slug = translatedFields.slug; | ||
this.editions = (rawCompetition.editions as any) || []; | ||
} | ||
|
||
get lastEdition() { | ||
if (!this.editions || !this.editions.length) { | ||
return null; | ||
} | ||
|
||
// FIXME: This is a naive selection of the last edition. It might not work, i.e. when adding older editions. | ||
return this.editions[this.editions.length - 1]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { MutationTree } from 'vuex/types/index'; | ||
import { CompetitionsState } from './state'; | ||
import Competition from '~/models/competition.model'; | ||
|
||
const competitionMutations: MutationTree<CompetitionsState> = { | ||
setCompetitions(state, competitions: Competition[]) { | ||
state.competitions = competitions; | ||
}, | ||
}; | ||
|
||
export default competitionMutations; |
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,9 @@ | ||
import Competition from '~/models/competition.model'; | ||
|
||
export interface CompetitionsState { | ||
competitions: Competition[]; | ||
} | ||
|
||
export default (): CompetitionsState => ({ | ||
competitions: [], | ||
}); |