Skip to content
Snippets Groups Projects
Commit c394169f authored by Tóth Miklós Tibor's avatar Tóth Miklós Tibor :shrug:
Browse files

Misc improvements

parent f2428548
Branches
No related tags found
No related merge requests found
import androidx.compose.runtime.Composable
import org.jetbrains.compose.web.dom.Div
@Composable
fun divFloatRight(content: @Composable () -> Unit) {
Div({ classes("uk-float-right", "uk-margin-right") }) {
content()
}
}
\ No newline at end of file
......@@ -23,7 +23,7 @@ fun main() {
renderComposable(root = rootDiv) {
var state by remember { mutableStateOf<State>(Ready) }
Div({ classes("uk-float-right", "uk-margin-right") }) {
divFloatRight {
val magicId = "csv-magic"
Button({
classes("uk-button", "uk-button-small", "uk-button-success")
......
......@@ -3,8 +3,13 @@ import kotlinx.browser.document
import kotlinx.browser.window
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.compose.web.attributes.InputType
import org.jetbrains.compose.web.attributes.disabled
import org.jetbrains.compose.web.css.background
import org.jetbrains.compose.web.css.color
import org.jetbrains.compose.web.css.rgb
import org.jetbrains.compose.web.dom.*
import org.w3c.dom.HTMLAnchorElement
import org.w3c.dom.HTMLElement
......@@ -24,53 +29,123 @@ fun modal(setState: (State) -> Unit) {
Text("CSV import")
}
H4 {
Text("Paraméterek")
Text("Mi is ez?")
}
Text("Válassz ki egy CSV filet, ami megfelel a következő követelményeknek:")
Ul {
Li { Text("az első sorában a pontozási elvek szerepelnek") }
}
Ul {
Li { Text("az első oszlopa az emberek neveit tartalmazza") }
}
Ul {
Li { Text("a közte lévő rész értelemszerűen van kitöltve") }
}
Ul {
Li { Text("tartalmazhat a táblázat szumma oszlopokat, " +
"azok automatikusan ignorálva lesznek, amennyiben tartalmazzák a szumma szót") }
}
P { Text("Példa táblázat:") }
Table({classes("uk-table")}) {
Thead { Tr {
Th { Text("Név") }
Th { Text("Gyűlésen alvás") }
Th { Text("Közös liftezés") }
Th { Text("Szumma munka") }
} }
Tbody { Tr {
Td { Text("Teszt Elek") }
Td { Text("5") }
Td { Text("8") }
Td { Text("13") }
} }
Tbody { Tr {
Td { Text("Techno Kolos ") }
Td { Text("7") }
Td { Text("3") }
Td { Text("10") }
} }
}
params()
Hr { }
val scope = rememberCoroutineScope()
var csvTable by remember { mutableStateOf<CSVTable?>(null) }
val valassz = "hint: válassz ki fájlt"
var hint by remember { mutableStateOf(valassz) }
Input(type = InputType.File, attrs = {
classes("uk-button", "uk-button-small")
onChange {
if (it.value.isBlank()) {
hint = valassz
return@onChange
}
scope.launch {
val content = it.target.getFileContents()
content?.let { csv ->
try {
csvTable = CSVTable(csv)
} catch (e: Exception) {
hint = "hibás CSV fájl (infó a console-ban)"
e.printStackTrace()
}
hint = ""
}
}
}
})
Hr { }
divFloatRight {
Button({
classes("uk-button", "uk-button-small", "uk-button-danger")
classes("uk-button", "uk-button-primary")
onClick {
scope.launch {
PekTable.clear { setState(ImportInProgress(it)) }
try {
csvTable?.process { setState(ImportInProgress(it)) }
} catch (e: ProcessingError) {
delay(100)
window.alert(e.toString())
}
setState(Ready)
}
(document.getElementById(closeId) as HTMLElement).click()
}
if (hint.isNotBlank()) {
disabled()
}
}) {
Text("PéKen lévő adatok törlése")
Text("Zsa!")
}
}
divFloatRight {
Button({
classes("uk-button", "uk-button-small", "uk-button-primary")
classes("uk-button", "uk-button-danger")
onClick {
scope.launch {
try {
csvTable?.process { setState(ImportInProgress(it)) }
} catch (e: ProcessingError) {
window.alert(e.toString())
}
PekTable.clear { setState(ImportInProgress(it)) }
setState(Ready)
}
(document.getElementById(closeId) as HTMLElement).click()
}
}) {
Text("Zsa!")
Text("PéKen lévő adatok törlése")
}
}
divFloatRight {
Button({
classes("uk-button")
style {
background("#0000")
color(rgb(0x55, 0x55, 0x55))
}
disabled()
}) {
Text(hint)
}
}
Br { }
}
}
\ No newline at end of file
import kotlinx.coroutines.delay
import kotlin.math.max
class ProcessingError(val errors: List<String>): Exception(errors.toString()) {
override fun toString(): String {
......@@ -11,9 +11,9 @@ suspend fun CSVTable.process(
progress: (Double) -> Unit = {}
) {
progress(0.0)
val principleMappings = principles.map {
it to PekTable.principles.findClosestMatch(it)
}.toMap()
val principleMappings = principles.associateWith {
PekTable.principles.findClosestMatch(it)
}
val errors = mutableListOf<String>()
......@@ -21,7 +21,7 @@ suspend fun CSVTable.process(
val pekPerson = PekTable.people.findClosestMatch(person)
if (pekPerson == null) {
errors += "HIBA: Nem található '$person'"
errors += "Nem található '$person'"
return@forEachIndexed
}
principles.forEachIndexed { j, principle ->
......@@ -33,10 +33,10 @@ suspend fun CSVTable.process(
progress(calcProgress(i, j, people.size, principles.size))
if (person != pekPerson) {
errors += "WARN: assuming '$person' == '$pekPerson'"
errors += "Assuming '$person' == '$pekPerson'"
}
if (principle != pekPrinciple) {
errors += "WARN: assuming '$principle' == '$pekPrinciple'"
errors += "Assuming '$principle' == '$pekPrinciple'"
}
}
}
......@@ -49,9 +49,14 @@ fun List<String>.findClosestMatch(
): String? {
val parts = string.split(" ", "-")
return this.map {
val currParts = it.split(" ", "-")
val needleInHaystack = currParts.count { part -> string.contains(part) }
val haystackInNeedle = parts.count { part -> it.contains(part) }
it to when {
it == string -> Int.MAX_VALUE
else -> parts.count { part -> it.contains(part) }
else -> max(needleInHaystack, haystackInNeedle)
}
}.filter { it.second != 0 }.maxByOrNull { it.second }?.first
}
\ No newline at end of file
......@@ -96,10 +96,28 @@ suspend fun PekTable.clear(
private val saveIconElem = document.getElementById("save-icon") as HTMLDivElement
val isUpdating get() = saveIconElem.style.display == "block"
suspend fun pekDelay() {
delay(10)
while (isUpdating) { // még sül
delay(100)
fun observeSaveChange(changed: () -> Unit): MutationObserver {
val observer = MutationObserver { _, _ ->
changed()
}
observer.observe(saveIconElem, MutationObserverInit(
attributes = true,
attributeFilter = arrayOf("style")
))
return observer
}
suspend fun pekDelay() = suspendCoroutine<Unit> { cont ->
lateinit var observer: MutationObserver
observer = observeSaveChange {
if (!isUpdating) {
cont.resume(Unit)
observer.disconnect()
}
}
if (!isUpdating) {
cont.resume(Unit)
observer.disconnect()
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment