API, Backend & Frontend

This commit is contained in:
Ichitux
2026-04-01 01:18:21 +02:00
parent 331c04fbef
commit 0fe8ec9bc0
44 changed files with 10060 additions and 0 deletions

15
frontend/index.html Normal file
View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FarmaFinder | Find Your Medicine</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>

1629
frontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
frontend/package.json Normal file
View File

@@ -0,0 +1,19 @@
{
"name": "farma-finder-frontend",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.2.1",
"vite": "^5.0.8"
}
}

221
frontend/src/App.css Normal file
View File

@@ -0,0 +1,221 @@
.app {
min-height: 100vh;
padding: 3rem 1.5rem;
max-width: 1000px;
margin: 0 auto;
}
.view-switcher {
display: flex;
justify-content: center;
background: var(--surface);
padding: 5px;
border-radius: 999px;
border: 1px solid var(--border);
box-shadow: var(--glass-shadow);
width: fit-content;
margin: 0 auto 3rem auto;
}
.view-switcher button {
background: transparent;
border: none;
color: var(--text-muted);
padding: 0.8rem 2rem;
border-radius: 999px;
cursor: pointer;
font-size: 0.95rem;
font-weight: 600;
transition: color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
display: flex;
align-items: center;
gap: 0.5rem;
}
.view-switcher button:hover {
color: var(--text-main);
}
.view-switcher button.active {
background: var(--surface-muted);
color: var(--primary);
box-shadow: inset 0 0 0 1px var(--border);
}
.app-header {
text-align: center;
margin-bottom: 4rem;
animation: fadeInDown 0.8s ease-out;
}
.app-header h1 {
font-size: 3.5rem;
font-weight: 800;
margin-bottom: 0.75rem;
color: var(--text-main);
letter-spacing: -0.03em;
line-height: 1.1;
}
.app-header h1::after {
content: "";
display: block;
width: 3rem;
height: 4px;
margin: 1rem auto 0;
background: var(--primary);
border-radius: 2px;
}
.app-header p {
font-size: 1.2rem;
color: var(--text-muted);
font-weight: 400;
max-width: 28rem;
margin-left: auto;
margin-right: auto;
line-height: 1.5;
}
.app-main {
width: 100%;
}
.glass-card {
background: var(--glass-bg);
border: 1px solid var(--glass-border);
border-radius: var(--radius);
box-shadow: var(--glass-shadow);
overflow: hidden;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.loading {
text-align: center;
color: var(--text-muted);
font-size: 1.05rem;
font-weight: 500;
margin: 3rem 0;
display: flex;
flex-direction: column;
gap: 1rem;
align-items: center;
}
.loading::after {
content: "";
width: 40px;
height: 40px;
border: 3px solid var(--border);
border-top-color: var(--primary);
border-radius: 50%;
animation: spin 0.75s linear infinite;
}
.selected-medicine-section {
margin-top: 2rem;
animation: fadeInUp 0.6s ease-out;
}
.medicine-info {
background: var(--surface);
border-radius: var(--radius);
padding: 2.5rem;
margin-bottom: 2rem;
box-shadow: var(--glass-shadow);
border: 1px solid var(--border);
}
.medicine-info h2 {
color: var(--text-main);
margin-bottom: 1.2rem;
font-size: 2.25rem;
font-weight: 700;
letter-spacing: -0.02em;
}
.medicine-details {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1.5rem;
margin-bottom: 2rem;
padding: 1.5rem;
background: var(--surface-muted);
border-radius: var(--radius-sm);
border: 1px solid var(--border);
}
.medicine-details span {
font-size: 1rem;
color: var(--text-muted);
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.medicine-details strong {
color: var(--text-main);
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.06em;
font-weight: 600;
}
.back-button {
background: var(--surface-muted);
color: var(--text-main);
border: 1px solid var(--border);
padding: 0.8rem 1.8rem;
border-radius: var(--radius-sm);
cursor: pointer;
font-size: 0.95rem;
font-weight: 600;
transition: background 0.2s, border-color 0.2s, transform 0.2s;
}
.back-button:hover {
background: var(--surface-card);
border-color: var(--border-strong);
transform: translateX(-3px);
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@keyframes fadeInDown {
from { opacity: 0; transform: translateY(-16px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeInUp {
from { opacity: 0; transform: translateY(16px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 768px) {
.app {
padding: 2rem 1rem;
}
.app-header h1 {
font-size: 2.35rem;
}
.app-header p {
font-size: 1rem;
}
.medicine-info {
padding: 1.5rem;
}
.medicine-info h2 {
font-size: 1.65rem;
}
.view-switcher button {
padding: 0.65rem 1.15rem;
font-size: 0.85rem;
}
}

31
frontend/src/App.jsx Normal file
View File

@@ -0,0 +1,31 @@
import React, { useState } from 'react';
import './App.css';
import PublicView from './views/PublicView';
import AdminView from './views/AdminView';
function App() {
const [view, setView] = useState('public'); // 'public' or 'admin'
return (
<div className="app">
<div className="view-switcher">
<button
className={view === 'public' ? 'active' : ''}
onClick={() => setView('public')}
>
🔍 Public Search
</button>
<button
className={view === 'admin' ? 'active' : ''}
onClick={() => setView('admin')}
>
Admin Panel
</button>
</div>
{view === 'public' ? <PublicView /> : <AdminView />}
</div>
);
}
export default App;

BIN
frontend/src/assets/bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB

View File

@@ -0,0 +1,87 @@
.medicine-results {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1.25rem;
margin-top: 1rem;
animation: fadeInUp 0.8s ease-out 0.4s backwards;
}
.medicine-card {
background: var(--surface);
border-radius: var(--radius);
padding: 1.65rem;
cursor: pointer;
transition: transform 0.25s ease, box-shadow 0.25s ease, border-color 0.25s ease;
border: 1px solid var(--border);
display: flex;
flex-direction: column;
justify-content: space-between;
box-shadow: 0 1px 3px rgba(28, 25, 23, 0.04);
}
.medicine-card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 28px rgba(28, 25, 23, 0.08);
border-color: var(--primary);
}
.medicine-card h3 {
color: var(--text-main);
margin-bottom: 0.75rem;
font-size: 1.2rem;
font-weight: 700;
letter-spacing: -0.01em;
transition: color 0.2s;
}
.medicine-card:hover h3 {
color: var(--primary);
}
.medicine-card-body {
margin-bottom: 1.35rem;
}
.medicine-card-body p {
font-size: 0.9rem;
color: var(--text-muted);
line-height: 1.55;
margin-bottom: 0.25rem;
}
.medicine-card-body strong {
color: var(--text-main);
font-weight: 600;
}
.medicine-card-footer {
padding-top: 1.15rem;
border-top: 1px solid var(--border);
}
.view-pharmacies {
color: var(--primary);
font-weight: 600;
font-size: 0.9rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.view-pharmacies::after {
content: "→";
transition: transform 0.2s;
}
.medicine-card:hover .view-pharmacies::after {
transform: translateX(4px);
}
.no-results {
text-align: center;
padding: 2.75rem 1.5rem;
background: var(--surface-muted);
border-radius: var(--radius);
color: var(--text-muted);
border: 1px dashed var(--border-strong);
}

View File

@@ -0,0 +1,38 @@
import React from 'react';
import './MedicineResults.css';
function MedicineResults({ medicines, onSelect, query }) {
if (medicines.length === 0 && query.length >= 2) {
return (
<div className="no-results">
<p>No medicines found matching "{query}"</p>
</div>
);
}
return (
<div className="medicine-results">
{medicines.map((medicine) => (
<div
key={medicine.id}
className="medicine-card"
onClick={() => onSelect(medicine)}
>
<div className="medicine-card-header">
<h3>{medicine.name}</h3>
</div>
<div className="medicine-card-body">
<p><strong>Active Ingredient:</strong> {medicine.active_ingredient}</p>
<p><strong>Dosage:</strong> {medicine.dosage} <strong>Form:</strong> {medicine.form}</p>
</div>
<div className="medicine-card-footer">
<span className="view-pharmacies">View pharmacies </span>
</div>
</div>
))}
</div>
);
}
export default MedicineResults;

View File

@@ -0,0 +1,117 @@
.pharmacy-list {
margin-top: 1rem;
}
.pharmacy-list-title {
font-size: 1.4rem;
font-weight: 700;
margin-bottom: 1.35rem;
color: var(--text-main);
display: flex;
align-items: center;
gap: 0.65rem;
letter-spacing: -0.02em;
}
.pharmacy-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1.25rem;
}
.pharmacy-card {
background: var(--surface);
border-radius: var(--radius);
padding: 1.65rem;
border: 1px solid var(--border);
transition: transform 0.25s ease, box-shadow 0.25s ease, border-color 0.25s ease;
box-shadow: 0 1px 3px rgba(28, 25, 23, 0.04);
}
.pharmacy-card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 28px rgba(28, 25, 23, 0.08);
border-color: var(--primary);
}
.pharmacy-header h4 {
color: var(--text-main);
font-weight: 700;
margin-bottom: 1rem;
font-size: 1.1rem;
transition: color 0.2s;
}
.pharmacy-card:hover .pharmacy-header h4 {
color: var(--primary);
}
.pharmacy-details {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.pharmacy-address,
.pharmacy-phone {
color: var(--text-muted);
font-size: 0.9rem;
margin: 0;
line-height: 1.45;
}
.pharmacy-pricing {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 1rem;
padding-top: 1rem;
border-top: 1px solid var(--border);
}
.price {
font-size: 1.2rem;
font-weight: 700;
color: var(--primary);
}
.stock {
font-size: 0.72rem;
padding: 0.35rem 0.8rem;
border-radius: 999px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.04em;
}
.stock.in-stock {
background: rgba(4, 120, 87, 0.12);
color: var(--accent);
}
.stock.low-stock {
background: rgba(180, 83, 9, 0.12);
color: var(--accent-warm);
}
.stock.out-of-stock {
background: rgba(185, 28, 28, 0.1);
color: #b91c1c;
}
.loading-pharmacies,
.no-pharmacies {
text-align: center;
padding: 2.75rem 1.5rem;
background: var(--surface-muted);
border-radius: var(--radius);
color: var(--text-muted);
border: 1px solid var(--border);
margin-top: 1rem;
}
@media (max-width: 768px) {
.pharmacy-grid {
grid-template-columns: 1fr;
}
}

View File

@@ -0,0 +1,56 @@
import React from 'react';
import './PharmacyList.css';
function PharmacyList({ pharmacies, loading }) {
if (loading) {
return (
<div className="loading-pharmacies">
<p>Loading pharmacies...</p>
</div>
);
}
if (pharmacies.length === 0) {
return (
<div className="no-pharmacies">
<p>No pharmacies found selling this medicine</p>
</div>
);
}
return (
<div className="pharmacy-list">
<h3 className="pharmacy-list-title">
Available at {pharmacies.length} {pharmacies.length === 1 ? 'pharmacy' : 'pharmacies'}
</h3>
<div className="pharmacy-grid">
{pharmacies.map((pharmacy) => (
<div key={pharmacy.id} className="pharmacy-card">
<div className="pharmacy-header">
<h4>🏥 {pharmacy.name}</h4>
</div>
<div className="pharmacy-details">
<p className="pharmacy-address">📍 {pharmacy.address}</p>
{pharmacy.phone && (
<p className="pharmacy-phone">📞 {pharmacy.phone}</p>
)}
<div className="pharmacy-pricing">
{pharmacy.price && (
<span className="price">{parseFloat(pharmacy.price).toFixed(2)}</span>
)}
{pharmacy.stock !== undefined && (
<span className={`stock ${pharmacy.stock > 20 ? 'in-stock' : pharmacy.stock > 0 ? 'low-stock' : 'out-of-stock'}`}>
{pharmacy.stock > 20 ? '✓ In Stock' : pharmacy.stock > 0 ? `⚠ Low Stock (${pharmacy.stock})` : '✗ Out of Stock'}
</span>
)}
</div>
</div>
</div>
))}
</div>
</div>
);
}
export default PharmacyList;

View File

@@ -0,0 +1,66 @@
.search-bar-container {
margin-bottom: 2.5rem;
width: 100%;
animation: fadeInUp 0.8s ease-out 0.2s backwards;
}
.search-bar {
display: flex;
align-items: center;
background: var(--surface);
border-radius: var(--radius);
padding: 0.45rem 1.25rem;
box-shadow: var(--glass-shadow);
border: 1px solid var(--border);
transition: border-color 0.2s ease, box-shadow 0.2s ease;
position: relative;
}
.search-bar:focus-within {
border-color: var(--primary);
box-shadow: 0 0 0 3px var(--primary-ring), var(--glass-shadow);
}
.search-icon {
font-size: 1.2rem;
margin-right: 0.85rem;
opacity: 0.45;
}
.search-input {
flex: 1;
border: none;
background: transparent;
padding: 1rem 0;
font-size: 1.1rem;
font-family: inherit;
color: var(--text-main);
outline: none;
}
.search-input::placeholder {
color: var(--text-muted);
opacity: 0.65;
}
.clear-button {
background: var(--surface-muted);
border: 1px solid var(--border);
color: var(--text-muted);
width: 30px;
height: 30px;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.75rem;
transition: background 0.2s, color 0.2s, transform 0.2s;
margin-left: 0.5rem;
}
.clear-button:hover {
background: var(--surface-card);
color: var(--text-main);
transform: rotate(90deg);
}

View File

@@ -0,0 +1,32 @@
import React from 'react';
import './SearchBar.css';
function SearchBar({ value, onChange, placeholder }) {
return (
<div className="search-bar-container">
<div className="search-bar">
<span className="search-icon">🔍</span>
<input
type="text"
value={value}
onChange={(e) => onChange(e.target.value)}
placeholder={placeholder}
className="search-input"
autoFocus
/>
{value && (
<button
className="clear-button"
onClick={() => onChange('')}
aria-label="Clear search"
>
</button>
)}
</div>
</div>
);
}
export default SearchBar;

View File

@@ -0,0 +1,525 @@
.admin-section {
width: 100%;
}
.section-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2rem;
flex-wrap: wrap;
gap: 1rem;
}
.section-header h2 {
color: var(--text-main);
margin: 0;
font-weight: 700;
font-size: 1.5rem;
}
.admin-form {
background: var(--surface-muted);
padding: 2rem;
border-radius: var(--radius-sm);
margin-bottom: 2rem;
border: 1px solid var(--border);
}
.admin-form h3 {
color: var(--primary);
margin-top: 0;
margin-bottom: 1.5rem;
font-weight: 700;
}
.form-group {
margin-bottom: 1rem;
}
.form-group label {
display: block;
margin-bottom: 0.5rem;
color: var(--text-main);
font-weight: 600;
font-size: 0.85rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.form-group input,
.form-group select {
width: 100%;
padding: 0.85rem 1rem;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
font-size: 1rem;
font-family: inherit;
transition: border-color 0.2s, box-shadow 0.2s;
background: var(--surface);
}
.form-group input:focus,
.form-group select:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px var(--primary-ring);
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.form-actions {
display: flex;
gap: 1rem;
margin-top: 1.5rem;
}
.btn-primary,
.btn-secondary,
.btn-edit,
.btn-delete {
padding: 0.75rem 1.5rem;
border: none;
border-radius: 10px;
font-size: 0.9rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
font-family: inherit;
}
.btn-primary {
background: var(--primary);
color: white;
}
.btn-primary:hover {
background: var(--primary-hover);
transform: translateY(-1px);
box-shadow: 0 4px 14px var(--primary-shadow);
}
.btn-secondary {
background: var(--surface-muted);
color: var(--text-main);
border: 1px solid var(--border);
}
.btn-secondary:hover {
background: var(--surface-card);
border-color: var(--border-strong);
}
.btn-edit {
background: rgba(4, 120, 87, 0.1);
color: var(--accent);
padding: 0.5rem 1rem;
font-size: 0.85rem;
}
.btn-edit:hover {
background: rgba(4, 120, 87, 0.16);
}
.btn-delete {
background: rgba(239, 68, 68, 0.1);
color: #ef4444;
padding: 0.5rem 1rem;
font-size: 0.85rem;
}
.btn-delete:hover {
background: rgba(239, 68, 68, 0.2);
}
.admin-list {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.admin-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1.25rem 1.5rem;
background: var(--surface-muted);
border-radius: var(--radius-sm);
border: 1px solid var(--border);
transition: border-color 0.2s, box-shadow 0.2s;
}
.admin-item:hover {
border-color: var(--primary);
box-shadow: 0 2px 8px rgba(28, 25, 23, 0.06);
}
.item-content {
flex: 1;
}
.item-content h4 {
color: var(--text-main);
margin: 0 0 0.35rem 0;
font-size: 1.05rem;
font-weight: 600;
}
.item-content p {
margin: 0.2rem 0;
color: var(--text-muted);
font-size: 0.9rem;
}
.item-actions {
display: flex;
gap: 0.5rem;
}
.empty-state {
text-align: center;
color: var(--text-muted);
padding: 2.5rem;
font-style: italic;
background: var(--surface-muted);
border-radius: var(--radius-sm);
border: 1px dashed var(--border-strong);
}
.info-text {
color: var(--text-muted);
margin-bottom: 1rem;
font-size: 0.95rem;
}
.info-box {
background: var(--primary-faint);
padding: 1rem 1.25rem;
margin-bottom: 1.5rem;
border-radius: var(--radius-sm);
border: 1px solid var(--border);
border-left: 4px solid var(--primary);
}
.info-box p {
margin: 0.5rem 0;
color: var(--text-muted);
font-size: 0.95rem;
}
.info-box p:first-child {
margin-top: 0;
}
.info-box p:last-child {
margin-bottom: 0;
}
.medicine-meta {
font-size: 0.8rem;
color: var(--text-muted);
margin-top: 0.5rem;
}
.pharmacy-medicines-section {
margin-top: 2rem;
padding-top: 2rem;
border-top: 1px solid var(--border);
}
.pharmacy-medicines-section h3 {
color: var(--text-main);
font-weight: 700;
margin-bottom: 1rem;
}
/* Medicine search styles */
.loading-text {
color: var(--text-muted);
font-size: 0.9rem;
margin-top: 0.5rem;
font-weight: 500;
}
.medicine-search-results {
position: relative;
max-height: 300px;
overflow-y: auto;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
margin-top: 0.5rem;
background: var(--surface);
box-shadow: var(--glass-shadow);
}
.search-result-item {
padding: 1rem 1.25rem;
cursor: pointer;
border-bottom: 1px solid var(--border);
transition: background-color 0.15s;
}
.search-result-item:last-child {
border-bottom: none;
}
.search-result-item:hover {
background-color: var(--surface-muted);
}
.search-result-item strong {
color: var(--text-main);
display: block;
margin-bottom: 0.25rem;
font-weight: 600;
}
.search-result-item span {
color: var(--text-muted);
font-size: 0.9rem;
}
.selected-medicine-info {
background: var(--primary-faint);
border: 1px solid var(--primary);
border-radius: var(--radius-sm);
padding: 1.25rem;
margin-top: 0.5rem;
}
.selected-medicine-info p {
margin: 0.25rem 0;
}
.selected-medicine-info .medicine-details {
color: var(--text-muted);
font-size: 0.85rem;
margin-top: 0.5rem;
}
.btn-small {
padding: 0.4rem 0.8rem;
font-size: 0.85rem;
background: var(--surface-muted);
color: var(--text-main);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
cursor: pointer;
margin-top: 0.5rem;
transition: background 0.2s, border-color 0.2s;
font-family: inherit;
font-weight: 600;
}
.btn-small:hover {
background: var(--surface-card);
border-color: var(--border-strong);
}
/* Pharmacies: search, region import, list filter */
.pharmacy-tools-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
padding: 1.5rem 1.75rem;
margin-bottom: 1.75rem;
box-shadow: 0 1px 2px rgba(28, 25, 23, 0.04);
}
.pharmacy-tools-card h3 {
margin: 0 0 0.35rem 0;
font-size: 1.05rem;
font-weight: 700;
color: var(--text-main);
}
.pharmacy-tools-hint {
font-size: 0.88rem;
color: var(--text-muted);
margin: 0 0 1.25rem 0;
line-height: 1.45;
}
.city-lookup-form {
display: flex;
flex-wrap: wrap;
align-items: flex-end;
gap: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
.city-lookup-input-wrap {
flex: 1 1 220px;
margin-bottom: 0;
}
.city-lookup-submit {
flex-shrink: 0;
margin-bottom: 0;
}
.city-lookup-feedback {
font-size: 0.88rem;
margin: 0 0 1.1rem 0;
line-height: 1.45;
}
.city-lookup-feedback.ok {
color: var(--accent);
}
.city-lookup-feedback.err {
color: #b91c1c;
}
.pharmacy-tools-hint a {
color: var(--primary);
text-decoration: underline;
text-underline-offset: 2px;
}
.pharmacy-tools-hint a:hover {
color: var(--primary-hover);
}
.region-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
margin-bottom: 1rem;
}
@media (max-width: 640px) {
.region-grid {
grid-template-columns: 1fr;
}
}
.region-presets {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.5rem 1rem;
margin-bottom: 1rem;
}
.region-presets label {
font-size: 0.8rem;
font-weight: 600;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.04em;
}
.region-presets select {
padding: 0.45rem 0.75rem;
border-radius: var(--radius-sm);
border: 1px solid var(--border);
font-family: inherit;
font-size: 0.9rem;
background: var(--surface);
color: var(--text-main);
}
.import-mode-row {
margin-bottom: 1rem;
}
.import-mode-select-wrap {
max-width: 28rem;
margin-bottom: 0;
}
.open-data-url-row {
margin-bottom: 1rem;
}
.pharmacy-tools-hint code {
font-size: 0.85em;
background: var(--surface-muted);
padding: 0.15rem 0.4rem;
border-radius: 4px;
border: 1px solid var(--border);
}
.tool-actions-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 1rem 1.25rem;
}
.tool-actions-row .btn-import-webhook {
margin: 0;
}
.filter-region-toggle {
display: inline-flex;
align-items: center;
gap: 0.5rem;
font-size: 0.9rem;
color: var(--text-main);
cursor: pointer;
user-select: none;
}
.filter-region-toggle input {
width: 1rem;
height: 1rem;
accent-color: var(--primary);
}
.import-feedback {
margin-top: 1rem;
padding: 0.85rem 1rem;
border-radius: var(--radius-sm);
font-size: 0.9rem;
line-height: 1.5;
}
.import-feedback.success {
background: rgba(4, 120, 87, 0.1);
border: 1px solid rgba(4, 120, 87, 0.25);
color: var(--text-main);
}
.import-feedback.error {
background: #fef2f2;
border: 1px solid #fecaca;
color: #991b1b;
}
.list-meta {
font-size: 0.88rem;
color: var(--text-muted);
margin: -0.5rem 0 1rem 0;
}
@media (max-width: 768px) {
.form-row {
grid-template-columns: 1fr;
}
.admin-item {
flex-direction: column;
align-items: flex-start;
gap: 1rem;
}
.item-actions {
width: 100%;
}
.item-actions button {
flex: 1;
}
.section-header {
flex-direction: column;
align-items: flex-start;
}
}

View File

@@ -0,0 +1,162 @@
.login-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 60vh;
padding: 2rem;
animation: fadeInUp 0.8s ease-out;
}
.login-box {
background: var(--surface);
border-radius: var(--radius);
padding: 3rem;
box-shadow: var(--glass-shadow);
border: 1px solid var(--border);
width: 100%;
max-width: 420px;
}
.login-header {
text-align: center;
margin-bottom: 2.5rem;
}
.login-header h2 {
color: var(--text-main);
margin-bottom: 0.5rem;
font-size: 1.85rem;
font-weight: 800;
letter-spacing: -0.02em;
}
.login-header h2::after {
content: "";
display: block;
width: 2.5rem;
height: 3px;
margin: 0.85rem auto 0;
background: var(--primary);
border-radius: 2px;
}
.login-header p {
color: var(--text-muted);
font-size: 0.95rem;
}
.login-form {
display: flex;
flex-direction: column;
gap: 1.5rem;
}
.login-form .form-group {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.login-form .form-group label {
color: var(--text-main);
font-weight: 600;
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.login-form .form-group input {
padding: 0.85rem 1rem;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
font-size: 1rem;
font-family: inherit;
transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
background: var(--surface-muted);
}
.login-form .form-group input:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px var(--primary-ring);
background: var(--surface);
}
.login-form .form-group input:disabled {
background: var(--surface-muted);
cursor: not-allowed;
opacity: 0.7;
}
.error-message {
background: #fef2f2;
color: #b91c1c;
padding: 0.75rem 1rem;
border-radius: var(--radius-sm);
border: 1px solid #fecaca;
font-size: 0.9rem;
text-align: center;
font-weight: 500;
}
.login-button {
background: var(--primary);
color: #fff;
border: none;
padding: 0.95rem;
border-radius: var(--radius-sm);
font-size: 1rem;
font-weight: 700;
cursor: pointer;
transition: background 0.2s, transform 0.2s, box-shadow 0.2s;
margin-top: 0.5rem;
font-family: inherit;
}
.login-button:hover:not(:disabled) {
background: var(--primary-hover);
transform: translateY(-1px);
box-shadow: 0 6px 18px var(--primary-shadow);
}
.login-button:disabled {
background: var(--border-strong);
cursor: not-allowed;
}
.login-footer {
margin-top: 2rem;
text-align: center;
}
.help-text {
color: var(--text-muted);
font-size: 0.85rem;
margin: 0.5rem 0;
}
.help-text code {
background: var(--surface-muted);
padding: 0.2rem 0.5rem;
border-radius: 6px;
font-size: 0.85em;
color: var(--primary);
font-weight: 600;
border: 1px solid var(--border);
}
.warning-text {
color: var(--accent-warm);
background: rgba(180, 83, 9, 0.08);
padding: 0.75rem 1rem;
border-radius: var(--radius-sm);
font-size: 0.85rem;
margin-top: 1rem;
border: 1px solid rgba(180, 83, 9, 0.2);
}
@media (max-width: 768px) {
.login-box {
padding: 2rem;
}
}

View File

@@ -0,0 +1,106 @@
import React, { useState } from 'react';
import './LoginForm.css';
function LoginForm({ onLogin }) {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const handleSubmit = async (e) => {
e.preventDefault();
setError('');
setLoading(true);
try {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include', // Important for sessions
body: JSON.stringify({ username, password }),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || 'Login failed');
}
// Success - notify parent component
onLogin(data.user);
} catch (error) {
console.error('Login error:', error);
setError(error.message || 'Invalid username or password');
} finally {
setLoading(false);
}
};
return (
<div className="login-container">
<div className="login-box">
<div className="login-header">
<h2>🔐 Admin Login</h2>
<p>Please enter your credentials to access the admin panel</p>
</div>
<form onSubmit={handleSubmit} className="login-form">
{error && (
<div className="error-message">
{error}
</div>
)}
<div className="form-group">
<label htmlFor="username">Username</label>
<input
id="username"
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Enter username"
required
autoFocus
disabled={loading}
/>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Enter password"
required
disabled={loading}
/>
</div>
<button
type="submit"
className="login-button"
disabled={loading || !username || !password}
>
{loading ? 'Logging in...' : 'Login'}
</button>
</form>
<div className="login-footer">
<p className="help-text">
Default credentials: <code>admin</code> / <code>admin123</code>
</p>
<p className="warning-text">
Change the default password after first login!
</p>
</div>
</div>
</div>
);
}
export default LoginForm;

View File

@@ -0,0 +1,102 @@
import React, { useState, useEffect } from 'react';
import './AdminComponents.css';
const SEARCH_DEBOUNCE_MS = 400;
function MedicineManagement() {
const [searchQuery, setSearchQuery] = useState('');
const [medicines, setMedicines] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
const q = searchQuery.trim();
if (q.length < 2) {
setMedicines([]);
setLoading(false);
return;
}
const controller = new AbortController();
const timeoutId = setTimeout(async () => {
setLoading(true);
try {
const response = await fetch(
`/api/medicines/search?q=${encodeURIComponent(q)}`,
{ credentials: 'include', signal: controller.signal }
);
const data = await response.json();
setMedicines(Array.isArray(data) ? data : []);
} catch (error) {
if (error.name === 'AbortError') return;
console.error('Error searching medicines:', error);
alert('Error searching medicines from CIMA API');
} finally {
if (!controller.signal.aborted) setLoading(false);
}
}, SEARCH_DEBOUNCE_MS);
return () => {
clearTimeout(timeoutId);
controller.abort();
};
}, [searchQuery]);
return (
<div className="admin-section">
<div className="section-header">
<h2>Search Medicines (CIMA API)</h2>
</div>
<div className="info-box">
<p> Los medicamentos ahora se obtienen directamente de la <strong>API de CIMA</strong> (Agencia Española de Medicamentos y Productos Sanitarios).</p>
<p>Busca medicamentos para vincularlos a farmacias en la pestaña "Link Medicine".</p>
</div>
<div className="admin-form">
<div className="form-group">
<label>Search for medicines</label>
<input
type="text"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Escribe el nombre de un medicamento..."
/>
</div>
</div>
{loading && <div className="loading">Searching CIMA API...</div>}
{!loading && medicines.length > 0 && (
<div className="admin-list">
<p className="info-text">Found {medicines.length} medicines</p>
{medicines.map((medicine) => (
<div key={medicine.nregistro} className="admin-item">
<div className="item-content">
<h4>{medicine.name}</h4>
{medicine.active_ingredient && (
<p><strong>Principio Activo:</strong> {medicine.active_ingredient}</p>
)}
<p>
{medicine.dosage && <span><strong>Dosis:</strong> {medicine.dosage}</span>}
{medicine.dosage && medicine.form && ' • '}
{medicine.form && <span><strong>Forma:</strong> {medicine.form}</span>}
</p>
<p className="medicine-meta">
<strong>Laboratorio:</strong> {medicine.laboratory}
<strong> Registro:</strong> {medicine.nregistro}
{medicine.generic ? ' Genérico' : ' Marca'}
</p>
</div>
</div>
))}
</div>
)}
{!loading && searchQuery.trim().length >= 2 && medicines.length === 0 && (
<p className="empty-state">No se encontraron medicamentos con ese nombre.</p>
)}
</div>
);
}
export default MedicineManagement;

View File

@@ -0,0 +1,629 @@
import React, { useState, useEffect, useMemo } from 'react';
import './AdminComponents.css';
/** Distance in metres between two WGS84 points */
function haversineMeters(lat1, lon1, lat2, lon2) {
const R = 6371000;
const toRad = (d) => (d * Math.PI) / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
return 2 * R * Math.asin(Math.sqrt(Math.min(1, a)));
}
const REGION_PRESETS = [
{ id: 'custom', label: 'Custom coordinates', lat: '', lon: '', radio: '' },
{
id: 'rubi',
label: 'Example: Rubí area (1.5 km)',
lat: '41.5631',
lon: '2.0038',
radio: '1500',
},
];
async function geocodeErrorMessage(response) {
const text = await response.text();
let body = {};
try {
body = text ? JSON.parse(text) : {};
} catch {
/* non-JSON */
}
if (typeof body.error === 'string' && body.error.trim()) return body.error;
if (response.status === 401) {
return 'Session expired or not logged in. Sign in again on Admin, then retry.';
}
if (response.status === 404) {
const looksLikeHtml = /<!DOCTYPE|<html[\s>]/i.test(text || '');
if (looksLikeHtml) {
return 'The app could not reach the API (404). Use http://localhost:3000 with both frontend and backend running, or configure your server to proxy /api to the backend.';
}
return 'Geocode service not found. Update the backend and restart it.';
}
return `Lookup failed (HTTP ${response.status}).`;
}
function PharmacyManagement() {
const [pharmacies, setPharmacies] = useState([]);
const [loading, setLoading] = useState(false);
const [saving, setSaving] = useState(false);
const [showForm, setShowForm] = useState(false);
const [editingPharmacy, setEditingPharmacy] = useState(null);
const [formData, setFormData] = useState({
name: '',
address: '',
phone: '',
latitude: '',
longitude: '',
});
const [cityQuery, setCityQuery] = useState('');
const [cityLookupLoading, setCityLookupLoading] = useState(false);
const [cityLookupMessage, setCityLookupMessage] = useState(null);
const [regionLat, setRegionLat] = useState('41.5631');
const [regionLon, setRegionLon] = useState('2.0038');
const [regionRadio, setRegionRadio] = useState('1500');
const [regionPreset, setRegionPreset] = useState('rubi');
const [filterByRegion, setFilterByRegion] = useState(false);
const [importing, setImporting] = useState(false);
const [importFeedback, setImportFeedback] = useState(null);
/** @type {'webhook' | 'osm' | 'openData'} */
const [importMode, setImportMode] = useState('osm');
const [openDataUrl, setOpenDataUrl] = useState('');
useEffect(() => {
fetchPharmacies();
}, []);
const fetchPharmacies = async () => {
setLoading(true);
try {
const response = await fetch('/api/pharmacies', {
credentials: 'include',
});
const data = await response.json();
setPharmacies(data);
} catch (error) {
console.error('Error fetching pharmacies:', error);
alert('Error loading pharmacies');
} finally {
setLoading(false);
}
};
const applyPreset = (id) => {
setRegionPreset(id);
const p = REGION_PRESETS.find((x) => x.id === id);
if (!p || id === 'custom') return;
setRegionLat(p.lat);
setRegionLon(p.lon);
setRegionRadio(p.radio);
};
const displayedPharmacies = useMemo(() => {
if (!filterByRegion) return pharmacies;
const lat = parseFloat(regionLat);
const lon = parseFloat(regionLon);
const r = parseFloat(regionRadio);
if (!Number.isFinite(lat) || !Number.isFinite(lon) || !Number.isFinite(r)) {
return pharmacies;
}
return pharmacies.filter((p) => {
if (p.latitude == null || p.longitude == null) return false;
return haversineMeters(lat, lon, p.latitude, p.longitude) <= r;
});
}, [pharmacies, filterByRegion, regionLat, regionLon, regionRadio]);
const handleCityLookup = async (e) => {
e?.preventDefault();
const q = cityQuery.trim();
if (!q) {
setCityLookupMessage({ type: 'err', text: 'Enter a city or place name.' });
return;
}
setCityLookupLoading(true);
setCityLookupMessage(null);
try {
const response = await fetch(`/api/admin/geocode?q=${encodeURIComponent(q)}`, {
credentials: 'include',
});
if (!response.ok) {
throw new Error(await geocodeErrorMessage(response));
}
const data = await response.json();
setRegionLat(String(data.lat));
setRegionLon(String(data.lon));
setRegionRadio(String(data.radius));
setRegionPreset('custom');
setCityLookupMessage({
type: 'ok',
text: `Using: ${data.displayName} — radius ~${data.radius} m (you can edit below).`,
});
} catch (err) {
setCityLookupMessage({ type: 'err', text: err.message });
} finally {
setCityLookupLoading(false);
}
};
const handlePharmacyImport = async () => {
setImporting(true);
setImportFeedback(null);
try {
if (importMode === 'webhook') {
const body = {};
const lat = parseFloat(regionLat);
const lon = parseFloat(regionLon);
const radio = parseFloat(regionRadio);
if (Number.isFinite(lat) && Number.isFinite(lon) && Number.isFinite(radio)) {
body.lat = lat;
body.lon = lon;
body.radio = radio;
}
const response = await fetch('/api/admin/pharmacies/import-webhook', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(body),
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || `Import failed (${response.status})`);
}
setImportFeedback({
type: 'success',
text: `[n8n] Imported ${data.inserted} new. Skipped ${data.skipped} duplicate(s). ${data.invalid || 0} invalid. ${data.totalReceived} from webhook.`,
});
await fetchPharmacies();
return;
}
if (importMode === 'openData') {
const url = openDataUrl.trim();
if (!url) {
throw new Error('Paste an open-data JSON URL (array or GeoJSON).');
}
const response = await fetch('/api/admin/pharmacies/import-external', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ source: 'openData', openDataUrl: url }),
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || `Import failed (${response.status})`);
}
const label = data.message || `openData: ${data.totalReceived} rows`;
setImportFeedback({
type: 'success',
text: `${label}. Inserted ${data.inserted}, skipped ${data.skipped}, invalid ${data.invalid || 0}.`,
});
await fetchPharmacies();
return;
}
const lat = parseFloat(regionLat);
const lon = parseFloat(regionLon);
const radio = parseFloat(regionRadio);
if (!Number.isFinite(lat) || !Number.isFinite(lon) || !Number.isFinite(radio)) {
throw new Error('Set latitude, longitude and radius (use Find city or a preset).');
}
const response = await fetch('/api/admin/pharmacies/import-external', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({
source: importMode,
lat,
lon,
radio,
}),
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || `Import failed (${response.status})`);
}
const src = data.source === 'osm' ? 'OSM' : 'OpenStreetMap';
setImportFeedback({
type: 'success',
text: `[${src}] ${data.message || `Received ${data.totalReceived} pharmacies.`} Inserted ${data.inserted}, skipped ${data.skipped}, invalid ${data.invalid || 0}.`,
});
await fetchPharmacies();
} catch (error) {
setImportFeedback({ type: 'error', text: error.message });
} finally {
setImporting(false);
}
};
const handleSubmit = async (e) => {
e.preventDefault();
if (saving) return;
setSaving(true);
try {
const payload = {
...formData,
latitude: formData.latitude ? parseFloat(formData.latitude) : null,
longitude: formData.longitude ? parseFloat(formData.longitude) : null,
};
if (editingPharmacy) {
const response = await fetch(`/api/admin/pharmacies/${editingPharmacy.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(payload),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Failed to update pharmacy');
}
} else {
const response = await fetch('/api/admin/pharmacies', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(payload),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Failed to create pharmacy');
}
}
resetForm();
fetchPharmacies();
alert(editingPharmacy ? 'Pharmacy updated successfully!' : 'Pharmacy added successfully!');
} catch (error) {
console.error('Error saving pharmacy:', error);
alert(`Error saving pharmacy: ${error.message}`);
} finally {
setSaving(false);
}
};
const handleEdit = (pharmacy) => {
setEditingPharmacy(pharmacy);
setFormData({
name: pharmacy.name || '',
address: pharmacy.address || '',
phone: pharmacy.phone || '',
latitude: pharmacy.latitude ?? '',
longitude: pharmacy.longitude ?? '',
});
setShowForm(true);
};
const handleDelete = async (id) => {
if (!confirm('Are you sure you want to delete this pharmacy?')) return;
try {
const response = await fetch(`/api/admin/pharmacies/${id}`, {
method: 'DELETE',
credentials: 'include',
});
if (!response.ok) throw new Error('Failed to delete pharmacy');
fetchPharmacies();
alert('Pharmacy deleted successfully!');
} catch (error) {
console.error('Error deleting pharmacy:', error);
alert('Error deleting pharmacy');
}
};
const resetForm = () => {
setFormData({
name: '',
address: '',
phone: '',
latitude: '',
longitude: '',
});
setEditingPharmacy(null);
setShowForm(false);
};
const onRegionFieldChange = (setter) => (e) => {
setRegionPreset('custom');
setter(e.target.value);
};
return (
<div className="admin-section">
<div className="section-header">
<h2>Manage Pharmacies</h2>
<button
type="button"
className="btn-primary"
onClick={() => {
resetForm();
setShowForm(true);
}}
>
+ Add New Pharmacy
</button>
</div>
<div className="pharmacy-tools-card">
<h3>City, region &amp; import</h3>
<p className="pharmacy-tools-hint">
<strong>Find city</strong> sets latitude, longitude and radius for the map filter and for imports.
Choose a <strong>data source</strong> below: <strong>OpenStreetMap</strong> is free (no key);{' '}
<strong>Open data URL</strong> loads JSON you host (array or GeoJSON). Geocoding uses{' '}
<a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noreferrer">
OpenStreetMap
</a>{' '}
(Nominatim).
</p>
<form className="city-lookup-form" onSubmit={handleCityLookup}>
<div className="form-group city-lookup-input-wrap">
<label htmlFor="city-finder">Find city</label>
<input
id="city-finder"
type="search"
placeholder="e.g. Rubí, Spain — or Madrid, Valencia…"
value={cityQuery}
onChange={(e) => {
setCityQuery(e.target.value);
setCityLookupMessage(null);
}}
autoComplete="address-level2"
/>
</div>
<button
type="submit"
className="btn-secondary city-lookup-submit"
disabled={cityLookupLoading}
>
{cityLookupLoading ? 'Looking up…' : 'Look up city'}
</button>
</form>
{cityLookupMessage && (
<p
className={`city-lookup-feedback ${cityLookupMessage.type === 'ok' ? 'ok' : 'err'}`}
role="status"
>
{cityLookupMessage.text}
</p>
)}
<div className="region-presets">
<label htmlFor="region-preset">Area preset</label>
<select
id="region-preset"
value={regionPreset}
onChange={(e) => applyPreset(e.target.value)}
>
{REGION_PRESETS.map((p) => (
<option key={p.id} value={p.id}>
{p.label}
</option>
))}
</select>
</div>
<div className="region-grid">
<div className="form-group">
<label htmlFor="region-lat">Latitude</label>
<input
id="region-lat"
type="text"
inputMode="decimal"
value={regionLat}
onChange={onRegionFieldChange(setRegionLat)}
placeholder="41.5631"
/>
</div>
<div className="form-group">
<label htmlFor="region-lon">Longitude</label>
<input
id="region-lon"
type="text"
inputMode="decimal"
value={regionLon}
onChange={onRegionFieldChange(setRegionLon)}
placeholder="2.0038"
/>
</div>
<div className="form-group">
<label htmlFor="region-radio">Radius (m)</label>
<input
id="region-radio"
type="text"
inputMode="numeric"
value={regionRadio}
onChange={onRegionFieldChange(setRegionRadio)}
placeholder="1500"
/>
</div>
</div>
<div className="import-mode-row">
<div className="form-group import-mode-select-wrap">
<label htmlFor="import-mode">Data source</label>
<select
id="import-mode"
value={importMode}
onChange={(e) => {
setImportMode(e.target.value);
setImportFeedback(null);
}}
>
<option value="osm">OpenStreetMap (Overpass, free)</option>
<option value="webhook">n8n webhook (legacy)</option>
<option value="openData">Open data JSON URL</option>
</select>
</div>
</div>
{importMode === 'openData' && (
<div className="form-group open-data-url-row">
<label htmlFor="open-data-url">JSON URL</label>
<input
id="open-data-url"
type="url"
placeholder="https://…/pharmacies.json"
value={openDataUrl}
onChange={(e) => setOpenDataUrl(e.target.value)}
autoComplete="off"
/>
</div>
)}
<div className="tool-actions-row">
<button
type="button"
className="btn-primary btn-import-webhook"
onClick={handlePharmacyImport}
disabled={importing}
>
{importing
? 'Importing…'
: importMode === 'webhook'
? 'Import from webhook'
: importMode === 'openData'
? 'Import from URL'
: `Import from ${importMode === 'osm' ? 'Overpass' : 'OpenStreetMap'}`}
</button>
<label className="filter-region-toggle">
<input
type="checkbox"
checked={filterByRegion}
onChange={(e) => setFilterByRegion(e.target.checked)}
/>
Show only pharmacies inside radius
</label>
</div>
{importFeedback && (
<div
className={`import-feedback ${importFeedback.type === 'success' ? 'success' : 'error'}`}
role="status"
>
{importFeedback.text}
</div>
)}
</div>
{showForm && (
<form className="admin-form" onSubmit={handleSubmit}>
<h3>{editingPharmacy ? 'Edit Pharmacy' : 'Add New Pharmacy'}</h3>
<div className="form-group">
<label>Name *</label>
<input
type="text"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
required
/>
</div>
<div className="form-group">
<label>Address *</label>
<input
type="text"
value={formData.address}
onChange={(e) => setFormData({ ...formData, address: e.target.value })}
required
/>
</div>
<div className="form-group">
<label>Phone</label>
<input
type="text"
value={formData.phone}
onChange={(e) => setFormData({ ...formData, phone: e.target.value })}
/>
</div>
<div className="form-row">
<div className="form-group">
<label>Latitude</label>
<input
type="number"
step="any"
value={formData.latitude}
onChange={(e) => setFormData({ ...formData, latitude: e.target.value })}
/>
</div>
<div className="form-group">
<label>Longitude</label>
<input
type="number"
step="any"
value={formData.longitude}
onChange={(e) => setFormData({ ...formData, longitude: e.target.value })}
/>
</div>
</div>
<div className="form-actions">
<button type="submit" className="btn-primary" disabled={saving}>
{saving ? 'Saving...' : editingPharmacy ? 'Update' : 'Add'} Pharmacy
</button>
<button type="button" className="btn-secondary" onClick={resetForm} disabled={saving}>
Cancel
</button>
</div>
</form>
)}
{loading ? (
<div className="loading">Loading pharmacies...</div>
) : (
<div className="admin-list">
<p className="list-meta">
Showing {displayedPharmacies.length} of {pharmacies.length} pharmacies
{filterByRegion && ' (inside radius)'}
</p>
{displayedPharmacies.length === 0 ? (
<p className="empty-state">
{pharmacies.length === 0
? 'No pharmacies yet. Import from webhook or add one manually.'
: 'No pharmacies in this radius with coordinates. Widen the radius, look up a different city, or turn off the region filter.'}
</p>
) : (
displayedPharmacies.map((pharmacy) => (
<div key={pharmacy.id} className="admin-item">
<div className="item-content">
<h4>{pharmacy.name}</h4>
<p>📍 {pharmacy.address}</p>
{pharmacy.phone && <p>📞 {pharmacy.phone}</p>}
{(pharmacy.latitude != null || pharmacy.longitude != null) && (
<p>
🌐 {pharmacy.latitude}, {pharmacy.longitude}
</p>
)}
</div>
<div className="item-actions">
<button type="button" className="btn-edit" onClick={() => handleEdit(pharmacy)}>
Edit
</button>
<button type="button" className="btn-delete" onClick={() => handleDelete(pharmacy.id)}>
Delete
</button>
</div>
</div>
))
)}
</div>
)}
</div>
);
}
export default PharmacyManagement;

View File

@@ -0,0 +1,339 @@
import React, { useState, useEffect } from 'react';
import './AdminComponents.css';
function PharmacyMedicineLink() {
const [pharmacies, setPharmacies] = useState([]);
const [medicineSearch, setMedicineSearch] = useState('');
const [medicineResults, setMedicineResults] = useState([]);
const [selectedPharmacy, setSelectedPharmacy] = useState(null);
const [selectedMedicine, setSelectedMedicine] = useState(null);
const [pharmacyMedicines, setPharmacyMedicines] = useState([]);
const [loading, setLoading] = useState(false);
const [searching, setSearching] = useState(false);
const [formData, setFormData] = useState({
pharmacy_id: '',
price: '',
stock: ''
});
useEffect(() => {
fetchPharmacies();
}, []);
useEffect(() => {
if (selectedPharmacy) {
fetchPharmacyMedicines(selectedPharmacy.id);
}
}, [selectedPharmacy]);
// Buscar medicamentos en la API de CIMA mientras el usuario escribe
useEffect(() => {
const q = medicineSearch.trim();
if (q.length < 2) {
setMedicineResults([]);
setSearching(false);
return;
}
const controller = new AbortController();
const timeoutId = setTimeout(async () => {
setSearching(true);
try {
const response = await fetch(`/api/medicines/search?q=${encodeURIComponent(q)}`, {
credentials: 'include',
signal: controller.signal,
});
const data = await response.json();
setMedicineResults(Array.isArray(data) ? data : []);
} catch (error) {
if (error.name === 'AbortError') return;
console.error('Error searching medicines:', error);
} finally {
if (!controller.signal.aborted) setSearching(false);
}
}, 500);
return () => {
clearTimeout(timeoutId);
controller.abort();
};
}, [medicineSearch]);
const fetchPharmacies = async () => {
try {
const response = await fetch('/api/pharmacies', {
credentials: 'include',
});
const data = await response.json();
setPharmacies(data);
} catch (error) {
console.error('Error fetching pharmacies:', error);
}
};
const fetchPharmacyMedicines = async (pharmacyId) => {
setLoading(true);
try {
const response = await fetch(`/api/admin/pharmacies/${pharmacyId}/medicines`, {
credentials: 'include',
});
const data = await response.json();
setPharmacyMedicines(data);
} catch (error) {
console.error('Error fetching pharmacy medicines:', error);
} finally {
setLoading(false);
}
};
const handleSubmit = async (e) => {
e.preventDefault();
if (!selectedMedicine) {
alert('Please select a medicine first');
return;
}
try {
const payload = {
pharmacy_id: parseInt(formData.pharmacy_id),
medicine_nregistro: selectedMedicine.nregistro,
medicine_name: selectedMedicine.name,
price: formData.price ? parseFloat(formData.price) : null,
stock: formData.stock ? parseInt(formData.stock) : 0
};
const response = await fetch('/api/admin/pharmacy-medicines', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(payload)
});
if (!response.ok) throw new Error('Failed to link medicine to pharmacy');
resetForm();
if (selectedPharmacy) {
fetchPharmacyMedicines(selectedPharmacy.id);
}
alert('Medicine linked to pharmacy successfully!');
} catch (error) {
console.error('Error linking medicine:', error);
alert('Error linking medicine to pharmacy');
}
};
const handleUpdate = async (id, price, stock) => {
try {
const response = await fetch(`/api/admin/pharmacy-medicines/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ price, stock })
});
if (!response.ok) throw new Error('Failed to update');
fetchPharmacyMedicines(selectedPharmacy.id);
alert('Updated successfully!');
} catch (error) {
console.error('Error updating:', error);
alert('Error updating');
}
};
const handleDelete = async (id) => {
if (!confirm('Remove this medicine from the pharmacy?')) return;
try {
const response = await fetch(`/api/admin/pharmacy-medicines/${id}`, {
method: 'DELETE',
credentials: 'include'
});
if (!response.ok) throw new Error('Failed to delete');
fetchPharmacyMedicines(selectedPharmacy.id);
alert('Medicine removed from pharmacy!');
} catch (error) {
console.error('Error deleting:', error);
alert('Error removing medicine');
}
};
const resetForm = () => {
setFormData({
pharmacy_id: selectedPharmacy ? selectedPharmacy.id.toString() : '',
price: '',
stock: ''
});
setSelectedMedicine(null);
setMedicineSearch('');
setMedicineResults([]);
};
const selectedPharmacyObj = pharmacies.find(p => p.id === parseInt(formData.pharmacy_id));
const selectMedicine = (medicine) => {
setSelectedMedicine(medicine);
setMedicineSearch(medicine.name);
setMedicineResults([]);
};
return (
<div className="admin-section">
<h2>Link Medicine to Pharmacy</h2>
<form className="admin-form" onSubmit={handleSubmit}>
<div className="form-group">
<label>Pharmacy *</label>
<select
value={formData.pharmacy_id}
onChange={(e) => {
const pharmacy = pharmacies.find(p => p.id === parseInt(e.target.value));
setSelectedPharmacy(pharmacy);
setFormData({ ...formData, pharmacy_id: e.target.value });
}}
required
>
<option value="">Select a pharmacy</option>
{pharmacies.map((pharmacy) => (
<option key={pharmacy.id} value={pharmacy.id}>
{pharmacy.name} - {pharmacy.address}
</option>
))}
</select>
</div>
<div className="form-group">
<label>Search Medicine (CIMA API) *</label>
<input
type="text"
value={medicineSearch}
onChange={(e) => {
setMedicineSearch(e.target.value);
setSelectedMedicine(null);
}}
placeholder="Type to search medicines from CIMA..."
required
/>
{searching && <p className="loading-text">Searching...</p>}
{medicineResults.length > 0 && !selectedMedicine && (
<div className="medicine-search-results">
{medicineResults.slice(0, 10).map((medicine) => (
<div
key={medicine.nregistro}
className="search-result-item"
onClick={() => selectMedicine(medicine)}
>
<strong>{medicine.name}</strong>
{medicine.active_ingredient && <span> - {medicine.active_ingredient}</span>}
{medicine.dosage && <span> ({medicine.dosage})</span>}
</div>
))}
</div>
)}
{selectedMedicine && (
<div className="selected-medicine-info">
<p> Selected: <strong>{selectedMedicine.name}</strong></p>
<p className="medicine-details">
{selectedMedicine.active_ingredient && `Principio activo: ${selectedMedicine.active_ingredient}`}
{selectedMedicine.dosage && `Dosis: ${selectedMedicine.dosage}`}
Registro: {selectedMedicine.nregistro}
</p>
<button
type="button"
className="btn-small"
onClick={() => {
setSelectedMedicine(null);
setMedicineSearch('');
}}
>
Change medicine
</button>
</div>
)}
</div>
<div className="form-row">
<div className="form-group">
<label>Price ()</label>
<input
type="number"
step="0.01"
value={formData.price}
onChange={(e) => setFormData({ ...formData, price: e.target.value })}
placeholder="e.g., 12.50"
/>
</div>
<div className="form-group">
<label>Stock</label>
<input
type="number"
value={formData.stock}
onChange={(e) => setFormData({ ...formData, stock: e.target.value })}
placeholder="e.g., 50"
/>
</div>
</div>
<div className="form-actions">
<button type="submit" className="btn-primary">
Link Medicine
</button>
<button type="button" className="btn-secondary" onClick={resetForm}>
Reset
</button>
</div>
</form>
{selectedPharmacy && (
<div className="pharmacy-medicines-section">
<h3>Medicines at {selectedPharmacy.name}</h3>
{loading ? (
<div className="loading">Loading...</div>
) : pharmacyMedicines.length === 0 ? (
<p className="empty-state">No medicines linked to this pharmacy yet.</p>
) : (
<div className="admin-list">
{pharmacyMedicines.map((pm) => (
<div key={pm.id} className="admin-item">
<div className="item-content">
<h4>{pm.medicine_name}</h4>
<p>
<strong>Price:</strong> {pm.price ? `${parseFloat(pm.price).toFixed(2)}` : 'Not set'}
<strong> Stock:</strong> {pm.stock || 0}
</p>
</div>
<div className="item-actions">
<button
className="btn-edit"
onClick={() => {
const newPrice = prompt('Enter new price:', pm.price || '');
const newStock = prompt('Enter new stock:', pm.stock || '0');
if (newPrice !== null && newStock !== null) {
handleUpdate(pm.id, newPrice ? parseFloat(newPrice) : null, parseInt(newStock) || 0);
}
}}
>
Update
</button>
<button className="btn-delete" onClick={() => handleDelete(pm.id)}>
Remove
</button>
</div>
</div>
))}
</div>
)}
</div>
)}
</div>
);
}
export default PharmacyMedicineLink;

