Merge branch 'main' into production

This commit is contained in:
Dobromir Popov
2024-03-13 04:06:08 +02:00
6 changed files with 186 additions and 53 deletions

View File

@ -153,8 +153,13 @@ Remove-Item package-lock.json
npm install
# -- mysql
# fix
# ---------------------------------- mysql ----------------------------------- #
#backup: (--no-data to skip data)
mysqldump -h mariadb -P 3306 -ujwpwsofia -p"dwxhns9p9vp248V39xJyRthUsZ2gR9" jwpwsofia --skip-add-locks > /backup/manual-$(date +\%Y-\%m-\%d-\%H\%M\%S)-$MYSQL_DATABASE.sql
# fix++
mysql -u root -pi4966cWBtP3xJ7BLsbsgo93C8Q5262
--
mysqld_safe --skip-grant-tables &

43
components/Modal.tsx Normal file
View File

@ -0,0 +1,43 @@
import React from 'react';
import common from '../src/helpers/common'; // Ensure this path is correct
interface ModalProps {
children: React.ReactNode;
isOpen: boolean;
onClose: () => void;
forDate: Date;
useFilterDate: boolean;
onUseFilterDateChange: (value: boolean) => void;
}
function Modal({ children, isOpen, onClose, forDate, useFilterDate, onUseFilterDateChange }: ModalProps) {
if (!isOpen) return null;
const isValidDate = forDate instanceof Date && !isNaN(forDate.getTime());
console.log("forDate", forDate, isValidDate);
return (
<div className="fixed inset-0 flex items-center justify-center z-50">
<div className="bg-white p-4 rounded-md shadow-lg modal-content">
{isValidDate && (
<h2 className="text-xl font-bold mb-2">
<label className="cursor-pointer">
<input
type="checkbox"
checked={useFilterDate}
onChange={(e) => onUseFilterDateChange(e.target.checked)}
/>
{` на разположение ${common.getDateFormated(forDate)} или ${common.getDayOfWeekName(forDate)}`}
</label>
</h2>
)}
{children}
<button type="button" onClick={onClose} className="mt-4 text-red-500">
Close
</button>
</div>
<div className="fixed inset-0 bg-black opacity-50 modal-overlay" onClick={onClose}></div>
</div>
);
}
export default Modal;

View File

