|
| 1 | +package net.wiringbits.webapp.utils.ui.web.components |
| 2 | + |
| 3 | +import net.wiringbits.webapp.utils.api.models.AdminGetTables |
| 4 | +import net.wiringbits.webapp.utils.ui.web.facades.reactadmin.ReactAdmin.useEditContext |
| 5 | +import net.wiringbits.webapp.utils.ui.web.facades.reactadmin._ |
| 6 | +import net.wiringbits.webapp.utils.ui.web.models.{ButtonAction, ColumnType, DataExplorerSettings} |
| 7 | +import net.wiringbits.webapp.utils.ui.web.utils.ResponseGuesser |
| 8 | +import org.scalajs.dom |
| 9 | +import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits.global |
| 10 | +import slinky.core.facade.{Fragment, ReactElement} |
| 11 | +import slinky.core.{FunctionalComponent, KeyAddingStage} |
| 12 | + |
| 13 | +import scala.scalajs.js |
| 14 | +import scala.util.{Failure, Success} |
| 15 | + |
| 16 | +object EditGuesser { |
| 17 | + case class Props(response: AdminGetTables.Response.DatabaseTable, dataExplorerSettings: DataExplorerSettings) |
| 18 | + |
| 19 | + def apply( |
| 20 | + response: AdminGetTables.Response.DatabaseTable, |
| 21 | + dataExplorerSettings: DataExplorerSettings |
| 22 | + ): KeyAddingStage = { |
| 23 | + component(Props(response, dataExplorerSettings)) |
| 24 | + } |
| 25 | + |
| 26 | + val component: FunctionalComponent[Props] = FunctionalComponent[Props] { props => |
| 27 | + val fields = ResponseGuesser.getTypesFromResponse(props.response) |
| 28 | + val inputs: Seq[ReactElement] = fields.map { field => |
| 29 | + field.`type` match { |
| 30 | + case ColumnType.Date => DateTimeInput(DateTimeInput.Props(source = field.name, disabled = field.disabled)) |
| 31 | + case ColumnType.Text => TextInput(TextInput.Props(source = field.name, disabled = field.disabled)) |
| 32 | + case ColumnType.Email => TextInput(TextInput.Props(source = field.name, disabled = field.disabled)) |
| 33 | + case ColumnType.Image => ImageField(ImageField.Props(source = field.name)) |
| 34 | + case ColumnType.Number => NumberInput(NumberInput.Props(source = field.name, disabled = field.disabled)) |
| 35 | + case ColumnType.Reference(reference, source) => |
| 36 | + ReferenceInput( |
| 37 | + ReferenceInput.Props( |
| 38 | + source = field.name, |
| 39 | + reference = reference, |
| 40 | + children = Seq(SelectInput(SelectInput.Props(optionText = source, disabled = field.disabled))) |
| 41 | + ) |
| 42 | + ) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + def onClick(action: ButtonAction, ctx: js.Dictionary[js.Any]): Unit = { |
| 47 | + val primaryKey = dom.window.location.hash.split("/").lastOption.getOrElse("") |
| 48 | + action.onClick(primaryKey).onComplete { |
| 49 | + case Failure(ex) => ex.printStackTrace() |
| 50 | + case Success(_) => refetch(ctx) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + def refetch(ctx: js.Dictionary[js.Any]): Unit = { |
| 55 | + val _ = ctx.get("refetch").map(_.asInstanceOf[js.Dynamic].apply()) |
| 56 | + } |
| 57 | + |
| 58 | + val tableAction = props.dataExplorerSettings.actions.find(_.tableName == props.response.name) |
| 59 | + |
| 60 | + def buttons(): Seq[ReactElement] = { |
| 61 | + val ctx = useEditContext() |
| 62 | + tableAction |
| 63 | + .map { x => |
| 64 | + x.actions.map { action => |
| 65 | + Button(Button.Props(onClick = () => onClick(action, ctx), children = Seq(action.text))) |
| 66 | + }: Seq[ReactElement] |
| 67 | + } |
| 68 | + .getOrElse(Seq.empty) |
| 69 | + } |
| 70 | + |
| 71 | + val actions = TopToolbar(TopToolbar.Props(children = buttons())) |
| 72 | + |
| 73 | + val deleteButton: ReactElement = if (props.response.canBeDeleted) DeleteButton() else Fragment() |
| 74 | + val toolbar: ReactElement = Toolbar( |
| 75 | + Toolbar.Props(children = |
| 76 | + Seq( |
| 77 | + SaveButton(), |
| 78 | + deleteButton |
| 79 | + ) |
| 80 | + ) |
| 81 | + ) |
| 82 | + |
| 83 | + Edit( |
| 84 | + Edit.Props( |
| 85 | + actions = actions(), |
| 86 | + children = Seq(SimpleForm(SimpleForm.Props(toolbar = toolbar, children = inputs))) |
| 87 | + ) |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
0 commit comments