Skip to content
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

Feature/normalizr/add country entity and students that belongs to contry #36

Open
wants to merge 23 commits into
base: feature/normalizer_implementation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion 18 Normalizr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion 18 Normalizr/src/common/actionsEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export const actionsEnums = {
STUDENT_FIELD_VALUE_CHANGED: "STUDENT_FIELD_VALUE_CHANGED",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this action is covered by a thunk function

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"
};
51 changes: 17 additions & 34 deletions 18 Normalizr/src/common/components/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import {ValidationFieldComponent} from './validationFieldComponent';

interface Props {
name: string;
Expand All @@ -10,37 +11,19 @@ interface Props {
error?: string;
}

export class Input extends React.Component<Props, {}> {
constructor(props: Props) {
super(props);
}

public render() {
let wrapperClass: string = "form-group";
if (this.props.error && this.props.error.length > 0) {
wrapperClass += " " + "has-error";
}
return (
<div className={wrapperClass}>
<label htmlFor={this.props.name}>
{this.props.label}
</label>
<div className="field">
<input
type="text"
name={this.props.name}
className="form-control"
placeholder={this.props.placeholder}
ref={this.props.name}
value={this.props.value}
onChange={this.props.onChange}
onBlur={this.props.onBlur}
/>
<div className="input">
{this.props.error}
</div>
</div>
</div>
);
}
}
export const Input = (props: Props) => {
return (
<ValidationFieldComponent
name={props.name} label={props.label} error={props.error}>
<input
type="text"
name={props.name}
className="form-control"
placeholder={props.placeholder}
value={props.value}
onChange={props.onChange}
onBlur={props.onBlur}
/>
</ValidationFieldComponent>
);
};
30 changes: 30 additions & 0 deletions 18 Normalizr/src/common/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as React from "react";
import {ValidationFieldComponent} from './validationFieldComponent';

interface Props {
name: string;
label: string;
onChange: any;
onBlur?: any;
value: any;
error?: string;
option: any;
}

export const Select = (props: Props) => {

return (
<ValidationFieldComponent
name={props.name} label={props.label} error={props.error}>
<select
name={props.name}
className="form-control"
value={props.value}
onChange={props.onChange}
onBlur={props.onBlur} >
{props.option}
</select>
</ValidationFieldComponent>

);
}
7 changes: 7 additions & 0 deletions 18 Normalizr/src/common/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Input} from './Input';
import {Select} from './Select';

export {
Input,
Select
}
34 changes: 34 additions & 0 deletions 18 Normalizr/src/common/components/validationFieldComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as React from "react";

interface Props {
name: string;
label: string;
error?: string;
}

