Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaztp/6594246ede443c83278f6ce32ab513ce to your computer and use it in GitHub Desktop.
Save akaztp/6594246ede443c83278f6ce32ab513ce to your computer and use it in GitHub Desktop.
Models for case study on article "Web app architecture based on Redux"
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;
}
}
@akaztp
Copy link
Author

akaztp commented Sep 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment