Skip to content

Commit

Permalink
Update Correios API integration
Browse files Browse the repository at this point in the history
Related to issue #4

Signed-off-by: antoniocoj <[email protected]>
Signed-off-by: Lucas Amoêdo <[email protected]>
  • Loading branch information
LucasAmoedo committed Apr 28, 2017
1 parent 213ff5f commit 66d9815
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 59 deletions.
18 changes: 16 additions & 2 deletions src/Categories/Owners/OwnerCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ export class OwnerCreate extends Component {
'district',
data.estado,
),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'addressNumber',
'',
),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'complement',
'',
),
);
},
400: () => { },
Expand All @@ -87,7 +101,7 @@ export class OwnerCreate extends Component {
}
render() {
return (
<Create {...this.props}>
<Create title={'Cadastrar Proprietário'} {...this.props}>
<SimpleForm ref={(form) => { this.form = form; }}>
<TextInput
source="cpf"
Expand Down Expand Up @@ -119,7 +133,7 @@ export class OwnerCreate extends Component {
label="Número"
/>
<TextInput
source="Complement"
source="complement"
label="Complemento"
/>
<TextInput
Expand Down
182 changes: 126 additions & 56 deletions src/Categories/Owners/OwnerEdit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react';
import React, { Component } from 'react';
import $ from 'jquery';
import { change } from 'redux-form';
import {
SelectInput, SimpleForm, TextInput, DisabledInput, Edit,
} from 'admin-on-rest/lib/mui';
Expand Down Expand Up @@ -33,58 +35,126 @@ const federalStates = [
{ id: 'TO', name: 'Tocantins (TO)' },
];

export const OwnerEdit = props => (
<Edit title={`Proprietário ${props.record.ownerName}`} {...props}>
<SimpleForm>
<DisabledInput
source="id"
label="ID"
/>
<DisabledInput
source="cpf"
label="CPF"
/>
<TextInput
source="ownerName"
label="Primeiro Nome"
/>
<TextInput
source="ownerLastName"
label="Sobrenome"
/>
<TextInput
source="phoneNumber"
label="Telefone"
/>
<TextInput
source="zipCode"
label="Código Postal"
/>
<TextInput
source="publicPlace"
label="Endereço"
/>
<TextInput
source="addressNumber"
label="Número"
/>
<TextInput
source="complement"
label="Complemento"
/>
<TextInput
source="neighborhood"
label="Bairro"
/>
<TextInput
source="city"
label="Cidade"
/>
<SelectInput
source="district"
label="Estado"
choices={federalStates}
/>
</SimpleForm>
</Edit>
);
export class OwnerEdit extends Component {
constructor(props) {
super(props);
this.handleZipInput = this.handleZipInput.bind(this);
}
handleZipInput = (event) => {
const zipCode = event.target.value;
if (zipCode.length === 8) {
$.ajax({
url: `http://correiosapi.apphb.com/cep/${zipCode}`,
dataType: 'jsonp',
crossDomain: true,
contentType: 'application/json',
statusCode: {
200: (data) => {
// console.log(data);
this.form.dispatchProps.dispatch(
change(
'record-form',
'publicPlace',
`${data.tipoDeLogradouro} ${data.logradouro}`),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'neighborhood',
data.bairro,
),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'city',
data.cidade,
),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'district',
data.estado,
),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'addressNumber',
'',
),
);
this.form.dispatchProps.dispatch(
change(
'record-form',
'complement',
'',
),
);
},
400: () => { },
404: () => { },
},
});
}
}
render() {
return(
<Edit title={'Editar Proprietário'} {...this.props}>
<SimpleForm ref={(form) => {this.form = form }}>
<DisabledInput
source="id"
label="ID"
/>
<DisabledInput
source="cpf"
label="CPF"
/>
<TextInput
source="ownerName"
label="Primeiro Nome"
/>
<TextInput
source="ownerLastName"
label="Sobrenome"
/>
<TextInput
source="phoneNumber"
label="Telefone"
/>
<TextInput
source="zipCode"
label="Código Postal"
onChange={this.handleZipInput}
/>
<TextInput
source="publicPlace"
label="Endereço"
/>
<TextInput
source="addressNumber"
label="Número"
/>
<TextInput
source="complement"
label="Complemento"
/>
<TextInput
source="neighborhood"
label="Bairro"
/>
<TextInput
source="city"
label="Cidade"
/>
<SelectInput
source="district"
label="Estado"
choices={federalStates}
/>
</SimpleForm>
</Edit>
);
}
}
2 changes: 1 addition & 1 deletion src/Categories/Owners/OwnerShow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from 'admin-on-rest/lib/mui';

export const OwnerShow = props => (
<Show title={`Proprietário ${props.record.ownerName}`} {...props}>
<Show title={'Cadastro do Proprietário'} {...props}>
<SimpleShowLayout>
<TextField
source="id"
Expand Down

0 comments on commit 66d9815

Please sign in to comment.