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

update on listing sources #59

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions pages/api/sources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import useSWR from "swr";
import { API_BASE_URL } from '../index';

export const getSources = () => {
const fetcher = (url) => fetch(url).then((res) => res.json());

const {
data,
isLoading,
isError,
mutate,
} = useSWR(
`${API_BASE_URL}/orgs/MOH-KENYA/sources?limit=1000&verbose=false&includeRetired=false`,
fetcher,
{ revalidateOnFocus: false, revalidateOnReconnect: false }
);

return {
data, isLoading, isError, mutate
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function ConceptDetail() {
isLoading,
} = getConceptDetail(org, source, concept);

// console.log("---conceptDetal", conceptDetail)

const conceptDetail1 = {
uuid: "231999",
Expand Down Expand Up @@ -464,7 +463,7 @@ function ConceptDetail() {
{org}
</Link>
<Link
href={`orgs/${org}/sources/${source}`}
href={`/orgs/[orgs]/sources/${source}`}
style={{ textDecoration: "none", color: "#1651B6" }}
title="Source"
className="breadcrumb-item"
Expand Down
110 changes: 105 additions & 5 deletions pages/orgs/[org]/sources/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,109 @@
import React from 'react'
import React from "react";
import { Box, TextField, Button, Skeleton, Stack, Alert, AlertTitle } from "@mui/material";
import Head from "next/head";
import { useState } from "react";
import { useRouter } from "next/router";
import { DataGrid, GridColDef, GridValueGetterParams } from "@mui/x-data-grid";
import { getSources } from "@/pages/api/sources";
import { Search } from "@mui/icons-material";

function SourceList(props) {
const router = useRouter();
const { data, isLoading, isError, mutate } = getSources();
const columns = ["id", "name", "type", "url"];
const [searchTerm, setSearchTerm] = useState("");

if (isLoading) {
return (
<Box sx={{ pt: 0.5 }}>
<Skeleton />
<Skeleton width="60%" />
</Box>
);
}

if (isError) {
return (
<Stack sx={{ width: "100%" }} spacing={2}>
<Alert severity="error">
<AlertTitle>Error</AlertTitle>
Error fetching data, please retry.
</Alert>
</Stack>
);
}

const handleSearch = (event) => {
event.preventDefault();
mutate();
};
const handleClick = (params) =>{
const rowId = params.id;
router.push(`/orgs/[orgs]/sources/${rowId}`);
}
const filteredData = Object.values(data).filter((row) =>
Object.values(row).some(
(value) =>
typeof value === "string" && value.toLowerCase().includes(searchTerm.toLowerCase())
)
);

function SourcesList() {
return (
<div>SourcesList</div>
)
<>
<Head>
<title>MOH KNHTS | Organizations</title>
<meta name="description" content="MOH KNHTS" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<Box width={"100%"} sx={{ display: "flex" }}>
<TextField
onChange={(e) => setSearchTerm(e.target.value)}
sx={{
flexGrow: 1,
backgroundColor: "#fcfcfc",
borderRadius: "8px",
}}
id="searchTerm"
name="searchTerm"
label="Search Sources"
variant="outlined"
color={"info"}
/>
<Button
onClick={handleSearch}
sx={{
borderRadius: "8px",
marginLeft: "10px",
backgroundColor: "#fff",
color: "#333",
}}
variant="contained"
color="primary"
>
<Search />
</Button>
</Box>
<Box my={2} sx={{ width: "100%" }}>
<DataGrid
rows={filteredData}
getRowId={(row) => row.id}
columns={columns.map((key) => {
return {
field: key.toLowerCase(),
headerName: key.charAt(0).toUpperCase() + key.slice(1),
width: 200,
};
})}
onRowClick={handleClick}
initialState={{
pagination: { paginationModel: { pageSize: 25 } },
}}
pageSizeOptions={[25, 50, 100, 250]}
/>
</Box>
</>
);
}

export default SourcesList
export default SourceList;