Skip to content

Commit

Permalink
Share results page optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
pokhrelashok committed Nov 28, 2023
1 parent eb23fca commit a49f337
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 45 deletions.
64 changes: 43 additions & 21 deletions app/result/page.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
"use client";
import useInvoke from "@/hooks/useInvoke";
import Wrapper from "../_components/Wrapper";
import { CompanyApplication, IpoResult } from "../../types";
import { CompanyApplication, IpoResult, User } from "../../types";
import SectionLoading from "../_components/SectionLoading";
import Button from "../_components/Button";
import { Fragment, useEffect, useState } from "react";
import { Dialog, Transition } from "@headlessui/react";
import LoadingSpinner from "../_components/LoadingSpinner";

function ResultPage() {
const { data: shares, loading } = useInvoke<CompanyApplication[]>(
"get_application_report",
[],
true
);
const { data: users } = useInvoke<User[]>("get_users", [], true);
const [selectedShare, setSelectedShare] = useState<null | CompanyApplication>(
null
);
const {
data: result,
handle: getResult,
loading: isFetchingShares,
} = useInvoke<IpoResult[]>("get_share_results", []);
const [report, setReport] = useState<Record<string, string>>({});
const { handle: getResult, loading: isFetchingShares } = useInvoke<string>(
"get_share_results",
[]
);

useEffect(() => {
if (selectedShare) getResult({ script: selectedShare.scrip });
if (!selectedShare || users.length === 0) return;
setReport({});
users.forEach((user) => {
getResult({ script: selectedShare.scrip, user })
.then((result) => {
setReport((old) => ({ ...old, [user.id as string]: result }));
})
.catch((e: string) => {
setReport((old) => ({ ...old, [user.id as string]: e }));
});
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedShare]);

Expand All @@ -48,7 +61,8 @@ function ResultPage() {
onClose={() => {
setSelectedShare(null);
}}
result={result}
users={users}
result={report}
/>
)}
</Wrapper>
Expand All @@ -60,11 +74,13 @@ function ViewResultDialog({
result,
onClose,
loading,
users,
}: {
users: User[];
share: CompanyApplication;
onClose: () => any;
loading: boolean;
result: IpoResult[];
result: Record<string, string>;
}) {
return (
<Transition appear show={true} as={Fragment}>
Expand Down Expand Up @@ -112,30 +128,36 @@ function ViewResultDialog({
{!loading && (
<div className="text-green-500 shrink-0 text-sm">
Alloted{" "}
{result.filter((r) => r.status === "Alloted").length}/
{result.length}
{
Object.values(result).filter((r) => r === "Alloted")
.length
}
/{users.length}
</div>
)}
</div>
</div>
</Dialog.Title>
<div className="flex relative flex-col gap-1 max-h-[80vh] overflow-y-auto">
{result.map((res, ind) => {
<div className="flex flex-col gap-1 overflow-y-auto max-h-[80vh]">
{users.map((user) => {
return (
<Button
className={`justify-between ${
res.status !== "Alloted"
? "bg-red-100 hover:bg-red-200"
: "bg-green-100 hover:bg-green-200"
key={user.id}
className={`flex justify-between ${
result[user.id] === "Alloted"
? "bg-green-100 hover:bg-green-200"
: ""
}`}
key={ind}
>
<div>{res.user}</div>
<div>{res.status}</div>
<div>{user.name}</div>
<div>
{result[user.id || "-1"] || (
<LoadingSpinner className="h-3 w-3" />
)}
</div>
</Button>
);
})}
{loading && <SectionLoading className="min-h-[100px]" />}
</div>
</Dialog.Panel>
</Transition.Child>
Expand Down
14 changes: 7 additions & 7 deletions app/users/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ function ManageUsers() {
return (
<Button key={user.username} className="justify-between">
<div className="font-semibold">{user.name}</div>
<div className="flex gap-1 items-center">
<div className="flex gap-2 items-center">
<a href={`/users/${user.id}`}>
<img
title="View User Details"
height={30}
width={30}
height={26}
width={26}
src="/eye-icon.svg"
/>
</a>
<button onClick={() => setUser(user)}>
<img
title="Edit User"
height={36}
width={36}
height={30}
width={30}
src="/edit-icon.svg"
/>
</button>
Expand All @@ -148,8 +148,8 @@ function ManageUsers() {
>
<img
title="Delete User"
height={30}
width={30}
height={26}
width={26}
src="/delete-icon.svg"
/>
</button>
Expand Down
29 changes: 15 additions & 14 deletions controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,21 @@ impl Controller {
}
}

pub async fn get_results(&mut self, script: String) -> Vec<IPOResult> {
let mut results: Vec<IPOResult> = vec![];
let users: Vec<User> = self.get_users().unwrap();
for user in users.iter() {
let result = self
.meroshare
.get_company_result(user, script.as_str())
.await;
results.push(IPOResult {
user: user.name.clone(),
status: result,
});
}
return results;
pub async fn get_results(&mut self, script: String, user: &User) -> String {
// let mut results: Vec<IPOResult> = vec![];
// let users: Vec<User> = self.get_users().unwrap();
// for user in users.iter() {
let result = self
.meroshare
.get_company_result(user, script.as_str())
.await;
// results.push(IPOResult {
// user: user.name.clone(),
// status: result,
// });
// }
// return results;
return result;
}

pub async fn get_user_portfolio(&mut self, id: String) -> Portfolio {
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ async fn get_application_report() -> Vec<CompanyApplication> {
return res;
}
#[tauri::command]
async fn get_share_results(script: String) -> Vec<IPOResult> {
async fn get_share_results(script: String, user: User) -> String {
let controller = CONTROLLER.clone();
let mut controller_lock = controller.lock().await;
let res = controller_lock.get_results(script).await;
let res = controller_lock.get_results(script, &user).await;
return res;
}
#[tauri::command]
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "share-sathi",
"version": "2.0.3"
"version": "2.0.4"
},
"tauri": {
"allowlist": {
Expand Down

0 comments on commit a49f337

Please # to comment.