export default { name: 'ManageGroupParticipants', data() { return { loading: false, group: '', action: 'add', // add, remove, promote, demote participants: ['', ''], }; }, computed: { group_id() { return `${this.group}${window.TYPEGROUP}`; }, }, methods: { openModal() { $('#modalGroupAddParticipant').modal({ onApprove: function() { return false; }, }).modal('show'); }, isValidForm() { if (this.participants.length < 1 || this.participants.every(participant => !participant.trim())) { return false; } return true; }, handleAddParticipant() { this.participants.push(''); }, handleDeleteParticipant(index) { this.participants.splice(index, 1); }, async handleSubmit() { if (!this.isValidForm() || this.loading) { return; } try { let response = await this.submitApi(); showSuccessInfo(response); $('#modalGroupAddParticipant').modal('hide'); } catch (err) { showErrorInfo(err); } }, async submitApi() { this.loading = true; try { const payload = { group_id: this.group_id, // convert participant become list of string participants: this.participants.filter(participant => participant !== '').map(participant => `${participant}`), }; let response; switch (this.action) { case 'add': response = await window.http.post(`/group/participants`, payload); break; case 'remove': response = await window.http.post(`/group/participants/remove`, payload); break; case 'promote': response = await window.http.post(`/group/participants/promote`, payload); break; case 'demote': response = await window.http.post(`/group/participants/demote`, payload); break; } this.handleReset(); return response.data.message; } catch (error) { if (error.response) { throw new Error(error.response.data.message); } throw new Error(error.message); } finally { this.loading = false; } }, handleReset() { this.group = ''; this.action = 'add'; this.participants = ['', '']; }, }, template: `