Skip to content

Instantly share code, notes, and snippets.

@terrysahaidak
Created November 23, 2019 19:19
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 terrysahaidak/cbe57f2028705bba7d69d2cede62d8ff to your computer and use it in GitHub Desktop.
Save terrysahaidak/cbe57f2028705bba7d69d2cede62d8ff to your computer and use it in GitHub Desktop.
Generic user store
let UserModel;
const ResolveUser = types
.model('ResolveUser', {
id: types.identifier,
})
.views((self) => ({
get user() {
return resolveIdentifier(UserModel, self, self.id);
},
}));
const UserSettingsModel = ResolveUser.named('UserSettings').props({
height: types.number,
color: types.string,
});
const UserShopModel = ResolveUser.named('UserShop').props({
name: types.string,
});
UserModel = types
.model('User', {
id: types.identifier,
firstName: types.string,
lastName: types.string,
settings: types.reference(UserSettingsModel),
shop: types.reference(UserShopModel),
})
.preProcessSnapshot((snapshot) => ({
settings: snapshot.id,
shop: snapshot.id,
}));
const UserStore = types.model('UserStore', {
users: types.map(UserModel),
shopUsers: types.map(UserShopModel),
settingUsers: types.map(UserSettingsModel),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment