Skip to content

Feature devices lookup #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -62,8 +62,16 @@ Object {
"getAllCountriesInit": [Function],
"getCountriesDone": [Function],
"getCountriesInit": [Function],
"getManufacturersDone": [Function],
"getManufacturersInit": [Function],
"getModelsDone": [Function],
"getModelsInit": [Function],
"getOsesDone": [Function],
"getOsesInit": [Function],
"getSkillTagsDone": [Function],
"getSkillTagsInit": [Function],
"getTypesDone": [Function],
"getTypesInit": [Function],
},
"memberTasks": Object {
"dropAll": [Function],
8 changes: 8 additions & 0 deletions __tests__/actions/__snapshots__/lookup.js.snap
Original file line number Diff line number Diff line change
@@ -7,8 +7,16 @@ Object {
"getAllCountriesInit": [Function],
"getCountriesDone": [Function],
"getCountriesInit": [Function],
"getManufacturersDone": [Function],
"getManufacturersInit": [Function],
"getModelsDone": [Function],
"getModelsInit": [Function],
"getOsesDone": [Function],
"getOsesInit": [Function],
"getSkillTagsDone": [Function],
"getSkillTagsInit": [Function],
"getTypesDone": [Function],
"getTypesInit": [Function],
},
}
`;
80 changes: 80 additions & 0 deletions __tests__/reducers/__snapshots__/lookup.js.snap
Original file line number Diff line number Diff line change
@@ -9,8 +9,15 @@ Object {
"countryCode": "AFG",
},
],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingCountriesError": false,
"loadingSkillTagsError": true,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -19,6 +26,7 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

@@ -31,8 +39,15 @@ Object {
"countryCode": "AFG",
},
],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingCountriesError": true,
"loadingSkillTagsError": true,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -41,14 +56,22 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

exports[`Default reducer Get skill tags 1`] = `
Object {
"allCountries": Array [],
"countries": Array [],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingSkillTagsError": false,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -57,14 +80,22 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

exports[`Default reducer Get skill tags error 1`] = `
Object {
"allCountries": Array [],
"countries": Array [],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingSkillTagsError": true,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -73,14 +104,23 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

exports[`Default reducer Initial state 1`] = `
Object {
"allCountries": Array [],
"countries": Array [],
"hasMoreModels": false,
"hasMoreOses": false,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [],
"types": Array [],
}
`;

@@ -93,8 +133,15 @@ Object {
"countryCode": "AFG",
},
],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingCountriesError": false,
"loadingSkillTagsError": true,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -103,6 +150,7 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

@@ -115,8 +163,15 @@ Object {
"countryCode": "AFG",
},
],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingCountriesError": true,
"loadingSkillTagsError": true,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -125,14 +180,22 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

exports[`Factory without server side rendering Get skill tags 1`] = `
Object {
"allCountries": Array [],
"countries": Array [],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingSkillTagsError": false,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -141,14 +204,22 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

exports[`Factory without server side rendering Get skill tags error 1`] = `
Object {
"allCountries": Array [],
"countries": Array [],
"hasMoreModels": false,
"hasMoreOses": false,
"loadingSkillTagsError": true,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [
Object {
"domain": "SKILLS",
@@ -157,13 +228,22 @@ Object {
"status": "APPROVED",
},
],
"types": Array [],
}
`;

exports[`Factory without server side rendering Initial state 1`] = `
Object {
"allCountries": Array [],
"countries": Array [],
"hasMoreModels": false,
"hasMoreOses": false,
"manufacturers": Array [],
"modelPage": 1,
"models": Array [],
"osPage": 1,
"oses": Array [],
"skillTags": Array [],
"types": Array [],
}
`;
100 changes: 100 additions & 0 deletions src/actions/lookup.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,98 @@
import { createActions } from 'redux-actions';
import { getService } from '../services/lookup';

/*
* device api PAGE_SIZE
*/
export const PAGE_SIZE = 100;

/**
* @static
* @desc Creates an action that signals beginning of getting all deveice types
* @return {Action}
*/
function getTypesInit() {}

/**
* @static
* @desc Creates an action that get all deveice types
* @return {Action}
*/
function getTypesDone() {
return getService().getTypes();
}


/**
* @static
* @desc Creates an action that signals beginning of getting all manufacturers
* @return {Action}
*/
function getManufacturersInit() {}

/**
* @static
* @desc Creates an action that get all deveice manufacturers
* @param {String} type
* @return {Action}
*/
function getManufacturersDone(type) {
return getService().getManufacturers(type);
}


/**
* @static
* @desc Creates an action that signals beginning of getting models
* @param {Number} page
* @return {Action}
*/
function getModelsInit(page) {
return {
page,
};
}

/**
* @static
* @desc Creates an action that get all deveice models
* @param {Number} page
* @param {String} manufacturer
* @param {String} type
* @return {Action}
*/
function getModelsDone(page, type, manufacturer) {
return getService().getDevices(page, PAGE_SIZE, type, manufacturer);
}


/**
* @static
* @desc Creates an action that signals beginning of getting operation systems
*
* @param {Number} page
* @return {Action}
*/
function getOsesInit(page) {
return {
page,
};
}

/**
* @static
* @desc Creates an action that get all operation systems
* @param {Number} page
* @param {String} manufacturer
* @param {String} type
* @param {String} model
* @return {Action}
*/
function getOsesDone(page, type, manufacturer, model) {
return getService().getDevices(page, PAGE_SIZE, type, manufacturer, model);
}


/**
* @static
* @desc Creates an action that signals beginning of getting all skill tags.
@@ -61,6 +153,14 @@ function getAllCountriesDone(tokenV3) {

export default createActions({
LOOKUP: {
GET_TYPES_INIT: getTypesInit,
GET_TYPES_DONE: getTypesDone,
GET_MANUFACTURERS_INIT: getManufacturersInit,
GET_MANUFACTURERS_DONE: getManufacturersDone,
GET_MODELS_INIT: getModelsInit,
GET_MODELS_DONE: getModelsDone,
GET_OSES_INIT: getOsesInit,
GET_OSES_DONE: getOsesDone,
GET_SKILL_TAGS_INIT: getSkillTagsInit,
GET_SKILL_TAGS_DONE: getSkillTagsDone,
GET_COUNTRIES_INIT: getCountriesInit,
166 changes: 165 additions & 1 deletion src/reducers/lookup.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,155 @@
import _ from 'lodash';
import { handleActions } from 'redux-actions';
import logger from '../utils/logger';
import actions from '../actions/lookup';
import actions, { PAGE_SIZE } from '../actions/lookup';


/**
* Handles LOOKUP/GET_TYPES_DONE action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetTypesDone(state, { payload, error }) {
if (error) {
logger.error('Failed to get types', payload);
return { ...state, loadingTypesError: true };
}

const types = _.map(_.uniq(payload.sort()), v => ({
key: v,
name: v,
}));
return ({
...state,
loadingTypesError: false,
types,
});
}


/**
* Handles LOOKUP/GET_MANUFACTURERS_INIT action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetManufacturersInit(state) {
return {
...state, manufacturers: [], models: [], oses: [],
};
}

/**
* Handles LOOKUP/GET_MANUFACTURERS_DONE action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetManufacturersDone(state, { payload, error }) {
if (error) {
logger.error('Failed to get types', payload);
return { ...state, loadingManufacturersError: true };
}

const manufacturers = _.map(_.uniq(payload.sort()), v => ({
key: v,
name: v,
}));
return ({
...state,
loadingManufacturersError: false,
manufacturers,
});
}


/**
* Handles LOOKUP/GET_MODELS_INIT action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetModelsInit(state, { payload }) {
// if load more, don't clear state
if (payload.page > 1) {
return { ...state, modelPage: payload.page, isModelsLoading: true };
}
return {
...state, models: [], oses: [], modelPage: payload.page, isModelsLoading: true,
};
}

/**
* Handles LOOKUP/GET_MODELS_DONE action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetModelsDone(state, { payload, error }) {
if (error) {
logger.error('Failed to get types', payload);
return { ...state, loadingModelsError: true, isModelsLoading: false };
}

let models = payload;

if (state.modelPage > 1) {
models = [...state.models, ...models];
}
models = _.orderBy(models, ['model'], ['asc']);
models = _.uniqBy(models, m => m.name);

return ({
...state,
loadingModelsError: false,
models,
hasMoreModels: models.length === PAGE_SIZE,
isModelsLoading: false,
});
}

/**
* Handles LOOKUP/GET_OSES_INIT action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetOsesInit(state, { payload }) {
return {
...state, osPage: payload.page, isOsesLoading: true,
};
}


/**
* Handles LOOKUP/GET_OSES_DONE action.
* @param {Object} state
* @param {Object} action Payload will be JSON from api call
* @return {Object} New state
*/
function onGetOsesDone(state, { payload, error }) {
if (error) {
logger.error('Failed to get types', payload);
return { ...state, loadingOsesError: true, isOsesLoading: false };
}

let oses = payload;
if (state.osPage > 1) {
oses = [...state.oses, ...oses];
}
oses = _.orderBy(payload, ['operatingSystem'], ['asc']);

oses = _.uniqBy(oses, m => m.name);
return ({
...state,
loadingOsesError: false,
oses,
hasMoreOses: oses.length === PAGE_SIZE,
isOsesLoading: false,
});
}


/**
* Handles LOOKUP/GET_SKILL_TAGS_DONE action.
@@ -75,6 +223,14 @@ function onGetAllCountriesDone(state, { payload, error }) {
function create(initialState = {}) {
const a = actions.lookup;
return handleActions({
[a.getTypesInit]: state => state,
[a.getTypesDone]: onGetTypesDone,
[a.getManufacturersInit]: onGetManufacturersInit,
[a.getManufacturersDone]: onGetManufacturersDone,
[a.getModelsInit]: onGetModelsInit,
[a.getModelsDone]: onGetModelsDone,
[a.getOsesInit]: onGetOsesInit,
[a.getOsesDone]: onGetOsesDone,
[a.getSkillTagsInit]: state => state,
[a.getSkillTagsDone]: onGetSkillTagsDone,
[a.getCountriesInit]: state => state,
@@ -85,6 +241,14 @@ function create(initialState = {}) {
skillTags: [],
countries: [],
allCountries: [],
types: [],
manufacturers: [],
models: [],
modelPage: 1,
hasMoreModels: false,
oses: [],
osPage: 1,
hasMoreOses: false,
}));
}

58 changes: 58 additions & 0 deletions src/services/lookup.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
* via API V3.
*/
import qs from 'qs';
import { assign } from 'lodash';
import { getApiResponsePayload } from '../utils/tc';
import { getApi } from './api';

@@ -19,6 +20,63 @@ class LookupService {
};
}

/**
* Gets types
* @return {Promise} Resolves to the types
*/
async getTypes() {
try {
const res = await this.private.apiV5.get('/lookups/devices/types');
return res.json();
} catch (e) {
throw e;
}
}

/**
* Gets Manufacturers.
* @param {String} params type
* @return {Promise} Resolves to the getManufacturers.
*/
async getManufacturers(type) {
const params = {
type,
};

try {
const res = await this.private.apiV5.get(`/lookups/devices/manufacturers?${qs.stringify(params)}`);
return res.json();
} catch (e) {
throw e;
}
}

/**
* Gets Devices
* @param {number} page
* @param {Number} pageSize
* @param {String} type
* @param {String} manufacturer
* @param {String} model
* @return {Promise} Resolves to the Devices.
*/
async getDevices(page = 1, pageSize, type, manufacturer, model) {
const params = {
perPage: pageSize,
};
assign(params, {
type, manufacturer, model, page,
});

try {
const res = await this.private.apiV5.get(`/lookups/devices?${qs.stringify(params)}`);
return res.json();
} catch (e) {
throw e;
}
}


/**
* Gets tags.
* @param {Object} params Parameters