export class ValidationFieldComponent extends React.Component<Props, {}> {
constructor(props: Props) {
super(props);
}

public render() {
let wrapperClass: string = "form-group";
if (this.props.error && this.props.error.length > 0) {
wrapperClass += " " + "has-error";
}
return (
<div className={wrapperClass}>
<label htmlFor={this.props.name}>
{this.props.label}
</label>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This label should not be included in this component

<div className="field">
{this.props.children}

<div className="help-block">
{this.props.error}
</div>
</div>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FieldValidationResult } from "lc-form-validation";
import { validationsEnums } from "../validationsEnums";

// TODO: Harcoded strings and Id's isolate them in a config class
export const requiredValidationHandler = (vm: any, value: any): FieldValidationResult => {
const isFieldInformed: boolean = (value != null && value.length > 0);
const errorInfo: string = (isFieldInformed) ? "" : "Mandatory field";
const errorInfo: string = (isFieldInformed) ? "" : validationsEnums.REQUIRED.FIELD.MESSAGE;

const fieldValidationResult: FieldValidationResult = new FieldValidationResult();
fieldValidationResult.type = "REQUIRED";
fieldValidationResult.type = validationsEnums.REQUIRED.FIELD.TYPE;
fieldValidationResult.succeeded = isFieldInformed;
fieldValidationResult.errorMessage = errorInfo;

Expand Down
14 changes: 14 additions & 0 deletions 18 Normalizr/src/common/validations/requiredId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FieldValidationResult } from "lc-form-validation";
import { validationsEnums } from "../validationsEnums";

export const requiredIdValidationHandler = (vm: any, value: {id: number}): FieldValidationResult => {
const isFieldInformed: boolean = (value != null && value.id && value.id > 0);
const errorInfo: string = (isFieldInformed) ? "" : validationsEnums.REQUIRED.FIELD.MESSAGE;

const fieldValidationResult: FieldValidationResult = new FieldValidationResult();
fieldValidationResult.type = validationsEnums.REQUIRED.FIELD.TYPE;
fieldValidationResult.succeeded = isFieldInformed;
fieldValidationResult.errorMessage = errorInfo;

return fieldValidationResult;
};
6 changes: 6 additions & 0 deletions 18 Normalizr/src/common/validationsEnums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export const validationsEnums = {
REQUIRED: {
FIELD: {
TYPE: "REQUIRED_FIELD",
MESSAGE: "Mandatory field",
}
},
EMAIL: {
NOT_VALID: {
TYPE: "EMAIL_NOT_VALID",
Expand Down
7 changes: 5 additions & 2 deletions 18 Normalizr/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions 18 Normalizr/src/model/api/country.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class Country {
id: number;
name: string;

constructor() {
this.id = -1;
this.name = "";
}
}
2 changes: 2 additions & 0 deletions 18 Normalizr/src/model/api/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 smells bad to me. Why not null or undefined?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think null can be a good value, on the other hand is it a good idea to define this as a class or should it be an interface? Should we add a factory to initialize a new instance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are using this value because Uncontrolled input warning (If you initialize input value to null, it means that is uncontrolled component) But I think that this initialization has to be component liability and it doesn't delegate to class constructor used in reducer default value state. What do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, if it's a component problem, let the component to deal with it.
On the other hand, I would use an interface too. I avoid factory methods unless you need to share some initialization logic that I don't see on this case.

}
}

Expand Down
19 changes: 19 additions & 0 deletions 18 Normalizr/src/model/mappers/countryMapper.ts
Original file line number Diff line number Diff line change
@@ -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
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a new object? Is not just an interface? In TS, any Country is a CountryView because of structural typing, so just use the original object, even when it has extra props, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, we can return original object and in the future, map additional props or some format

}

mapCountryListToCountryViewList(countrys: Country[]): CountryView[] {
return countrys.map((country) => {
return this.mapCountryToCountryView(country)
});
}
}

export const countryMapper = new CountryMapper();
6 changes: 5 additions & 1 deletion 18 Normalizr/src/model/mappers/studentMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class StudentMapper {
id: student.id,
gotActiveTraining: student.gotActiveTraining,
fullname: student.fullname,
email: student.email
email: student.email,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make trailing comma a convention to avoid this in PRs. It can be set up in TSLint.

country: {
id: student.countryId,
name: ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong (before looking more code).

}
}
}

Expand Down
9 changes: 9 additions & 0 deletions 18 Normalizr/src/model/view/countryView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class CountryView {
id: number;
name: string;

constructor() {
this.id = -1;
this.name = "";
}
}
1 change: 1 addition & 0 deletions 18 Normalizr/src/model/view/studentErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { FieldValidationResult } from "lc-form-validation";
export class StudentErrors {
fullname: FieldValidationResult;
email: FieldValidationResult;
country: FieldValidationResult;
}
4 changes: 4 additions & 0 deletions 18 Normalizr/src/model/view/studentView.ts
Original file line number Diff line number Diff line change
@@ -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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems wrong. Either leave country undefined, or use a CountryView.Empty like with the Null Object pattern. Otherwise it seems you're creating a brand new country for each student.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, or we could assign a default country entry. Thinking now about pros adding a factory to create then StudentView

}
}
2 changes: 1 addition & 1 deletion 18 Normalizr/src/pages/login/login.validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FieldValidationResult, BaseFormValidation } from "lc-form-validation";
import { requiredValidationHandler } from "../../common/validations/validators";
import { requiredValidationHandler } from "../../common/validations/required";
import { emailValidationHandler } from "../../common/validations/email";

class LoginFormValidation extends BaseFormValidation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
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'

export const getStudentRequestStartAction = (studentId: number) => {
return function(dispatcher) {
const promise = studentApi.getStudentById(studentId);

promise.then(
data => {
console.log(
'normalized response',
normalize(data, studentSchema)
);

dispatcher(getStudentRequestCompletedAction(data));
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { studentFieldValueChangedCompleted } from "./studentFieldValueChangedCompleted";
import { FieldValidationResult } from "lc-form-validation";
import { loginFormValidation} from "../../login/login.validation";
import { studentFormValidation} from "../student.validation";

export function studentFieldValueChangedStart(viewModel: any, fieldName: string, value: any, event?: any) {

return (dispatcher) => {
loginFormValidation.validateField(viewModel, fieldName, value, event).then(
studentFormValidation.validateField(viewModel, fieldName, value, event).then(
(fieldValidationResult: FieldValidationResult) => dispatcher(studentFieldValueChangedCompleted(fieldName, value, fieldValidationResult ))
);
};
Expand Down
Loading