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

store page with items #33

Merged
merged 1 commit into from
Nov 4, 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
4 changes: 4 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CheckoutPage } from './pages/CheckoutPage';
import { CompletePage } from './pages/CompletePage';
import { CreateStore } from './pages/CreateStore';
import { StoreDashboard } from './pages/StoreDashboard';
import { StorePage } from './pages/StorePage';
import { Elements } from '@stripe/react-stripe-js';
import { TestPage } from './pages/TestPage';
import { AccountSetting } from './pages/AccountSetting';
Expand Down Expand Up @@ -127,6 +128,9 @@ function App() {
<Route
path="/test"
element={<TestPage />}
/><Route
path="/stores/:id"
element={<StorePage />}
/>
</Routes>
</Elements>
Expand Down
38 changes: 20 additions & 18 deletions src/components/Dashboard/ManageItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ export default function ManageItems() {
const [itemToEdit, setItemToEdit] = useState<Item | null>(null);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [itemToDelete, setItemToDelete] = useState<Item | null>(null);
const fetchItemsData = async () => {
try {
const response = await axios.get(`${apiUrl}/api/stores/items`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
if (response.data.success && response.data.items) {
setItems(response.data.items);
}
} catch (err) {
console.error('Error fetching store data', err);
const fetchItemsData = async () => {
try {
const response = await axios.get(`${apiUrl}/api/stores/items`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
if (response.data.success && response.data.items) {
setItems(response.data.items);
}
};
} catch (err) {
console.error('Error fetching store data', err);
}
};
useEffect(() => {

fetchItemsData();
}, [apiUrl, authToken]);
const openCreateItemDialog = () => {
Expand All @@ -94,7 +93,6 @@ const fetchItemsData = async () => {
setOpen(true);
};


const openDeleteDialog = (item: Item) => {
setItemToDelete(item);
setDeleteDialogOpen(true);
Expand Down Expand Up @@ -195,7 +193,11 @@ const fetchItemsData = async () => {
setItems((prevItems) =>
prevItems.map((item) =>
item.item_id === itemToEdit.item_id
? { ...item, ...formValues, image_url: imageUrl || item.image_url}
? {
...item,
...formValues,
image_url: imageUrl || item.image_url,
}
: item
)
);
Expand All @@ -206,8 +208,8 @@ const fetchItemsData = async () => {
await axios.post(
`${apiUrl}/api/stores/items`,
{
...formValues,
image_url: imageUrl,
...formValues,
image_url: imageUrl,
},
{
headers: {
Expand Down
11 changes: 10 additions & 1 deletion src/pages/SingleItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,17 @@ const SingleItem = () => {
direction="row"
my={2}
alignItems={'center'}
onClick={async () => {
navigate(`/stores/${item.store.store_id}`);
}}

>
<Typography fontSize={'0.9rem'}>
<Typography fontSize={'0.9rem'}sx={{
cursor: 'pointer',
'&:hover': {
textDecoration: 'underline'
}
}}>
{item.store.name}
</Typography>
<Rating
Expand Down
177 changes: 177 additions & 0 deletions src/pages/StorePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { useState, useEffect } from 'react';
import { useTheme } from '@mui/material/styles';
import { useParams } from 'react-router-dom';
import {
Stack,
Grid2,
Card,
CardContent,
CardMedia,
Typography,
} from '@mui/material';
import axios from 'axios';

export const StorePage = () => {
const theme = useTheme();
const storeId = useParams().id;
console.log('storeId:', storeId);
const apiUrl = import.meta.env.VITE_API_URL;
interface Store {
store_id: number;
name: string;
description: string;
logo_url: string;
}
interface Items {
item_id: number;
name: string;
description: string;
image_url: string;
}
const [store, setStore] = useState<Store | null>(null);
const [items, setItems] = useState<Items[]>([]);
useEffect(() => {
const fetchStoreData = async () => {
try {
const response = await axios.get(
`${apiUrl}/api/stores/${storeId}`
);
console.log(response.data);
if (response.data.success && response.data.store) {
setStore(response.data.store);
}
} catch (err) {
console.error('Error fetching store data', err);
}
};
fetchStoreData();
}, [apiUrl, storeId]);

useEffect(() => {
const fetchItemsForStore = async () => {
try {
const response = await axios.get(
`${apiUrl}/api/stores/${storeId}/items`
);
console.log(response.data);
if (response.data.success && response.data.items) {
setItems(response.data.items);
}
} catch (err) {
console.error('Error fetching store items', err);
}
};
fetchItemsForStore();
}, [apiUrl, storeId]);

const defaultLogoUrl = import.meta.env.VITE_DEFAULT_LOGO_URL;
return (
<Stack>
{/*<Stack sx={{ width: '100%', background: theme.palette.grey[400], pt:5, pb:5 }}>
</Stack>*/}
<Stack
sx={{
width: '100%',
background: theme.palette.grey[100],
pt: 2,
pb: 2,
mb: 3
}}
>
{store ? (
<>
<Card sx={{ display: 'flex', maxWidth: 600 }}>
<Stack direction='row' spacing={2}>
<CardMedia
component='img'
image={store.logo_url || defaultLogoUrl}
alt=''
/>
</Stack>

<CardContent sx={{ flex: 1 }}>
<Grid2
container
alignItems='center'
justifyContent='space-between'
>
<Typography
variant='caption'
color='text.secondary'
>
Store Name
</Typography>
</Grid2>

<Typography
gutterBottom
variant='h5'
component='div'
>
{store.name}
</Typography>

<Typography
sx={{
fontSize: '10px',
color: 'text.secondary',
}}
variant='caption'
color='text.secondary'
display='block'
gutterBottom
>
Store Description
</Typography>
<Typography
variant='body2'
color='text.secondary'
>
{store.description}
</Typography>
</CardContent>
</Card>
</>
) : (
'Error fetching the store'
)}
</Stack>
<Stack>
{items.length > 0 ? (
<Stack>
<Grid2 container spacing={2}>
{items.map((item) => (
<Grid2 container spacing={2} key={item.item_id}>
<Card sx={{ maxWidth: 400 }}>
<CardMedia
sx={{ height: 250 }}
image={item.image_url}
title={item.name}
/>
<CardContent>
<Typography
gutterBottom
variant='h5'
component='div'
>
{item.name}
</Typography>
<Typography
variant='body2'
sx={{ color: 'text.secondary' }}
>
{item.description}
</Typography>
</CardContent>
</Card>
</Grid2>
))}
</Grid2>
</Stack>
) : (
<Typography variant='body1'>No items found.</Typography>
)}
</Stack>
</Stack>
);
};