Created
September 27, 2017 15:59
-
-
Save akaztp/6594246ede443c83278f6ce32ab513ce to your computer and use it in GitHub Desktop.
Models for case study on article "Web app architecture based on Redux"
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
class EntrySer { | |
constructor( | |
public id: string = null, | |
public title: string = '', | |
public type: string = 'FLICKR', | |
public data: string = '', | |
public postBy: UserRef = null, | |
public dateCreated: string = null) { } | |
public static fields: string = ` | |
id | |
title | |
type | |
data | |
postBy { ${ UserRef.fields} } | |
dateCreated | |
`; | |
} | |
class EntryRef { | |
constructor( | |
public id: string = null) { } | |
public static fields: string = ` | |
id | |
`; | |
public static resolveRef(ref: EntryRef, entries: Array<EntrySer>) { | |
return entries.find(e => e.id === ref.id); | |
} | |
} | |
class UserRef { | |
constructor( | |
public id: string) { } | |
public static fields: string = ` | |
id | |
`; | |
public static resolveRef(ref: UserRef, users: Array<UserSer>) { | |
return users.find(u => u.id === ref.id); | |
} | |
} | |
class AsyncDataSer<T> { | |
public error: boolean = false; | |
public errorsData?: Array<ServerError> = null; | |
constructor( | |
public data: T, | |
public loading: boolean = false, | |
public cursor: any = null) { } | |
public static hasData(adata: AsyncDataSer<any>, orError: boolean = false): boolean { | |
if (orError) | |
return adata.error || (!adata.loading && adata.data != null); | |
return !adata.error && !adata.loading && adata.data != null; | |
} | |
public static errored<T>(errors?: Array<any>, data: T = null): AsyncDataSer<T> { | |
const obs: AsyncDataSer<T> = new AsyncDataSer<T>(data); | |
obs.error = true; | |
if (errors && Array.isArray(errors)) | |
obs.errorsData = errors.filter((error) => (error.name && error.name === 'ServerError')); | |
return obs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For article Web app architecture based on Redux