54
frontend/src/index.css Normal file
View File

@@ -0,0 +1,54 @@
:root {
/* Brand — teal (trust / health), no purple */
--primary: #0f766e;
--primary-hover: #0d9488;
--primary-light: #ccfbf1;
--primary-faint: rgba(15, 118, 110, 0.08);
--primary-ring: rgba(15, 118, 110, 0.22);
--primary-shadow: rgba(15, 118, 110, 0.18);
/* Warm secondary for emphasis (badges, subtle highlights) */
--accent: #047857;
--accent-warm: #b45309;
/* Neutrals */
--text-main: #1c1917;
--text-muted: #57534e;
--surface: #ffffff;
--surface-muted: #f5f5f4;
--surface-card: #fafaf9;
--border: #e7e5e4;
--border-strong: #d6d3d1;
--glass-bg: rgba(255, 255, 255, 0.92);
--glass-border: rgba(231, 229, 228, 0.9);
--glass-shadow: 0 1px 3px rgba(28, 25, 23, 0.06), 0 8px 24px rgba(28, 25, 23, 0.04);
--radius: 14px;
--radius-sm: 10px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Outfit', system-ui, sans-serif;
color: var(--text-main);
background-color: var(--surface-muted);
background-image:
linear-gradient(rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0.85)),
url('./assets/bg.png');
background-size: cover;
background-position: center;
background-attachment: fixed;
min-height: 100vh;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
#root {
min-height: 100vh;
}

