234 lines
5.4 KiB
TypeScript
234 lines
5.4 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { DialogComponent } from '../dialog/dialog.component';
|
|
import { Rayconnect } from '../../../../services/rayconnect/rayconnect.service';
|
|
|
|
import { Chart } from 'chart.js'
|
|
import { FormControl } from '@angular/forms';
|
|
|
|
interface University {
|
|
value: string;
|
|
viewValue: string;
|
|
}
|
|
|
|
interface UniversityGroup {
|
|
disabled?: boolean;
|
|
name: string;
|
|
value: string;
|
|
universities: University[];
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-app',
|
|
templateUrl: './app.component.html',
|
|
styleUrls: ['./app.component.scss']
|
|
})
|
|
export class AppComponent implements OnInit {
|
|
|
|
universityControl = new FormControl();
|
|
univeristygroups: UniversityGroup[] = [
|
|
|
|
{
|
|
name: 'مرکزی',
|
|
value: "markazi",
|
|
universities: [
|
|
{ value: 'athar', viewValue: 'اطهر' },
|
|
{ value: 'amirkabir', viewValue: 'امیر کبیر' },
|
|
{ value: 'khomein', viewValue: 'دکتر حسابی خمین' },
|
|
{ value: 'saveh', viewValue: 'مرکز پسران ساوه' }
|
|
|
|
]
|
|
}
|
|
];
|
|
public list: { _id: string, name: string }[] = [];
|
|
options = {
|
|
responsive: true,
|
|
|
|
}
|
|
data: any = []
|
|
|
|
|
|
constructor(private dialog: MatDialog, private rayconnect: Rayconnect, private snackbar: MatSnackBar) { }
|
|
|
|
ngOnInit(): void {
|
|
this.fetch();
|
|
|
|
Chart.defaults.global.defaultFontFamily = "Font"
|
|
|
|
this.universityControl.setValue("all")
|
|
|
|
this.subscribe()
|
|
|
|
let cachedColor = null
|
|
let cachedLabel = null
|
|
|
|
this.rayconnect.Query({
|
|
scope: "kateb",
|
|
address: "volunteers/stats/live",
|
|
method: "LIVE",
|
|
}, (res => {
|
|
|
|
|
|
console.log(res['data'])
|
|
if (!cachedColor) cachedColor = this.RandomColor(res['data']['majors'].length)
|
|
if (!cachedLabel) cachedLabel = res['data']['majors'].map((major) => {
|
|
return major._id
|
|
}),
|
|
this.buildMajorsStats(res['data']['majors'], cachedColor, cachedLabel)
|
|
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
subscribe() {
|
|
this.rayconnect.RequestBack({
|
|
scope: 'kateb',
|
|
uniqueID: 'profile',
|
|
TokenID: '*',
|
|
address: 'volunteers/stats/subscribe',
|
|
info: {
|
|
method: 'SUB',
|
|
data: {
|
|
|
|
}
|
|
}
|
|
}).then(res => {
|
|
if (res['data']['status'] == true) { }
|
|
})
|
|
}
|
|
RandomColor(len) {
|
|
|
|
|
|
let colors = []
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
colors.push('#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6)
|
|
)
|
|
}
|
|
|
|
return colors
|
|
}
|
|
|
|
buildMajorsStats(majors, colors, labels) {
|
|
|
|
|
|
this.data[0] = {
|
|
labels: labels,
|
|
type: "doughnut",
|
|
datasets: [{
|
|
label: 'تعداد ثبت نامی به تفکیک رشته',
|
|
data: majors.map((major) => {
|
|
return major.count
|
|
}),
|
|
backgroundColor: colors,
|
|
borderWidth: 1
|
|
}]
|
|
}
|
|
|
|
/* this.data[2] = {
|
|
labels: majors.map((major) => {
|
|
return major.name
|
|
}),
|
|
type: "doughnut",
|
|
datasets: [{
|
|
label: 'تعداد ثبت نام در انتظار تایید در مرحله پنجم (آموزش)',
|
|
data: majors.map(() => {
|
|
return Math.floor(Math.random() * 6) + 1
|
|
}),
|
|
backgroundColor: this.RandomColor(majors.length),
|
|
borderWidth: 1
|
|
}]
|
|
} */
|
|
|
|
/* this.data[3] = {
|
|
labels: majors.map((major) => {
|
|
return major.name
|
|
}),
|
|
type: "doughnut",
|
|
datasets: [{
|
|
label: 'آمار مراجعات حضوری بر اساس رشته',
|
|
data: majors.map(() => {
|
|
return Math.floor(Math.random() * 6) + 1
|
|
}),
|
|
backgroundColor: this.RandomColor(majors.length),
|
|
borderWidth: 1
|
|
}]
|
|
|
|
|
|
}
|
|
*/
|
|
|
|
/* let labels = []
|
|
let m = moment()
|
|
for (let index = 0; index < 10; index++) {
|
|
m = m.add(1, 'd')
|
|
labels.push(m.format('jYYYY/jM/jD'));
|
|
|
|
}
|
|
/* this.data[4] = {
|
|
labels: labels,
|
|
type: "line",
|
|
datasets: [{
|
|
label: 'آمار مراجعات حضوری بر اساس تاریخ',
|
|
data: labels.map((da) => {
|
|
return Math.floor(Math.random() * 6) + 1;
|
|
}),
|
|
borderColor: 'rgb(75, 192, 192)',
|
|
lineTension: 0.1,
|
|
borderWidth: 1
|
|
}]
|
|
|
|
|
|
} */
|
|
|
|
|
|
}
|
|
fetch() {
|
|
this.rayconnect.RequestBack({
|
|
scope: 'kateb',
|
|
uniqueID: 'profile',
|
|
TokenID: '*',
|
|
address: 'major/list',
|
|
info: {
|
|
method: 'LIST',
|
|
data: {}
|
|
}
|
|
}).then(res => {
|
|
if (res['data']['status'] == true) return;
|
|
})
|
|
}
|
|
|
|
delete(id: string) {
|
|
this.rayconnect.RequestBack({
|
|
scope: 'kateb',
|
|
uniqueID: 'profile',
|
|
TokenID: '*',
|
|
address: 'major/delete',
|
|
info: {
|
|
method: 'DELETE',
|
|
data: { id }
|
|
}
|
|
}).then(res => {
|
|
this.snackbar.open(res['data']['message'], 'باشه', { duration: 3000, direction: 'rtl' });
|
|
if (res['data']['status'] == true) {
|
|
let index = this.list.findIndex(item => item._id == id);
|
|
this.list.splice(index, 1);
|
|
}
|
|
})
|
|
}
|
|
|
|
openDialog(data) {
|
|
this.dialog.open(DialogComponent, { data, autoFocus: false, disableClose: true }).afterClosed().subscribe((result: { name: string, _id: string }) => {
|
|
if (!result) return;
|
|
let index = this.list.findIndex(item => item._id == result._id);
|
|
if (index < 0) this.list.push(result);
|
|
else this.list[index] = result;
|
|
})
|
|
}
|
|
}
|