Skip to content

Commit

Permalink
Fix poll form (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Apr 18, 2023
1 parent a2ae417 commit 8278c78
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 37 deletions.
13 changes: 1 addition & 12 deletions frontend/src/components/MarkupEditor/uploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import misago from "../../"
import ajax from "../../services/ajax"
import snackbar from "../../services/snackbar"
import formatFilesize from "../../utils/file-size"
import getRandomString from "../../utils/getRandomString"

const ID_LEN = 32

Expand Down Expand Up @@ -73,16 +74,4 @@ const uploadFile = (file, setState) => {
)
}

const ALPHA = "12345678990abcdefghijklmnopqrstuvwxyz"
const ALPHA_LEN = ALPHA.length

const getRandomString = (len) => {
const chars = []
for (let i = 0; i < len; i++) {
const index = Math.floor(Math.random() * ALPHA_LEN)
chars.push(ALPHA[index])
}
return chars.join("")
}

export default uploadFile
28 changes: 13 additions & 15 deletions frontend/src/components/poll/form/choices-control.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from "react"
import getRandomString from "../../../utils/getRandomString"

const HASH_LENGTH = 12

export default class extends React.Component {
onAdd = () => {
let choices = this.props.choices.slice()
choices.push({
hash: generateRandomHash(),
hash: getRandomString(HASH_LENGTH),
label: "",
})

Expand Down Expand Up @@ -65,9 +68,15 @@ export class PollChoice extends React.Component {
}

onDelete = () => {
const deleteItem = window.confirm(
pgettext("thread poll", "Are you sure you want to remove this choice?")
)
const deleteItem =
this.props.choice.label.length === 0
? true
: window.confirm(
pgettext(
"thread poll",
"Are you sure you want to remove this choice?"
)
)
if (deleteItem) {
this.props.onDelete(this.props.choice.hash)
}
Expand Down Expand Up @@ -97,14 +106,3 @@ export class PollChoice extends React.Component {
)
}
}

export function generateRandomHash() {
let randomHash = ""
while (randomHash.length != 12) {
randomHash = Math.random()
.toString(36)
.replace(/[^a-zA-Z0-9]+/g, "")
.substr(1, 12)
}
return randomHash
}
10 changes: 5 additions & 5 deletions frontend/src/components/poll/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ export default class extends Form {
}

setChoices = (choices) => {
const errors = Object.assign({}, errors, { choices: null })

this.setState({
choices,
errors,
this.setState((state) => {
return {
choices,
errors: Object.assign({}, state.errors, { choices: null }),
}
})
}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/poll/results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (props) {
<Options
isPollOver={props.isPollOver}
poll={props.poll}
edit={props.edit}
showVoting={props.showVoting}
thread={props.thread}
/>
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/thread/ThreadPagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const ThreadPagination = ({ baseUrl, posts, scrollToTop }) => (
type="button"
onClick={resetScroll}
>
<span className="material-icon">
arrow_upward
</span>
<span className="material-icon">arrow_upward</span>
</button>
)}
{posts.isLoaded && posts.first ? (
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/utils/getRandomString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const ALPHA = "12345678990abcdefghijklmnopqrstuvwxyz"
export const ALPHA_LEN = ALPHA.length

export default function getRandomString(len) {
const chars = []
for (let i = 0; i < len; i++) {
const index = Math.floor(Math.random() * ALPHA_LEN)
chars.push(ALPHA[index])
}
return chars.join("")
}
2 changes: 1 addition & 1 deletion misago/static/misago/js/misago.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion misago/static/misago/js/misago.js.map

Large diffs are not rendered by default.

0 comments on commit 8278c78

Please sign in to comment.