Skip to content

Commit b87f437

Browse files
committed
chore: Refactor code to improve readability and maintainability
1 parent 945df5e commit b87f437

File tree

2 files changed

+38
-73
lines changed

2 files changed

+38
-73
lines changed

src-tauri/src/main.rs

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
12
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
23
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
34
use reqwest::get;
@@ -7,12 +8,6 @@ use std::collections::HashMap;
78
#[macro_use]
89
extern crate maplit;
910

10-
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
11-
#[tauri::command]
12-
fn greet(name: &str) -> String {
13-
format!("{} does not exist yet!", name)
14-
}
15-
1611
// Function to fetch type effectiveness
1712
async fn fetch_type_data(url: &str) -> Result<Value, String> {
1813
match get(url).await {
@@ -28,62 +23,46 @@ async fn fetch_type_data(url: &str) -> Result<Value, String> {
2823
}
2924
}
3025

26+
// Helper function to generate icon URL based on type ID
27+
fn generate_icon_url(type_id: &str) -> String {
28+
format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id)
29+
}
30+
3131
// Function to calculate weaknesses based on Pokémon types
3232
async fn calculate_weaknesses(types: &Vec<Value>) -> Result<HashMap<String, Vec<HashMap<String, String>>>, String> {
3333
let mut weaknesses: HashMap<String, Vec<HashMap<String, String>>> = HashMap::new();
3434

3535
for pokemon_type in types {
36-
let type_name = pokemon_type["type"]["name"].as_str().unwrap();
36+
//let type_name = pokemon_type["type"]["name"].as_str().unwrap();
37+
//let type_id = type_url.split('/').filter(|&s| !s.is_empty()).last().unwrap(); // Extract the type ID
3738
let type_url = pokemon_type["type"]["url"].as_str().unwrap();
38-
let type_id = type_url.split('/').filter(|&s| !s.is_empty()).last().unwrap(); // Extract the type ID
39-
40-
let icon_url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id);
4139

4240
if let Ok(type_data) = fetch_type_data(type_url).await {
4341
let damage_relations = &type_data["damage_relations"];
4442

45-
// Group by 2x effectiveness
46-
if let Some(double_damage_from) = damage_relations["double_damage_from"].as_array() {
47-
for damage_type in double_damage_from {
48-
let type_name = damage_type["name"].as_str().unwrap().to_string();
49-
let type_id = damage_type["url"].as_str().unwrap().split('/').filter(|&s| !s.is_empty()).last().unwrap();
50-
let icon_url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id);
51-
weaknesses.entry("2x".to_string()).or_default().push(
52-
hashmap!{
53-
"type".to_string() => type_name,
54-
"icon".to_string() => icon_url
55-
}
56-
);
57-
}
58-
}
59-
60-
// Group by 0.5x effectiveness
61-
if let Some(half_damage_from) = damage_relations["half_damage_from"].as_array() {
62-
for damage_type in half_damage_from {
63-
let type_name = damage_type["name"].as_str().unwrap().to_string();
64-
let type_id = damage_type["url"].as_str().unwrap().split('/').filter(|&s| !s.is_empty()).last().unwrap();
65-
let icon_url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id);
66-
weaknesses.entry("0.5x".to_string()).or_default().push(
67-
hashmap!{
68-
"type".to_string() => type_name,
69-
"icon".to_string() => icon_url
70-
}
71-
);
72-
}
73-
}
74-
75-
// Group by 0x effectiveness
76-
if let Some(no_damage_from) = damage_relations["no_damage_from"].as_array() {
77-
for damage_type in no_damage_from {
78-
let type_name = damage_type["name"].as_str().unwrap().to_string();
79-
let type_id = damage_type["url"].as_str().unwrap().split('/').filter(|&s| !s.is_empty()).last().unwrap();
80-
let icon_url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/types/generation-viii/sword-shield/{}.png", type_id);
81-
weaknesses.entry("0x".to_string()).or_default().push(
82-
hashmap!{
83-
"type".to_string() => type_name,
84-
"icon".to_string() => icon_url
85-
}
86-
);
43+
for (key, effectiveness) in &[
44+
("2x", "double_damage_from"),
45+
("0.5x", "half_damage_from"),
46+
("0x", "no_damage_from"),
47+
] {
48+
if let Some(damage_types) = damage_relations[effectiveness].as_array() {
49+
for damage_type in damage_types {
50+
let type_name = damage_type["name"].as_str().unwrap().to_string();
51+
let type_id = damage_type["url"]
52+
.as_str()
53+
.unwrap()
54+
.split('/')
55+
.filter(|&s| !s.is_empty())
56+
.last()
57+
.unwrap();
58+
let icon_url = generate_icon_url(type_id);
59+
weaknesses.entry(key.to_string()).or_default().push(
60+
hashmap!{
61+
"type".to_string() => type_name,
62+
"icon".to_string() => icon_url
63+
}
64+
);
65+
}
8766
}
8867
}
8968
} else {
@@ -123,7 +102,7 @@ async fn search_pokemon(name: &str) -> Result<String, String> {
123102

124103
fn main() {
125104
tauri::Builder::default()
126-
.invoke_handler(tauri::generate_handler![greet, search_pokemon])
105+
.invoke_handler(tauri::generate_handler![search_pokemon])
127106
.run(tauri::generate_context!())
128107
.expect("error while running tauri application");
129108
}

src/App.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,15 @@ function App() {
4040
</p>
4141
<div className="mt-10">
4242
{pokemonData?.weaknesses ? (
43-
<>
44-
<CollapsibleSection title="2x Weaknesses">
45-
{pokemonData.weaknesses['2x']?.map((weakness: any) => (
46-
<div key={weakness.type} className="inline-block m-2">
47-
<img src={weakness.icon} alt={weakness.type} className="h-8 inline-block mr-2" />
43+
['2x', '0.5x', '0x'].map((multiplier) => (
44+
<CollapsibleSection key={multiplier} title={`${multiplier} ${multiplier === '2x' ? 'Weaknesses' : multiplier === '0.5x' ? 'Resistances' : 'Immunities'}`}>
45+
{pokemonData.weaknesses[multiplier]?.map((entry: any) => (
46+
<div key={entry.type} className="inline-block m-2">
47+
<img src={entry.icon} alt={entry.type} className="h-6 inline-block" />
4848
</div>
4949
)) || "None"}
5050
</CollapsibleSection>
51-
<CollapsibleSection title="0.5x Resistances">
52-
{pokemonData.weaknesses['0.5x']?.map((resistance: any) => (
53-
<div key={resistance.type} className="inline-block m-2">
54-
<img src={resistance.icon} alt={resistance.type} className="h-8 inline-block mr-2" />
55-
</div>
56-
)) || "None"}
57-
</CollapsibleSection>
58-
<CollapsibleSection title="0x Immunities">
59-
{pokemonData.weaknesses['0x']?.map((immunity: any) => (
60-
<div key={immunity.type} className="inline-block m-2">
61-
<img src={immunity.icon} alt={immunity.type} className="h-8 inline-block mr-2" />
62-
</div>
63-
)) || "None"}
64-
</CollapsibleSection>
65-
</>
51+
))
6652
) : (
6753
"No weaknesses found"
6854
)}

0 commit comments

Comments
 (0)