2020-05-19 19:43:08 +00:00
|
|
|
<template>
|
|
|
|
<Modal
|
|
|
|
title="Add your child"
|
|
|
|
:isActive="isActive"
|
|
|
|
acceptText="Add"
|
|
|
|
rejectText="Cancel"
|
|
|
|
@accept="addChild()"
|
|
|
|
@close="onColse()"
|
|
|
|
>
|
|
|
|
<h2 class="subtitle">I am the proud parent of:</h2>
|
|
|
|
<form class="form" id="form-add-child">
|
|
|
|
<div class="field">
|
|
|
|
<label class="label">Name</label>
|
|
|
|
<div class="control has-icons-left">
|
|
|
|
<input
|
|
|
|
:class="`input ${!!errors.name ? 'is-danger' : ''}`"
|
|
|
|
required="true"
|
|
|
|
name="name"
|
|
|
|
type="text"
|
|
|
|
placeholder="John Snow"
|
|
|
|
:disabled="!childValidation.enableInput"
|
|
|
|
v-model="childValidation.name"
|
|
|
|
/>
|
|
|
|
<span class="icon is-small is-left">
|
|
|
|
<i class="fa fa-id-card"></i>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<p class="help is-danger" v-if="!!errors.name">{{ `${errors.name.message}` }}</p>
|
|
|
|
<!-- TODO: Error messages -->
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="field">
|
|
|
|
<label class="label">Birthday</label>
|
|
|
|
<div class="control has-icons-left">
|
|
|
|
<input
|
|
|
|
:class="`input ${!!errors.dob ? 'is-danger' : ''}`"
|
|
|
|
required="true"
|
|
|
|
name="dob"
|
|
|
|
type="date"
|
|
|
|
:disabled="!childValidation.enableInput"
|
|
|
|
v-model="childValidation.dob"
|
|
|
|
/>
|
|
|
|
<span class="icon is-small is-left">
|
|
|
|
<i class="fa fa-gift"></i>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<p class="help is-danger" v-if="!!errors.dob">{{ `${errors.dob.message}` }}</p>
|
|
|
|
</div>
|
2020-05-21 01:18:56 +00:00
|
|
|
<ImagePicker ref="imagePicker" />
|
2020-05-19 19:43:08 +00:00
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import Modal from "../../shared/components/Modal/Modal.vue";
|
|
|
|
import Services from "../../services";
|
2020-05-21 01:18:56 +00:00
|
|
|
import ImagePicker from "./ImagePicker.vue";
|
2020-05-19 19:43:08 +00:00
|
|
|
export default {
|
|
|
|
name: "AddChildModal",
|
|
|
|
components: {
|
|
|
|
Modal,
|
2020-05-21 01:18:56 +00:00
|
|
|
ImagePicker
|
2020-05-19 19:43:08 +00:00
|
|
|
},
|
|
|
|
props: ["isActive"],
|
2020-05-21 01:18:56 +00:00
|
|
|
|
2020-05-19 19:43:08 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
errors: {},
|
|
|
|
childValidation: {
|
|
|
|
enableInput: true,
|
|
|
|
name: null,
|
|
|
|
dob: null,
|
|
|
|
avatar: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onColse() {
|
|
|
|
this.$emit("onClose");
|
|
|
|
this.childValidation.name = null;
|
|
|
|
this.childValidation.dob = null;
|
|
|
|
this.childValidation.avatar = null;
|
|
|
|
},
|
|
|
|
async addChild() {
|
|
|
|
this.errors = {};
|
2020-05-21 01:18:56 +00:00
|
|
|
|
2020-05-19 19:43:08 +00:00
|
|
|
const childData = {
|
|
|
|
name: this.childValidation.name,
|
|
|
|
dob: this.childValidation.dob,
|
2020-05-21 01:18:56 +00:00
|
|
|
avatar: this.$refs.imagePicker.generateDataUrl("image/png", 0.8)
|
2020-05-19 19:43:08 +00:00
|
|
|
};
|
2020-05-21 01:18:56 +00:00
|
|
|
if (this.$refs.imagePicker.isDefaultImage) delete childData.avatar;
|
2020-05-19 19:43:08 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await Services.ApiService.createChild(
|
|
|
|
childData.name,
|
|
|
|
childData.dob,
|
|
|
|
childData.avatar
|
|
|
|
);
|
|
|
|
if (response.code === 201) this.$emit("onCreated", response.child);
|
|
|
|
else if (response.code === 400) {
|
|
|
|
response.message.forEach(m => {
|
|
|
|
this.errors[m.field] = m;
|
|
|
|
});
|
|
|
|
console.log(!!this.errors.name);
|
|
|
|
} else {
|
|
|
|
this.$emit("onFail", response.message);
|
|
|
|
this.childValidation.name = null;
|
|
|
|
this.childValidation.dob = null;
|
|
|
|
this.childValidation.avatar = null;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.$emit("onFail", e.message);
|
|
|
|
this.childValidation.name = null;
|
|
|
|
this.childValidation.dob = null;
|
|
|
|
this.childValidation.avatar = null;
|
|
|
|
}
|
|
|
|
this.childValidation.enableInput = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|