11
frontend/src/main.jsx Normal file
View File

@@ -0,0 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);

View File

@@ -0,0 +1,127 @@
.admin-header-content {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2rem;
text-align: left;
}
.admin-header-content h1 {
margin-bottom: 0.35rem;
font-size: 2.35rem;
font-weight: 800;
color: var(--text-main);
letter-spacing: -0.02em;
}
.admin-header-content h1::after {
display: none;
}
.admin-header-content p {
color: var(--text-muted);
font-size: 1.05rem;
margin: 0;
line-height: 1.5;
}
.admin-user-info {
display: flex;
align-items: center;
gap: 1rem;
background: var(--surface);
padding: 0.5rem 1rem;
border-radius: 999px;
border: 1px solid var(--border);
box-shadow: 0 1px 2px rgba(28, 25, 23, 0.04);
}
.admin-user-info span {
font-weight: 600;
color: var(--text-main);
font-size: 0.9rem;
}
.logout-button {
background: #fef2f2;
color: #b91c1c;
border: 1px solid #fecaca;
padding: 0.4rem 0.85rem;
border-radius: 999px;
font-size: 0.8rem;
font-weight: 600;
cursor: pointer;
transition: background 0.2s, border-color 0.2s;
}
.logout-button:hover {
background: #fee2e2;
border-color: #fca5a5;
}
.admin-tabs {
display: flex;
gap: 0.35rem;
margin-bottom: 2rem;
padding: 0.35rem;
background: var(--surface-muted);
border-radius: var(--radius);
border: 1px solid var(--border);
width: fit-content;
flex-wrap: wrap;
}
.admin-tab {
background: transparent;
border: none;
padding: 0.7rem 1.35rem;
border-radius: var(--radius-sm);
font-weight: 600;
font-size: 0.9rem;
color: var(--text-muted);
cursor: pointer;
transition: color 0.2s, background 0.2s, box-shadow 0.2s;
}
.admin-tab:hover {
color: var(--text-main);
}
.admin-tab.active {
background: var(--surface);
color: var(--primary);
box-shadow: 0 1px 3px rgba(28, 25, 23, 0.08);
border: 1px solid var(--border);
}
.admin-content {
background: var(--surface);
border-radius: var(--radius);
padding: 2.5rem;
border: 1px solid var(--border);
box-shadow: var(--glass-shadow);
animation: fadeInUp 0.6s ease-out;
min-height: 400px;
}
@media (max-width: 768px) {
.admin-header-content {
flex-direction: column;
text-align: center;
gap: 1rem;
}
.admin-tabs {
flex-direction: column;
width: 100%;
}
.admin-tab {
width: 100%;
text-align: center;
}
.admin-content {
padding: 1.5rem;
}
}

View File

@@ -0,0 +1,131 @@
import React, { useState, useEffect } from 'react';
import '../App.css';
import './AdminView.css';
import LoginForm from '../components/admin/LoginForm';
import PharmacyManagement from '../components/admin/PharmacyManagement';
import MedicineManagement from '../components/admin/MedicineManagement';
import PharmacyMedicineLink from '../components/admin/PharmacyMedicineLink';
function AdminView() {
const [authenticated, setAuthenticated] = useState(false);
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState('pharmacies'); // 'pharmacies', 'medicines', 'link'
useEffect(() => {
checkAuth();
}, []);
const checkAuth = async () => {
try {
const response = await fetch('/api/auth/check', {
credentials: 'include',
});
const data = await response.json();
if (data.authenticated) {
setAuthenticated(true);
setUser(data.user);
} else {
setAuthenticated(false);
setUser(null);
}
} catch (error) {
console.error('Error checking auth:', error);
setAuthenticated(false);
} finally {
setLoading(false);
}
};
const handleLogin = (userData) => {
setAuthenticated(true);
setUser(userData);
};
const handleLogout = async () => {
try {
await fetch('/api/auth/logout', {
method: 'POST',
credentials: 'include',
});
setAuthenticated(false);
setUser(null);
} catch (error) {
console.error('Error logging out:', error);
}
};
if (loading) {
return (
<div className="app-main">
<div className="loading">Checking authentication...</div>
</div>
);
}
if (!authenticated) {
return (
<>
<header className="app-header">
<h1> Admin Panel</h1>
<p>Authentication required</p>
</header>
<main className="app-main">
<LoginForm onLogin={handleLogin} />
</main>
</>
);
}
return (
<>
<header className="app-header">
<div className="admin-header-content">
<div>
<h1> Admin Panel</h1>
<p>Manage pharmacies and medicines</p>
</div>
<div className="admin-user-info">
<span>👤 {user?.username}</span>
<button className="logout-button" onClick={handleLogout}>
Logout
</button>
</div>
</div>
</header>
<main className="app-main">
<div className="admin-tabs">
<button
className={`admin-tab ${activeTab === 'pharmacies' ? 'active' : ''}`}
onClick={() => setActiveTab('pharmacies')}
>
🏥 Pharmacies
</button>
<button
className={`admin-tab ${activeTab === 'medicines' ? 'active' : ''}`}
onClick={() => setActiveTab('medicines')}
>
💊 Medicines
</button>
<button
className={`admin-tab ${activeTab === 'link' ? 'active' : ''}`}
onClick={() => setActiveTab('link')}
>
🔗 Link Medicine to Pharmacy
</button>
</div>
<div className="admin-content">
{activeTab === 'pharmacies' && <PharmacyManagement />}
{activeTab === 'medicines' && <MedicineManagement />}
{activeTab === 'link' && <PharmacyMedicineLink />}
</div>
</main>
</>
);
}
export default AdminView;

View File

@@ -0,0 +1,117 @@
import React, { useState, useEffect } from 'react';
import '../App.css';
import SearchBar from '../components/SearchBar';
import MedicineResults from '../components/MedicineResults';
import PharmacyList from '../components/PharmacyList';
function PublicView() {
const [searchQuery, setSearchQuery] = useState('');
const [medicines, setMedicines] = useState([]);
const [selectedMedicine, setSelectedMedicine] = useState(null);
const [pharmacies, setPharmacies] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
const searchMedicines = async () => {
if (searchQuery.trim().length < 2) {
setMedicines([]);
setSelectedMedicine(null);
setPharmacies([]);
return;
}
setLoading(true);
try {
const response = await fetch(`/api/medicines/search?q=${encodeURIComponent(searchQuery)}`);
const data = await response.json();
setMedicines(data);
} catch (error) {
console.error('Error searching medicines:', error);
} finally {
setLoading(false);
}
};
const timeoutId = setTimeout(searchMedicines, 300);
return () => clearTimeout(timeoutId);
}, [searchQuery]);
useEffect(() => {
const fetchPharmacies = async () => {
if (!selectedMedicine) {
setPharmacies([]);
return;
}
setLoading(true);
try {
const response = await fetch(`/api/medicines/${selectedMedicine.id}/pharmacies`);
const data = await response.json();
setPharmacies(data);
} catch (error) {
console.error('Error fetching pharmacies:', error);
} finally {
setLoading(false);
}
};
fetchPharmacies();
}, [selectedMedicine]);
return (
<>
<header className="app-header">
<h1>💊 FarmaFinder</h1>
<p>Find your medicine at nearby pharmacies</p>
</header>
<main className="app-main">
<SearchBar
value={searchQuery}
onChange={setSearchQuery}
placeholder="Search for a medicine..."
/>
{loading && <div className="loading">Searching...</div>}
{searchQuery && !selectedMedicine && (
<MedicineResults
medicines={medicines}
onSelect={setSelectedMedicine}
query={searchQuery}
/>
)}
{selectedMedicine && (
<div className="selected-medicine-section">
<div className="medicine-info">
<h2>{selectedMedicine.name}</h2>
<div className="medicine-details">
<span><strong>Active Ingredient:</strong> {selectedMedicine.active_ingredient}</span>
<span><strong>Dosage:</strong> {selectedMedicine.dosage}</span>
<span><strong>Form:</strong> {selectedMedicine.form}</span>
</div>
<button
className="back-button"
onClick={() => {
setSelectedMedicine(null);
setPharmacies([]);
}}
>
Back to search
</button>
</div>
<PharmacyList
pharmacies={pharmacies}
loading={loading}
/>
</div>
)}
</main>
</>
);
}
export default PublicView;

18
frontend/vite.config.js Normal file
View File

@@ -0,0 +1,18 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
server: {
allowedHosts: ['localhost', 'oligocarpous-bilaterally-keiko.ngrok-free.dev'],
port: 3000,
proxy: {
'/api': {
target: 'http://localhost:3001',
changeOrigin: true,
credentials: true,
},
},
},
});