Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prompt function to the dialogue service. #1350

Merged
merged 5 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions yew/src/services/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! This module contains the implementation of a service
//! to show alerts and confirm dialogs in a browser.
//!
//! If you call these methods repeatably browsers tend to disable these options to give users
//! a better experience.

use cfg_if::cfg_if;
use cfg_match::cfg_match;
Expand Down Expand Up @@ -41,4 +44,45 @@ impl DialogService {
feature = "web_sys" => utils::window().confirm_with_message(message).unwrap(),
}
}

/// Prompts the user to input a message. In most browsers this will open an alert box with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a note about what this function returns. For example, when does it return None? I think we might be able to add a test for that case btw

Copy link
Contributor Author

@teymour-aldridge teymour-aldridge Jul 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, that's a good idea. I want to look into developing a set of integration tests (ideally from the examples) as well so that we can test things which can only be tested in a browser.

/// an input field where the user can input a message.
///
/// It is possible to supply a default value which will be used if the user does not provide
/// a value.
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt)
///
/// This method will `panic!` if there is an error in the process of trying to carry out this
/// operation.
///
/// Note that this function is blocking; no other code can be run on the thread while
/// the user inputs their message which means that the page will appear to have 'frozen'
/// while the user types in their message.
pub fn prompt(
message: &str,
#[cfg(feature = "web_sys")] default: Option<&str>,
#[cfg(feature = "std_web")] _: Option<&str>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this argument on std_web?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't, but it gets messy if functions have different signatures IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generate separate docs for stdweb so I think it should be ok, do you have other concerns?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I'll remove the argument on stdweb (+ add a note in the docstring).

Copy link
Member

@jstarry jstarry Jul 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't think about the doc string! You can check out yew/src/lib.rs for example on how to render conditional docs

) -> Option<String> {
cfg_if! {
if #[cfg(feature="web_sys")] {
if let Some(default) = default {
utils::window()
.prompt_with_message_and_default(message, default)
.expect("Couldn't read input.")
}
else {
utils::window()
.prompt_with_message(message)
.expect("Couldn't read input.")
}
} else if #[cfg(feature="std_web")] {
let value: Value = js! { return prompt(@{message}); };
match value {
Value::String(result) => Some(result),
_ => None,
}
}
}
}
}
12 changes: 7 additions & 5 deletions yew/src/services/fetch/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub struct FetchService {}

impl FetchService {
/// Sends a request to a remote server given a Request object and a callback
/// fuction to convert a Response object into a loop's message.
/// function to convert a Response object into a loop's message.
///
/// You may use a Request builder to build your request declaratively as on the
/// following examples:
Expand All @@ -183,7 +183,7 @@ impl FetchService {
/// .expect("Failed to build request.");
/// ```
///
/// The callback function can build a loop message by passing or analizing the
/// The callback function can build a loop message by passing or analyzing the
/// response body and metadata.
///
/// ```
Expand Down Expand Up @@ -220,8 +220,8 @@ impl FetchService {
/// ```
///
/// For a full example, you can specify that the response must be in the JSON format,
/// and be a specific serialized data type. If the mesage isn't Json, or isn't the specified
/// data type, then you will get a message indicating failure.
/// and be a specific serialized data type. If the message isn't JSON, or isn't the specified
/// data type, then you will get an error message.
///
/// ```
///# use yew::format::{Json, Nothing, Format};
Expand Down Expand Up @@ -555,7 +555,9 @@ impl WindowOrWorker {
} else if !global.worker().is_undefined() {
Self::Worker(global.unchecked_into())
} else {
panic!("Only supported in a browser or web worker");
panic!(
"Yew's `FetchService` only works when a `window` or `worker` object is available."
);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions yew/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ pub use self::websocket::WebSocketService;

use std::time::Duration;

/// An universal task of a service.
/// The task must be handled when it is cancelled.
/// A task is an ongoing process which is part of a Yew application.
///
/// All tasks must be handled when they are cancelled, which is why the `Drop` trait is required.
/// Tasks should cancel themselves in their implementation of the `Drop` trait.
pub trait Task: Drop {
/// Returns `true` if task is active.
fn is_active(&self) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion yew/src/services/reader/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl ReaderService {

/// Reads all bytes from a file and returns them with a callback.
pub fn read_file(&mut self, file: File, callback: Callback<FileData>) -> Result<ReaderTask> {
let file_reader = FileReader::new().map_err(|_| anyhow!("couldn't aquire file reader"))?;
let file_reader = FileReader::new().map_err(|_| anyhow!("couldn't acquire file reader"))?;
let reader = file_reader.clone();
let name = file.name();
let callback = move |_event: &Event| {
Expand Down