This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
seepur/resources/scripts/applications/home/views/child_profile.vue

315 lines
9.8 KiB
Vue
Raw Permalink Normal View History

2020-04-12 14:25:42 +00:00
<template>
<div class="container is-fullwidth">
2020-04-12 14:25:42 +00:00
<div class="loading" v-if="loading">
<Loading />
</div>
<div class v-else>
<!-- Profile Cover Modal -->
<Modal
title="Change Cover"
:isActive="showCoverModal"
acceptText="Change"
rejectText="Cancel"
@accept="changeCover()"
@close="showCoverModal=false"
>
<ProfileHeader
:title="child.name"
:background="childCoverModalImage ? childCoverModalImage : child.profile_cover"
/>
<file-select v-model="childCoverModalImage" accept="image/*" lable="Select Cover:"></file-select>
</Modal>
2020-05-21 03:22:40 +00:00
<!-- Change Avatar Modal -->
<ChangeAvatarModal
@onAvatarSelected="updateAvatar($event)"
@onClose="showChangeAvatarModal=false"
:isActive="showChangeAvatarModal"
:defaultImage="child.avatar"
/>
2020-04-12 14:25:42 +00:00
<!-- Add Connection Modal -->
<AddConnectionModal
@createNewConnection="addConnection($event)"
@dismiss="showAddConnectionModal=false"
:isActive="showAddConnectionModal"
:childName="child.name"
/>
<!-- Profile -->
<ProfileHeader :title="child.name" :background="child.profile_cover" />
<div class="columns is-fullheight m-t-md">
2020-05-09 19:11:55 +00:00
<div class="column is-2">
2020-04-12 14:25:42 +00:00
<div class="card">
2020-05-19 19:43:08 +00:00
<div class="card-image p-md is-relative">
2020-05-21 03:22:40 +00:00
<figure
:class="`image is-1by1 is-light ${isParent ? 'editable-image' : ''}`"
@click="onAvatarClicked()"
>
2020-04-12 14:25:42 +00:00
<img :src="child.avatar" class="is-rounded is-avatar" />
</figure>
</div>
<div class="card-content">
<div class="content">
<p>Hi!</p>
<p>Im {{age}}</p>
<br />
</div>
</div>
<footer v-if="isParent" class="card-footer">
<a
class="card-footer-item"
@click="togleEditMode()"
>{{inEditMode ? 'Cancel' : 'Edit'}}</a>
<a class="card-footer-item is-danger" @click="onDeleteClicked()">Delete</a>
</footer>
</div>
</div>
<div class="column">
<div class="card">
<div class="card-content">
<nav class="level">
<!-- Left side -->
<div class="level-left">
<div class="level-item">
<h1 class="title">{{`${child.name.split(' ')[0]}'s Room`}}</h1>
</div>
</div>
</nav>
2020-04-12 14:25:42 +00:00
<nav class="level">
<!-- Left side -->
<div class="level-left">
<div class="level-item">
<h1 class="subtitle">Parents</h1>
</div>
</div>
<!-- Right side -->
<div class="level-right">
<div class="level-item" v-if="isParent">
<button class="button" @click="showAddConnectionModal = true">
<i class="fa fa-fw fa-plus"></i> Add Connection
</button>
</div>
</div>
</nav>
<div class="parents">
<div class="is-flex">
2020-04-12 14:25:42 +00:00
<AvatarBadge
class="column"
v-for="parent in child.parents"
:key="parent.id"
:img="parent.avatar"
:text="parent.name"
:isLink="user.id === parent.id"
@onClick="goToUserProfile(parent)"
/>
</div>
</div>
<nav class="level">
<!-- Left side -->
<div class="level-left">
<div class="level-item">
<h1 class="subtitle">Connections</h1>
</div>
</div>
<!-- Right side -->
</nav>
<div class="columns">
<AvatarBadge
class="column"
v-for="connection in child.connections"
:key="connection.id"
:img="connection.avatar"
:text="connection.name"
:isLink="user.id === connection.id"
@onClick="goToUserProfile(connection)"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import { mapActions, mapGetters } from "vuex";
import ChildAvatar, { IChildAvatar } from "../components/child_avatar.vue";
import Services from "../../services/index";
import Loading from "../../shared/components/Loading/Loading.vue";
import ProfileHeader from "../components/ProfileHeader.vue";
import AddConnectionModal from "../components/AddConnectionModal.vue";
import FileSelect from "../../shared/components/FileSelect/FileSelect.vue";
import AvatarBadge from "../components/AvatarBadge.vue";
import Moment from "moment";
import Modal from "../../shared/components/Modal/Modal.vue";
2020-05-21 03:22:40 +00:00
import ChangeAvatarModal from "../components/ChangeAvatarModal.vue";
2020-04-12 14:25:42 +00:00
export default {
name: "ChildProfile",
components: {
ChildAvatar,
Loading,
ProfileHeader,
Modal,
FileSelect,
AvatarBadge,
2020-05-21 03:22:40 +00:00
AddConnectionModal,
ChangeAvatarModal
2020-04-12 14:25:42 +00:00
},
beforeCreate() {},
2020-05-21 03:22:40 +00:00
async beforeDestroy() {
await this.getUser();
return true;
},
2020-04-12 14:25:42 +00:00
async created() {
const response = await Services.ApiService.getChild(this.$route.params.id);
this.loading = false;
if (response.code === 0) {
console.log(response);
//Cool
this.child = response.data;
if (!this.user) await this.getUser();
this.isParent = this.child.parents.reduce((isParent, parent) => {
if (isParent) return true;
return parent.id === this.user.id;
}, this.isParent);
} else {
// notCool
this.notify({ message: "Sorry, Child not found!", level: "danger" });
this.$router.push({ path: `/` });
}
return true;
},
data() {
return {
loading: true,
child: null,
isParent: false,
inEditMode: false,
showCoverModal: false,
showAddConnectionModal: false,
2020-05-21 03:22:40 +00:00
showChangeAvatarModal: false,
2020-04-12 14:25:42 +00:00
childCoverModalImage: null
};
},
methods: {
onDeleteClicked() {
2020-05-01 17:36:24 +00:00
this.notify({ message: "Delete button clicked. Still not working" });
2020-04-12 14:25:42 +00:00
},
goToUserProfile(user) {
if (this.user.id === user.id) {
this.$router.push({ path: `/` });
} else {
this.$router.push({ path: `/user/${user.id}` });
}
},
async addConnection(_connection) {
try {
this.loading = true;
const connection = await Services.ApiService.createConnection({
..._connection,
child_id: this.child.id
});
if (connection.code === 409) {
this.loading = false;
this.showAddConnectionModal = false;
return this.notify({ message: connection.message, level: "warning" });
} else if (connection.code !== 0) {
this.loading = false;
this.showAddConnectionModal = false;
return this.notify({ message: connection.message, level: "danger" });
}
// debugger;
this.notify({
message: `Awesome!\n${connection.data.user.name} is connected to ${this.child.name}`,
level: "success"
});
if (connection.data.is_parent) {
this.child.parents.push(connection.data.user);
} else {
this.child.connections.push(connection.data.user);
}
2020-05-19 19:43:08 +00:00
await this.getUser();
2020-04-12 14:25:42 +00:00
} catch (e) {
console.error(e);
}
this.loading = false;
this.showAddConnectionModal = false;
return true;
},
2020-05-21 03:22:40 +00:00
onAvatarClicked() {
if (this.isParent) {
this.showChangeAvatarModal = true;
}
},
async updateAvatar(event) {
if (!event.isDefaultImage) {
try {
const response = await Services.ApiService.updateChild(
this.child.id,
{
avatar: event.image
}
);
if (response.code === 0) {
this.notify({ message: "Updated Successfully!", level: "success" });
this.child.avatar = response.data.child.avatar;
}
} catch (e) {
console.error(e);
}
}
this.showChangeAvatarModal = false;
return true;
},
2020-04-12 14:25:42 +00:00
async changeCover() {
if (this.childCoverModalImage) {
this.loading = true;
try {
const response = await Services.ApiService.updateChild(
2020-04-12 14:25:42 +00:00
this.child.id,
{
profile_cover: this.childCoverModalImage
}
2020-04-12 14:25:42 +00:00
);
if (response.code === 0) {
this.child.profile_cover = response.data.child.profile_cover;
}
2020-04-12 14:25:42 +00:00
} catch (error) {
console.error(error.message);
2020-04-12 14:25:42 +00:00
}
this.loading = false;
}
this.showCoverModal = false;
this.this.childCoverModalImage = null;
return true;
2020-04-12 14:25:42 +00:00
},
togleEditMode() {
this.inEditMode = !this.inEditMode;
if (this.inEditMode) {
this.showCoverModal = true;
}
},
2020-05-23 21:32:25 +00:00
...mapActions(["getUser", "notify"])
2020-04-12 14:25:42 +00:00
},
computed: {
age() {
const years = Moment().diff(this.child.dob, "years");
const months = Moment().diff(this.child.dob, "months") % 12;
const isNewBorn = !years && !months;
let text = "a new boarn!";
if (!isNewBorn) {
text = "";
if (years) text += `${years} years`;
if (years && months) text += ` and`;
if (months) text += ` ${months} months`;
text += " old";
}
return `${text}`;
},
2020-05-23 21:32:25 +00:00
...mapGetters(["user"])
2020-04-12 14:25:42 +00:00
}
};
</script>