hino / app / controllers / news-management / create.js
create.js
Raw
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';

import config from '../../config/environment';
import Ember from 'ember';
import { schedule } from '@ember/runloop';

const { get, set } = Ember;

export default Controller.extend({
	newsManagement : service(),
	newsManagementData : service(),
	coreUserGroupData : service(),
	statusData : service(),
	coreAttachmentData : service(),
	dealerData : service(),
	newsManagementDealerData : service(),
	session : service(),
	errors : service(),
    didValidate: false,
    readonly:false,
	rightActionInProgress: false,
    disabled: true,
    placeholder:"DRAFT",
	sendTos: Array(),
	statuss: Array(),
	images: Array(),
	fileAttachments: Array(),
	dealers: Array(),

	init() {
		this._super(...arguments);
		var sendTos = this.coreUserGroupData.getList();
		this.set("sendTos", sendTos);

		var statuss = this.statusData.getList();
		this.set("statuss", statuss);
		
		var dealers = this.dealerData.getList();
		this.set("dealers", dealers);

		schedule("afterRender",this,function() {
			this.send("addNewsManagement");
		});
	},

	actions: {
		onChangeCombo(propertyName, itemCollection) {
			this.set(propertyName, itemCollection);
		},

		async saveNewsManagement(changeset) {
			this.set("rightActionInProgress", true);
			this.get('model')
				.validate()
				.then(async ({ validations }) => {
					this.set('didValidate', true); 
					if (validations.get('isValid')) {
						let imageDoc = get(changeset, 'image');
						if (imageDoc) {
							let fileImage = imageDoc;
							let currentModel = this.get('model');

							let imageAttachment = this.store.createRecord('core-attachment', {
								currentModel,
								originalFilename: get(fileImage, 'name'), 
								fileSize: get(fileImage, 'size')
							});
							if (fileImage.state != undefined && fileImage.state != "uploaded") {
								await fileImage.upload(config.APP.api.uploadPath, {
									headers: {Authorization: `Bearer ${this.session.data.authenticated.access_token}`}
								})
								.then(async response=> {
									set(imageAttachment, 'fileExtension', response.body.file_extension);
									set(imageAttachment, 'uploadedFilename', response.body.uploaded_filename);
									set(imageAttachment, 'originalFilename', response.body.original_filename);
									set(imageAttachment, 'url', response.headers.Location);

									await imageAttachment.save().then(() => {
										this.set('model.image', imageAttachment);
									});

								})
								.catch((error)=>{
									this.set("rightActionInProgress", false);
									console.log(error);
									alert("Failed to upload image");
								});
								console.log("image uploaded");
							}
						}

						let fileAttachmentDoc = get(changeset, 'fileAttachment');
						if (fileAttachmentDoc) {
							let fileFileAttachment = fileAttachmentDoc;
							let currentModel = this.get('model');

							let fileAttachmentAttachment = this.store.createRecord('core-attachment', {
								currentModel,
								originalFilename: get(fileFileAttachment, 'name'), 
								fileSize: get(fileFileAttachment, 'size')
							});
							if (fileFileAttachment.state != undefined && fileFileAttachment.state != "uploaded") {
								await fileFileAttachment.upload(config.APP.api.uploadPath, {
									headers: {Authorization: `Bearer ${this.session.data.authenticated.access_token}`}
								})
								.then(async response=> {
									set(fileAttachmentAttachment, 'fileExtension', response.body.file_extension);
									set(fileAttachmentAttachment, 'uploadedFilename', response.body.uploaded_filename);
									set(fileAttachmentAttachment, 'originalFilename', response.body.original_filename);
									set(fileAttachmentAttachment, 'url', response.headers.Location);

									await fileAttachmentAttachment.save().then(() => {
										this.set('model.fileAttachment', fileAttachmentAttachment);
									});

								})
								.catch((error)=>{
									this.set("rightActionInProgress", false);
									console.log(error);
									alert("Failed to upload fileAttachment");
								});
								console.log("fileAttachment uploaded");
							}
						}

						this.set('newsManagement.model.isApprovalData', false);
						this.newsManagementData.save(this.newsManagement.model).then(response => {
							if (response != undefined) {
                                this.transitionToRoute('news-management.detail', response.id); 
                                this.set("rightActionInProgress", false);
							}
						}) 
						.catch((e) => {
							this.set("rightActionInProgress", false);
                            this.set('errorMessages', this.errors.getErrorMessages(e));
							window.scrollTo(0, 0);
						});
					} else {
						this.set("rightActionInProgress", false);
					}
			});
		},

		addRowData(modal_type) {
			if (modal_type == "news-management-dealer") {
				let data = this.newsManagementDealerData.createRecord();
				this.newsManagement.addRow(data, "newsManagementDealer");
			}
		},
		deleteRowData(modal_type, data) {
			if (modal_type == "news-management-dealer") {
				this.newsManagement.deleteRow(data, "newsManagementDealer");
			}
		},
		uploadImage(changeset, propName, file) {
			changeset.set(propName, file);
		},
		addNewsManagement() {
			let data = this.newsManagementDealerData.createRecord();
			this.newsManagement.addRow(data, "newsManagementDealer");
		}
	},
});