Skip to content

Commit

Permalink
add dynamic gps dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
capital-G committed Jul 13, 2023
1 parent 748caa5 commit 353b9c3
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 64 deletions.
6 changes: 4 additions & 2 deletions caster-back/operations.gql
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ subscription stream($graphUuid: UUID!) {
checked
label
key
callbackActions
}
}
buttons {
__typename
buttonType
sendVariablesOnClick
text
sendVariableOnClick
key
callbackActions
value
}
title
}
Expand Down
23 changes: 21 additions & 2 deletions caster-back/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ union AudioFileUploadResponse = AudioFile | InvalidAudioFile
"""A button which can also trigger a set of functionality."""
type Button {
text: String!
value: String!
key: String!
buttonType: ButtonType!
sendVariablesOnClick: Boolean!
sendVariableOnClick: String
callbackActions: [CallbackAction!]!
}

"""An enumeration."""
Expand All @@ -139,6 +140,23 @@ enum ButtonType {
DANGER
}

"""
Allows to add a pre-defined JavaScript callback to a button or a checkbox.
ACTIVATE_GPS_STREAMING Activates streaming of GPS coordinates
as :class:`~stream.models.StreamVariable`
SEND_VARIABLES Send all variables of the form / dialog to
the server.
SEND_VARIABLE Sends a single :class:`~stream.models.StreamVariable`
with the key/value of the where the callback is
attached to.
"""
enum CallbackAction {
ACTIVATE_GPS_STREAMING
SEND_VARIABLES
SEND_VARIABLE
}

