Wire up delete button.

This commit is contained in:
Dillon Kearns 2022-08-12 18:44:59 -07:00
parent 8e84792621
commit d4f2f3e35d
2 changed files with 107 additions and 23 deletions

View File

@ -30,6 +30,21 @@ import Shared
import View exposing (View)
route : StatefulRoute RouteParams Data ActionData Model Msg
route =
RouteBuilder.serverRender
{ head = head
, data = data
, action = action
}
|> RouteBuilder.buildWithLocalState
{ view = view
, update = update
, subscriptions = subscriptions
, init = init
}
type alias Model =
{ visibility : String
}
@ -53,21 +68,6 @@ type alias ActionData =
{}
route : StatefulRoute RouteParams Data ActionData Model Msg
route =
RouteBuilder.serverRender
{ head = head
, data = data
, action = action
}
|> RouteBuilder.buildWithLocalState
{ view = view
, update = update
, subscriptions = subscriptions
, init = init
}
init :
Maybe PageUrl
-> Shared.Model
@ -131,7 +131,7 @@ data routeParams =
action : RouteParams -> Request.Parser (DataSource (Response ActionData ErrorPage))
action routeParams =
MySession.withSession
(Request.formData [ newItemForm, completeItemForm ])
(Request.formData [ newItemForm, completeItemForm, deleteItemForm ])
(\formResult session ->
let
okSessionThing : Session
@ -140,7 +140,51 @@ action routeParams =
|> Result.withDefault Nothing
|> Maybe.withDefault Session.empty
in
case formResult of
case formResult |> Debug.log "formResult" of
Ok (DeleteItem itemId) ->
okSessionThing
|> Session.get "sessionId"
|> Maybe.map Data.Session.get
|> Maybe.map Request.Hasura.dataSource
|> Maybe.map
(DataSource.andThen
(\maybeUserSession ->
let
bar : Maybe Uuid
bar =
maybeUserSession
|> Maybe.map .id
in
case bar of
Nothing ->
DataSource.succeed
( okSessionThing
, Response.render {}
)
Just userId ->
Data.Todo.delete
({ userId = userId
, itemId = Uuid itemId
}
|> Debug.log "@delete"
)
|> Request.Hasura.mutationDataSource
|> DataSource.map
(\() ->
( okSessionThing
, Response.render {}
)
)
)
)
|> Maybe.withDefault
(DataSource.succeed
( okSessionThing
, Response.render {}
)
)
Ok (ToggleItem ( newCompleteValue, itemId )) ->
okSessionThing
|> Session.get "sessionId"
@ -287,10 +331,12 @@ newItemForm =
}
)
|> Form.field "description" (Field.text |> Field.required "Must be present")
|> Form.hiddenKind ( "kind", "new-item" ) "Expected kind"
type Action
= CreateItem String
| DeleteItem String
| ToggleItem ( Bool, String )
@ -326,6 +372,28 @@ completeItemForm =
(Field.checkbox
|> Field.withInitialValue (.completed >> not >> Form.Value.bool)
)
|> Form.hiddenKind ( "kind", "complete" ) "Expected kind"
deleteItemForm : Form.HtmlForm String Action Todo Msg
deleteItemForm =
Form.init
(\todoId ->
{ combine =
Validation.succeed DeleteItem
|> Validation.andMap todoId
, view =
\formState ->
[ button [ class "destroy" ] []
]
}
)
|> Form.hiddenField "todoId"
(Field.text
|> Field.required "Must be present"
|> Field.withInitialValue (.id >> uuidToString >> Form.Value.string)
)
|> Form.hiddenKind ( "kind", "delete" ) "Expected kind"
@ -407,12 +475,12 @@ viewEntry app todo =
[--onDoubleClick (EditingEntry todo.id True)
]
[ text todo.description ]
, button
[ class "destroy"
--, onClick (Delete todo.id)
]
[]
, deleteItemForm
|> Form.toDynamicFetcher ("delete-" ++ uuidToString todo.id)
|> Form.renderHtml []
Nothing
app
todo
]
, input
[ class "edit"

View File

@ -92,3 +92,19 @@ eqUuid : Uuid -> Api.InputObject.Uuid_comparison_exp
eqUuid equalTo =
Api.InputObject.buildUuid_comparison_exp
(eq equalTo)
delete : { userId : Uuid, itemId : Uuid } -> SelectionSet () RootMutation
delete { userId, itemId } =
Api.Mutation.delete_todos
{ where_ =
Api.InputObject.buildTodos_bool_exp
(\opts ->
{ opts
| id = Present (eqUuid itemId)
, user_id = Present (eqUuid userId)
}
)
}
SelectionSet.empty
|> SelectionSet.nonNullOrFail