diff --git a/client/src/components/SignUp.js b/client/src/components/SignUp.js index a1111311..2a992558 100755 --- a/client/src/components/SignUp.js +++ b/client/src/components/SignUp.js @@ -36,15 +36,15 @@ const SignUp = () => { const [error, setError] = useState(''); const history = useHistory(); + const clientId = + '128601698558-80ae6kq3v7p8iuknfpkqu6bsfg05vgra.apps.googleusercontent.com'; // Put client ID here - const clientId = '128601698558-80ae6kq3v7p8iuknfpkqu6bsfg05vgra.apps.googleusercontent.com'; // Put client ID here - - const onSuccess = (res) => { + const onSuccess = res => { localStorage.setItem('googleToken', res.tokenId); console.log('Login Success: currentUser:', res); }; - const onFailure = (res) => { + const onFailure = res => { console.log('Login failed: res:', res); }; @@ -71,17 +71,17 @@ const SignUp = () => { ); }, []); - const onSelectSiteChange = (siteId) => { + const onSelectSiteChange = siteId => { // this.setState({ siteId }); setSiteId(siteId); }; - const onSelectRoleChange = (role) => { + const onSelectRoleChange = role => { // this.setState({ role }); setRole(role); }; - const _checkAccess = (event) => { + const _checkAccess = event => { // this.setState({ siteCode: event.target.value }); setSiteCode(event.target.value); }; @@ -99,7 +99,7 @@ const SignUp = () => { return semester; }; - const addUserSite = async (payload) => { + const addUserSite = async payload => { const semester = getCurrentSemester(); try { await fetch('/api/v1/site/addUserSemSite', { @@ -116,9 +116,9 @@ const SignUp = () => { } catch (error) { console.log(error); } - } + }; - const _submit = (event) => { + const _submit = event => { const googleToken = getGoogleToken(); // const { role, siteId, siteCode, sites } = this.state; @@ -182,7 +182,7 @@ const SignUp = () => { }); event.preventDefault(); }); - } + }; const loadSites = () => { const options = []; @@ -259,6 +259,6 @@ const SignUp = () => { ); -} +}; export default withRouter(SignUp); diff --git a/client/src/components/SiteLessons.js b/client/src/components/SiteLessons.js index f01062a1..ce185729 100755 --- a/client/src/components/SiteLessons.js +++ b/client/src/components/SiteLessons.js @@ -10,25 +10,8 @@ import { useHistory, withRouter } from 'react-router-dom'; const { Option } = Select; // TODO reset modal values onOk -const SiteLessons = (props) => { - // constructor(props) { - // super(props); - - // this.state = { - // isMentor: this.props.ismentor, - // showModal: false, - // siteLessons: [], - // site: '', - // otherLessons: [], - // modalSelectedValue: '', - // modalDate: '', - // }; - - // this.onDateChange = this.onDateChange.bind(this); - // this.onSelectChange = this.onSelectChange.bind(this); - // this.deleteHandler = this.deleteHandler.bind(this); - // } - const { ismentor } = props; +const SiteLessons = props => { + const { ismentor } = props; const [showModal, setShowModal] = useState(false); const [siteLessons, setSiteLessons] = useState([]); const [site, setSite] = useState(''); @@ -38,9 +21,9 @@ const SiteLessons = (props) => { const [error, setError] = useState(''); const history = useHistory(); const [dTok, setDTok] = useState({}); - + useEffect(() => { - const getToken = async () => { + const getToken = async () => { try { const tok = getAnovaToken(); const decodedToken = await decode(tok); @@ -49,7 +32,7 @@ const SiteLessons = (props) => { removeAnovaToken(); history.push(`/login`); } - } + }; getToken(); }, []); @@ -57,55 +40,58 @@ const SiteLessons = (props) => { if (Object.keys(dTok).length > 0) { loadData(); } - }, [dTok]) + }, [dTok]); const loadData = () => { - console.log("LOAD DATA DTOK") - console.log(dTok) + console.log('LOAD DATA DTOK'); + console.log(dTok); fetch(`/api/v1/site/current?uid=${dTok.id}`) .then(res => res.json()) .then(site => { setSite(site); - }) - + }); + fetch(`/api/v1/lesson_site/all?uid=${dTok.id}`) .then(res => res.json()) .then(siteLessons => { setSiteLessons(siteLessons); - }) - + }); + fetch(`/api/v1/lesson_site/all_but_current_site?uid=${dTok.id}`) .then(res => res.json()) .then(otherLessons => { setOtherLessons(otherLessons); - } - ) - } + }); + }; - const onDateChange = (date) => { + const onDateChange = date => { setModalDate(date); - } + }; - const onSelectChange = (value) => { - // this.setState({ modalSelectedValue: value }); + const onSelectChange = value => { setModalSelectedValue(value); - } - - const deleteHandler = (lessonDetails) => { + }; + const deleteHandler = lessonDetails => { fetch(`/api/v1/lesson_site/delete?uid=${dTok.id}`, { method: 'POST', body: JSON.stringify({ lesson_id: lessonDetails.id }), headers: new Headers({ 'Content-Type': 'application/json', }), - }).then(() => - { - setSiteLessons(prevState => prevState.siteLessons.filter( - lesson => lesson.id !== lessonDetails.id)); - setOtherLessons(prevState => [...prevState.otherLessons, lessonDetails]); - }) - } + }).then(() => { + setSiteLessons(prevSiteLessons => { + if (prevSiteLessons) { + return prevSiteLessons.filter(lesson => lesson.id !== lessonDetails.id); + } + }); + setOtherLessons(prevOtherLessons => { + if (prevOtherLessons) { + return [...prevOtherLessons, lessonDetails]; + } + }); + }); + }; const addLessonToSite = (lessonId, date) => { if (!lessonId || !date) { @@ -114,7 +100,6 @@ const SiteLessons = (props) => { centered: true, }); } else { - fetch(`/api/v1/lesson_site/add?uid=${dTok.id}`, { method: 'POST', body: JSON.stringify({ lesson_id: lessonId, date }), @@ -131,26 +116,24 @@ const SiteLessons = (props) => { ); }); setSiteLessons(sorted_lessons); - setOtherLessons(prevState => prevState.otherLessons.filter( - otherLesson => otherLesson.id !== lessonId)); + setOtherLessons(prevOtherLessons => { + if (prevOtherLessons && otherLessons) { + return prevOtherLessons.filter(otherLesson => otherLesson.id !== lessonId); + } + }); setShowModal(false); setModalSelectedValue(''); setModalDate(''); - }); } - } + }; const renderLessons = () => { let maybeAddCard; if (ismentor) { maybeAddCard = (