Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner

akaztp commented Sep 27, 2017

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