Save timetable button connected to backend

This commit is contained in:
maciekglowacki 2020-10-27 01:24:35 +01:00
parent 8ceef555d1
commit 157dc2f35e
5 changed files with 61 additions and 54 deletions

9
package-lock.json generated
View File

@ -8794,6 +8794,15 @@
"sort-keys": "^1.0.0" "sort-keys": "^1.0.0"
} }
}, },
"notistack": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/notistack/-/notistack-1.0.1.tgz",
"integrity": "sha512-2T1WkokzRCM8N9EdueaXja160IMFIMHVhRu0fGkDje7qCzwBHlTMZY2NULQzB2GFOO6iGVzl5GCX2XrJIzI8bw==",
"requires": {
"clsx": "^1.1.0",
"hoist-non-react-statics": "^3.3.0"
}
},
"npm-run-path": { "npm-run-path": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz",

View File

@ -9,6 +9,7 @@
"@testing-library/react": "^9.5.0", "@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1", "@testing-library/user-event": "^7.2.1",
"axios": "^0.19.2", "axios": "^0.19.2",
"notistack": "^1.0.1",
"react": "^16.13.1", "react": "^16.13.1",
"react-dom": "^16.13.1", "react-dom": "^16.13.1",
"react-scripts": "3.4.1", "react-scripts": "3.4.1",

View File

@ -1,10 +1,8 @@
import React, { useContext } from 'react'; import React, { useContext } from 'react';
import Snackbar from '@material-ui/core/Snackbar';
import { CourseCard } from './CourseCard'; import { CourseCard } from './CourseCard';
import { coursesContext } from '../contexts/CoursesProvider'; import { coursesContext } from '../contexts/CoursesProvider';
import MuiAlert, { AlertProps } from '@material-ui/lab/Alert';
import styled from 'styled-components'; import styled from 'styled-components';
import { debounce } from "lodash"; import { debounce } from 'lodash';
const RightbarStyled = styled.div` const RightbarStyled = styled.div`
padding-top: 10px; padding-top: 10px;
@ -50,15 +48,9 @@ const SaveButton = styled.div`
box-shadow: 6px 6px 6px -2px rgba(0, 0, 0, 0.59); box-shadow: 6px 6px 6px -2px rgba(0, 0, 0, 0.59);
`; `;
function Alert(props: AlertProps) {
return <MuiAlert elevation={6} variant="filled" {...props} />;
}
export const Rightbar = () => { export const Rightbar = () => {
const { courses, basket, saveBasket } = useContext(coursesContext)!; const { courses, basket, saveBasket } = useContext(coursesContext)!;
const [open, setOpen] = React.useState(false);
const getBasketGroups = () => { const getBasketGroups = () => {
const names = basket.map(({ name }) => name); const names = basket.map(({ name }) => name);
return courses.filter(({ name }) => names.includes(name)); return courses.filter(({ name }) => names.includes(name));
@ -66,19 +58,7 @@ export const Rightbar = () => {
const filteredCourses = getBasketGroups(); const filteredCourses = getBasketGroups();
const save = debounce(() => { const handleSave = debounce(() => saveBasket(), 500);
saveBasket();
setOpen(true);
console.log("zmiana")
},500);
const handleClose = (event?: React.SyntheticEvent, reason?: string) => {
if (reason === 'clickaway') {
return;
}
setOpen(false);
};
//need to insert student name from db and course maybe based on current time or from db too //need to insert student name from db and course maybe based on current time or from db too
return ( return (
@ -88,16 +68,11 @@ export const Rightbar = () => {
Hubert Wrzesiński<br></br> Hubert Wrzesiński<br></br>
Semestr zimowy 2020/2021 Semestr zimowy 2020/2021
</p> </p>
<SaveButton onClick={save}>ZAPISZ</SaveButton> <SaveButton onClick={handleSave}>ZAPISZ</SaveButton>
</RightbarTextStyled> </RightbarTextStyled>
{filteredCourses.map((course, index) => ( {filteredCourses.map((course, index) => (
<CourseCard course={course} key={index} /> <CourseCard course={course} key={index} />
))} ))}
<Snackbar open={open} autoHideDuration={5000} onClose={handleClose}>
<Alert onClose={handleClose} severity="success">
Zapisano plan!
</Alert>
</Snackbar>
</RightbarStyled> </RightbarStyled>
); );
}; };

View File

@ -1,7 +1,8 @@
import React, { useState, createContext, useEffect, ReactNode, useContext } from 'react'; import React, { useState, createContext, useEffect, ReactNode, useContext } from 'react';
import { Course, Group, Basket, GroupType } from '../types'; import { Course, Group, Basket, GroupType } from '../types';
import axios from 'axios'; import axios from 'axios';
import { CASContext, CASProvider } from './CASProvider'; import { CASContext } from './CASProvider';
import { useSnackbar } from 'notistack';
interface CourseContext { interface CourseContext {
courses: Array<Course>; courses: Array<Course>;
@ -22,9 +23,18 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
const [courses, setCourses] = useState<Array<Course>>([]); const [courses, setCourses] = useState<Array<Course>>([]);
const [basket, setBasket] = useState<Array<Basket>>([]); const [basket, setBasket] = useState<Array<Basket>>([]);
const { enqueueSnackbar } = useSnackbar();
const CAS = useContext(CASContext)!; const CAS = useContext(CASContext)!;
const token = CAS?.user?.token; const token = CAS?.user?.token;
const selectBasketIds = (basket: Array<Basket>) => {
const classesIds = basket.map((course) => course.classes.id);
const lecturesIds = basket.map((course) => course?.lecture?.id);
return lecturesIds[0] === undefined ? classesIds : [...classesIds, ...lecturesIds];
};
const addToBasket = (course: Course) => { const addToBasket = (course: Course) => {
const courseToBasket: Basket = { const courseToBasket: Basket = {
name: course.name, name: course.name,
@ -38,30 +48,33 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
const deleteFromBasket = (id: number) => setBasket(basket.filter((course) => course.id !== id)); const deleteFromBasket = (id: number) => setBasket(basket.filter((course) => course.id !== id));
const saveBasket = async () => { const saveBasket = async () => {
try { const basketIds = selectBasketIds(basket);
//to be deleted
let data = [7, 43, 54]; const config = {
let json = JSON.stringify(data); method: 'post' as const,
let post_data = { json_data: json }; url: `${process.env.REACT_APP_API_URL}/api/v1/commisions/add?`,
const ech = await axios.post<Array<number>>(
`${process.env.REACT_APP_API_URL}/api/v1/commisions/add?`,
[7, 43, 54],
{
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
}, },
}, data: JSON.stringify(basketIds),
); };
console.log('api response;', ech);
try {
await axios.request(config);
enqueueSnackbar('Plan został zapisany', {
variant: 'success',
});
} catch (e) { } catch (e) {
console.log(e); enqueueSnackbar('Zapisywanie planu nie powiodło się', {
variant: 'error',
});
} }
console.log('saving to basket');
}; };
const addGroup = (choosenGroup: Group, id: number) => { const addGroup = (choosenGroup: Group, id: number) => {
const basketCourse = basket.filter((course) => course.id === id)[0]; const basketCourse = basket.filter((course) => course.id === id)[0];
const type = choosenGroup.type; const { type } = choosenGroup;
if (type === GroupType.CLASS) { if (type === GroupType.CLASS) {
setBasket( setBasket(
basket.map((basket) => (basket.id === basketCourse.id ? { ...basket, classes: choosenGroup } : basket)), basket.map((basket) => (basket.id === basketCourse.id ? { ...basket, classes: choosenGroup } : basket)),
@ -78,7 +91,7 @@ export const CoursesProvider = ({ children }: CoursesProviderProps) => {
const { data: courses } = await axios.get<Array<Course>>( const { data: courses } = await axios.get<Array<Course>>(
`${process.env.REACT_APP_API_URL}/api/v1/courses/getCoursesWithGroups`, `${process.env.REACT_APP_API_URL}/api/v1/courses/getCoursesWithGroups`,
); );
courses.sort((a: Course, b: Course) => (a.name > b.name ? 1 : -1)); courses.sort((a, b) => (a.name > b.name ? 1 : -1));
setCourses(courses); setCourses(courses);
}; };
fetchData(); fetchData();

View File

@ -4,15 +4,24 @@ import { App } from './components/App';
import { CASProvider } from './contexts/CASProvider'; import { CASProvider } from './contexts/CASProvider';
import { CoursesProvider } from './contexts/CoursesProvider'; import { CoursesProvider } from './contexts/CoursesProvider';
import { GlobalStyles } from './styles/GlobalStyles'; import { GlobalStyles } from './styles/GlobalStyles';
import { SnackbarProvider } from 'notistack';
ReactDOM.render( ReactDOM.render(
<> <>
<SnackbarProvider
maxSnack={3}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
>
<CASProvider> <CASProvider>
<CoursesProvider> <CoursesProvider>
<GlobalStyles /> <GlobalStyles />
<App /> <App />
</CoursesProvider> </CoursesProvider>
</CASProvider> </CASProvider>
</SnackbarProvider>
</>, </>,
document.getElementById('root'), document.getElementById('root'),
); );