2021-01-19 21:32:43 +01:00
|
|
|
import React, { ChangeEvent, useContext, useEffect, MouseEvent, useState } from 'react';
|
2020-08-18 18:32:26 +02:00
|
|
|
import Modal from '@material-ui/core/Modal';
|
|
|
|
import Fade from '@material-ui/core/Fade';
|
|
|
|
import { makeStyles } from '@material-ui/core/styles';
|
|
|
|
import styled from 'styled-components';
|
2021-01-18 00:57:01 +01:00
|
|
|
import { FormControl, MenuItem, Select, useControlled, useEventCallback } from '@material-ui/core';
|
|
|
|
import { axiosInstance } from '../utils/axiosInstance';
|
|
|
|
import { Group } from '../types';
|
|
|
|
import { coursesContext } from '../contexts/CoursesProvider';
|
|
|
|
import { Dropdown } from './Dropdown';
|
|
|
|
import { DropdownModal } from './DropdownModal';
|
2021-01-18 22:37:31 +01:00
|
|
|
import { dayMapping } from '../constants';
|
2021-01-19 16:10:43 +01:00
|
|
|
import TransferIcon from '../assets/switch.svg';
|
2021-01-19 21:32:43 +01:00
|
|
|
import DeleteIcon from '@material-ui/icons/Delete';
|
2021-01-22 19:52:31 +01:00
|
|
|
import InformationIcon from '../assets/information.svg';
|
2020-08-18 18:32:26 +02:00
|
|
|
|
|
|
|
interface TransferProps {
|
|
|
|
handleClose: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
|
2021-01-18 22:37:31 +01:00
|
|
|
isTransferOpen: boolean;
|
2020-08-18 18:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const useStyles = makeStyles({
|
|
|
|
wrapper: {
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
textAlign: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-19 16:10:43 +01:00
|
|
|
const TransferStyled = styled.div`
|
2021-01-19 07:23:35 +01:00
|
|
|
display: flex;
|
2021-01-19 16:10:43 +01:00
|
|
|
flex-direction: column;
|
2021-01-19 07:23:35 +01:00
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
2020-08-18 18:32:26 +02:00
|
|
|
outline: none;
|
|
|
|
min-width: 35%;
|
|
|
|
height: 70%;
|
2021-01-17 22:24:44 +01:00
|
|
|
background: white;
|
2020-08-18 18:32:26 +02:00
|
|
|
margin: 0 auto;
|
2021-01-17 22:24:44 +01:00
|
|
|
border-radius: 5px;
|
2021-01-22 19:52:31 +01:00
|
|
|
letter-spacing: 0.1ch;
|
2021-01-19 16:10:43 +01:00
|
|
|
`;
|
|
|
|
|
2021-01-19 21:32:43 +01:00
|
|
|
const BinIcon = styled(DeleteIcon)`
|
|
|
|
max-width: 30px;
|
|
|
|
min-width: 30px;
|
|
|
|
cursor: pointer;
|
|
|
|
&:hover {
|
|
|
|
fill: white;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-01-19 16:10:43 +01:00
|
|
|
const InputWrapper = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
flex: 1;
|
2021-01-22 19:52:31 +01:00
|
|
|
margin-top:40px;
|
2020-08-18 18:32:26 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const TransferGiveStyled = styled.div`
|
2021-01-19 16:10:43 +01:00
|
|
|
flex: 1;
|
2020-08-18 18:32:26 +02:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
2021-01-19 16:10:43 +01:00
|
|
|
text-transform: uppercase;
|
2020-08-18 18:32:26 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const TransferReceiveStyled = styled.div`
|
2021-01-19 16:10:43 +01:00
|
|
|
flex: 1;
|
2020-08-18 18:32:26 +02:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
2021-01-19 16:10:43 +01:00
|
|
|
text-transform: uppercase;
|
2020-08-18 18:32:26 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const TransferTextStyled = styled.div`
|
|
|
|
font-size: 30px;
|
|
|
|
margin-bottom: 10px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const TransferInputStyled = styled.div`
|
|
|
|
width: 250px;
|
|
|
|
height: 25px;
|
|
|
|
padding: 10px;
|
|
|
|
font-size: 24px;
|
|
|
|
transition-duration: 0.3s;
|
|
|
|
input::placeholder {
|
|
|
|
color: black;
|
|
|
|
font-weight: bold;
|
|
|
|
text-align: center;
|
|
|
|
}
|
2021-01-19 16:10:43 +01:00
|
|
|
text-transform: none;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const SaveWrapper = styled.div`
|
2021-01-19 17:04:07 +01:00
|
|
|
margin-top: 40px;
|
2021-01-19 16:10:43 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
const SaveButton = styled.div`
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
user-select: none;
|
|
|
|
background-color: #43a047;
|
|
|
|
border-radius: 10px;
|
|
|
|
cursor: pointer;
|
|
|
|
height: 40px;
|
|
|
|
width: 150px;
|
2021-01-19 17:04:07 +01:00
|
|
|
font-size: 12px;
|
2021-01-19 16:10:43 +01:00
|
|
|
letter-spacing: 0.1ch;
|
|
|
|
line-height: normal;
|
|
|
|
&:hover {
|
|
|
|
color: #ffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
|
|
|
background-color: #54c457;
|
|
|
|
}
|
|
|
|
|
|
|
|
text-transform: uppercase;
|
2021-01-22 19:52:31 +01:00
|
|
|
box-shadow: 3px 3px 5px 0px rgba(189,189,189,1);
|
2021-01-19 16:10:43 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
const ExchangesWrapper = styled.div`
|
2021-01-19 17:04:07 +01:00
|
|
|
flex: 4;
|
2021-01-19 16:10:43 +01:00
|
|
|
overflow-y: scroll;
|
2021-01-19 17:04:07 +01:00
|
|
|
width: 100%;
|
2021-01-19 16:10:43 +01:00
|
|
|
::-webkit-scrollbar-track {
|
|
|
|
border-radius: 10px;
|
|
|
|
}
|
|
|
|
::-webkit-scrollbar {
|
|
|
|
width: 5px;
|
|
|
|
border-style: none;
|
|
|
|
}
|
|
|
|
::-webkit-scrollbar-thumb {
|
|
|
|
border-radius: 10px;
|
|
|
|
background-color: #4b4b4b;
|
|
|
|
}
|
2021-01-22 19:52:31 +01:00
|
|
|
border-top: 1px solid #b8b8b8;
|
2021-01-19 16:10:43 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
const ExchangesRow = styled.div`
|
2021-01-19 17:04:07 +01:00
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
2021-01-19 16:10:43 +01:00
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Icon = styled.img`
|
|
|
|
width: 20px;
|
2021-01-22 19:52:31 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
const Information = styled.img`
|
|
|
|
width: 35px;
|
2020-08-18 18:32:26 +02:00
|
|
|
`;
|
|
|
|
|
2021-01-19 16:10:43 +01:00
|
|
|
const Exchange = styled.div`
|
2021-01-22 19:52:31 +01:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-01-19 16:10:43 +01:00
|
|
|
background-color: #b5d2e0;
|
2021-01-22 19:52:31 +01:00
|
|
|
border-radius: 2px;
|
2021-01-19 17:46:14 +01:00
|
|
|
width: 280px;
|
2021-01-19 17:04:07 +01:00
|
|
|
margin: 10px;
|
|
|
|
display: flex;
|
2021-01-19 16:10:43 +01:00
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
2021-01-22 19:52:31 +01:00
|
|
|
padding: 0.5rem;
|
|
|
|
line-height: 0;
|
2021-01-19 17:04:07 +01:00
|
|
|
`;
|
2021-01-19 16:10:43 +01:00
|
|
|
|
2021-01-22 19:52:31 +01:00
|
|
|
const ExchangeTitle = styled.p`
|
|
|
|
font-size: 16px;
|
|
|
|
font-weight: bold;
|
|
|
|
`;
|
2021-01-19 21:32:43 +01:00
|
|
|
|
2021-01-22 19:52:31 +01:00
|
|
|
const ExchangeParagraph = styled.p`
|
|
|
|
font-size: 13px;
|
|
|
|
color: #1a1a1a;
|
|
|
|
`;
|
2021-01-18 00:57:01 +01:00
|
|
|
|
2021-01-18 22:37:31 +01:00
|
|
|
export const Transfer = ({ handleClose, isTransferOpen }: TransferProps) => {
|
2021-01-19 07:23:35 +01:00
|
|
|
const { basket, selectBasketCourses } = useContext(coursesContext)!;
|
|
|
|
// const basketCourseGroups = useMemo(() => selectBasketCourseGroups(course.name), []);
|
|
|
|
const basketCourses = selectBasketCourses();
|
|
|
|
|
2020-08-18 18:32:26 +02:00
|
|
|
const classes = useStyles();
|
2021-01-18 22:37:31 +01:00
|
|
|
// const groups = selectGroups();
|
2021-01-18 00:57:01 +01:00
|
|
|
const [input, setInput] = useState('');
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
2021-01-19 07:23:35 +01:00
|
|
|
const [assignmentsClasses, setAssignmentsClasses] = useState<Array<any>>([]);
|
|
|
|
const [selectedAssignmentsClasses, setSelectedAssignmentsClasses] = useState<any>('');
|
2021-01-18 00:57:01 +01:00
|
|
|
const [selectedGroup, setSelectedGroup] = useState<any>('');
|
2021-01-19 21:32:43 +01:00
|
|
|
const [groups, setGroups] = useState<any>([]);
|
2021-01-18 00:57:01 +01:00
|
|
|
const [exchanges, setExchanges] = useState<any>(null);
|
2021-01-19 16:10:43 +01:00
|
|
|
const [save, setSave] = useState(false);
|
2021-01-22 19:52:31 +01:00
|
|
|
const [tour, setTour] = useState<any>(null);
|
2021-01-18 00:57:01 +01:00
|
|
|
// const allGroups
|
|
|
|
const handleSelectedAssignmentsGroupChange = (event: React.ChangeEvent<{ value: unknown }>) => {
|
2021-01-19 21:32:43 +01:00
|
|
|
setSelectedAssignmentsClasses(event.target.value as any);
|
2021-01-18 00:57:01 +01:00
|
|
|
};
|
|
|
|
|
2021-01-19 07:23:35 +01:00
|
|
|
const handleGroupsChange = (event: React.ChangeEvent<{ value: unknown }>) => {
|
2021-01-18 00:57:01 +01:00
|
|
|
setSelectedGroup(event.target.value as any);
|
|
|
|
};
|
2021-01-19 21:32:43 +01:00
|
|
|
|
2021-01-18 00:57:01 +01:00
|
|
|
const handleChange = (event: ChangeEvent<HTMLInputElement>) => setInput(event.target.value);
|
|
|
|
const handleShowDropdown = () => setOpen(true);
|
|
|
|
|
|
|
|
const handleCloseDropdown = () => setOpen(false);
|
2021-01-19 07:23:35 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (selectedAssignmentsClasses) {
|
|
|
|
const allGroups = basketCourses.filter((el) => el.name === selectedAssignmentsClasses.name);
|
|
|
|
const allClasses = allGroups[0]?.classes;
|
|
|
|
if (allClasses) {
|
|
|
|
const filteredClasses = allClasses.filter((el: any) => {
|
|
|
|
return el.time !== selectedAssignmentsClasses.time;
|
|
|
|
});
|
|
|
|
setGroups(filteredClasses);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [selectedAssignmentsClasses]);
|
|
|
|
|
2021-01-19 21:32:43 +01:00
|
|
|
const getExchanges = async () => {
|
|
|
|
try {
|
|
|
|
const { data } = await axiosInstance.get(`${process.env.REACT_APP_API_URL}/api/v1/exchanges/exchange/all`);
|
|
|
|
setExchanges(data);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
2021-01-19 16:10:43 +01:00
|
|
|
|
2021-01-19 21:32:43 +01:00
|
|
|
useEffect(() => {
|
2021-01-18 00:57:01 +01:00
|
|
|
const getAssignmentsGroups = async () => {
|
2021-01-18 22:37:31 +01:00
|
|
|
try {
|
|
|
|
const { data } = await axiosInstance.get(`${process.env.REACT_APP_API_URL}/api/v1/commisions/user/assignments`);
|
|
|
|
const classes = data.filter((el: any) => el.type === 'CLASS');
|
2021-01-19 07:23:35 +01:00
|
|
|
setAssignmentsClasses(classes);
|
2021-01-18 22:37:31 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2021-01-18 00:57:01 +01:00
|
|
|
};
|
|
|
|
getExchanges();
|
2021-01-22 19:52:31 +01:00
|
|
|
getCurrentTour();
|
2021-01-18 00:57:01 +01:00
|
|
|
getAssignmentsGroups();
|
2021-01-19 17:04:07 +01:00
|
|
|
}, [isTransferOpen, save]);
|
2021-01-19 16:10:43 +01:00
|
|
|
|
|
|
|
const createExchange = async (groupsIds: Array<number>) => {
|
|
|
|
try {
|
|
|
|
const response = await axiosInstance.post(
|
|
|
|
`${process.env.REACT_APP_API_URL}/api/v1/exchanges/exchange`,
|
|
|
|
JSON.stringify({ assignment: groupsIds[0], group: groupsIds[1] }),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2021-01-19 21:32:43 +01:00
|
|
|
setSelectedGroup('');
|
|
|
|
setSelectedAssignmentsClasses('');
|
2021-01-19 16:10:43 +01:00
|
|
|
setSave(!save);
|
|
|
|
};
|
2020-08-18 18:32:26 +02:00
|
|
|
|
2021-01-22 19:52:31 +01:00
|
|
|
const getCurrentTour = async () => {
|
2021-01-19 21:32:43 +01:00
|
|
|
try {
|
2021-01-22 19:52:31 +01:00
|
|
|
const { data } = await axiosInstance.get(`${process.env.REACT_APP_API_URL}/api/v1/configurator/config/tour`);
|
|
|
|
setTour(data.currentTour);
|
2021-01-19 21:32:43 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteExchange = async (id: number) => {
|
|
|
|
try {
|
|
|
|
const response = await axiosInstance.delete(`${process.env.REACT_APP_API_URL}/api/v1/exchanges/exchange/${id}`);
|
|
|
|
getExchanges();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-18 18:32:26 +02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Modal
|
|
|
|
className={classes.wrapper}
|
2021-01-18 22:37:31 +01:00
|
|
|
open={isTransferOpen}
|
2020-08-18 18:32:26 +02:00
|
|
|
onClose={handleClose}
|
|
|
|
aria-labelledby="simple-modal-title"
|
|
|
|
aria-describedby="simple-modal-description"
|
|
|
|
>
|
2021-01-18 22:37:31 +01:00
|
|
|
<Fade in={isTransferOpen}>
|
2021-01-22 19:52:31 +01:00
|
|
|
{tour === 'FIRST_TOUR' ? (
|
|
|
|
<TransferStyled>
|
|
|
|
<Information src={InformationIcon}></Information>
|
|
|
|
<p>Wymiana przedmiotami jest dostępna dopiero podczas drugiej tury</p>
|
|
|
|
<p>Wymianie podlegają tylko zaakceptowane grupy przedmiotów</p>
|
|
|
|
<p>Wymiana grupami odbywa się tylko w obrębie danego przedmiotu</p></TransferStyled>
|
|
|
|
) : (
|
|
|
|
<TransferStyled>
|
|
|
|
<InputWrapper>
|
|
|
|
<TransferGiveStyled>
|
|
|
|
<TransferTextStyled>Oddam</TransferTextStyled>
|
|
|
|
<TransferInputStyled>
|
|
|
|
<FormControl>
|
|
|
|
<Select
|
|
|
|
labelId="demo-simple-select-label"
|
|
|
|
id="assignments-groups"
|
|
|
|
value={selectedAssignmentsClasses}
|
|
|
|
onChange={handleSelectedAssignmentsGroupChange}
|
|
|
|
placeholder="Wyszukaj..."
|
|
|
|
style={{ width: '200px' }}
|
|
|
|
>
|
|
|
|
{assignmentsClasses.map((el: any, index: number) => {
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
key={index}
|
|
|
|
value={el}
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
textAlign: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{`${el.name} `}
|
|
|
|
<br></br>
|
|
|
|
{`(${dayMapping[el.day]} ${el.time} - ${el.endTime})`}
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</TransferInputStyled>
|
|
|
|
</TransferGiveStyled>
|
|
|
|
<SaveWrapper>
|
|
|
|
{' '}
|
|
|
|
<SaveButton
|
|
|
|
onClick={() => {
|
|
|
|
createExchange([selectedAssignmentsClasses.id, selectedGroup.id]);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Zaproponuj wymianę
|
|
|
|
</SaveButton>
|
|
|
|
</SaveWrapper>
|
|
|
|
<TransferReceiveStyled>
|
|
|
|
<TransferTextStyled>Przyjmę</TransferTextStyled>
|
|
|
|
<TransferInputStyled>
|
|
|
|
<FormControl disabled={selectedAssignmentsClasses ? false : true}>
|
|
|
|
<Select
|
|
|
|
labelId="demo-simple-select-label"
|
|
|
|
id="assignments-groups"
|
|
|
|
value={selectedGroup}
|
|
|
|
onChange={handleGroupsChange}
|
|
|
|
placeholder="Wyszukaj..."
|
|
|
|
style={{ width: '200px' }}
|
|
|
|
>
|
|
|
|
{groups.map((el: any, index: number) => {
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
key={index}
|
|
|
|
value={el}
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
textAlign: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{`${selectedAssignmentsClasses.name} `}
|
|
|
|
<br></br>
|
|
|
|
{`(${dayMapping[el.day]} ${el.time} - ${el.endTime})`}
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</TransferInputStyled>
|
|
|
|
</TransferReceiveStyled>
|
|
|
|
</InputWrapper>
|
|
|
|
<ExchangesWrapper>
|
|
|
|
{exchanges ? (
|
|
|
|
exchanges.map((name: any, index: number) => (
|
|
|
|
<ExchangesRow key={index}>
|
|
|
|
{' '}
|
|
|
|
<Exchange>
|
|
|
|
<ExchangeTitle>{name.courseName}</ExchangeTitle>
|
|
|
|
<ExchangeParagraph>{name.ownedAssignment.lecturer} </ExchangeParagraph>
|
|
|
|
<ExchangeParagraph> {dayMapping[name.ownedAssignment.day]} </ExchangeParagraph>
|
|
|
|
<ExchangeParagraph>
|
|
|
|
{name.ownedAssignment.time} - {name.ownedAssignment.endTime}
|
|
|
|
</ExchangeParagraph>
|
|
|
|
</Exchange>
|
|
|
|
<Icon alt="transfer" src={TransferIcon} />
|
|
|
|
<Exchange>
|
|
|
|
<ExchangeTitle>{name.courseName}</ExchangeTitle>
|
|
|
|
<ExchangeParagraph>{name.desiredGroup.lecturer} </ExchangeParagraph>
|
|
|
|
<ExchangeParagraph> {dayMapping[name.desiredGroup.day]} </ExchangeParagraph>
|
|
|
|
<ExchangeParagraph>
|
|
|
|
{name.desiredGroup.time} - {name.desiredGroup.endTime}
|
|
|
|
</ExchangeParagraph>
|
|
|
|
</Exchange>{' '}
|
|
|
|
<BinIcon
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
const id = Number(e.currentTarget.id);
|
|
|
|
deleteExchange(id);
|
|
|
|
}}
|
|
|
|
id={name.id}
|
|
|
|
></BinIcon>
|
|
|
|
</ExchangesRow>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<div></div>
|
|
|
|
)}
|
|
|
|
</ExchangesWrapper>
|
|
|
|
</TransferStyled>
|
|
|
|
)}
|
2020-08-18 18:32:26 +02:00
|
|
|
</Fade>
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|