Created
July 15, 2019 08:13
-
-
Save Maqsim/f811ce773e9e08bc468c10dd963cc227 to your computer and use it in GitHub Desktop.
Mocks angular resource service to quickly emulate server communication
This file contains hidden or 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
import angular from 'angular'; | |
import AngularResource from 'angular-resource'; | |
import AppEnvironment from 'app-environment'; | |
import { each, where, findWhere, extend, pick, omit } from 'underscore'; | |
export const RewardShopProductModule = angular.module('app.resources.reward-shop-product', [ | |
AngularResource, | |
AppEnvironment | |
]) | |
.service('RewardShopProduct', ($resource: ng.resource.IResourceService, $timeout: any, SHOP_API_BASE: string) => { | |
'ngInject'; | |
const useMockData = true; | |
const resource = $resource(SHOP_API_BASE + '/company/:companyId/products/:id/:path', | |
{ companyId: '@companyId', id: '@id', path: '@path' }, | |
{ | |
get: { | |
cancellable: true, | |
method: 'get' | |
} | |
} | |
); | |
if (useMockData) { | |
const rewards = [ | |
{ | |
name: 'Foo', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 10, | |
description: 'Very short description', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 23, | |
levelRequired: 15, | |
status: 'PUBLISHED' | |
}, | |
{ | |
name: 'Best name', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
quantityAvailable: 7, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 60, | |
status: 'PUBLISHED' | |
}, | |
{ | |
name: 'Foo baz', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 1, | |
description: 'Looong description that has to lines of text and con', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'PUBLISHED' | |
}, | |
{ | |
name: 'Foo bar', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 0, | |
description: 'longtextwritteningermanlongtextwritteningerman', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'PUBLISHED' | |
}, | |
{ | |
name: 'fffsssss', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
quantityAvailable: 1, | |
description: 'fasf asfa sfasf asf af', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 89, | |
status: 'PUBLISHED' | |
}, | |
{ | |
name: 'Foo 223', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'ARCHIVED' | |
}, | |
{ | |
name: 'Foo44', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'ARCHIVED' | |
}, | |
{ | |
name: '556Foo', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'ARCHIVED' | |
}, | |
{ | |
name: 'BABABar', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'DRAFT' | |
}, | |
{ | |
name: '@@@Bar', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'DRAFT' | |
}, | |
{ | |
name: 'fasfBar', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'DRAFT' | |
}, | |
{ | |
name: 'ffBar', | |
companyId: 'fb0aaac5-a99d-46dd-963d-d5c611962e97', | |
image: { | |
id: 5, | |
href: 'https://picsum.photos/300/165' | |
}, | |
quantityAvailable: 4, | |
description: 'foo', | |
monetaryValue: { | |
currency: 'EUR', | |
value: 1000 | |
}, | |
priceInCoins: 10, | |
status: 'DRAFT' | |
} | |
]; | |
each(rewards, (reward, index) => { | |
// @ts-ignore | |
reward.id = index + 1; | |
if (reward.image) { | |
reward.image.href += `?t=${Math.random()}`; | |
} | |
// @ts-ignore | |
reward.version = 1; | |
}); | |
// @ts-ignore | |
resource.query = (filters, cb) => { | |
const collectionFiltersProperties = ['limit', 'offset']; | |
const collectionFilters = pick(filters, collectionFiltersProperties); | |
collectionFilters.offset = collectionFilters.offset || 0; | |
collectionFilters.limit = collectionFilters.limit || 3; | |
filters = omit(filters, collectionFiltersProperties); | |
const filteredRewards = angular.copy(where(rewards, filters).reverse()); | |
const filteredRewardsWithCollectionModifiers = filteredRewards.slice(collectionFilters.offset, collectionFilters.offset + collectionFilters.limit); | |
const result = {}; | |
const timer1: ng.ITimeoutService = $timeout(() => { | |
result.items = filteredRewardsWithCollectionModifiers; | |
result.limit = collectionFilters.limit; | |
result.offset = collectionFilters.offset; | |
result.total = filteredRewards.length; | |
cb && cb(result); | |
}, 2000); | |
let timer2: ng.ITimeoutService; | |
result.$promise = new Promise((resolve) => { | |
timer2 = $timeout(() => { | |
resolve(result); | |
}, 2000); | |
}); | |
result.$cancelRequest = () => { | |
$timeout.cancel(timer1); | |
$timeout.cancel(timer2); | |
}; | |
return result; | |
}; | |
// @ts-ignore | |
resource.save = (obj, cb) => { | |
if (!obj.id) { | |
obj.id = Date.now(); | |
rewards.push(obj); | |
} else { | |
const existingReward = findWhere(rewards, { id: obj.id }); | |
extend(existingReward, obj); | |
} | |
obj.version = (obj.version || 0) + 1; | |
cb && cb(obj); | |
return { | |
$promise: new Promise((resolve) => { | |
resolve(obj); | |
}) | |
}; | |
}; | |
// @ts-ignore | |
resource.delete = (obj, cb) => { | |
cb && cb(); | |
return { | |
$promise: new Promise((resolve) => { | |
resolve(obj); | |
}) | |
}; | |
}; | |
} | |
return resource; | |
}) | |
.name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment