status-desktop/ui/app/AppLayouts/Chat/CommunityComponents/CreateCommunityPopup.qml

390 lines
14 KiB
QML
Raw Normal View History

2020-12-11 20:29:46 +00:00
import QtQuick 2.12
import QtQuick.Controls 2.3
import QtGraphicalEffects 1.13
import QtQuick.Dialogs 1.3
import "../../../../imports"
import "../../../../shared"
import "../../../../shared/status"
ModalPopup {
readonly property int maxDescChars: 140
property string nameValidationError: ""
property string colorValidationError: ""
property string selectedImageValidationError: ""
property string selectedImage: ""
property var imageDimensions: ({
aX: 0,
aY: 0,
bY: 1,
bY: 1
})
2020-12-17 13:24:33 +00:00
property QtObject community: chatsModel.activeCommunity
property bool isEdit: false
2020-12-11 20:29:46 +00:00
id: popup
height: 600
onOpened: {
2020-12-17 13:24:33 +00:00
nameInput.text = isEdit ? community.name : "";
descriptionTextArea.text = isEdit ? community.description : "";
nameValidationError = "";
colorValidationError = "";
selectedImageValidationError = "";
// TODO: add color and profile pic
// TODO: can privacy be changed?
2020-12-11 20:29:46 +00:00
nameInput.forceActiveFocus(Qt.MouseFocusReason)
}
function validate() {
nameValidationError = ""
colorValidationError = ""
selectedImageValidationError = ""
if (nameInput.text === "") {
2021-02-18 16:36:05 +00:00
//% "You need to enter a name"
nameValidationError = qsTrId("you-need-to-enter-a-name")
2020-12-11 20:29:46 +00:00
} else if (!(/^[a-z0-9\-\ ]+$/i.test(nameInput.text))) {
2021-02-18 16:36:05 +00:00
//% "Please restrict your name to letters, numbers, dashes and spaces"
nameValidationError = qsTrId("please-restrict-your-name-to-letters--numbers--dashes-and-spaces")
2020-12-11 20:29:46 +00:00
} else if (nameInput.text.length > 100) {
2021-02-18 16:36:05 +00:00
//% "Your name needs to be 100 characters or shorter"
nameValidationError = qsTrId("your-name-needs-to-be-100-characters-or-shorter")
2020-12-11 20:29:46 +00:00
}
if (selectedImage === "") {
2021-02-18 16:36:05 +00:00
//% "You need to select an image"
selectedImageValidationError = qsTrId("you-need-to-select-an-image")
2020-12-11 20:29:46 +00:00
}
// if (colorPicker.text === "") {
// colorValidationError = qsTr("You need to enter a color")
// } else if (!Utils.isHexColor(colorPicker.text)) {
// colorValidationError = qsTr("This field needs to be an hexadecimal color (eg: #4360DF)")
// }
2020-12-11 20:29:46 +00:00
return !nameValidationError && !descriptionTextArea.validationError && !colorValidationError
}
2020-12-17 13:24:33 +00:00
title: isEdit ?
2021-02-18 16:36:05 +00:00
//% "Edit community"
qsTrId("edit-community") :
//% "New community"
qsTrId("new-community")
2020-12-11 20:29:46 +00:00
ScrollView {
property ScrollBar vScrollBar: ScrollBar.vertical
id: scrollView
anchors.fill: parent
rightPadding: Style.current.bigPadding
anchors.rightMargin: - Style.current.bigPadding
leftPadding: Style.current.bigPadding
anchors.leftMargin: - Style.current.bigPadding
2020-12-11 20:29:46 +00:00
contentHeight: content.height
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
ScrollBar.vertical.policy: ScrollBar.AlwaysOn
clip: true
function scrollBackUp() {
vScrollBar.setPosition(0)
}
Item {
id: content
height: childrenRect.height
width: parent.width
Input {
id: nameInput
2021-02-18 16:36:05 +00:00
//% "Name your community"
label: qsTrId("name-your-community")
//% "A catchy name"
placeholderText: qsTrId("name-your-community-placeholder")
2020-12-11 20:29:46 +00:00
validationError: popup.nameValidationError
}
StyledTextArea {
id: descriptionTextArea
2021-02-18 16:36:05 +00:00
//% "Give it a short description"
label: qsTrId("give-a-short-description-community")
//% "What your community is about"
placeholderText: qsTrId("what-your-community-is-about")
//% "The description cannot exceed 140 characters"
validationError: descriptionTextArea.text.length > maxDescChars ? qsTrId("the-description-cannot-exceed-140-characters") : ""
2020-12-11 20:29:46 +00:00
anchors.top: nameInput.bottom
anchors.topMargin: Style.current.bigPadding
customHeight: 88
2020-12-17 13:24:33 +00:00
textField.selectByMouse: true
textField.wrapMode: TextEdit.Wrap
2020-12-11 20:29:46 +00:00
}
StyledText {
id: charLimit
text: `${descriptionTextArea.text.length}/${maxDescChars}`
anchors.top: descriptionTextArea.bottom
anchors.topMargin: !descriptionTextArea.validationError ? 5 : - Style.current.smallPadding
anchors.right: descriptionTextArea.right
font.pixelSize: 12
color: !descriptionTextArea.validationError ? Style.current.textColor : Style.current.danger
}
StyledText {
id: thumbnailText
2021-02-18 16:36:05 +00:00
//% "Thumbnail image"
text: qsTrId("thumbnail-image")
2020-12-11 20:29:46 +00:00
anchors.top: descriptionTextArea.bottom
anchors.topMargin: Style.current.smallPadding
font.pixelSize: 15
color: Style.current.secondaryText
}
Rectangle {
id: addImageButton
color: imagePreview.visible ? "transparent" : Style.current.inputBackground
width: 128
height: width
radius: width / 2
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: thumbnailText.bottom
anchors.topMargin: Style.current.padding
FileDialog {
id: imageDialog
//% "Please choose an image"
title: qsTrId("please-choose-an-image")
folder: shortcuts.pictures
nameFilters: [
//% "Image files (*.jpg *.jpeg *.png)"
qsTrId("image-files----jpg---jpeg---png-")
]
onAccepted: {
popup.selectedImage = imageDialog.fileUrls[0]
imageCropperModal.open()
2020-12-11 20:29:46 +00:00
}
}
Image {
id: imagePreview
visible: !!popup.selectedImage
source: popup.selectedImage
fillMode: Image.PreserveAspectCrop
width: parent.width
height: parent.height
layer.enabled: true
layer.effect: OpacityMask {
maskSource: Rectangle {
anchors.centerIn: parent
width: imagePreview.width
height: imagePreview.height
radius: imagePreview.width / 2
}
}
}
Item {
id: addImageCenter
visible: !imagePreview.visible
width: uploadText.width
height: childrenRect.height
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
SVGImage {
id: imageImg
source: "../../../img/images_icon.svg"
width: 20
height: 18
anchors.horizontalCenter: parent.horizontalCenter
}
StyledText {
id: uploadText
2021-02-18 16:36:05 +00:00
//% "Upload"
text: qsTrId("upload")
2020-12-11 20:29:46 +00:00
anchors.top: imageImg.bottom
anchors.topMargin: 5
font.pixelSize: 15
color: Style.current.secondaryText
}
}
Rectangle {
color: Style.current.primary
width: 40
height: width
radius: width / 2
anchors.top: parent.top
anchors.right: parent.right
anchors.rightMargin: Style.current.halfPadding
SVGImage {
source: "../../../img/plusSign.svg"
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
width: 13
height: 13
}
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: imageDialog.open()
}
ImageCropperModal {
id: imageCropperModal
selectedImage: popup.selectedImage
onCropFinished: {
imageDimensions.aX = aX
imageDimensions.aY = aY
imageDimensions.bX = bX
imageDimensions.bY = bY
2020-12-11 20:29:46 +00:00
}
}
}
// TODO re-add color picker when status-go supports it
// Input {
// id: colorPicker
// label: qsTr("Community color")
// placeholderText: qsTr("Pick a color")
// anchors.top: addImageButton.bottom
// anchors.topMargin: Style.current.smallPadding
// validationError: popup.colorValidationError
// StatusIconButton {
// icon.name: "caret"
// iconRotation: -90
// iconColor: Style.current.textColor
// icon.width: 13
// icon.height: 7
// anchors.right: parent.right
// anchors.rightMargin: Style.current.smallPadding
// anchors.top: parent.top
// anchors.topMargin: colorPicker.textField.height / 2 - height / 2 + Style.current.bigPadding
// onClicked: colorDialog.open()
// }
// ColorDialog {
// id: colorDialog
// title: qsTr("Please choose a color")
// onAccepted: {
// colorPicker.text = colorDialog.color
// }
// }
// }
2020-12-11 20:29:46 +00:00
Separator {
id: separator1
anchors.top: addImageButton.bottom
2020-12-17 13:24:33 +00:00
anchors.topMargin: isEdit ? 0 : Style.current.bigPadding
visible: !isEdit
2020-12-11 20:29:46 +00:00
}
StatusSettingsLineButton {
id: membershipRequirementSetting
2020-12-11 20:29:46 +00:00
anchors.top: separator1.bottom
anchors.topMargin: Style.current.halfPadding
text: qsTr("Membership requirement")
currentValue: {
switch (membershipRequirementSettingPopup.checkedMembership) {
case Constants.communityChatInvitationOnlyAccess: return qsTr("Require invite from another member")
case Constants.communityChatOnRequestAccess: return qsTr("Require approval")
default: return qsTr("No requirement")
}
2020-12-11 20:29:46 +00:00
}
onClicked: {
membershipRequirementSettingPopup.open()
2020-12-11 20:29:46 +00:00
}
}
StyledText {
2020-12-17 13:24:33 +00:00
visible: !isEdit
height: visible ? implicitHeight : 0
2020-12-11 20:29:46 +00:00
id: privateExplanation
anchors.top: membershipRequirementSetting.bottom
2020-12-11 20:29:46 +00:00
wrapMode: Text.WordWrap
anchors.topMargin: isEdit ? 0 : Style.current.halfPadding
2020-12-11 20:29:46 +00:00
width: parent.width
text: qsTr("You can require new members to meet certain criteria before they can join. This can be changed at any time")
2020-12-11 20:29:46 +00:00
}
StatusSettingsLineButton {
id: ensOnlySwitch
anchors.top: privateExplanation.bottom
anchors.topMargin: Style.current.padding
text: qsTr("Require ENS username")
isSwitch: true
onClicked: switchChecked = checked
}
StyledText {
visible: !isEdit
height: visible ? implicitHeight : 0
id: ensExplanation
anchors.top: ensOnlySwitch.bottom
wrapMode: Text.WordWrap
anchors.topMargin: isEdit ? 0 : Style.current.halfPadding
width: parent.width
text: qsTr("Your community requires an ENS username to be able to join")
}
}
MembershipRequirementPopup {
id: membershipRequirementSettingPopup
2020-12-11 20:29:46 +00:00
}
}
2020-12-11 20:38:10 +00:00
2020-12-11 20:29:46 +00:00
footer: StatusButton {
2020-12-17 13:24:33 +00:00
text: isEdit ?
2021-02-18 16:36:05 +00:00
//% "Edit"
qsTrId("edit") :
//% "Create"
qsTrId("create")
2020-12-11 20:29:46 +00:00
anchors.right: parent.right
onClicked: {
if (!validate()) {
scrollView.scrollBackUp()
return
}
2020-12-17 13:24:33 +00:00
let error = false;
if(isEdit) {
console.log("TODO: implement this (not available in status-go yet)");
} else {
error = chatsModel.createCommunity(Utils.filterXSS(nameInput.text),
Utils.filterXSS(descriptionTextArea.text),
membershipRequirementSettingPopup.checkedMembership,
ensOnlySwitch.switchChecked,
popup.selectedImage,
imageDimensions.aX,
imageDimensions.aY,
imageDimensions.bX,
imageDimensions.bY)
2020-12-17 13:24:33 +00:00
}
2020-12-11 20:29:46 +00:00
if (error) {
creatingError.text = error
return creatingError.open()
}
// TODO Open the community once we have designs for it
popup.close()
}
MessageDialog {
id: creatingError
2021-02-18 16:36:05 +00:00
//% "Error creating the community"
title: qsTrId("error-creating-the-community")
2020-12-11 20:29:46 +00:00
icon: StandardIcon.Critical
standardButtons: StandardButton.Ok
}
}
}