taskDetailLogic.js 5 KB
import {observable} from 'mobx'
import {
	_getTask,
	_updateTask,
	_commentList,
	_commentCreate,
	_postFileIds,
	_getFiles,
	_del
} from "../service/AppService";
import moment from 'moment';
import 'moment/locale/zh-cn';
import {upload,ownerUserId} from "../service/rpc";
import Toast from "react-native-simple-toast";

export default class taskDetailLogic {
	@observable
	expireDate = ''
	@observable
	remindDate = ""
	@observable
	children = []
	@observable
	annex = []
	@observable
	annexResult = []
	@observable
	text = ''
	@observable
	name = ""
	@observable
	isStar = false
	@observable
	comment = []
	@observable
	userList = []
	id = ""
	objectType = ""
	objectId = ""
	objectCategory = ""
	rowVersion = ""
	ownerUserId = ""
	listId = ""
	task = null
	isDone = false
	ownerUserName = ""

	async getTask() {
		try {
			const result = await _getTask({id: this.id})
			const {errors, firstErrorMessage, task, subtaskList, userList} = result
			if (errors.length > 0) {
				// Toast.show(firstErrorMessage)
			} else {
				const {
					deadlineDate, hasDeadline, hasSubtask, id, isDone, isOverdue,
					isStar, listId, needReminding, objectCategory, objectDescription
					, objectId, objectName, objectType, ownerUserId, ownerUserName, remndingTime, rowVersion, subtaskCount, subtaskDoneCount,
				} = task
				this.task = task
				this.objectType = objectType
				this.objectId = objectId
				this.name = objectName
				this.objectCategory = objectCategory
				this.isStar = isStar
				this.rowVersion = rowVersion
				this.ownerUserId = ownerUserId
				this.ownerUserName = ownerUserName
				this.listId = listId
				if (needReminding) {
					this.remindDate = moment(remndingTime - 0).format("YYYY-MM-DD HH:mm:ss")
				}
				if (hasDeadline) {
					this.expireDate = moment(deadlineDate - 0).format("YYYY-MM-DD")
				}
				if (hasSubtask) {
					this.children.clear()
					subtaskList.map(v => {
						if (!v.isDone) {
							v.isDone = false
						}
						this.children.push(v)
					})
				}
				if (isDone) {
					this.isDone = isDone
				}
				this.userList.replace(userList)
			}
		} catch (e) {

		}


	}

	async getFiles() {
		try {
			const data = await _getFiles({
				businessId: this.id,
				businessType: "COLLABORATION_TASK",
				businessCategory: "TASK",
			})
			const {errors, firstErrorMessage,} = data
			if (errors.length > 0) {
				// Toast.show(firstErrorMessage)
			} else {
				const {attachmentList} = data
				this.annexResult.replace(attachmentList)
			}
		} catch (e) {

		}

	}

	async send() {
		try {
			const result = await _commentCreate({
				businessId: this.id,
				businessType: "TASKLIST_TASK",
				Anonymous: false,
				userId: ownerUserId,
				content: this.text
			})
			const {errors, firstErrorMessage,} = result
			if (errors.length > 0) {
				Toast.show(firstErrorMessage)
			} else {
				this.text = ""
			}
		} catch (e) {

		}

	}

	async uploadFile(pic) {
		this.annex.clear()
		if (pic instanceof Array) {
			pic.map(v => {
				const {path} = v
				this.annex.push("file://" + path)
			})
		} else {
			this.annex.push("file://" + pic.path)
		}
		try {
			const result = await Promise.all(this.annex.map(v => upload(v)))
			const result1 = await Promise.all(result.map(v => _postFileIds({
				fileId: v.id,
				businessId: this.id,
				businessType: "COLLABORATION_TASK",
				businessCategory: "TASK",
			})))
			console.log(result1)
		} catch (e) {

		}

	}

	async getCommentList() {
		try {
			const data = await _commentList({
				businessType: "TASKLIST_TASK",
				businessId: this.id,
			})
			const {result, errors, firstErrorMessage} = data
			if (errors.length > 0) {
				// Toast.show(firstErrorMessage)
			} else {
				this.comment.replace(result)
			}

		} catch (e) {

		}

	}

	async update() {
		const options =
			{
				id: this.id,
				objectType: this.objectType,
				objectId: this.objectId,
				objectCategory: this.objectCategory,
				objectName: this.name,
				isStar: this.isStar,
				rowVersion: this.rowVersion,
				ownerUserId: this.ownerUserId,
				listId: this.listId,
				isDone: this.isDone,
				ownerUserName: this.ownerUserName
			}
		if (!!this.expireDate) {
			options.hasDeadline = true
			options.deadlineDate = moment(new Date(this.expireDate).getTime()).format("YYYY-MM-DD HH:mm:ss")
		} else {
			options.hasDeadline = false
		}
		if (!!this.remindDate) {
				options.needReminding = true
				options.remndingTime = this.remindDate
		} else {
			options.needReminding = false
		}
		if (this.children.length > 0) {
			options.hasSubtask = true
			options.subtaskList = JSON.stringify(this.children.peek())
		} else {
			options.hasSubtask = false
		}
		const userList = JSON.stringify(this.userList.map(v => {
			return {userId: v.userId, username: v.userName}
		}))
		console.log(userList)
		options.userList = userList
		try {
			const data = await _updateTask(options)
		} catch (e) {

		}
	}

	async del(attachmentId) {
		try {
			const data = await _del({attachmentId})
			const {errors, firstErrorMessage} = data
			if (errors.length > 0) {
				Toast.show(firstErrorMessage)
			} else {

			}
		} catch (e) {

		}

	}
}