From 32c1ca226c7f22f8762fa017d67ac938ebfe85de Mon Sep 17 00:00:00 2001 From: nasdan Date: Thu, 19 Jan 2017 12:57:21 +0100 Subject: [PATCH 1/2] Add student and country Normalized entities --- .../src/model/normalized/countryNormalized.ts | 9 +++++++++ .../src/model/normalized/studentNormalized.ts | 15 +++++++++++++++ 18 Normalizr/src/reducers/domain/country/byId.ts | 4 ++-- 18 Normalizr/src/reducers/domain/country/index.ts | 4 ++-- .../reducers/domain/country/selectors/index.ts | 3 ++- 18 Normalizr/src/reducers/domain/student/byId.ts | 4 ++-- 18 Normalizr/src/reducers/domain/student/index.ts | 4 ++-- .../reducers/domain/student/selectors/index.ts | 2 +- 8 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 18 Normalizr/src/model/normalized/countryNormalized.ts create mode 100644 18 Normalizr/src/model/normalized/studentNormalized.ts diff --git a/18 Normalizr/src/model/normalized/countryNormalized.ts b/18 Normalizr/src/model/normalized/countryNormalized.ts new file mode 100644 index 0000000..b07bf9f --- /dev/null +++ b/18 Normalizr/src/model/normalized/countryNormalized.ts @@ -0,0 +1,9 @@ +export class CountryNormalized { + id: number; + name: string; + + constructor() { + this.id = -1; + this.name = ""; + } +} diff --git a/18 Normalizr/src/model/normalized/studentNormalized.ts b/18 Normalizr/src/model/normalized/studentNormalized.ts new file mode 100644 index 0000000..7fd150a --- /dev/null +++ b/18 Normalizr/src/model/normalized/studentNormalized.ts @@ -0,0 +1,15 @@ +export class StudentNormalized { + id: number; + gotActiveTraining: boolean; + fullname: string; + email: string; + country: number; + + public constructor() { + this.id = -1; + this.gotActiveTraining = false; + this.fullname = ""; + this.email = ""; + this.country = null; + } +} diff --git a/18 Normalizr/src/reducers/domain/country/byId.ts b/18 Normalizr/src/reducers/domain/country/byId.ts index 122ef36..5c5d93f 100644 --- a/18 Normalizr/src/reducers/domain/country/byId.ts +++ b/18 Normalizr/src/reducers/domain/country/byId.ts @@ -1,6 +1,6 @@ -import { CountryView } from '../../../model/view/countryView'; +import { CountryNormalized } from '../../../model/normalized/countryNormalized'; -export const byId = (state: {[id: number]: CountryView} = {}, action) => { +export const byId = (state: {[id: number]: CountryNormalized} = {}, action) => { if (action.payload && action.payload.entities) { return { ...state, diff --git a/18 Normalizr/src/reducers/domain/country/index.ts b/18 Normalizr/src/reducers/domain/country/index.ts index d1d0a89..f5859b2 100644 --- a/18 Normalizr/src/reducers/domain/country/index.ts +++ b/18 Normalizr/src/reducers/domain/country/index.ts @@ -1,10 +1,10 @@ import { combineReducers } from 'redux'; import {byId} from './byId'; import {allIds} from './list'; -import { CountryView } from '../../../model/view/countryView'; +import { CountryNormalized } from '../../../model/normalized/countryNormalized'; export interface CountryDomain { - byId: {[id: number]: CountryView}; + byId: {[id: number]: CountryNormalized}; allIds: number[]; } diff --git a/18 Normalizr/src/reducers/domain/country/selectors/index.ts b/18 Normalizr/src/reducers/domain/country/selectors/index.ts index efbc265..4d57a67 100644 --- a/18 Normalizr/src/reducers/domain/country/selectors/index.ts +++ b/18 Normalizr/src/reducers/domain/country/selectors/index.ts @@ -1,6 +1,7 @@ import {createSelector} from 'reselect'; import {State} from '../../../index'; import {CountryDomain} from '../index'; +import {CountryView} from '../../../../model/view/countryView'; export const countryDomain = (state: State) => state.countryDomain; @@ -9,6 +10,6 @@ export const getCountries = createSelector( (state: CountryDomain) => state.allIds.map(id => getCountry(state, id)) ); -export const getCountry = (state: CountryDomain, id) => ({ +export const getCountry = (state: CountryDomain, id: number): CountryView => ({ ...state.byId[id] }); diff --git a/18 Normalizr/src/reducers/domain/student/byId.ts b/18 Normalizr/src/reducers/domain/student/byId.ts index 2e15ca3..b1e8957 100644 --- a/18 Normalizr/src/reducers/domain/student/byId.ts +++ b/18 Normalizr/src/reducers/domain/student/byId.ts @@ -1,6 +1,6 @@ -import { StudentView } from "../../../model/view/studentView"; +import { StudentNormalized } from "../../../model/normalized/studentNormalized"; -export const byId = (state : {[id: number] : StudentView} = {}, action) => { +export const byId = (state : {[id: number] : StudentNormalized} = {}, action) => { if (action.payload && action.payload.entities) { return { ...state, diff --git a/18 Normalizr/src/reducers/domain/student/index.ts b/18 Normalizr/src/reducers/domain/student/index.ts index dbedc3e..da74a67 100644 --- a/18 Normalizr/src/reducers/domain/student/index.ts +++ b/18 Normalizr/src/reducers/domain/student/index.ts @@ -2,10 +2,10 @@ import { combineReducers } from 'redux'; import {byId} from './byId'; import {allIds} from './list'; import { edit, EditState } from './edit'; -import { StudentView } from '../../../model/view/studentView'; +import { StudentNormalized } from '../../../model/normalized/studentNormalized'; export interface StudentDomain { - byId: {[id: number] : StudentView}, + byId: {[id: number] : StudentNormalized}, allIds: number[]; edit: EditState; }; diff --git a/18 Normalizr/src/reducers/domain/student/selectors/index.ts b/18 Normalizr/src/reducers/domain/student/selectors/index.ts index a7a3329..0d711f3 100644 --- a/18 Normalizr/src/reducers/domain/student/selectors/index.ts +++ b/18 Normalizr/src/reducers/domain/student/selectors/index.ts @@ -14,7 +14,7 @@ export const getStudents = createSelector( studentDomain.allIds.map(id => getStudent(studentDomain, countryDomain, id)) ); -const getStudent = (studentDomain: StudentDomain, countryDomain: CountryDomain, id): StudentView => ({ +const getStudent = (studentDomain: StudentDomain, countryDomain: CountryDomain, id: number): StudentView => ({ ...studentDomain.byId[id], country: getCountry(countryDomain, studentDomain.byId[id].country) }); From c9cc07f597a1c54e2f64d5b8230c55585f51bbae Mon Sep 17 00:00:00 2001 From: nasdan Date: Thu, 19 Jan 2017 12:59:14 +0100 Subject: [PATCH 2/2] Refactor normalized entities to interfaces --- 18 Normalizr/src/model/normalized/countryNormalized.ts | 7 +------ 18 Normalizr/src/model/normalized/studentNormalized.ts | 10 +--------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/18 Normalizr/src/model/normalized/countryNormalized.ts b/18 Normalizr/src/model/normalized/countryNormalized.ts index b07bf9f..fc87532 100644 --- a/18 Normalizr/src/model/normalized/countryNormalized.ts +++ b/18 Normalizr/src/model/normalized/countryNormalized.ts @@ -1,9 +1,4 @@ -export class CountryNormalized { +export interface CountryNormalized { id: number; name: string; - - constructor() { - this.id = -1; - this.name = ""; - } } diff --git a/18 Normalizr/src/model/normalized/studentNormalized.ts b/18 Normalizr/src/model/normalized/studentNormalized.ts index 7fd150a..0f9de98 100644 --- a/18 Normalizr/src/model/normalized/studentNormalized.ts +++ b/18 Normalizr/src/model/normalized/studentNormalized.ts @@ -1,15 +1,7 @@ -export class StudentNormalized { +export interface StudentNormalized { id: number; gotActiveTraining: boolean; fullname: string; email: string; country: number; - - public constructor() { - this.id = -1; - this.gotActiveTraining = false; - this.fullname = ""; - this.email = ""; - this.country = null; - } }