@ -1,51 +1,13 @@
import React, { useState, useEffect } from 'react';
import axiosInstance from '../../src/axiosSecure';
import PublisherSearchBox from '../publisher/PublisherSearchBox'; // Update the path
import Modal from '../Modal';
import LocalShippingIcon from '@mui/icons-material/LocalShipping';
const common = require('src/helpers/common');
interface ModalProps {
children: React.ReactNode;
isOpen: boolean;
onClose: () => void;
forDate: Date;
useFilterDate: boolean;
onUseFilterDateChange: (value: boolean) => void;
}
function Modal({ children, isOpen, onClose, forDate, useFilterDate, onUseFilterDateChange }: ModalProps) {
if (!isOpen) return null;
const isValidDate = forDate instanceof Date && !isNaN(forDate.getTime());
console.log("forDate", forDate, isValidDate);
return (
<div className="fixed inset-0 flex items-center justify-center z-50">
<div className="bg-white p-4 rounded-md shadow-lg modal-content">
{isValidDate && (
<h2 className="text-xl font-bold mb-2">
<label className="cursor-pointer">
<input
type="checkbox"
checked={useFilterDate}
onChange={(e) => onUseFilterDateChange(e.target.checked)}
/>
{` на разположение ${common.getDateFormated(forDate)} или ${common.getDayOfWeekName(forDate)}`}
</label>
</h2>
)}
{children}
<button type="button" onClick={onClose} className="mt-4 text-red-500">
Close
</button>
</div>
<div className="fixed inset-0 bg-black opacity-50 modal-overlay" onClick={onClose}></div>
</div>
);
}
function ShiftComponent({ shift, onShiftSelect, isSelected, onPublisherSelect, allPublishersInfo }) {

View File

@ -15,11 +15,11 @@ const sidemenu = [
text: "График",
url: "/cart/calendar/schedule",
},
// {
// id: "myshedule",
// text: "Моя График",
// url: "/cart/publishers/myschedule",
// },
{
id: "myshedule",
text: "Моя График",
url: "/cart/publishers/myschedule",
},
{
id: "locations",
text: "Местоположения",

View File

@ -210,6 +210,12 @@ export default async function handler(req, res) {
res.status(200).json(shiftsForDate);
break;
case "replaceInAssignment":
const { oldPublisherId, newPublisherId, shiftId } = req.method === "POST" ? req.body : req.query;
const prisma = common.getPrismaClient();
const result = await replaceInAssignment(oldPublisherId, newPublisherId, shiftId);
res.status(200).json(result);
break;
default:
res.status(200).json({
@ -224,6 +230,7 @@ export default async function handler(req, res) {
}
export async function getMonthlyStatistics(selectFields, filterDate) {
let publishers = [];
@ -670,4 +677,22 @@ async function getCalendarEvents(publisherId, date, availabilities = true, assig
}
}
return result;
}
async function replaceInAssignment(oldPublisherId, newPublisherId, shiftId) {
const prisma = common.getPrismaClient();
const result = await prisma.assignment.updateMany({
where: {
publisherId: oldPublisherId,
shiftId: shiftId
},
data: {
publisherId: newPublisherId,
isConfirmed: false,
isTentative: true,
isMailSent: false
}
});
return result;
}

View File

@ -4,18 +4,53 @@ import ProtectedRoute from '../../../components/protectedRoute';
import { UserRole } from '@prisma/client';
import axiosServer from '../../../src/axiosServer';
import common from '../../../src/helpers/common';
import ShiftsList from '../../../components/publisher/ShiftsList';
import Modal from 'components/Modal';
import ConfirmationModal from 'components/ConfirmationModal';
import PublisherSearchBox from '../../../components/publisher/PublisherSearchBox'; // Update the path
import { monthNamesBG, GetTimeFormat, GetDateFormat } from "../../../src/helpers/const"
import { useSession, getSession } from 'next-auth/react';
import axiosInstance from 'src/axiosSecure';
import { toast } from 'react-toastify';
import LocalShippingIcon from '@mui/icons-material/LocalShipping';
export default function MySchedulePage({ assignments }) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [isConfirmModalOpen, setIsConfirmModalOpen] = useState(false);
const [useFilterDate, setUseFilterDate] = useState(true);
const [assignment, setАssignment] = useState(null);
const [newPublisher, setNewPublisher] = useState(null);
const { data: session, status } = useSession();
if (status === "loading") {
return <div className="flex justify-center items-center h-screen">Loading...</div>;
}
const handleReplaceInAssignment = () => {
// Add publisher as assignment logic
setIsModalOpen(false);
setIsConfirmModalOpen(false);
console.log("publisher", newPublisher.firstName, " ", newPublisher.lastName, " set to shift ", assignment.shift.id);
//api.replaceInAssignment()
axiosInstance.post('/api/?action=replaceInAssignment', {
oldPublisherId: session.user.id,
newPublisherId: newPublisher.id,
shiftId: assignment.shift.id,
}).then(response => {
console.log("response", response);
//toast success and confirm the change
// refresh the page after toast is closed
toast.success("Промяната е записана!", {
onClose: () => {
window.location.reload();
}
});
}).catch(error => {
console.log("error", error);
});
};
return (
<Layout>
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER, UserRole.USER]}>
@ -23,7 +58,7 @@ export default function MySchedulePage({ assignments }) {
<h1 className="text-2xl md:text-3xl font-bold text-center my-4">Моите смени</h1>
<div className="space-y-4">
{assignments && assignments.map((assignment) => (
<div key={assignment.dateStr} className="bg-white shadow overflow-hidden rounded-lg">
<div key={assignment.dateStr + assignments.indexOf(assignment)} className="bg-white shadow overflow-hidden rounded-lg">
<div className="px-4 py-5 sm:px-6">
<h3 className="text-lg leading-6 font-medium text-gray-900">{assignment.dateStr}</h3>
</div>
@ -31,10 +66,23 @@ export default function MySchedulePage({ assignments }) {
<dl>
<div className="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="text-sm font-medium text-gray-500">Час</dt>
<dd className="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
<dd className="mt-1 text text-gray-900 sm:mt-0 sm:col-span-2">
{GetTimeFormat(assignment.shift.startTime)} - {GetTimeFormat(assignment.shift.endTime)}
</dd>
</div>
<div className="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="text-sm font-medium text-gray-500">Смяна</dt>
<dd className="mt-1 text text-gray-900 sm:mt-0 sm:col-span-2">
{assignment.shift.assignments.map((a, index) => {
return (
<span key={index} className="inline-flex items-center mr-1 px-1 py-0.5 rounded-full text-sm font-medium ">
{a.publisher.firstName} {a.publisher.lastName}{a.isWithTransport && <LocalShippingIcon style={{ paddingLeft: '4px' }} />}
</span>
)
}
)}
</dd>
</div>
<div className="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="text-sm font-medium text-gray-500">Действия</dt>
<dd className="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
@ -45,11 +93,20 @@ export default function MySchedulePage({ assignments }) {
Добави в календар
</button>
<button
className="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
onClick={() => {
setАssignment(assignment);
setIsModalOpen(true)
}}
>
Заместник
</button>
{/* <button
className="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
onClick={() => searchReplacement(assignment.id)}
>
Търси заместник
</button>
</button> */}
</dd>
</div>
</dl>
@ -58,6 +115,33 @@ export default function MySchedulePage({ assignments }) {
))}
</div>
</div>
<Modal isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
forDate={new Date(assignment?.shift.startTime)}
useFilterDate={useFilterDate}
onUseFilterDateChange={(value) => setUseFilterDate(value)}>
<PublisherSearchBox
selectedId={null}
isFocused={isModalOpen}
filterDate={useFilterDate && assignment ? new Date(assignment.shift.startTime) : null}
onChange={(publisher) => {
setIsConfirmModalOpen(true);
setNewPublisher(publisher);
}}
showAllAuto={true}
showSearch={true}
showList={false}
/>
</Modal>
<ConfirmationModal
isOpen={isConfirmModalOpen}
onClose={() => { setIsConfirmModalOpen(false); setNewPublisher(null); }}
onConfirm={handleReplaceInAssignment}
message="Това действие ще те замести в назначената ти смяна. Погвърждаваш ли, че заместника знае за тази промяна."
/>
</ProtectedRoute>
</Layout>
);
@ -96,15 +180,29 @@ export const getServerSideProps = async (context) => {
include: {
assignments: {
include: {
shift: true,
shift: {
include: {
assignments: {
include: {
publisher: {
select: {
id: true,
firstName: true,
lastName: true,
}
}
}
}
},
},
},
},
},
});
const assignments = publisher[0]?.assignments;
const assignments = publisher[0]?.assignments || [];
const transformedAssignments = assignments.map(assignment => {
const transformedAssignments = assignments?.map(assignment => {
if (assignment.shift && assignment.shift.startTime) {
return {
...assignment,