2022-06-30 12:41:36 +02:00
|
|
|
import {Component} from "@angular/core";
|
2022-09-23 16:14:20 +02:00
|
|
|
import {UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
|
2021-05-19 13:40:29 +02:00
|
|
|
import {EnvProperties} from "../../../../openaireLibrary/utils/properties/env-properties";
|
|
|
|
import {properties} from "../../../../../environments/environment";
|
|
|
|
import {CommunityInfo} from "../../../../openaireLibrary/connect/community/communityInfo";
|
|
|
|
import {Session, User} from "../../../../openaireLibrary/login/utils/helper.class";
|
|
|
|
import {CommunityService} from "../../../../openaireLibrary/connect/community/community.service";
|
|
|
|
import {UtilitiesService} from "../../../../openaireLibrary/services/utilities.service";
|
|
|
|
import {UserManagementService} from "../../../../openaireLibrary/services/user-management.service";
|
|
|
|
import {StringUtils} from "../../../../openaireLibrary/utils/string-utils.class";
|
|
|
|
import {Subscription} from "rxjs";
|
|
|
|
import {Option} from "../../../../openaireLibrary/sharedComponents/input/input.component";
|
2022-07-08 20:59:17 +02:00
|
|
|
import {NotificationHandler} from "../../../../openaireLibrary/utils/notification-handler";
|
2022-07-12 15:50:56 +02:00
|
|
|
import {ClearCacheService} from "../../../../openaireLibrary/services/clear-cache.service";
|
2021-05-19 13:40:29 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'edit-community',
|
|
|
|
template: `
|
2022-06-29 12:19:29 +02:00
|
|
|
<form *ngIf="communityFb" [formGroup]="communityFb" class="uk-margin-xlarge-bottom">
|
|
|
|
<div class="uk-grid uk-grid-large" uk-grid>
|
|
|
|
<div class="uk-width-1-2@m">
|
|
|
|
<div input id="name" [formInput]="communityFb.get('name')"
|
|
|
|
placeholder="Name of the community profile."></div>
|
|
|
|
</div>
|
|
|
|
<div class="uk-width-1-2@m">
|
|
|
|
<div input [formInput]="communityFb.get('shortName')"
|
|
|
|
placeholder="Short name of the community."></div>
|
|
|
|
</div>
|
|
|
|
<div class="uk-width-1-1">
|
|
|
|
<div class="uk-text-bold uk-margin-bottom uk-form-hint">Description of the community</div>
|
|
|
|
<ckeditor [readonly]="false"
|
|
|
|
debounce="500"
|
|
|
|
[formControl]="communityFb.get('description')"
|
|
|
|
[config]="{ extraAllowedContent: '* [uk-*](*) ; span', disallowedContent: 'script; *[on*]',
|
2021-05-19 13:40:29 +02:00
|
|
|
removeButtons: 'Save,NewPage,DocProps,Preview,Print,' +
|
|
|
|
'Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,' +
|
|
|
|
'CreateDiv,Flash,PageBreak,' +
|
|
|
|
'Subscript,Superscript,Anchor,Smiley,Iframe,Styles,Font,About,Language',
|
|
|
|
extraPlugins: 'divarea'}">
|
2022-06-29 12:19:29 +02:00
|
|
|
</ckeditor>
|
|
|
|
</div>
|
|
|
|
<div class="uk-width-1-1">
|
2021-05-19 13:40:29 +02:00
|
|
|
<input #file id="photo" type="file" class="uk-hidden" (change)="fileChangeEvent($event)"/>
|
2022-06-29 12:19:29 +02:00
|
|
|
<div *ngIf="!communityFb.get('isUpload').value" class="uk-grid uk-grid-column-large" uk-grid>
|
2023-10-09 16:14:43 +02:00
|
|
|
<div class="uk-margin-xsmall-top uk-width-auto@l uk-width-1-1">
|
2022-06-29 12:19:29 +02:00
|
|
|
<div class="uk-grid uk-grid-column-large uk-flex-middle" uk-grid>
|
|
|
|
<div class="uk-width-auto@l uk-width-1-1 uk-flex uk-flex-center">
|
|
|
|
<button class="uk-button uk-button-primary uk-flex uk-flex-middle uk-flex-wrap"
|
2021-05-19 13:40:29 +02:00
|
|
|
(click)="file.click()">
|
|
|
|
<icon name="cloud_upload" [flex]="true"></icon>
|
|
|
|
<span class="uk-margin-small-left">Upload a file</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="uk-text-center uk-text-bold uk-width-expand">
|
|
|
|
OR
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-08 20:59:17 +02:00
|
|
|
<div input class="uk-width-expand" type="logoURL" [placeholder]="'Link to the logo'"
|
|
|
|
[formInput]="communityFb.get('logoUrl')"></div>
|
2022-06-29 12:19:29 +02:00
|
|
|
</div>
|
|
|
|
<div *ngIf="communityFb.get('isUpload').value" class="uk-width-1-1 uk-flex uk-flex-middle">
|
|
|
|
<div class="uk-card uk-card-default uk-text-center uk-border-circle">
|
|
|
|
<img class="uk-position-center uk-blend-multiply" [src]="photo">
|
|
|
|
</div>
|
|
|
|
<div class="uk-margin-left">
|
2022-06-30 12:41:36 +02:00
|
|
|
<button (click)="remove()" class="uk-button-danger uk-icon-button uk-icon-button-small">
|
|
|
|
<icon [flex]="true" ratio="0.8" name="delete"></icon>
|
2022-06-29 12:19:29 +02:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="uk-margin-small-left">
|
2022-06-30 12:41:36 +02:00
|
|
|
<button class="uk-button-secondary uk-icon-button uk-icon-button-small" (click)="file.click()">
|
|
|
|
<icon [flex]="true" ratio="0.8" name="edit"></icon>
|
2022-06-29 12:19:29 +02:00
|
|
|
</button>
|
2021-05-19 13:40:29 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-29 12:19:29 +02:00
|
|
|
<!-- Full width error message -->
|
|
|
|
<div *ngIf="uploadError" class="uk-text-danger uk-margin-small-top uk-width-1-1">{{uploadError}}</div>
|
|
|
|
</div>
|
2023-10-10 11:30:47 +02:00
|
|
|
<div class="uk-width-1-3@m">
|
2022-07-08 20:59:17 +02:00
|
|
|
<div input [formInput]="communityFb.get('status')"
|
2022-06-29 12:19:29 +02:00
|
|
|
placeholder="Visibility status for your community's profile."
|
|
|
|
[options]="statuses" type="select"></div>
|
2021-05-19 13:40:29 +02:00
|
|
|
</div>
|
2023-10-10 11:30:47 +02:00
|
|
|
<div class="uk-width-1-3@m">
|
2023-07-19 13:26:16 +02:00
|
|
|
<div input [formInput]="communityFb.get('claim')"
|
|
|
|
placeholder="Who can create links for your community"
|
|
|
|
[options]="claimOptions" type="select"></div>
|
|
|
|
</div>
|
2023-10-10 11:30:47 +02:00
|
|
|
<div class="uk-width-1-3@m">
|
2023-07-19 13:26:16 +02:00
|
|
|
<div input [formInput]="communityFb.get('membership')"
|
|
|
|
placeholder="Who can join"
|
|
|
|
[options]="membershipOptions" type="select"></div>
|
2023-10-10 11:30:47 +02:00
|
|
|
</div>
|
2022-06-29 12:19:29 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
`,
|
2022-07-17 22:34:41 +02:00
|
|
|
styleUrls: ['edit-community.component.less']
|
2021-05-19 13:40:29 +02:00
|
|
|
})
|
|
|
|
export class EditCommunityComponent {
|
2022-09-23 16:14:20 +02:00
|
|
|
public communityFb: UntypedFormGroup;
|
2021-05-19 13:40:29 +02:00
|
|
|
public statuses: Option[] = [
|
2023-11-21 11:59:46 +01:00
|
|
|
// {label: 'Visible', value: 'all'},
|
|
|
|
{label: 'Visible', value: 'PUBLIC'},
|
|
|
|
// {label: 'Visible to managers', value: 'manager'},
|
|
|
|
{label: 'Visible to managers', value: 'RESTRICTED'},
|
2021-05-19 13:40:29 +02:00
|
|
|
{label: 'Hidden', value: 'hidden'}
|
|
|
|
]
|
2023-07-19 13:26:16 +02:00
|
|
|
public claimOptions: Option[] = [
|
2023-10-04 11:17:26 +02:00
|
|
|
{label: 'Logged in users', value: 'all'},
|
|
|
|
{label: 'Members only', value: 'membersOnly'},
|
|
|
|
{label: 'Managers only', value: 'managersOnly'}
|
|
|
|
|
2023-07-19 13:26:16 +02:00
|
|
|
]
|
|
|
|
public membershipOptions: Option[] = [
|
|
|
|
{label: 'Anyone can join', value: 'open'},
|
|
|
|
{label: 'By invitation', value: 'byInvitation'}
|
|
|
|
]
|
2021-05-19 13:40:29 +02:00
|
|
|
public community: CommunityInfo;
|
|
|
|
public isNew: boolean;
|
|
|
|
public properties: EnvProperties = properties
|
|
|
|
;
|
|
|
|
private subscriptions: any[] = [];
|
|
|
|
/**
|
|
|
|
* Photo upload
|
|
|
|
* */
|
|
|
|
public file: File;
|
|
|
|
public photo: string | ArrayBuffer;
|
|
|
|
public uploadError: string;
|
|
|
|
public deleteCurrentPhoto: boolean = false;
|
|
|
|
private maxsize: number = 200 * 1024;
|
|
|
|
user: User;
|
|
|
|
|
2022-09-23 16:14:20 +02:00
|
|
|
constructor(private fb: UntypedFormBuilder,
|
2021-05-19 13:40:29 +02:00
|
|
|
private communityService: CommunityService,
|
|
|
|
private utilsService: UtilitiesService,
|
2022-07-12 15:50:56 +02:00
|
|
|
private userManagementService: UserManagementService,
|
|
|
|
private _clearCacheService: ClearCacheService) {
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public init(community: CommunityInfo, isNew: boolean = false) {
|
|
|
|
this.reset();
|
|
|
|
this.deleteCurrentPhoto = false;
|
|
|
|
this.community = community;
|
|
|
|
this.isNew = isNew;
|
|
|
|
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
|
|
|
|
this.user = user;
|
|
|
|
this.communityFb = this.fb.group({
|
|
|
|
communityId: this.fb.control(this.community.communityId),
|
|
|
|
name: this.fb.control(this.community.title, Validators.required),
|
|
|
|
shortName: this.fb.control(this.community.shortTitle, Validators.required),
|
|
|
|
description: this.fb.control(this.community.description),
|
|
|
|
status: this.fb.control(this.community.status),
|
2023-07-19 13:26:16 +02:00
|
|
|
claim: this.fb.control(this.community.claim),
|
|
|
|
membership: this.fb.control(this.community.membership),
|
2021-05-19 13:40:29 +02:00
|
|
|
managers: this.fb.control(this.community.managers),
|
|
|
|
isUpload: this.fb.control(this.community.isUpload),
|
|
|
|
logoUrl: this.fb.control(this.community.logoUrl)
|
|
|
|
});
|
|
|
|
if (this.community.isUpload) {
|
|
|
|
this.communityFb.get('logoUrl').clearValidators();
|
|
|
|
this.communityFb.get('logoUrl').updateValueAndValidity();
|
|
|
|
} else {
|
|
|
|
this.communityFb.get('logoUrl').setValidators([StringUtils.urlValidator()]);
|
|
|
|
this.communityFb.get('logoUrl').updateValueAndValidity();
|
|
|
|
}
|
|
|
|
this.subscriptions.push(this.communityFb.get('isUpload').valueChanges.subscribe(value => {
|
|
|
|
if (value == true) {
|
|
|
|
this.communityFb.get('logoUrl').clearValidators();
|
|
|
|
this.communityFb.updateValueAndValidity();
|
|
|
|
} else {
|
|
|
|
this.communityFb.get('logoUrl').setValidators([StringUtils.urlValidator()]);
|
|
|
|
this.communityFb.updateValueAndValidity();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
this.initPhoto();
|
|
|
|
if (!isNew) {
|
|
|
|
if (!this.isAdmin) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.communityFb.get('shortName').disable();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public get isAdmin() {
|
|
|
|
return Session.isPortalAdministrator(this.user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get disabled(): boolean {
|
|
|
|
return (this.communityFb && this.communityFb.invalid) ||
|
|
|
|
(this.communityFb && this.communityFb.pristine && !this.isNew && !this.file) ||
|
|
|
|
(this.uploadError && this.uploadError.length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get dirty(): boolean {
|
|
|
|
return this.communityFb && this.communityFb.dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.uploadError = null;
|
|
|
|
this.communityFb = null;
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscription) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public save(callback: Function, errorCallback: Function = null) {
|
|
|
|
if (this.file) {
|
2021-11-26 10:51:24 +01:00
|
|
|
this.subscriptions.push(this.utilsService.uploadPhoto(this.properties.utilsService + "/upload/community/" + encodeURIComponent(this.community.communityId), this.file).subscribe(res => {
|
2021-05-19 13:40:29 +02:00
|
|
|
this.deletePhoto();
|
|
|
|
this.communityFb.get('logoUrl').setValue(res.filename);
|
2021-11-12 11:37:01 +01:00
|
|
|
this.removePhoto();
|
2021-05-19 13:40:29 +02:00
|
|
|
this.saveCommunity(callback, errorCallback);
|
|
|
|
}, error => {
|
|
|
|
this.uploadError = "An error has been occurred during upload your image. Try again later";
|
|
|
|
this.saveCommunity(callback, errorCallback);
|
|
|
|
}));
|
|
|
|
} else if (this.deleteCurrentPhoto) {
|
|
|
|
this.deletePhoto();
|
|
|
|
this.saveCommunity(callback, errorCallback);
|
|
|
|
} else {
|
|
|
|
this.saveCommunity(callback, errorCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveCommunity(callback: Function, errorCallback: Function = null) {
|
|
|
|
if (this.isNew) {
|
|
|
|
this.removePhoto();
|
|
|
|
this.subscriptions.push(this.communityService.updateCommunity(
|
2022-07-02 13:45:34 +02:00
|
|
|
this.properties.communityAPI + this.community.communityId, this.communityFb.getRawValue()).subscribe(() => {
|
2022-07-12 15:50:56 +02:00
|
|
|
this._clearCacheService.clearCache("Community saved");
|
2022-08-11 11:58:20 +02:00
|
|
|
this._clearCacheService.purgeBrowserCache("Community saved", this.community.communityId);
|
2021-05-19 13:40:29 +02:00
|
|
|
this.communityService.getCommunity(this.community.communityId, true).subscribe(community => {
|
2022-07-08 20:59:17 +02:00
|
|
|
NotificationHandler.rise(community.shortTitle + ' has been <b>successfully created</b>');
|
2021-05-19 13:40:29 +02:00
|
|
|
callback(community);
|
|
|
|
});
|
|
|
|
}, error => {
|
2022-07-08 20:59:17 +02:00
|
|
|
NotificationHandler.rise('An error has occurred. Please try again later', 'danger');
|
2021-05-19 13:40:29 +02:00
|
|
|
if (errorCallback) {
|
|
|
|
errorCallback(error)
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else {
|
2022-07-02 13:45:34 +02:00
|
|
|
this.subscriptions.push(this.communityService.updateCommunity(this.properties.communityAPI + this.community.communityId, this.communityFb.getRawValue()).subscribe(() => {
|
2022-07-12 15:50:56 +02:00
|
|
|
this._clearCacheService.clearCache("Community updated");
|
2022-08-11 11:58:20 +02:00
|
|
|
this._clearCacheService.purgeBrowserCache("Community updated", this.community.communityId);
|
2021-05-19 13:40:29 +02:00
|
|
|
this.communityService.getCommunity(this.community.communityId, true).subscribe(community => {
|
2022-07-08 20:59:17 +02:00
|
|
|
NotificationHandler.rise(community.shortTitle + ' has been <b>successfully saved</b>');
|
2021-05-19 13:40:29 +02:00
|
|
|
callback(community);
|
|
|
|
});
|
|
|
|
}, error => {
|
2022-07-08 20:59:17 +02:00
|
|
|
NotificationHandler.rise('An error has occurred. Please try again later', 'danger');
|
2021-05-19 13:40:29 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileChangeEvent(event) {
|
|
|
|
if (event.target.files && event.target.files[0]) {
|
|
|
|
this.file = event.target.files[0];
|
|
|
|
if (this.file.type !== 'image/png' && this.file.type !== 'image/jpeg') {
|
|
|
|
this.uploadError = 'You must choose a file with type: image/png or image/jpeg!';
|
|
|
|
this.communityFb.get('isUpload').setValue(false);
|
|
|
|
this.communityFb.get('isUpload').markAsDirty();
|
|
|
|
this.removePhoto();
|
|
|
|
} else if (this.file.size > this.maxsize) {
|
|
|
|
this.uploadError = 'File exceeds size\'s limit! Maximum resolution is 256x256 pixels.';
|
|
|
|
this.communityFb.get('isUpload').setValue(false);
|
|
|
|
this.communityFb.get('isUpload').markAsDirty();
|
|
|
|
this.removePhoto();
|
|
|
|
} else {
|
|
|
|
this.uploadError = null;
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsDataURL(this.file);
|
|
|
|
reader.onload = () => {
|
|
|
|
this.photo = reader.result;
|
|
|
|
this.communityFb.get('isUpload').setValue(true);
|
|
|
|
this.communityFb.get('isUpload').markAsDirty();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initPhoto() {
|
|
|
|
if (this.communityFb.value.isUpload) {
|
|
|
|
this.photo = this.properties.utilsService + "/download/" + this.communityFb.get('logoUrl').value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removePhoto() {
|
|
|
|
if (this.file) {
|
|
|
|
if (typeof document != 'undefined') {
|
|
|
|
(<HTMLInputElement>document.getElementById("photo")).value = "";
|
|
|
|
}
|
|
|
|
this.initPhoto();
|
|
|
|
this.file = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove() {
|
|
|
|
this.communityFb.get('isUpload').setValue(false);
|
|
|
|
this.communityFb.get('isUpload').markAsDirty();
|
|
|
|
this.removePhoto();
|
|
|
|
this.communityFb.get('logoUrl').setValue(null);
|
|
|
|
if (this.community.isUpload) {
|
|
|
|
this.deleteCurrentPhoto = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public deletePhoto() {
|
2021-11-12 11:37:01 +01:00
|
|
|
if (this.community.logoUrl && this.community.isUpload) {
|
2021-11-26 10:51:24 +01:00
|
|
|
this.subscriptions.push(this.utilsService.deletePhoto(this.properties.utilsService + '/delete/community/' +
|
|
|
|
encodeURIComponent(this.community.communityId) + '/' + this.community.logoUrl).subscribe());
|
2021-05-19 13:40:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|