"""Choice of foobar"""
enum CellType {
MARKDOWN
Expand All @@ -156,6 +174,7 @@ type Checkbox {
key: String!
label: String!
checked: Boolean!
callbackActions: [CallbackAction!]!
}

union Content = Text | Input | Checkbox
Expand Down
78 changes: 65 additions & 13 deletions caster-back/stream/frontend_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
The stream subscription makes it possible to yield the
"""

from dataclasses import field
from enum import Enum
from typing import List, Optional
from typing import List

import strawberry
import strawberry.django


@strawberry.enum
class ButtonType(Enum):
"""Derived from ElementPlus framework, see
`https://element-plus.org/en-US/component/button.html`_."""

DEFAULT = "default"
PRIMARY = "primary"
SUCCESS = "success"
Expand All @@ -25,6 +29,27 @@ class ButtonType(Enum):
DANGER = "danger"


@strawberry.enum
class CallbackAction(Enum):
"""Allows to add a pre-defined JavaScript callback to a button or a checkbox.
ACTIVATE_GPS_STREAMING Activates streaming of GPS coordinates
as :class:`~stream.models.StreamVariable`.
If the GPS request succeeds the dialog will be closed,
if not it the user will be forwarded to an error page
which describes the setup procedure for the OS.
SEND_VARIABLES Send all variables of the form / dialog to
the server.
SEND_VARIABLE Sends a single :class:`~stream.models.StreamVariable`
with the key/value of the where the callback is
attached to.
"""

ACTIVATE_GPS_STREAMING = "activate_gps_streaming"
SEND_VARIABLES = "send_variables"
SEND_VARIABLE = "send_variable"


@strawberry.type
class Text:
"""Displays plain text."""
Expand All @@ -41,6 +66,17 @@ class Checkbox:
key: str
label: str
checked: bool = False
callback_actions: List[CallbackAction] = field(default_factory=lambda: [])

@classmethod
def gps(
cls, label: str = "Is it OK to access your GPS?", key: str = "gps", **kwargs
):
return cls(
label=label,
key=key,
callback_actions=[CallbackAction.ACTIVATE_GPS_STREAMING],
)


@strawberry.type
Expand All @@ -49,7 +85,7 @@ class Input:
under the ``key`` as a :class:`~stream.models.StreamVariable`."""

key: str
label: str = "Info"
label: str = "input"
placeholder: str = "Please input"


Expand All @@ -58,36 +94,43 @@ class Button:
"""A button which can also trigger a set of functionality."""

text: str
value: str
key: str = "button"
button_type: ButtonType = ButtonType.DEFAULT

send_variables_on_click: bool = False
# will be used as key and its value will be set to true
send_variable_on_click: Optional[str] = None
callback_actions: List[CallbackAction] = field(
default_factory=lambda: [CallbackAction.SEND_VARIABLE]
)

@classmethod
def ok(
cls,
text: str = "OK",
send_variables_on_click: bool = True,
value="ok",
button_type=ButtonType.PRIMARY,
send_variable_on_click: str = "OK",
callback_actions: List[CallbackAction] = [
CallbackAction.SEND_VARIABLE,
CallbackAction.SEND_VARIABLES,
],
**kwargs
):
"""Constructor for a OK button which will"""
return cls(
text=text,
send_variables_on_click=send_variables_on_click,
value=value,
button_type=button_type,
send_variable_on_click=send_variable_on_click,
callback_actions=callback_actions,
**kwargs,
)

@classmethod
def cancel(
cls,
text: str = "Cancel",
value="cancel",
button_type=ButtonType.WARNING,
send_variable_on_click: str = "CANCEL",
callback_actions: List[CallbackAction] = [
CallbackAction.SEND_VARIABLE,
],
**kwargs
):
"""Constructor for a cancel button which will simply close
Expand All @@ -96,8 +139,9 @@ def cancel(
"""
return cls(
text=text,
value=value,
callback_actions=callback_actions,
button_type=button_type,
send_variable_on_click=send_variable_on_click,
**kwargs,
)

Expand All @@ -111,5 +155,13 @@ class Dialog:
"""Triggers a popup on the frontend of the listener."""

title: str
content: List[Content] # type: ignore
content: List[Content] = strawberry.field() # type: ignore
buttons: List[Button]

@classmethod
def gps(cls, title: str = "GPS"):
return cls(
title=title,
content=[Checkbox.gps()],
buttons=[],
)
35 changes: 29 additions & 6 deletions caster-editor/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ export type AudioFileUploadResponse = AudioFile | InvalidAudioFile;
/** A button which can also trigger a set of functionality. */
export type Button = {
buttonType: ButtonType;
sendVariableOnClick?: Maybe<Scalars["String"]>;
sendVariablesOnClick: Scalars["Boolean"];
callbackActions: Array<CallbackAction>;
key: Scalars["String"];
text: Scalars["String"];
value: Scalars["String"];
};

/** An enumeration. */
Expand All @@ -150,6 +151,23 @@ export enum ButtonType {
Warning = "WARNING",
}

/**
* Allows to add a pre-defined JavaScript callback to a button or a checkbox.
*
* ACTIVATE_GPS_STREAMING Activates streaming of GPS coordinates
* as :class:`~stream.models.StreamVariable`
* SEND_VARIABLES Send all variables of the form / dialog to
* the server.
* SEND_VARIABLE Sends a single :class:`~stream.models.StreamVariable`
* with the key/value of the where the callback is
* attached to.
*/
export enum CallbackAction {
ActivateGpsStreaming = "ACTIVATE_GPS_STREAMING",
SendVariable = "SEND_VARIABLE",
SendVariables = "SEND_VARIABLES",
}

/** Choice of foobar */
export enum CellType {
Audio = "AUDIO",
Expand All @@ -164,6 +182,7 @@ export enum CellType {
* saved **as a string** under ``key`` in a :class:`~stream.models.StreamVariable`.
*/
export type Checkbox = {
callbackActions: Array<CallbackAction>;
checked: Scalars["Boolean"];
key: Scalars["String"];
label: Scalars["String"];
Expand Down Expand Up @@ -982,6 +1001,7 @@ export type StreamSubscription = {
checked: boolean;
label: string;
key: string;
callbackActions: Array<CallbackAction>;
}
| {
__typename: "Input";
Expand All @@ -994,9 +1014,10 @@ export type StreamSubscription = {
buttons: Array<{
__typename: "Button";
buttonType: ButtonType;
sendVariablesOnClick: boolean;
text: string;
sendVariableOnClick?: string | null;
key: string;
callbackActions: Array<CallbackAction>;
value: string;
}>;
}
| { __typename: "NoStreamAvailable"; error: string }
Expand Down Expand Up @@ -1517,14 +1538,16 @@ export const StreamDocument = gql`
checked
label
key
callbackActions
}
}
buttons {
__typename
buttonType
sendVariablesOnClick
text
sendVariableOnClick
key
callbackActions
value
}
title
}
Expand Down
Loading

0 comments on commit 353b9c3

Please sign in to comment.