From ee76103c062a9404405fadbc068fa812898e2635 Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 16:47:44 +0100 Subject: [PATCH 01/23] Add redux dev tools --- 18 Normalizr/src/main.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/18 Normalizr/src/main.tsx b/18 Normalizr/src/main.tsx index 4069e1a..84e5a4f 100644 --- a/18 Normalizr/src/main.tsx +++ b/18 Normalizr/src/main.tsx @@ -2,7 +2,7 @@ import * as React from "react"; import * as ReactDOM from "react-dom"; import { Router, Route, IndexRoute, hashHistory } from "react-router"; import { syncHistoryWithStore} from "react-router-redux"; -import { createStore, applyMiddleware } from "redux"; +import { createStore, applyMiddleware, compose } from "redux"; import { Provider } from "react-redux"; import { reducers } from "./reducers"; import { App } from "./app"; @@ -13,7 +13,10 @@ import reduxThunk from "redux-thunk"; let store = createStore( reducers, - applyMiddleware(reduxThunk), + compose( + applyMiddleware(reduxThunk), + window['devToolsExtension'] ? window['devToolsExtension']() : f => f + ) ); const history = syncHistoryWithStore(hashHistory, store); From 78527b1e0c8ad1d24d300f9c1f6727e4fcfe1536 Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 17:04:39 +0100 Subject: [PATCH 02/23] Add country and countryView --- 18 Normalizr/src/model/api/country.ts | 9 +++++++++ 18 Normalizr/src/model/api/student.ts | 2 ++ .../src/model/mappers/countryMapper.ts | 19 +++++++++++++++++++ .../src/model/mappers/studentMapper.ts | 3 ++- 18 Normalizr/src/model/view/countryView.ts | 9 +++++++++ 18 Normalizr/src/model/view/studentView.ts | 4 ++++ 18 Normalizr/src/rest-api/mock-data.ts | 8 ++++---- 18 Normalizr/src/rest-api/student-api.ts | 3 ++- 8 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 18 Normalizr/src/model/api/country.ts create mode 100644 18 Normalizr/src/model/mappers/countryMapper.ts create mode 100644 18 Normalizr/src/model/view/countryView.ts diff --git a/18 Normalizr/src/model/api/country.ts b/18 Normalizr/src/model/api/country.ts new file mode 100644 index 0000000..3268dbb --- /dev/null +++ b/18 Normalizr/src/model/api/country.ts @@ -0,0 +1,9 @@ +export class Country { + id: number; + name: string; + + constructor() { + this.id = -1; + this.name = ""; + } +} diff --git a/18 Normalizr/src/model/api/student.ts b/18 Normalizr/src/model/api/student.ts index ff99025..0208db3 100644 --- a/18 Normalizr/src/model/api/student.ts +++ b/18 Normalizr/src/model/api/student.ts @@ -3,12 +3,14 @@ export class Student { gotActiveTraining: boolean; fullname: string; email: string; + countryId: number; public constructor() { this.id = -1; this.gotActiveTraining = false; this.fullname = ""; this.email = ""; + this.countryId = -1; } } diff --git a/18 Normalizr/src/model/mappers/countryMapper.ts b/18 Normalizr/src/model/mappers/countryMapper.ts new file mode 100644 index 0000000..d715f17 --- /dev/null +++ b/18 Normalizr/src/model/mappers/countryMapper.ts @@ -0,0 +1,19 @@ +import { Country } from '../api/country'; +import { CountryView } from '../view/countryView'; + +class CountryMapper { + mapCountryToCountryView(country: Country): CountryView { + return { + id: country.id, + name: country.name + } + } + + mapCountryListToCountryViewList(countrys: Country[]): CountryView[] { + return countrys.map((country) => { + return this.mapCountryToCountryView(country) + }); + } +} + +export const countryMapper = new CountryMapper(); diff --git a/18 Normalizr/src/model/mappers/studentMapper.ts b/18 Normalizr/src/model/mappers/studentMapper.ts index 1e8226b..894dd56 100644 --- a/18 Normalizr/src/model/mappers/studentMapper.ts +++ b/18 Normalizr/src/model/mappers/studentMapper.ts @@ -7,7 +7,8 @@ class StudentMapper { id: student.id, gotActiveTraining: student.gotActiveTraining, fullname: student.fullname, - email: student.email + email: student.email, + country: null } } diff --git a/18 Normalizr/src/model/view/countryView.ts b/18 Normalizr/src/model/view/countryView.ts new file mode 100644 index 0000000..53e3784 --- /dev/null +++ b/18 Normalizr/src/model/view/countryView.ts @@ -0,0 +1,9 @@ +export class CountryView { + id: number; + name: string; + + constructor() { + this.id = -1; + this.name = ""; + } +} diff --git a/18 Normalizr/src/model/view/studentView.ts b/18 Normalizr/src/model/view/studentView.ts index 426d1b4..212b1e7 100644 --- a/18 Normalizr/src/model/view/studentView.ts +++ b/18 Normalizr/src/model/view/studentView.ts @@ -1,13 +1,17 @@ +import { CountryView } from './countryView'; + export class StudentView { id: number; gotActiveTraining: boolean; fullname: string; email: string; + country: CountryView; public constructor() { this.id = -1; this.gotActiveTraining = false; this.fullname = ""; this.email = ""; + this.country = new CountryView(); } } diff --git a/18 Normalizr/src/rest-api/mock-data.ts b/18 Normalizr/src/rest-api/mock-data.ts index 6f29571..4510596 100644 --- a/18 Normalizr/src/rest-api/mock-data.ts +++ b/18 Normalizr/src/rest-api/mock-data.ts @@ -1,8 +1,8 @@ import { Student } from "../model/api/student"; export const studentsMockData: Student[] = [ - {id: 1, gotActiveTraining: true, fullname: "John Doe", email: "test@fakeemail.com"}, - {id: 2, gotActiveTraining: false, fullname: "Mike Huff", email: "mark@fakeemail.com"}, - {id: 3, gotActiveTraining: true, fullname: "Harry Poe", email: "harry@fakeemail.com"}, - {id: 4, gotActiveTraining: true, fullname: "Mary Joe", email: "mary@fakeemail.com"} + {id: 1, gotActiveTraining: true, fullname: "John Doe", email: "test@fakeemail.com", countryId: 1}, + {id: 2, gotActiveTraining: false, fullname: "Mike Huff", email: "mark@fakeemail.com", countryId: 2}, + {id: 3, gotActiveTraining: true, fullname: "Harry Poe", email: "harry@fakeemail.com", countryId: 0 }, + {id: 4, gotActiveTraining: true, fullname: "Mary Joe", email: "mary@fakeemail.com", countryId: 0} ]; diff --git a/18 Normalizr/src/rest-api/student-api.ts b/18 Normalizr/src/rest-api/student-api.ts index 5d88cdf..f96df0a 100644 --- a/18 Normalizr/src/rest-api/student-api.ts +++ b/18 Normalizr/src/rest-api/student-api.ts @@ -30,7 +30,8 @@ class StudentApi { id: studentView.id, gotActiveTraining: studentView.gotActiveTraining, fullname: studentView.fullname, - email: studentView.email + email: studentView.email, + countryId: 0 }; if (student.id > 0) { From 79abc3c536ca6e598e1edfdc5ecbcfb11305a6d6 Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 17:12:32 +0100 Subject: [PATCH 03/23] Implement country api --- 18 Normalizr/src/rest-api/country-api.ts | 21 +++++++++++++++++++++ 18 Normalizr/src/rest-api/mock-data.ts | 10 +++++++++- 18 Normalizr/src/rest-api/student-api.ts | 7 ++++--- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 18 Normalizr/src/rest-api/country-api.ts diff --git a/18 Normalizr/src/rest-api/country-api.ts b/18 Normalizr/src/rest-api/country-api.ts new file mode 100644 index 0000000..ba21c02 --- /dev/null +++ b/18 Normalizr/src/rest-api/country-api.ts @@ -0,0 +1,21 @@ +import { Country } from '../model/api/country'; +import { CountryView } from '../model/view/countryView'; +import { countriesMockData } from './mock-data'; +import { countryMapper } from '../model/mappers/countryMapper'; + +class CountryApi { + countriesData: Country[]; + + constructor() { + this.countriesData = countriesMockData; + } + + fetchCountries(): Promise { + const countryViewList = countryMapper + .mapCountryListToCountryViewList(this.countriesData); + + return Promise.resolve(countryViewList); + } +} + +export const countryApi = new CountryApi(); diff --git a/18 Normalizr/src/rest-api/mock-data.ts b/18 Normalizr/src/rest-api/mock-data.ts index 4510596..54e6e5e 100644 --- a/18 Normalizr/src/rest-api/mock-data.ts +++ b/18 Normalizr/src/rest-api/mock-data.ts @@ -1,4 +1,5 @@ -import { Student } from "../model/api/student"; +import { Student } from '../model/api/student'; +import { Country } from '../model/api/country'; export const studentsMockData: Student[] = [ {id: 1, gotActiveTraining: true, fullname: "John Doe", email: "test@fakeemail.com", countryId: 1}, @@ -6,3 +7,10 @@ export const studentsMockData: Student[] = [ {id: 3, gotActiveTraining: true, fullname: "Harry Poe", email: "harry@fakeemail.com", countryId: 0 }, {id: 4, gotActiveTraining: true, fullname: "Mary Joe", email: "mary@fakeemail.com", countryId: 0} ]; + +export const countriesMockData: Country[] = [ + { id: 1, name: "Spain" }, + { id: 2, name: "Italy" }, + { id: 3, name: "Germany" }, + { id: 4, name: "France" } +] diff --git a/18 Normalizr/src/rest-api/student-api.ts b/18 Normalizr/src/rest-api/student-api.ts index f96df0a..7fe412f 100644 --- a/18 Normalizr/src/rest-api/student-api.ts +++ b/18 Normalizr/src/rest-api/student-api.ts @@ -13,9 +13,10 @@ class StudentApi { } loadStudentList(): Promise { - const studentView = studentMapper.mapStudentListToStudentViewList(this.studentsData); + const studentViewList = studentMapper + .mapStudentListToStudentViewList(this.studentsData); - return Promise.resolve(studentView); + return Promise.resolve(studentViewList); } getStudentById(id: number): Promise { @@ -31,7 +32,7 @@ class StudentApi { gotActiveTraining: studentView.gotActiveTraining, fullname: studentView.fullname, email: studentView.email, - countryId: 0 + countryId: studentView.country.id }; if (student.id > 0) { From ae14bf59c90f8a67ea3058ff5384d9dbda6f8043 Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 18:02:48 +0100 Subject: [PATCH 04/23] Add country, byId and allids reducers --- 18 Normalizr/src/common/actionsEnums.ts | 3 ++- .../actions/getStudentRequestStart.ts | 4 ++-- .../student-list/actions/fetchCountryList.ts | 22 +++++++++++++++++++ .../actions/studentListRequestStarted.ts | 4 ++-- .../student-list/studentListContainer.tsx | 2 +- .../src/reducers/domain/country/byId.ts | 14 ++++++++++++ .../src/reducers/domain/country/index.ts | 14 ++++++++++++ .../src/reducers/domain/country/list.ts | 12 ++++++++++ 18 Normalizr/src/reducers/index.ts | 14 +++++++----- 18 Normalizr/src/schemas/countrySchema.ts | 4 ++++ .../schema.ts => schemas/studentSchema.ts} | 2 +- 11 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts create mode 100644 18 Normalizr/src/reducers/domain/country/byId.ts create mode 100644 18 Normalizr/src/reducers/domain/country/index.ts create mode 100644 18 Normalizr/src/reducers/domain/country/list.ts create mode 100644 18 Normalizr/src/schemas/countrySchema.ts rename 18 Normalizr/src/{schema/schema.ts => schemas/studentSchema.ts} (72%) diff --git a/18 Normalizr/src/common/actionsEnums.ts b/18 Normalizr/src/common/actionsEnums.ts index 145aa5c..a791210 100644 --- a/18 Normalizr/src/common/actionsEnums.ts +++ b/18 Normalizr/src/common/actionsEnums.ts @@ -6,5 +6,6 @@ export const actionsEnums = { STUDENT_FIELD_VALUE_CHANGED: "STUDENT_FIELD_VALUE_CHANGED", STUDENT_FIELD_VALUE_CHANGED_COMPLETED: "STUDENT_FIELD_VALUE_CHANGED_COMPLETED", STUDENT_SAVE_COMPLETED: "STUDENT_SAVE_COMPLETED", - RESET_EDITING_STUDENT: "RESET_EDITING_STUDENT" + RESET_EDITING_STUDENT: "RESET_EDITING_STUDENT", + FETCH_COUNTRY_LIST_REQUEST_COMPLETED: "FETCH_COUNTRY_LIST_REQUEST_COMPLETED" }; diff --git a/18 Normalizr/src/pages/student-detail/actions/getStudentRequestStart.ts b/18 Normalizr/src/pages/student-detail/actions/getStudentRequestStart.ts index d3175ce..69201b2 100644 --- a/18 Normalizr/src/pages/student-detail/actions/getStudentRequestStart.ts +++ b/18 Normalizr/src/pages/student-detail/actions/getStudentRequestStart.ts @@ -1,8 +1,8 @@ import { actionsEnums } from "../../../common/actionsEnums"; import { studentApi } from "../../../rest-api/student-api"; import { getStudentRequestCompletedAction } from "./getStudentRequestCompleted"; -import { normalize } from 'normalizr' -import { studentSchema } from '../../../schema/schema' +import { normalize } from 'normalizr'; +import { studentSchema } from '../../../schemas/studentSchema'; export const getStudentRequestStartAction = (studentId: number) => { return function(dispatcher) { diff --git a/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts b/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts new file mode 100644 index 0000000..eb8e4a5 --- /dev/null +++ b/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts @@ -0,0 +1,22 @@ +import { normalize, NormalizeOutput } from 'normalizr'; +import { actionsEnums } from '../../../common/actionsEnums'; +import { countryApi } from '../../../rest-api/country-api'; +import {arrayOfCountriesSchema} from '../../../schemas/countrySchema'; + +export const fetchCountryListRequestStartedAction = () => (dispatcher) => { + const promise = countryApi.fetchCountries();dispatcher + + promise.then( + data => { + const normalizedCountries = normalize(data, arrayOfCountriesSchema); + dispatcher(fetchCountryListRequestCompletedAction(normalizedCountries)); + } + ); + + return promise; +} + +export const fetchCountryListRequestCompletedAction = (countries: NormalizeOutput) => ({ + type: actionsEnums.FETCH_COUNTRY_LIST_REQUEST_COMPLETED, + payload: countries +}); diff --git a/18 Normalizr/src/pages/student-list/actions/studentListRequestStarted.ts b/18 Normalizr/src/pages/student-list/actions/studentListRequestStarted.ts index 58955e4..6b78c28 100644 --- a/18 Normalizr/src/pages/student-list/actions/studentListRequestStarted.ts +++ b/18 Normalizr/src/pages/student-list/actions/studentListRequestStarted.ts @@ -1,8 +1,8 @@ import { actionsEnums } from "../../../common/actionsEnums"; import { studentApi } from "../../../rest-api/student-api"; import { studentListRequestCompletedAction } from "./studentListRequestCompleted"; -import { normalize } from 'normalizr' -import { arrayOfStudentsSchema } from '../../../schema/schema' +import { normalize } from 'normalizr'; +import { arrayOfStudentsSchema } from '../../../schemas/studentSchema'; export const studentListRequestStartedAction = () => { return function(dispatcher) { diff --git a/18 Normalizr/src/pages/student-list/studentListContainer.tsx b/18 Normalizr/src/pages/student-list/studentListContainer.tsx index 694efd0..844e725 100644 --- a/18 Normalizr/src/pages/student-list/studentListContainer.tsx +++ b/18 Normalizr/src/pages/student-list/studentListContainer.tsx @@ -6,7 +6,7 @@ import { getStudents } from '../../reducers' const mapStateToProps = (state) => { return { - studentList: getStudents(state), + studentList: getStudents(state) }; }; diff --git a/18 Normalizr/src/reducers/domain/country/byId.ts b/18 Normalizr/src/reducers/domain/country/byId.ts new file mode 100644 index 0000000..c3071c5 --- /dev/null +++ b/18 Normalizr/src/reducers/domain/country/byId.ts @@ -0,0 +1,14 @@ +import { CountryView } from '../../../model/view/countryView'; + +export const byId = (state: {[id: number]: CountryView} = {}, action) => { + if (action.payload && action.payload.entities) { + return { + ...state, + ...action.payload.entities.countries + } + } + + return state; +} + +export const getCountry = (state, id): CountryView => state[id]; diff --git a/18 Normalizr/src/reducers/domain/country/index.ts b/18 Normalizr/src/reducers/domain/country/index.ts new file mode 100644 index 0000000..d9e9e45 --- /dev/null +++ b/18 Normalizr/src/reducers/domain/country/index.ts @@ -0,0 +1,14 @@ +import { combineReducers } from 'redux'; +import { byId, getCountry } from './byId'; +import { allIds, getIds } from './list'; +import { CountryView } from '../../../model/view/countryView'; + +export const countryDomain = combineReducers({ + byId, + allIds +}); + +export const getCountries = (state) : CountryView[] => { + const ids = getIds(state.countryDomain.allIds); + return ids.map(id => getCountry(state.countryDomain.byId, id)); +} diff --git a/18 Normalizr/src/reducers/domain/country/list.ts b/18 Normalizr/src/reducers/domain/country/list.ts new file mode 100644 index 0000000..91fb218 --- /dev/null +++ b/18 Normalizr/src/reducers/domain/country/list.ts @@ -0,0 +1,12 @@ +import { actionsEnums } from '../../../common/actionsEnums'; + +export const allIds = (state: number[] = [], action) => { + switch (action.type) { + case actionsEnums.FETCH_COUNTRY_LIST_REQUEST_COMPLETED: + return action.payload.result; + } + + return state; +} + +export const getIds = (state: number[]) => state; diff --git a/18 Normalizr/src/reducers/index.ts b/18 Normalizr/src/reducers/index.ts index f2312d6..7f8f26c 100644 --- a/18 Normalizr/src/reducers/index.ts +++ b/18 Normalizr/src/reducers/index.ts @@ -1,13 +1,15 @@ -import { combineReducers } from "redux"; -import { sessionReducer } from "./logic/session"; -import { studentDomain, getStudent, getIds } from "./domain/student"; -import { routerReducer } from "react-router-redux"; -import { StudentView } from '../model/view/studentView' +import { combineReducers } from 'redux'; +import { routerReducer } from 'react-router-redux'; +import { sessionReducer } from './logic/session'; +import { studentDomain, getStudent, getIds } from './domain/student'; +import { StudentView } from '../model/view/studentView'; +import { countryDomain } from './domain/country'; export const reducers = combineReducers({ studentDomain, sessionReducer, - routing: routerReducer + routing: routerReducer, + countryDomain }); export const getStudents = (state) : StudentView[] => { diff --git a/18 Normalizr/src/schemas/countrySchema.ts b/18 Normalizr/src/schemas/countrySchema.ts new file mode 100644 index 0000000..18ad3ae --- /dev/null +++ b/18 Normalizr/src/schemas/countrySchema.ts @@ -0,0 +1,4 @@ +import { Schema, arrayOf} from 'normalizr'; + +export const countrySchema = new Schema('countries'); +export const arrayOfCountriesSchema = arrayOf(countrySchema); diff --git a/18 Normalizr/src/schema/schema.ts b/18 Normalizr/src/schemas/studentSchema.ts similarity index 72% rename from 18 Normalizr/src/schema/schema.ts rename to 18 Normalizr/src/schemas/studentSchema.ts index 44463b7..97f4757 100644 --- a/18 Normalizr/src/schema/schema.ts +++ b/18 Normalizr/src/schemas/studentSchema.ts @@ -1,4 +1,4 @@ -import { Schema, arrayOf} from 'normalizr' +import { Schema, arrayOf} from 'normalizr'; export const studentSchema = new Schema('students'); export const arrayOfStudentsSchema = arrayOf(studentSchema); From 67033b564812220f5843dde992abb7797abacd34 Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 18:05:45 +0100 Subject: [PATCH 05/23] Fetch countries on load table --- 18 Normalizr/src/pages/student-list/studentList.tsx | 4 ++-- .../src/pages/student-list/studentListContainer.tsx | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/18 Normalizr/src/pages/student-list/studentList.tsx b/18 Normalizr/src/pages/student-list/studentList.tsx index db239d1..8894483 100644 --- a/18 Normalizr/src/pages/student-list/studentList.tsx +++ b/18 Normalizr/src/pages/student-list/studentList.tsx @@ -8,13 +8,13 @@ interface Props { getStudentList: () => void; editStudent: (id: number) => void; navigateToAddNewStudent: () => void; + fetchCountryList: () => void; } - export class StudentListComponent extends React.Component { - componentDidMount() { this.props.getStudentList(); + this.props.fetchCountryList(); } render() { diff --git a/18 Normalizr/src/pages/student-list/studentListContainer.tsx b/18 Normalizr/src/pages/student-list/studentListContainer.tsx index 844e725..fe59ab1 100644 --- a/18 Normalizr/src/pages/student-list/studentListContainer.tsx +++ b/18 Normalizr/src/pages/student-list/studentListContainer.tsx @@ -2,7 +2,8 @@ import { connect } from "react-redux"; import { studentListRequestStartedAction } from "./actions/studentListRequestStarted"; import { StudentListComponent } from "./studentList"; import { navigateToEditStudentAction, navigateToNewStudentAction } from "./actions/navigateToEditStudent"; -import { getStudents } from '../../reducers' +import { getStudents } from '../../reducers'; +import { fetchCountryListRequestStartedAction } from './actions/fetchCountryList'; const mapStateToProps = (state) => { return { @@ -14,7 +15,8 @@ const mapDispatchToProps = (dispatch) => { return { getStudentList: () => dispatch(studentListRequestStartedAction()), editStudent: (id: number) => dispatch(navigateToEditStudentAction(id)), - navigateToAddNewStudent: () => dispatch(navigateToNewStudentAction()) + navigateToAddNewStudent: () => dispatch(navigateToNewStudentAction()), + fetchCountryList: () => dispatch(fetchCountryListRequestStartedAction()) }; }; From acd04505d95030c8c4d94f7842ad3868f2d17852 Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 18:26:17 +0100 Subject: [PATCH 06/23] Update normalizr to 3.0.0 version --- 18 Normalizr/package.json | 2 +- .../src/pages/student-list/actions/fetchCountryList.ts | 4 ++-- 18 Normalizr/src/schemas/countrySchema.ts | 6 +++--- 18 Normalizr/src/schemas/studentSchema.ts | 9 ++++++--- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/18 Normalizr/package.json b/18 Normalizr/package.json index d0cd2b6..d557231 100644 --- a/18 Normalizr/package.json +++ b/18 Normalizr/package.json @@ -35,7 +35,7 @@ "bootstrap": "^3.3.7", "jquery": "^3.1.1", "lc-form-validation": "^0.1.7", - "normalizr": "^2.3.1", + "normalizr": "^3.0.0", "react": "^15.3.2", "react-dom": "^15.3.2", "react-redux": "^4.4.5", diff --git a/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts b/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts index eb8e4a5..ca6d297 100644 --- a/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts +++ b/18 Normalizr/src/pages/student-list/actions/fetchCountryList.ts @@ -1,4 +1,4 @@ -import { normalize, NormalizeOutput } from 'normalizr'; +import { normalize } from 'normalizr'; import { actionsEnums } from '../../../common/actionsEnums'; import { countryApi } from '../../../rest-api/country-api'; import {arrayOfCountriesSchema} from '../../../schemas/countrySchema'; @@ -16,7 +16,7 @@ export const fetchCountryListRequestStartedAction = () => (dispatcher) => { return promise; } -export const fetchCountryListRequestCompletedAction = (countries: NormalizeOutput) => ({ +export const fetchCountryListRequestCompletedAction = (countries) => ({ type: actionsEnums.FETCH_COUNTRY_LIST_REQUEST_COMPLETED, payload: countries }); diff --git a/18 Normalizr/src/schemas/countrySchema.ts b/18 Normalizr/src/schemas/countrySchema.ts index 18ad3ae..87c62d7 100644 --- a/18 Normalizr/src/schemas/countrySchema.ts +++ b/18 Normalizr/src/schemas/countrySchema.ts @@ -1,4 +1,4 @@ -import { Schema, arrayOf} from 'normalizr'; +import { schema } from 'normalizr'; -export const countrySchema = new Schema('countries'); -export const arrayOfCountriesSchema = arrayOf(countrySchema); +export const countrySchema = new schema.Entity('countries'); +export const arrayOfCountriesSchema = [countrySchema]; diff --git a/18 Normalizr/src/schemas/studentSchema.ts b/18 Normalizr/src/schemas/studentSchema.ts index 97f4757..465ebd8 100644 --- a/18 Normalizr/src/schemas/studentSchema.ts +++ b/18 Normalizr/src/schemas/studentSchema.ts @@ -1,4 +1,7 @@ -import { Schema, arrayOf} from 'normalizr'; +import { schema } from 'normalizr'; +import { countrySchema } from './countrySchema'; -export const studentSchema = new Schema('students'); -export const arrayOfStudentsSchema = arrayOf(studentSchema); +export const studentSchema = new schema.Entity('students', { + country: countrySchema +}); +export const arrayOfStudentsSchema = [studentSchema]; From 257bf67e6eebc04c7aa20888dd57f91a3497533f Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 18:34:37 +0100 Subject: [PATCH 07/23] Fix country field mapping in studentMapper --- 18 Normalizr/src/model/mappers/studentMapper.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/18 Normalizr/src/model/mappers/studentMapper.ts b/18 Normalizr/src/model/mappers/studentMapper.ts index 894dd56..7da0dbd 100644 --- a/18 Normalizr/src/model/mappers/studentMapper.ts +++ b/18 Normalizr/src/model/mappers/studentMapper.ts @@ -8,7 +8,10 @@ class StudentMapper { gotActiveTraining: student.gotActiveTraining, fullname: student.fullname, email: student.email, - country: null + country: { + id: student.countryId, + name: '' + } } } From 7a8e2cce527f5d48670a868d4f85bb3d6de8992f Mon Sep 17 00:00:00 2001 From: nasdan Date: Tue, 3 Jan 2017 19:23:29 +0100 Subject: [PATCH 08/23] Showing countries in student list --- .../src/pages/student-list/components/studentHeader.tsx | 1 + .../src/pages/student-list/components/studentRow.tsx | 4 ++++ 18 Normalizr/src/reducers/domain/country/byId.ts | 6 +++++- 18 Normalizr/src/reducers/domain/country/index.ts | 2 +- 18 Normalizr/src/reducers/domain/student/index.ts | 8 +++++++- 18 Normalizr/src/reducers/index.ts | 2 +- 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/18 Normalizr/src/pages/student-list/components/studentHeader.tsx b/18 Normalizr/src/pages/student-list/components/studentHeader.tsx index 17fdade..99b67fe 100644 --- a/18 Normalizr/src/pages/student-list/components/studentHeader.tsx +++ b/18 Normalizr/src/pages/student-list/components/studentHeader.tsx @@ -7,6 +7,7 @@ export const StudentHeaderComponent = () => { Active Training Name Email + Country Commands diff --git a/18 Normalizr/src/pages/student-list/components/studentRow.tsx b/18 Normalizr/src/pages/student-list/components/studentRow.tsx index cf6d39d..68e7128 100644 --- a/18 Normalizr/src/pages/student-list/components/studentRow.tsx +++ b/18 Normalizr/src/pages/student-list/components/studentRow.tsx @@ -7,6 +7,7 @@ interface Props { } export const StudentRowComponent = (props: Props) => { + console.log(props.student.country); return ( @@ -24,6 +25,9 @@ export const StudentRowComponent = (props: Props) => { {props.student.email} + + {props.student.country.name} + props.editStudent(props.student.id)}>