feat: edit categories

This commit is contained in:
Richard Ramos 2021-05-17 11:05:45 -04:00 committed by Iuri Matias
parent 7128e08408
commit 872aa7794c
7 changed files with 154 additions and 14 deletions

View File

@ -255,6 +255,17 @@ QtObject:
error "Error creating the category", msg = e.msg
result = fmt"Error creating the category: {e.msg}"
proc editCommunityCategory*(self: CommunitiesView, communityId: string, categoryId: string, name: string, channels: string): string {.slot.} =
result = ""
try:
let channelSeq = map(parseJson(channels).getElems(), proc(x:JsonNode):string = x.getStr().replace(communityId, ""))
self.status.chat.editCommunityCategory(communityId, categoryId, name, channelSeq)
except Exception as e:
error "Error editing the category", msg = e.msg
result = fmt"Error editing the category: {e.msg}"
proc deleteCommunityCategory*(self: CommunitiesView, communityId: string, categoryId: string): string {.slot.} =
result = ""
try:

View File

@ -1,4 +1,4 @@
import NimQml, std/wrapnils
import NimQml, std/wrapnils, json
import ../../../status/[chat/chat, status]
import channels_list
import ../../../eventemitter
@ -182,6 +182,13 @@ QtObject:
QtProperty[QVariant] members:
read = getMembers
proc getChatIdsByCategory*(self: CommunityItemView, categoryId: string): string {.slot.} =
var res:seq[string] = @[]
for chat in self.communityItem.chats:
if chat.categoryId == categoryId:
res.add(chat.id)
return $(%*res)
proc getCommunityMembershipRequest*(self: CommunityItemView): QVariant {.slot.} =
result = newQVariant(self.communityMembershipRequestList)

View File

