Created
May 28, 2018 21:11
-
-
Save tghelere/ea19f157c6aeecc735b8e806abda32b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<b-form @submit.prevent="onSubmit" class="m50"> | |
<b-row> | |
<b-col md="4"> | |
<b-form-group> | |
<b-form-select @input="getCities(form.state.id)" id="states" required v-model="form.state" :options="states"> | |
<template slot="first"> | |
<option :value="null" disabled>UF *</option> | |
</template> | |
</b-form-select> | |
</b-form-group> | |
</b-col> | |
<b-col md="8"> | |
<b-form-group> | |
<b-form-select id="cities" :disabled="form.state == null" required v-model="form.city" :options="cities"> | |
<template slot="first"> | |
<option :value="null" disabled>Cidade *</option> | |
</template> | |
</b-form-select> | |
</b-form-group> | |
</b-col> | |
</b-row> | |
<b-form-group> | |
<b-form-textarea id="message" required v-model="form.message" placeholder="Mensagem *" :rows="5" :max-rows="6"></b-form-textarea> | |
</b-form-group> | |
<b-row> | |
<b-col md="12"> | |
<b-button class="float-right mt-5" type="submit" variant="primary">Enviar</b-button> | |
</b-col> | |
</b-row> | |
</b-form> | |
</div> | |
</template> | |
<script> | |
import BootstrapVue from "bootstrap-vue" | |
import "bootstrap-vue/dist/bootstrap-vue.css" | |
Vue.use(BootstrapVue) | |
export default { | |
data() { | |
return { | |
states: [], | |
cities: [], | |
form: { | |
state: null, | |
city: null, | |
message: "" | |
} | |
} | |
}, | |
created() { | |
this.getStates() | |
}, | |
methods: { | |
onSubmit() { | |
const action = "/api/ombud/" | |
axios | |
.post(action, this.form) | |
.then(response => { | |
this.resetForm() | |
}) | |
.catch(error => { | |
console.error(error.message) | |
}) | |
}, | |
getStates() { | |
const action = "/api/states" | |
axios | |
.get(action) | |
.then(response => { | |
this.states = response.data.data.map(state => ({ | |
value: { id: state.id, name: state.name }, | |
text: state.abbr | |
})) | |
}) | |
.catch(error => { | |
console.error(error) | |
}) | |
}, | |
getCities(id) { | |
const action = "/api/cities/state/" + id | |
axios | |
.get(action) | |
.then(response => { | |
this.cities = [ | |
...response.data.data.map(city => ({ | |
value: { id: city.id, name: city.name }, | |
text: city.name | |
})) | |
] | |
}) | |
.catch(error => { | |
console.error(error) | |
}) | |
}, | |
resetForm() { | |
this.form = { | |
state: null, | |
city: null, | |
message: "" | |
} | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment