src/app/my-groups/create-edit-group/create-edit-group.page.ts
selector | app-create-edit-group |
styleUrls | ./create-edit-group.page.scss |
templateUrl | ./create-edit-group.page.html |
Properties |
Methods |
|
Accessors |
constructor(groupService: GroupService, commonUtilService: CommonUtilService, fb: FormBuilder, translate: TranslateService, headerService: AppHeaderService, location: Location, platform: Platform, alertCtrl: AlertController, utilityService: UtilityService, telemetryGeneratorService: TelemetryGeneratorService, router: Router)
|
||||||||||||||||||||||||||||||||||||
Parameters :
|
Private Async createGroup | ||||
createGroup(formVal)
|
||||
Parameters :
Returns :
any
|
Private Async editGroup | ||||
editGroup(formVal)
|
||||
Parameters :
Returns :
any
|
handleBackButtonEvents |
handleBackButtonEvents()
|
Returns :
void
|
handleHeaderEvents | ||||
handleHeaderEvents($event)
|
||||
Parameters :
Returns :
void
|
initializeForm |
initializeForm()
|
Returns :
void
|
ionViewWillEnter |
ionViewWillEnter()
|
Returns :
void
|
ionViewWillLeave |
ionViewWillLeave()
|
Returns :
void
|
onSubmit |
onSubmit()
|
Returns :
void
|
Async openTermsOfUse |
openTermsOfUse()
|
Returns :
any
|
appName |
Type : string
|
backButtonFunc |
Type : Subscription
|
corRelationList |
Type : Array<CorrelationData>
|
createGroupForm |
Type : FormGroup
|
createGroupFormSubmitted |
Default value : false
|
errorMessages |
Type : object
|
Default value : {
groupName: {
message: 'GROUP_NAME_IS_REQUIRED'
},
groupTerms: {
message: 'GROUP_TERMS_IS_REQUIRED'
}
}
|
extras |
Type : any
|
groupDetails |
Type : any
|
Public groupService |
Type : GroupService
|
Decorators :
@Inject('GROUP_SERVICE')
|
hasFilledLocation |
Default value : false
|
headerObservable |
Type : Subscription
|
createGroupFormControls |
getcreateGroupFormControls()
|
import { Subscription } from 'rxjs';
import { Component, Inject } from '@angular/core';
import { Platform, AlertController } from '@ionic/angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import {
GroupService, GroupCreateRequest, GroupMembershipType,
UpdateByIdRequest, CorrelationData
} from 'sunbird-sdk';
import { CommonUtilService } from '@app/services/common-util.service';
import { AppHeaderService } from '@app/services/app-header.service';
import { Location } from '@angular/common';
import { UtilityService, Environment, ID, ImpressionSubtype,
ImpressionType, InteractType, PageId,
TelemetryGeneratorService, InteractSubtype } from '@app/services';
import { RouterLinks, GroupErrorCodes } from '@app/app/app.constant';
import { Router } from '@angular/router';
@Component({
selector: 'app-create-edit-group',
templateUrl: './create-edit-group.page.html',
styleUrls: ['./create-edit-group.page.scss'],
})
export class CreateEditGroupPage {
corRelationList: Array<CorrelationData>;
appName: string;
createGroupFormSubmitted = false;
createGroupForm: FormGroup;
backButtonFunc: Subscription;
hasFilledLocation = false;
errorMessages = {
groupName: {
message: 'GROUP_NAME_IS_REQUIRED'
},
groupTerms: {
message: 'GROUP_TERMS_IS_REQUIRED'
}
};
headerObservable: Subscription;
extras: any;
groupDetails: any;
constructor(
@Inject('GROUP_SERVICE') public groupService: GroupService,
private commonUtilService: CommonUtilService,
private fb: FormBuilder,
private translate: TranslateService,
private headerService: AppHeaderService,
private location: Location,
private platform: Platform,
private alertCtrl: AlertController,
private utilityService: UtilityService,
private telemetryGeneratorService: TelemetryGeneratorService,
private router: Router
) {
const extras = this.router.getCurrentNavigation().extras.state;
if (extras && extras.groupDetails) {
this.groupDetails = extras.groupDetails;
this.corRelationList = extras.corRelation;
}
this.initializeForm();
}
ionViewWillEnter() {
this.headerService.showHeaderWithBackButton();
this.headerObservable = this.headerService.headerEventEmitted$.subscribe(eventName => {
this.handleHeaderEvents(eventName);
});
this.handleBackButtonEvents();
this.commonUtilService.getAppName().then((res) => { this.appName = res; });
this.telemetryGeneratorService.generateImpressionTelemetry(
ImpressionType.VIEW, ImpressionSubtype.CREATE_GROUP_FORM, PageId.CREATE_GROUP, Environment.GROUP,
undefined, undefined, undefined, undefined, this.corRelationList);
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.INITIATED,
'',
Environment.GROUP,
PageId.CREATE_GROUP,
undefined,
undefined,
undefined,
this.corRelationList,
ID.CREATE_GROUP
);
}
ionViewWillLeave() {
this.commonUtilService.getAppName().then((res) => { this.appName = res; });
if (this.headerObservable) {
this.headerObservable.unsubscribe();
}
if (this.backButtonFunc) {
this.backButtonFunc.unsubscribe();
}
}
handleBackButtonEvents() {
this.backButtonFunc = this.platform.backButton.subscribeWithPriority(0, async () => {
const activePortal = await this.alertCtrl.getTop();
if (activePortal) {
activePortal.dismiss();
} else {
this.telemetryGeneratorService.generateBackClickedTelemetry(PageId.CREATE_GROUP,
Environment.GROUP, false, undefined, this.corRelationList);
this.location.back();
}
});
}
initializeForm() {
this.createGroupForm = this.fb.group({
groupName: [(this.groupDetails && this.groupDetails.name) || '', Validators.required],
groupDesc: (this.groupDetails && this.groupDetails.description) || '',
groupTerms: [(this.groupDetails && true || ''), Validators.required]
});
}
get createGroupFormControls() {
return this.createGroupForm.controls;
}
onSubmit() {
this.createGroupFormSubmitted = true;
const formVal = this.createGroupForm.value;
if (!formVal.groupTerms) {
this.createGroupForm.controls['groupTerms'].setErrors({ incorrect: true });
}
if (this.createGroupForm.valid) {
if (this.groupDetails) {
this.editGroup(formVal);
} else {
this.createGroup(formVal);
}
}
}
private async createGroup(formVal) {
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.SELECT_CREATE_GROUP,
InteractSubtype.CREATE_GROUP_CLICKED,
Environment.GROUP, PageId.CREATE_GROUP,
undefined, undefined, undefined, this.corRelationList,
ID.CREATE_GROUP
);
if (!this.commonUtilService.networkInfo.isNetworkAvailable) {
this.commonUtilService.presentToastForOffline('YOU_ARE_NOT_CONNECTED_TO_THE_INTERNET');
return;
}
const loader = await this.commonUtilService.getLoader();
await loader.present();
const groupCreateRequest: GroupCreateRequest = {
name: formVal.groupName,
description: formVal.groupDesc,
membershipType: GroupMembershipType.MODERATED
};
this.groupService.create(groupCreateRequest).toPromise().then(async (res) => {
console.log('create suc');
await loader.dismiss();
this.commonUtilService.showToast('GROUP_CREATED');
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.SUCCESS,
'',
Environment.GROUP,
PageId.CREATE_GROUP,
undefined, undefined, undefined, this.corRelationList,
ID.CREATE_GROUP
);
this.location.back();
}).catch(async (err) => {
console.error(err);
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.FAILURE,
'',
Environment.GROUP,
PageId.CREATE_GROUP,
undefined, undefined, undefined, this.corRelationList,
ID.CREATE_GROUP
);
await loader.dismiss();
if (err.response && err.response.body && err.response.body.params
&& err.response.body.params.status === GroupErrorCodes.EXCEEDED_GROUP_MAX_LIMIT) {
this.commonUtilService.showToast('ERROR_MAXIMUM_GROUP_COUNT_EXCEEDS');
} else {
this.commonUtilService.showToast('SOMETHING_WENT_WRONG');
}
});
}
private async editGroup(formVal) {
const loader = await this.commonUtilService.getLoader();
await loader.present();
const updateCreateRequest: UpdateByIdRequest = {
id: this.groupDetails.id,
updateRequest: {
name: formVal.groupName,
description: formVal.groupDesc
}
};
this.groupService.updateById(updateCreateRequest).toPromise().then(async (res) => {
await loader.dismiss();
this.commonUtilService.showToast('GROUP_UPDATE_SUCCESS');
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.SUCCESS,
'',
Environment.GROUP,
PageId.CREATE_GROUP,
undefined,
undefined,
undefined,
this.corRelationList,
ID.CREATE_GROUP
);
this.location.back();
}).catch(async (err) => {
await loader.dismiss();
});
}
async openTermsOfUse() {
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.TOUCH,
InteractSubtype.TERMS_OF_USE_CLICKED,
Environment.GROUP,
PageId.CREATE_GROUP,
undefined, undefined, undefined, this.corRelationList);
const baseUrl = await this.utilityService.getBuildConfigValue('TOU_BASE_URL');
const url = baseUrl + RouterLinks.TERM_OF_USE + '#groupGuidelines';
const options
= 'hardwareback=yes,clearcache=no,zoom=no,toolbar=yes,disallowoverscroll=yes';
(window as any).cordova.InAppBrowser.open(url, '_blank', options);
}
handleHeaderEvents($event) {
if ($event.name === 'back'){
this.telemetryGeneratorService.generateBackClickedTelemetry(PageId.CREATE_GROUP,
Environment.GROUP, true, undefined, this.corRelationList);
this.location.back();
}
}
}
<ion-content>
<form [formGroup]="createGroupForm">
<h4 class="cr-title" *ngIf="!groupDetails">{{'CREATE_GROUP' | translate}}</h4>
<h4 class="cr-title" *ngIf="groupDetails">{{'EDIT_GROUP' | translate}}</h4>
<ion-item class="input-item"
[ngClass]="{'cr-input-primary': !((createGroupFormControls.groupName.touched || createGroupFormSubmitted) && createGroupFormControls.groupName.errors?.required) ,
'cr-input-error': ((createGroupFormControls.groupName.touched || createGroupFormSubmitted) && createGroupFormControls.groupName.errors?.required)}">
<ion-label position="stacked" class="label-font align-text">{{'GROUP_NAME' | translate }}<sup>*</sup></ion-label>
<ion-input formControlName="groupName" maxlength="50" placeholder="{{'ENTER_GROUP_NAME' | translate}}"></ion-input>
<span class="char-info"
[innerHTML]="'CHARACTERS_REMAINING' | translate: {'character_count': 50 - createGroupFormControls.groupName.value?.length} "
*ngIf="!((createGroupFormControls.groupName.touched || createGroupFormSubmitted) && createGroupFormControls.groupName.errors?.required)">
</span>
<span *ngIf="(createGroupFormControls.groupName.touched || createGroupFormSubmitted) && createGroupFormControls.groupName.errors?.required" class="cr-error">{{ errorMessages.groupName.message | translate }}</span>
</ion-item>
<ion-item class="input-item cr-input-primary">
<ion-label position="stacked" class="label-font align-text">{{'GROUP_DESCRIPTION' | translate }}</ion-label>
<ion-textarea class="cr-desc" formControlName="groupDesc" maxlength="150" rows="4" placeholder="{{'GROUP_DESCRIPTION_PLACEHOLDER' | translate}}"></ion-textarea>
<span class="char-info"
[innerHTML]="'CHARACTERS_REMAINING' | translate: {'character_count': 150 - createGroupFormControls.groupDesc.value?.length} "
></span>
</ion-item>
<div class="cr-terms-container" *ngIf="!groupDetails">
<div class="input-item cr-input-primary" lines="none">
<ion-checkbox id="cr-term-label-accessible" class="cr-terms-checkbox" formControlName="groupTerms" color="secondary" checked="false"></ion-checkbox>
<label class="hide" for="cr-term-label-accessible" >{{'I_AGREE_TO' | translate: {'app_name': appName} }} checkbox </label>
<p class="cr-terms-checkbox-label">
<span>{{'I_AGREE_TO' | translate: {'app_name': appName} }} </span><span class="cr-terms" (click)="openTermsOfUse()">{{'GROUP_GUIDELINES' | translate}}</span>
</p>
</div>
<p
*ngIf="(createGroupFormControls.groupTerms.touched || createGroupFormSubmitted) && createGroupFormControls.groupTerms.invalid"
class="cr-error cr-error-checkbox"
[innerHTML]="'GROUP_TERMS_IS_REQUIRED' | translate: {'app_name': appName}" ></p>
</div>
</form>
</ion-content>
<ion-footer>
<div class="padding-16" *ngIf="!groupDetails">
<button class="sb-btn-large" expand="block" attr.aria-label="create group {{createGroupForm.invalid ? 'disable' : ''}}" [class.inactive]="createGroupForm.invalid" (click)="onSubmit()">
<img src="assets/imgs/ic_group_add_white.svg" alt="">
{{'CREATE_GROUP' | translate}}
</button>
</div>
<div class="padding-16" *ngIf="groupDetails">
<button class="sb-btn-large" expand="block" [class.inactive]="createGroupForm.invalid" (click)="onSubmit()">
<img src="assets/imgs/ic_group_add_white.svg" alt="">
{{'EDIT_GROUP' | translate}}
</button>
</div>
</ion-footer>
./create-edit-group.page.scss
@import "src/assets/styles/_variables.scss";
:host {
.label-font {
color: $gray-800 !important;
font-family: "Noto Sans", sans-serif !important;
font-size: 1.375rem !important;
font-weight: normal;
}
.padding-12 {
padding: 12px !important;
}
.cr-error-checkbox{
margin: 0;
}
.char-info{
display: block;
text-align: right;
width: 100%;
font-size: 0.75rem;
padding-top: 4px;
color: $gray-300;
}
ion-item {
// --border-color: var(--ion-color-danger, #f1453d);
}
.sb-btn-large{
&.inactive{
background-color: $gray-200;
}
img{
margin-right: 8px;
position: relative;
bottom: 0.125rem;
}
}
.item-label-stacked ion-input {
margin-top: 16px;
padding-left: 16px !important;
padding-right: 16px !important;
font-size: $font-size-base;
}
.item-label-stacked .cr-desc {
margin-top: 16px;
padding: 0 16px;
border: 1px solid $gray-800 !important;
border-radius: 2px;
}
.cr-input-primary ion-input{
border: 1px solid $gray-800;
border-radius: 2px;
}
.cr-input-error ion-input{
border: 1px solid $red;
}
ion-item.item-label-stacked {
--border-width: 0;
--highlight-background: transparent;
}
// .item-select ion-label {
// color: #006de5;
// }
ion-checkbox{
&.cr-terms-checkbox{
--ion-color-base: green !important;
}
}
.cr-terms-container{
padding: 0 16px;
font-size: $font-size-base;
.cr-terms-checkbox-label{
margin-left: 16px;
display: inline-block;
vertical-align: bottom;
margin-bottom: 0;
}
.cr-terms{
color: $blue;
}
}
ion-label {
color: $gray-800;
font-family: "Noto Sans", sans-serif;
font-size: $font-size-base;
letter-spacing: 0;
sup{
font-size: 1.375rem;
top: 0;
margin-left: 4px;
color: $red;
}
}
}
.cr-title{
font-size: 1rem;
font-weight: bold;
color: $blue;
margin: 16px 16px 0;
}
.cr-input-title{
color: $gray-800;
font-family: "Noto Sans", sans-serif;
font-size: $font-size-base;
letter-spacing: 0;
line-height: 1.188rem;
margin: 16px 0 8px;
}
.cr-input-box{
border: 0.5px solid $gray-800;
border-radius: 2px;
ion-input{
margin: 0 8px;
}
}
.cr-error{
margin-top: 8px;
display: block;
font-size: 0.75rem;
color: $red;
}
.input-item{
padding-top: 8px;
}