@ -450,6 +450,9 @@ proc createCommunityChannel*(self: ChatModel, communityId: string, name: string,
proc createCommunityCategory*(self: ChatModel, communityId: string, name: string, channels: seq[string]): CommunityCategory =
result = status_chat.createCommunityCategory(communityId, name, channels)
proc editCommunityCategory*(self: ChatModel, communityId: string, categoryId: string, name: string, channels: seq[string]) =
status_chat.editCommunityCategory(communityId, categoryId, name, channels)
proc deleteCommunityCategory*(self: ChatModel, communityId: string, categoryId: string) =
status_chat.deleteCommunityCategory(communityId, categoryId)

View File

@ -348,6 +348,18 @@ proc createCommunityCategory*(communityId: string, name: string, channels: seq[s
result.name = v["name"].getStr()
result.position = v{"position"}.getInt()
proc editCommunityCategory*(communityId: string, categoryId: string, name: string, channels: seq[string]) =
let rpcResult = callPrivateRPC("editCommunityCategory".prefix, %*[
{
"communityId": communityId,
"categoryId": categoryId,
"categoryName": name,
"chatIds": channels
}]).parseJSON()
if rpcResult.contains("error"):
raise newException(StatusGoException, rpcResult["error"]["message"].getStr())
proc reorderCommunityChat*(communityId: string, categoryId: string, chatId: string, position: int) =
let rpcResult = callPrivateRPC("reorderCommunityChat".prefix, %*[
{

View File

@ -100,6 +100,14 @@ Column {
icon.source: "../../../img/edit.svg"
icon.width: 20
icon.height: 20
onTriggered: {
openPopup(createCategoryPopup, {
communityId: chatsModel.communities.activeCommunity.id,
isEdit: true,
categoryId: model.categoryId,
categoryName: model.name
})
}
}
Separator {

View File

@ -8,11 +8,12 @@ import "../../../../shared/status"
Rectangle {
property string name: "channel-name"
property string channelId: "channel-id"
property bool isEdit: false
property alias checked: chk.checked
property string categoryId: ""
property var onItemChecked
property bool isHovered: false
id: container
visible: categoryId == ""
height: visible ? 52 : 0
width: 425
anchors.left: parent.left

View File

@ -8,17 +8,28 @@ import "../../../../shared/status"
ModalPopup {
property string communityId
property string categoryId
property string categoryName: ""
property var channels: []
property bool isEdit: false
readonly property int maxDescChars: 140
property string nameValidationError: ""
property bool isValid: nameInput.isValid
property var channels: []
id: popup
height: 453
onOpened: {
nameInput.text = "";
nameInput.text = isEdit ? categoryName : "";
if(isEdit){
channels = JSON.parse(chatsModel.communities.activeCommunity.getChatIdsByCategory(categoryId))
}
nameInput.forceActiveFocus(Qt.MouseFocusReason)
if(isEdit){
validate();
}
}
onClosed: destroy()
@ -27,7 +38,9 @@ ModalPopup {
return isValid
}
title: qsTr("New category")
title: isEdit ?
qsTr("Edit category") :
qsTr("New category")
ScrollView {
property ScrollBar vScrollBar: ScrollBar.vertical
@ -86,12 +99,13 @@ ModalPopup {
StatusSectionHeadline {
id: chatsTitle
text: qsTr("Chats")
text: qsTr("Channels")
anchors.top: sep.bottom
anchors.topMargin: Style.current.smallPadding
}
ListView {
id: communityChannelList
height: childrenRect.height
model: chatsModel.communities.activeCommunity.chats
anchors.top: chatsTitle.bottom
@ -102,6 +116,8 @@ ModalPopup {
name: model.name
channelId: model.id
categoryId: model.categoryId
checked: popup.isEdit ? channels.indexOf(model.id) > - 1 : false
visible: popup.isEdit ? model.categoryId == popup.categoryId || model.categoryId == "" : model.categoryId == ""
onItemChecked: function(channelId, itemChecked){
var idx = channels.indexOf(channelId)
if(itemChecked){
@ -117,14 +133,88 @@ ModalPopup {
}
}
Separator {
id: sep2
anchors.left: parent.left
anchors.right: parent.right
anchors.top: communityChannelList.bottom
anchors.topMargin: Style.current.padding
anchors.leftMargin: -Style.current.padding
anchors.rightMargin: -Style.current.padding
}
Item {
id: deleteCategory
anchors.top: sep2.bottom
anchors.topMargin: Style.current.padding
width: deleteBtn.width + deleteTxt.width + Style.current.padding
height: deleteBtn.height
StatusRoundButton {
id: deleteBtn
icon.name: "delete"
size: "medium"
type: "warn"
anchors.verticalCenter: parent.verticalCenter
}
StyledText {
id: deleteTxt
text: qsTr("Delete category")
color: Style.current.red
anchors.left: deleteBtn.right
anchors.leftMargin: Style.current.padding
anchors.verticalCenter: deleteBtn.verticalCenter
font.pixelSize: 15
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
openPopup(deleteCategoryConfirmationDialogComponent, {
title: qsTr("Delete %1 category").arg(categoryName),
confirmationText: qsTr("Are you sure you want to delete %1 category? Channels inside the category wont be deleted.").arg(categoryName)
})
}
}
Component {
id: deleteCategoryConfirmationDialogComponent
ConfirmationDialog {
btnType: "warn"
height: 216
showCancelButton: true
onClosed: {
destroy()
}
onCancelButtonClicked: {
close();
}
onConfirmButtonClicked: function(){
const error = chatsModel.communities.deleteCommunityCategory(chatsModel.communities.activeCommunity.id, popup.categoryId)
if (error) {
creatingError.text = error
return creatingError.open()
}
close();
popup.close()
}
}
}
}
}
}
footer: StatusButton {
enabled: popup.isValid
//% "Create"
text: qsTrId("create")
text: isEdit ?
qsTr("Save") :
qsTr("Create")
anchors.right: parent.right
onClicked: {
if (!validate()) {
@ -132,19 +222,27 @@ ModalPopup {
return
}
const error = chatsModel.communities.createCommunityCategory(communityId, Utils.filterXSS(nameInput.text), JSON.stringify(channels))
let error = ""
if(isEdit){
error = chatsModel.communities.editCommunityCategory(communityId, categoryId, Utils.filterXSS(nameInput.text), JSON.stringify(channels))
} else {
error = chatsModel.communities.createCommunityCategory(communityId, Utils.filterXSS(nameInput.text), JSON.stringify(channels))
}
if (error) {
creatingError.text = error
return creatingError.open()
categoryError.text = error
return categoryError.open()
}
popup.close()
}
MessageDialog {
id: creatingError
title: qsTr("Error creating the category")
id: categoryError
title: isEdit ?
qsTr("Error editing the category") :
qsTr("Error creating the category")
icon: StandardIcon.Critical
standardButtons: StandardButton.Ok
}