Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
fix: storable class, and site class cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
villetakanen committed Dec 6, 2021
1 parent d74611e commit 8fb7322
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/components/sites/SiteList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import Card from '@/components/layout/Card.vue'
import { fireStoreURL, toDisplayString } from '@/utils/firebaseTools'
import Icon from '../material/Icon.vue'
import { useSites } from '@/state/sites'
import { Site } from '@/state/site'
import { Site } from '@/state/site/Site'
import Column from '../layout/Column.vue'
import { useAuth } from '@/state/authz'
import Button from '../form/Button.vue'
Expand Down Expand Up @@ -104,7 +104,7 @@ export default defineComponent({
return a.name > b.name ? 1 : -1
}
if (sort.value === 'date') {
return (a.lastUpdate?.seconds || 0) < (b.lastUpdate?.seconds || 0) ? 1 : -1
return a.compareChangeTime(b)
}
return -1
})
Expand Down
54 changes: 46 additions & 8 deletions src/state/Storable.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
import { DocumentData, Timestamp, serverTimestamp } from '@firebase/firestore'

export interface StorableDoc {
id: string
created?: Timestamp
updated?: Timestamp
}

/**
* A helper superclass for entities storable to Firestone
*/
export class Storable {
export class Storable implements StorableDoc {
private _id: string

constructor (id: string) {
this._id = id
private _created: Timestamp|undefined
private _updated: Timestamp|undefined

constructor (storable: string|StorableDoc, doc?: DocumentData) {
if (typeof storable === 'string') {
this._id = storable
if (doc) this.docData = doc
return
}
this._id = storable?.id || ''
this.docData = storable
}

get id (): string {
return this._id
}

get docData(): {} {
console.error('docData getter must be overridden')
return {}
get created (): Timestamp|undefined {
return this._created
}

get updated (): Timestamp|undefined {
return this._updated
}

get docData (): DocumentData {
const data: DocumentData = {
created: serverTimestamp(),
updated: serverTimestamp()
}
if (this._created) data.created = this._created

return data
}

set docData (doc: DocumentData) {
console.error('docData setter must be overridden')
if (doc.updated) this._updated = doc.updated
}

compareChangeTime (other:Storable): number {
if (!other.updated) return 1 // other site has no lastUpdate, this is more recent
if (!this.updated) return -1 // this site has no lastUpdate, other is more recent
if (other.updated.toMillis() > this.updated.toMillis()) {
return -1
}
return 1
}
}
115 changes: 48 additions & 67 deletions src/state/site/Site.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DocumentData, Timestamp } from '@firebase/firestore'
import { DocumentData } from '@firebase/firestore'
import { Storable, StorableDoc } from '../Storable'

export interface SiteData {
id?: string
export interface SiteDoc extends StorableDoc {
name?: string
description?: string
owners?: string[]
Expand All @@ -10,84 +10,65 @@ export interface SiteData {
hidden?: boolean
system?: string
systemBadge?: string
lastUpdate?: Timestamp
theme?: string
}

export class Site {
id: string
name: string
description: string
owners: string[]
players: string[]
usePlayers: boolean
hidden: boolean
system: string
systemBadge: string
lastUpdate?: Timestamp
export class Site extends Storable {
name = ''
description = ''
owners: string[] = []
players: string[] = []
usePlayers = false
hidden = true
system = ''
systemBadge = ''
theme = ''

constructor (site: string|SiteData = '', data?: SiteData) {
const d = typeof site !== 'string' ? site : data ? { id: site, ...data } : { id: site }
constructor (site: string|SiteDoc = '', data?: SiteDoc) {
super(site as StorableDoc, data)
const d:SiteDoc = typeof site !== 'string' ? site : data ? { ...data } : { id: site }

this.id = d.id || ''
this.name = d.name || ''
this.description = d.description || ''
this.owners = d.owners || []
this.players = d.players || []
this.usePlayers = d.usePlayers || false
this.hidden = d.hidden || false
this.docData = d
}

get docData (): DocumentData {
const data = super.docData
data.name = this.name
data.description = this.description
data.owners = this.owners
data.players = this.players
data.usePlayers = this.usePlayers
data.hidden = this.hidden
data.system = this.system
data.systemBadge = this.systemBadge
return data
}

// Optional fields
if (d.lastUpdate) this.lastUpdate = d.lastUpdate
set docData (doc: DocumentData) {
super.docData = doc
this.name = doc.name || ''
this.description = doc.description || ''
this.owners = doc.owners || []
this.players = doc.players || []
this.usePlayers = doc.usePlayers || false
this.hidden = doc.hidden || false

// Backwards compatibility, remove in future when all sites have the system field set
const s = d?.system || d?.systemBadge || ''
const s = doc?.system || doc?.systemBadge || ''
this.system = s
this.systemBadge = s
this.theme = s
}

static fromDocumentData (data:DocumentData): Site {
return new Site(data as SiteData)
}

toJSON (): SiteData {
const s:SiteData = {
id: this.id,
name: this.name,
description: this.description,
owners: this.owners,
players: this.players,
usePlayers: this.usePlayers,
hidden: this.hidden,
system: this.system,
systemBadge: this.systemBadge
}
if (this.lastUpdate) s.lastUpdate = this.lastUpdate
return s
}

isOwner (uid:string): boolean {
if (this.owners && this.owners.includes(uid)) {
return true
}
return false
hasOwner (uid:string): boolean {
return this.owners && this.owners.includes(uid)
}

canEdit (uid:string): boolean {
if (this.owners && this.owners.includes(uid)) {
return true
}
if (this.players && this.players.includes(uid)) {
return true
}
return false
hasPlayer (uid:string): boolean {
return this.players && this.players.includes(uid)
}

compareChangeTime (other:Site): number {
if (!other.lastUpdate) return 1 // other site has no lastUpdate, this is more recent
if (!this.lastUpdate) return -1 // this site has no lastUpdate, other is more recent
if (other.lastUpdate.toMillis() > this.lastUpdate.toMillis()) {
return -1
}
return 1
hasEditor (uid:string): boolean {
return this.hasOwner(uid) || this.hasPlayer(uid)
}
}

0 comments on commit 8fb7322

Please sign in to comment.