diff --git a/18 Normalizr/src/common/actionsEnums.ts b/18 Normalizr/src/common/actionsEnums.ts index 7aa8180..b08dfd3 100644 --- a/18 Normalizr/src/common/actionsEnums.ts +++ b/18 Normalizr/src/common/actionsEnums.ts @@ -1,8 +1,8 @@ export const actionsEnums = { USERPROFILE_UPDATE_EDITING_LOGIN: "USERPROFILE_UPDATE_EDITING_LOGIN", USERPROFILE_PERFORM_LOGIN: "USERPROFILE_PERFORM_LOGIN", - STUDENTS_GET_LIST_REQUEST_COMPLETED: "STUDENTS_GET_LIST_REQUEST_COMPLETED", - STUDENT_GET_STUDENT_REQUEST_COMPLETED: "STUDENT_GET_STUDENT_REQUEST_COMPLETED", + FETCH_STUDENT_LIST_REQUEST_COMPLETED: "FETCH_STUDENT_LIST_REQUEST_COMPLETED", + FETCH_STUDENT_REQUEST_COMPLETED: "FETCH_STUDENT_REQUEST_COMPLETED", STUDENT_FIELD_VALUE_CHANGED_COMPLETED: "STUDENT_FIELD_VALUE_CHANGED_COMPLETED", STUDENT_SAVE_COMPLETED: "STUDENT_SAVE_COMPLETED", RESET_EDITING_STUDENT: "RESET_EDITING_STUDENT", diff --git a/18 Normalizr/src/pages/login/loginContainer.tsx b/18 Normalizr/src/pages/login/loginContainer.tsx index 9ec0310..c1e82ba 100644 --- a/18 Normalizr/src/pages/login/loginContainer.tsx +++ b/18 Normalizr/src/pages/login/loginContainer.tsx @@ -7,7 +7,7 @@ import {State} from '../../reducers'; const mapStateToProps = (state: State) => { return { - loginInfo: state.sessionReducer.editingLogin + loginInfo: state.logic.session.editingLogin } } diff --git a/18 Normalizr/src/pages/student-detail/actions/getStudentRequestCompleted.ts b/18 Normalizr/src/pages/student-detail/actions/getStudentRequestCompleted.ts index 12eefc6..a749e8d 100644 --- a/18 Normalizr/src/pages/student-detail/actions/getStudentRequestCompleted.ts +++ b/18 Normalizr/src/pages/student-detail/actions/getStudentRequestCompleted.ts @@ -3,7 +3,7 @@ import { StudentView } from "../../../model/view/studentView"; export const getStudentRequestCompletedAction = (student: StudentView) => { return { - type: actionsEnums.STUDENT_GET_STUDENT_REQUEST_COMPLETED, + type: actionsEnums.FETCH_STUDENT_REQUEST_COMPLETED, payload: student, }; }; diff --git a/18 Normalizr/src/pages/student-detail/studentDetailContainer.tsx b/18 Normalizr/src/pages/student-detail/studentDetailContainer.tsx index 94aa55d..8161ba0 100644 --- a/18 Normalizr/src/pages/student-detail/studentDetailContainer.tsx +++ b/18 Normalizr/src/pages/student-detail/studentDetailContainer.tsx @@ -5,15 +5,15 @@ import { studentFieldValueChangedStart } from "./actions/studentFieldValueChange import { studentSaveRequestStart } from "./actions/studentSaveRequestStart"; import { resetStudentAction } from './actions/resetStudent'; import { StudentView } from "../../model/view/studentView"; -import { getCountries } from '../../reducers/domain/country/selectors'; +import { getCountries } from '../../reducers/domain/selectors'; import {addCountryAction} from '../student-detail/actions/addCountry'; import {CountryView} from '../../model/view/countryView'; import {State} from '../../reducers'; const mapStateToProps = (state: State) => { return { - student: state.studentDomain.edit.editingStudent, - errors: state.studentDomain.edit.editingStudentErrors, + student: state.logic.student.edit.editingStudent, + errors: state.logic.student.edit.editingStudentErrors, countries: getCountries(state) }; }; diff --git a/18 Normalizr/src/pages/student-list/actions/studentListRequestCompleted.ts b/18 Normalizr/src/pages/student-list/actions/studentListRequestCompleted.ts index 5ff2209..6ef3c40 100644 --- a/18 Normalizr/src/pages/student-list/actions/studentListRequestCompleted.ts +++ b/18 Normalizr/src/pages/student-list/actions/studentListRequestCompleted.ts @@ -4,7 +4,7 @@ import { actionsEnums } from "../../../common/actionsEnums"; // dictionary not that easy export const studentListRequestCompletedAction = (studentList: any) => { return { - type: actionsEnums.STUDENTS_GET_LIST_REQUEST_COMPLETED, + type: actionsEnums.FETCH_STUDENT_LIST_REQUEST_COMPLETED, payload: studentList }; }; diff --git a/18 Normalizr/src/pages/student-list/studentListContainer.tsx b/18 Normalizr/src/pages/student-list/studentListContainer.tsx index 1600331..5abd544 100644 --- a/18 Normalizr/src/pages/student-list/studentListContainer.tsx +++ b/18 Normalizr/src/pages/student-list/studentListContainer.tsx @@ -2,7 +2,7 @@ import { connect } from "react-redux"; import { studentListRequestStartedAction } from "./actions/studentListRequestStarted"; import { StudentListComponent } from "./studentList"; import { navigateToEditStudentAction, navigateToNewStudentAction } from "./actions/navigateToEditStudent"; -import { getStudents } from '../../reducers/domain/student/selectors'; +import { getStudents } from '../../reducers/domain/selectors'; import { fetchCountryListRequestStartedAction } from './actions/fetchCountryList'; import {State} from '../../reducers'; diff --git a/18 Normalizr/src/reducers/domain/byId.ts b/18 Normalizr/src/reducers/domain/byId.ts new file mode 100644 index 0000000..8ba74f3 --- /dev/null +++ b/18 Normalizr/src/reducers/domain/byId.ts @@ -0,0 +1,29 @@ +import { StudentNormalized } from "../../model/normalized/studentNormalized"; +import { CountryNormalized } from '../../model/normalized/countryNormalized'; + +export interface ById { + students: {[id: number] : StudentNormalized}; + countries: {[id: number]: CountryNormalized}; +} + +const initialState = { + students: {}, + countries: {} +}; + +export const byId = (state: ById = initialState, action) => { + if (action.payload && action.payload.entities) { + return { + ...state, + students: { + ...state.students, + ...action.payload.entities.students + }, + countries: { + ...state.countries, + ...action.payload.entities.countries + } + } + } + return state; +} diff --git a/18 Normalizr/src/reducers/domain/country/byId.ts b/18 Normalizr/src/reducers/domain/country/byId.ts deleted file mode 100644 index 5c5d93f..0000000 --- a/18 Normalizr/src/reducers/domain/country/byId.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { CountryNormalized } from '../../../model/normalized/countryNormalized'; - -export const byId = (state: {[id: number]: CountryNormalized} = {}, action) => { - if (action.payload && action.payload.entities) { - return { - ...state, - ...action.payload.entities.countries - } - } - - return state; -} diff --git a/18 Normalizr/src/reducers/domain/country/index.ts b/18 Normalizr/src/reducers/domain/country/index.ts deleted file mode 100644 index f5859b2..0000000 --- a/18 Normalizr/src/reducers/domain/country/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { combineReducers } from 'redux'; -import {byId} from './byId'; -import {allIds} from './list'; -import { CountryNormalized } from '../../../model/normalized/countryNormalized'; - -export interface CountryDomain { - byId: {[id: number]: CountryNormalized}; - allIds: number[]; -} - -export const countryDomain = combineReducers({ - byId, - allIds -}); diff --git a/18 Normalizr/src/reducers/domain/country/list.ts b/18 Normalizr/src/reducers/domain/country/list.ts deleted file mode 100644 index 8f5e173..0000000 --- a/18 Normalizr/src/reducers/domain/country/list.ts +++ /dev/null @@ -1,13 +0,0 @@ -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; - - case actionsEnums.ADD_COUNTRY: - return [...state, action.payload.result]; - } - - return state; -} diff --git a/18 Normalizr/src/reducers/domain/country/selectors/index.ts b/18 Normalizr/src/reducers/domain/country/selectors/index.ts deleted file mode 100644 index 4d57a67..0000000 --- a/18 Normalizr/src/reducers/domain/country/selectors/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -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; - -export const getCountries = createSelector( - countryDomain, - (state: CountryDomain) => state.allIds.map(id => getCountry(state, id)) -); - -export const getCountry = (state: CountryDomain, id: number): CountryView => ({ - ...state.byId[id] -}); diff --git a/18 Normalizr/src/reducers/domain/index.ts b/18 Normalizr/src/reducers/domain/index.ts new file mode 100644 index 0000000..2fba1a2 --- /dev/null +++ b/18 Normalizr/src/reducers/domain/index.ts @@ -0,0 +1,12 @@ +import { combineReducers } from 'redux'; +import {byId, ById} from './byId'; +import {allIds, AllIds} from './list'; + +export interface Domain { + byId: ById; + allIds: AllIds; +} +export const domain = combineReducers({ + byId, + allIds +}); diff --git a/18 Normalizr/src/reducers/domain/list.ts b/18 Normalizr/src/reducers/domain/list.ts new file mode 100644 index 0000000..53a2ada --- /dev/null +++ b/18 Normalizr/src/reducers/domain/list.ts @@ -0,0 +1,47 @@ +import { actionsEnums } from "../../common/actionsEnums"; + +export interface AllIds { + students: number[]; + countries: number[]; +} + +const initialState = { + students: [], + countries: [] +}; + +export const allIds = (state: AllIds = initialState, action) => { + switch (action.type) { + case actionsEnums.FETCH_STUDENT_LIST_REQUEST_COMPLETED: + return handleFetchStudents(state, action.payload.result); + + case actionsEnums.FETCH_COUNTRY_LIST_REQUEST_COMPLETED: + return handleFetchCountries(state, action.payload.result); + + case actionsEnums.ADD_COUNTRY: + return handleAddCountry(state, action.payload.result); + } + + return state; +}; + +const handleFetchStudents = (state: AllIds, ids: number[]) => { + return { + ...state, + students: ids + } +} + +const handleFetchCountries = (state: AllIds, ids: number[]) => { + return { + ...state, + countries: ids + } +} + +const handleAddCountry = (state: AllIds, id: number) => { + return { + ...state, + countries: [...state.countries, id] + } +} diff --git a/18 Normalizr/src/reducers/domain/selectors/country.ts b/18 Normalizr/src/reducers/domain/selectors/country.ts new file mode 100644 index 0000000..9c03fc7 --- /dev/null +++ b/18 Normalizr/src/reducers/domain/selectors/country.ts @@ -0,0 +1,15 @@ +import {createSelector} from 'reselect'; +import {State} from '../../index'; +import {Domain} from '../index'; +import {CountryView} from '../../../model/view/countryView'; + +const domain = (state: State) => state.domain; + +export const getCountries = createSelector( + domain, + (state: Domain) => state.allIds.countries.map(id => getCountry(state, id)) +); + +export const getCountry = (state: Domain, id: number): CountryView => ({ + ...state.byId.countries[id] +}); diff --git a/18 Normalizr/src/reducers/domain/selectors/index.ts b/18 Normalizr/src/reducers/domain/selectors/index.ts new file mode 100644 index 0000000..4a7fcbe --- /dev/null +++ b/18 Normalizr/src/reducers/domain/selectors/index.ts @@ -0,0 +1,7 @@ +import {getStudents} from './student'; +import {getCountries} from './country'; + +export { + getStudents, + getCountries +} diff --git a/18 Normalizr/src/reducers/domain/selectors/student.ts b/18 Normalizr/src/reducers/domain/selectors/student.ts new file mode 100644 index 0000000..9f819bb --- /dev/null +++ b/18 Normalizr/src/reducers/domain/selectors/student.ts @@ -0,0 +1,17 @@ +import {createSelector} from 'reselect'; +import {State} from '../../index'; +import {Domain} from '../index'; +import {StudentView} from '../../../model/view/studentView'; +import {getCountry} from './country'; + +const domain = (state: State) => state.domain; + +export const getStudents = createSelector( + domain, + (domain: Domain) => domain.allIds.students.map(id => getStudent(domain, id)) +); + +const getStudent = (domain: Domain, id: number): StudentView => ({ + ...domain.byId.students[id], + country: getCountry(domain, domain.byId.students[id].country) +}); diff --git a/18 Normalizr/src/reducers/domain/student/byId.ts b/18 Normalizr/src/reducers/domain/student/byId.ts deleted file mode 100644 index b1e8957..0000000 --- a/18 Normalizr/src/reducers/domain/student/byId.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { StudentNormalized } from "../../../model/normalized/studentNormalized"; - -export const byId = (state : {[id: number] : StudentNormalized} = {}, action) => { - if (action.payload && action.payload.entities) { - return { - ...state, - ...action.payload.entities.students - } - } - return state; -} diff --git a/18 Normalizr/src/reducers/domain/student/index.ts b/18 Normalizr/src/reducers/domain/student/index.ts deleted file mode 100644 index da74a67..0000000 --- a/18 Normalizr/src/reducers/domain/student/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { combineReducers } from 'redux'; -import {byId} from './byId'; -import {allIds} from './list'; -import { edit, EditState } from './edit'; -import { StudentNormalized } from '../../../model/normalized/studentNormalized'; - -export interface StudentDomain { - byId: {[id: number] : StudentNormalized}, - allIds: number[]; - edit: EditState; -}; - -export const studentDomain = combineReducers({ - byId, - allIds, - edit -}); diff --git a/18 Normalizr/src/reducers/domain/student/list.ts b/18 Normalizr/src/reducers/domain/student/list.ts deleted file mode 100644 index 7f0d2c3..0000000 --- a/18 Normalizr/src/reducers/domain/student/list.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { actionsEnums } from "../../../common/actionsEnums"; - -export const allIds = (state: Number[] = [], action) => { - switch (action.type) { - case actionsEnums.STUDENTS_GET_LIST_REQUEST_COMPLETED: - return action.payload.result; - } - return state; -}; diff --git a/18 Normalizr/src/reducers/domain/student/selectors/index.ts b/18 Normalizr/src/reducers/domain/student/selectors/index.ts deleted file mode 100644 index 0d711f3..0000000 --- a/18 Normalizr/src/reducers/domain/student/selectors/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -import {createSelector} from 'reselect'; -import {State} from '../../../index'; -import {StudentDomain} from '../index'; -import {StudentView} from '../../../../model/view/studentView'; -import {CountryDomain} from '../../country'; -import {countryDomain, getCountry} from '../../country/selectors'; - -const studentDomain = (state: State) => state.studentDomain; - -export const getStudents = createSelector( - studentDomain, - countryDomain, - (studentDomain: StudentDomain, countryDomain: CountryDomain) => - studentDomain.allIds.map(id => getStudent(studentDomain, countryDomain, id)) -); - -const getStudent = (studentDomain: StudentDomain, countryDomain: CountryDomain, id: number): StudentView => ({ - ...studentDomain.byId[id], - country: getCountry(countryDomain, studentDomain.byId[id].country) -}); diff --git a/18 Normalizr/src/reducers/index.ts b/18 Normalizr/src/reducers/index.ts index c105468..9bfa06c 100644 --- a/18 Normalizr/src/reducers/index.ts +++ b/18 Normalizr/src/reducers/index.ts @@ -1,19 +1,15 @@ import { combineReducers } from 'redux'; import { routerReducer } from 'react-router-redux'; -import { sessionReducer, SessionState } from './logic/session'; -import { studentDomain, StudentDomain } from './domain/student'; -import { StudentView } from '../model/view/studentView'; -import { countryDomain, CountryDomain } from './domain/country'; +import { domain, Domain } from './domain'; +import { logic, Logic } from './logic'; export interface State { - studentDomain: StudentDomain; - countryDomain: CountryDomain; - sessionReducer: SessionState; + domain: Domain; + logic: Logic; routing: any; } export const reducers = combineReducers({ - studentDomain, - countryDomain, - sessionReducer, + domain, + logic, routing: routerReducer, }); diff --git a/18 Normalizr/src/reducers/logic/index.ts b/18 Normalizr/src/reducers/logic/index.ts new file mode 100644 index 0000000..9203f6a --- /dev/null +++ b/18 Normalizr/src/reducers/logic/index.ts @@ -0,0 +1,12 @@ +import { combineReducers } from 'redux'; +import { session, Session } from './session'; +import { student, Student } from './student'; + +export interface Logic { + student: Student; + session: Session +} +export const logic = combineReducers({ + student, + session, +}); diff --git a/18 Normalizr/src/reducers/logic/session.ts b/18 Normalizr/src/reducers/logic/session/index.ts similarity index 58% rename from 18 Normalizr/src/reducers/logic/session.ts rename to 18 Normalizr/src/reducers/logic/session/index.ts index 27937fe..f28b794 100644 --- a/18 Normalizr/src/reducers/logic/session.ts +++ b/18 Normalizr/src/reducers/logic/session/index.ts @@ -1,9 +1,9 @@ -import {actionsEnums} from '../../common/actionsEnums'; -import {UserProfile} from '../../model/view/userProfile'; -import {LoginResponse} from '../../model/view/loginResponse'; -import {LoginEntity} from '../../model/view/login'; +import {actionsEnums} from '../../../common/actionsEnums'; +import {UserProfile} from '../../../model/view/userProfile'; +import {LoginResponse} from '../../../model/view/loginResponse'; +import {LoginEntity} from '../../../model/view/login'; -export class SessionState { +export class Session { isUserLoggedIn : boolean; userProfile : UserProfile; editingLogin : LoginEntity; @@ -16,7 +16,7 @@ export class SessionState { } } -export const sessionReducer = (state : SessionState = new SessionState(), action) => { +export const session = (state : Session = new Session(), action) => { switch (action.type) { case actionsEnums.USERPROFILE_PERFORM_LOGIN: return handlePerformLogin(state, action.payload); @@ -29,7 +29,7 @@ export const sessionReducer = (state : SessionState = new SessionState(), actio }; -const handlePerformLogin = (state : SessionState, payload : LoginResponse) => { +const handlePerformLogin = (state : Session, payload : LoginResponse) => { return { ...state, isUserLoggedIn: payload.succeeded, @@ -37,7 +37,7 @@ const handlePerformLogin = (state : SessionState, payload : LoginResponse) => { }; } -const handleUpdateEditingLogin = (state: SessionState, payload : LoginEntity) => { +const handleUpdateEditingLogin = (state: Session, payload : LoginEntity) => { return { ...state, editingLogin: payload diff --git a/18 Normalizr/src/reducers/domain/student/edit.ts b/18 Normalizr/src/reducers/logic/student/edit.ts similarity index 96% rename from 18 Normalizr/src/reducers/domain/student/edit.ts rename to 18 Normalizr/src/reducers/logic/student/edit.ts index eb1aa86..46288f4 100644 --- a/18 Normalizr/src/reducers/domain/student/edit.ts +++ b/18 Normalizr/src/reducers/logic/student/edit.ts @@ -15,7 +15,7 @@ export class EditState { export const edit = (state : EditState = new EditState(), action) => { switch (action.type) { - case actionsEnums.STUDENT_GET_STUDENT_REQUEST_COMPLETED: + case actionsEnums.FETCH_STUDENT_REQUEST_COMPLETED: return handleGetStudent(state, action.payload); case actionsEnums.STUDENT_FIELD_VALUE_CHANGED_COMPLETED: return handleFieldValueChanged(state, action.payload); diff --git a/18 Normalizr/src/reducers/logic/student/index.ts b/18 Normalizr/src/reducers/logic/student/index.ts new file mode 100644 index 0000000..5b991b7 --- /dev/null +++ b/18 Normalizr/src/reducers/logic/student/index.ts @@ -0,0 +1,10 @@ +import { combineReducers } from 'redux'; +import { edit, EditState } from './edit'; + +export interface Student { + edit: EditState; +}; + +export const student = combineReducers({ + edit +});