Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit 55ab515

Browse files
committed
update
1 parent 4f65f1a commit 55ab515

8 files changed

+3687
-117
lines changed

.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-unknown-unknown"

Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ codegen-units = 1
1111
lto = true
1212

1313
[dependencies]
14+
anyhow = "1.0"
1415
console_error_panic_hook = "0.1.6"
1516
js-sys = "0.3.25"
1617
log = "0.4"
18+
http = "0.2"
1719
serde = "1"
1820
serde_derive = "1"
1921
serde_json = "1"
20-
serde_urlencoded = "0.6.1"
21-
strum = "0.17"
22-
strum_macros = "0.17"
22+
serde_urlencoded = "0.7.0"
23+
strum = "0.20"
24+
strum_macros = "0.20.1"
2325
wasm-bindgen = "0.2.58"
2426
wasm-bindgen-futures = "0.4.18"
2527
wasm-logger = "0.2"

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
},
2424
"devDependencies": {
2525
"@wasm-tool/wasm-pack-plugin": "^1.3.1",
26-
"copy-webpack-plugin": "^6.2.1",
27-
"cross-env": "^7.0.2",
26+
"copy-webpack-plugin": "^7.0.0",
27+
"cross-env": "^7.0.3",
2828
"css-loader": "^5.0.0",
2929
"style-loader": "^2.0.0",
3030
"wasm-pack": "^0.9.1",
31-
"webpack": "^5.2.0",
31+
"webpack": "^5.11.0",
3232
"webpack-cli": "^4.1.0",
33-
"webpack-dev-server": "^3.11.0"
33+
"webpack-dev-server": "^4.0.0-beta.0"
3434
}
3535
}

src/app.rs

+65-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
1-
use crate::autocomplete_service::AutocompleteService;
1+
use http::{Request, Response};
22
use serde_derive::{Deserialize, Serialize};
3+
use serde_json::value::Value;
34
use serde_urlencoded;
45
use yew::{
56
events::InputData,
6-
format::Json,
7+
format::{Json, Nothing},
78
html,
8-
services::storage::{Area, StorageService},
9+
services::{
10+
fetch::{FetchOptions, FetchService, FetchTask},
11+
storage::{Area, StorageService},
12+
},
913
Component, ComponentLink, Href, Html, Renderable, ShouldRender,
1014
};
1115

1216
pub struct App {
1317
suggestions: Vec<String>,
1418
value: String,
15-
autocomplete_service: AutocompleteService,
19+
autcomplete_fetcher: Option<FetchTask>,
1620
link: ComponentLink<Self>,
21+
api_url: String,
1722
}
1823

1924
pub enum Msg {
2025
Input(String),
2126
CompleteUpdate(Vec<String>),
22-
Nop,
27+
Noop,
28+
}
29+
30+
struct AutocompleteItem {
31+
name: String,
32+
url: String,
33+
}
34+
35+
#[derive(Serialize)]
36+
struct AutocompleteRequest {
37+
format: &'static str,
38+
action: &'static str,
39+
search: String,
40+
redirects: &'static str,
41+
limit: usize,
42+
origin: &'static str,
43+
}
44+
45+
impl AutocompleteRequest {
46+
fn new(search_term: &str) -> Self {
47+
Self {
48+
format: "json",
49+
action: "opensearch",
50+
search: search_term.into(),
51+
limit: 10,
52+
origin: "*",
53+
redirects: "resolve",
54+
}
55+
}
2356
}
2457

2558
impl Component for App {
@@ -30,9 +63,8 @@ impl Component for App {
3063
Self {
3164
suggestions: Vec::new(),
3265
value: "".into(),
33-
autocomplete_service: AutocompleteService::new(
34-
"https://en.wikipedia.org/w/api.php".into(),
35-
),
66+
autcomplete_fetcher: None,
67+
api_url: "https://en.wikipedia.org/w/api.php".into(),
3668
link,
3769
}
3870
}
@@ -41,8 +73,31 @@ impl Component for App {
4173
match msg {
4274
Msg::Input(text) => {
4375
self.suggestions = Vec::new();
44-
self.autocomplete_service
45-
.get_suggestions(&text, self.link.callback(Msg::CompleteUpdate));
76+
let query = AutocompleteRequest::new(&text);
77+
let query = serde_urlencoded::to_string(query).unwrap();
78+
let url = format!("{}?{}", self.api_url, query);
79+
let request = Request::get(url).body(Nothing).unwrap();
80+
let task = FetchService::fetch(
81+
request,
82+
self.link
83+
.callback(|response: Response<Json<Result<Value, anyhow::Error>>>| {
84+
if let (meta, Json(Ok(body))) = response.into_parts() {
85+
if meta.status.is_success() {
86+
return body[1]
87+
.as_array()
88+
.map(|list| {
89+
list.iter()
90+
.filter_map(|i| i.as_str().map(|s| s.to_string()))
91+
.collect()
92+
})
93+
.map(Msg::CompleteUpdate)
94+
.unwrap_or(Msg::Noop);
95+
}
96+
}
97+
Msg::Noop
98+
}),
99+
);
100+
self.autcomplete_fetcher = task.ok();
46101
self.value = text;
47102
}
48103
Msg::CompleteUpdate(list) => {

src/autocomplete_service.rs

-98
This file was deleted.

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![recursion_limit = "512"]
22

33
mod app;
4-
mod autocomplete_service;
54
mod utils;
65

76
use wasm_bindgen::prelude::*;

webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const distPath = path.resolve(__dirname, "dist");
66
module.exports = (env, argv) => {
77
return {
88
devServer: {
9-
contentBase: distPath,
9+
static: [distPath],
1010
compress: argv.mode === "production",
1111
port: 8000,
1212
},

0 commit comments

Comments
 (0)