warehouse locations

This commit is contained in:
Dobromir Popov
2024-12-01 22:26:41 +02:00
parent e67fb399cd
commit c06b93f6ad
13 changed files with 129 additions and 25 deletions

View File

@ -57,12 +57,14 @@ const ViewLocationPage: React.FC<ViewLocationPageProps> = ({ location }) => {
{location.name}
</button>
{/* Backup Location Tab */}
<button
className={`tab flex-1 text-lg py-2 px-4 ${activeTab === 'backupLocation' ? 'border-b-4 border-blue-500 text-blue-600 font-semibold' : 'text-gray-600 hover:text-blue-500'}`}
onClick={() => handleTabChange('backupLocation')}
>
При лошо време: <strong>{location.backupLocationName}</strong>
</button>
{location.backupLocationId !== null && (
<button
className={`tab flex-1 text-lg py-2 px-4 ${activeTab === 'backupLocation' ? 'border-b-4 border-blue-500 text-blue-600 font-semibold' : 'text-gray-600 hover:text-blue-500'}`}
onClick={() => handleTabChange('backupLocation')}
>
При лошо време: <strong>{location.backupLocationName}</strong>
</button>
)}
</div>
{/* Carousel */}
@ -98,6 +100,8 @@ const ViewLocationPage: React.FC<ViewLocationPageProps> = ({ location }) => {
);
};
const common = require("../../../src/helpers/common");
export const getServerSideProps: GetServerSideProps = async (context) => {
const axios = await axiosServer(context);
@ -126,17 +130,19 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
});
};
const { data: location } = await axios.get(
`${process.env.NEXT_PUBLIC_PUBLIC_URL}/api/data/locations/${context.params.id}`
);
const prismaClient = common.getPrismaClient();
const location = await getLocation(prismaClient, context);
location.content = replacePlaceholders(location.content);
if (location.backupLocationId !== null) {
const { data: backupLocation } = await axios.get(
process.env.NEXT_PUBLIC_PUBLIC_URL + "/api/data/locations/" + location.backupLocationId
);
// const { data: backupLocation } = await axios.get(
// process.env.NEXT_PUBLIC_PUBLIC_URL + "/api/data/locations/" + location.backupLocationId
// );
const backupLocation = await prismaClient.location.findFirst({
where: {
id: location.backupLocationId,
},
});
location.backupLocationName = backupLocation.name;
location.backupLocationContent = backupLocation ? replacePlaceholders(backupLocation.content) : "";
location.backupLocationImages = backupLocation ? [backupLocation.picture1, backupLocation.picture2, backupLocation.picture3].filter(Boolean) : [];
@ -151,4 +157,30 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
};
};
async function getLocation(prismaClient, context) {
// Try to parse the ID as a number
const numericId = parseInt(context.params.id);
let location;
// If it's a valid number, search by ID
if (!isNaN(numericId)) {
location = await prismaClient.location.findFirst({
where: {
id: numericId
}
});
}
// If no location found by ID or if ID is not numeric, search by name
if (!location) {
location = await prismaClient.location.findFirst({
where: {
name: context.params.id
}
});
}
return location;
}
export default ViewLocationPage;