Skip to content

Commit

Permalink
added timetable backend and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwolf08 committed Jun 7, 2020
1 parent 77629ec commit e20f6c0
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 39 deletions.
1 change: 1 addition & 0 deletions api/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BASE_API = 'https://webkiosk-server.azurewebsites.net/api';
4 changes: 4 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getAttendance, getDateWiseAttendance, getTimeTable } from './requests';
import { BASE_API } from './constants';

export { getAttendance, getDateWiseAttendance, getTimeTable, BASE_API };
66 changes: 66 additions & 0 deletions api/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import axios from 'axios';
import { BASE_API } from './constants';

export const getAttendance = async ({
enrollmentNumber,
password,
dateOfBirth,
college,
}) => {
let formData = new FormData();
formData.append('enrll', String(enrollmentNumber));
formData.append('psswd', String(password));
formData.append('dob', String(dateOfBirth));
formData.append('college', String(college));
formData.append('type', 'S');
try {
let res = await axios({
method: 'post',
url: `${BASE_API}/attendance`,
data: formData,
config: { headers: { 'Content-Type': 'multipart/form-data' } },
});
return res.data;
} catch (error) {
console.log(error);
return false;
}
};

export const getDateWiseAttendance = async ({
enrollmentNumber,
password,
dateOfBirth,
college,
}) => {
let formData = new FormData();
formData.append('enrll', String(enrollmentNumber));
formData.append('psswd', String(password));
formData.append('dob', String(dateOfBirth));
formData.append('college', String(college));
formData.append('type', 'S');
try {
let res = await axios({
method: 'post',
url: `${BASE_API}/datewiseattendance`,
data: formData,
config: { headers: { 'Content-Type': 'multipart/form-data' } },
});
return res.data;
} catch (error) {
console.log(error);
return false;
}
};

export const getTimeTable = async (subString, batch, year) => {
try {
let res = await axios.post(
`${BASE_API}/timetable?batch=${batch}&subject=${subString}&year=${year}&college=62`
);
return res.data;
} catch (error) {
console.log(error);
return false;
}
};
44 changes: 10 additions & 34 deletions contexts/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from 'react';
import axios from 'axios';
import { AsyncStorage } from 'react-native';
import { useUser } from './user';
import { BASE_API, getAttendance, getDateWiseAttendance } from '../api';

const AuthContext = React.createContext();

Expand All @@ -19,49 +20,24 @@ export const AuthProvider = ({ children }) => {
college,
}) => {
setisLoading(true);
let formData = new FormData();
formData.append('enrll', String(enrollmentNumber));
formData.append('psswd', String(password));
formData.append('dob', String(dateOfBirth));
formData.append('college', String(college));
formData.append('type', 'S');
await AsyncStorage.setItem(
'user',
JSON.stringify({
enrollmentNumber,
password,
dateOfBirth,
batch,
year,
college,
})
);
try {
let attendance = await axios({
method: 'post',
url: 'https://webkiosk-server.azurewebsites.net/api/attendance',
data: formData,
config: { headers: { 'Content-Type': 'multipart/form-data' } },
});
let datewiseattendance = await axios({
method: 'post',
url: 'https://webkiosk-server.azurewebsites.net/api/datewiseattendance',
data: formData,
config: { headers: { 'Content-Type': 'multipart/form-data' } },
});

let user = {
enrollmentNumber,
password,
dateOfBirth,
batch,
year,
college,
attendance: attendance.data,
datewiseattendance: datewiseattendance.data,
loggedIn: true,
};

let attendance = await getAttendance(user);
let datewiseattendance = await getDateWiseAttendance(user);
if (!attendance || !datewiseattendance) {
setisLoading(false);
return;
}
user.loggedIn = true;
user.attendance = attendance;
user.datewiseattendance = datewiseattendance;
await AsyncStorage.setItem('user', JSON.stringify(user));
setUser(user);
setisAuthenticated(true);
Expand Down
36 changes: 35 additions & 1 deletion contexts/user.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { getSubjectString } from '../utils';
import { getTimeTable } from '../api';
import { AsyncStorage } from 'react-native';

const UserContext = React.createContext();

export const UserProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [timeTable, settimeTable] = useState(null);

const _getTimeTable = async () => {
// if no user
if (!user) return;

// if timtable cached
let cachedTimeTable = await AsyncStorage.getItem('timetable');
if (cachedTimeTable) settimeTable(JSON.parse(cachedTimeTable));

// if timetable not cached
let attendance = getSubjectString(user.attendance);
let res = await getTimeTable(attendance, user.batch, user.year);
let timetable = {
monday: res['monday'],
tuesday: res['tuesday'],
wednesday: res['wednesday'],
thursday: res['thursday'],
friday: res['friday'],
saturday: res['saturday'],
};
await AsyncStorage.setItem('timetable', JSON.stringify(timetable));

// set timetable
settimeTable(timetable);
};

useEffect(() => {
_getTimeTable();
}, []);

return (
<UserContext.Provider
value={{
user,
setUser,
timeTable,
}}
>
{children}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"expo-image-picker": "~8.1.0",
"expo-local-authentication": "~9.0.0",
"expo-web-browser": "~8.2.1",
"moment": "^2.26.0",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
Expand All @@ -35,6 +36,7 @@
"react-native-reanimated": "~1.7.0",
"react-native-safe-area-context": "0.7.3",
"react-native-screens": "~2.2.0",
"react-native-snap-carousel": "^3.9.1",
"react-native-web": "~0.11.7"
},
"devDependencies": {
Expand Down
18 changes: 14 additions & 4 deletions screens/App/TimeTable/TimeTable.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import React from 'react';
import { StyleSheet, Text, View, ScrollView } from 'react-native';
import { useTheme, useUser } from '../../../contexts';
import React, { useEffect } from 'react';
import {
StyleSheet,
Text,
View,
ScrollView,
AsyncStorage,
ActivityIndicator,
} from 'react-native';
import { useTheme, useUser, useAuth } from '../../../contexts';
import { Mixins, Typography } from '../../../styles';
import { getCurrClass, getAttendance } from '../../../utils';
const TimeTable = () => {
const {
theme: {
colors: { background, card, text, primary, black },
},
} = useTheme();
let { user } = useUser();
let { user, timeTable } = useUser();

if (!timeTable) return <ActivityIndicator color='white' />;
return (
<ScrollView>
<Text style={[styles.title, { color: text }]}>TimeTable</Text>
Expand Down
7 changes: 7 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {
getSubjectString,
getCurrClass,
getAttendance,
} from './utilityFunctions';

export { getSubjectString, getCurrClass, getAttendance };
45 changes: 45 additions & 0 deletions utils/utilityFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export const getSubjectString = (attendance) => {
let subject_codes = [];

Object.keys(attendance).forEach((key) =>
subject_codes.push(key.substring(key.length - 11, key.length))
);

let sub_string = '';

for (let i = 0; i < subject_codes.length; i++) {
if (i == subject_codes.length - 1) {
sub_string += subject_codes[i].split(' ')[1];
} else {
sub_string += subject_codes[i].split(' ')[1] + ',';
}
}

return sub_string;
};

export const getCurrClass = (code, attendance) => {
let res = '';
code = code.substring(code.length - 4, code.length);
Object.keys(attendance).forEach((key) => {
if (key.indexOf(code) > 0) {
res = `${key.split(' - ')[0]}`;
}
});
return res;
};

export const getAttendance = (code, att) => {
let res;
code = code.substring(code.length - 4, code.length);

Object.keys(att).forEach((sub) => {
if (sub.indexOf(code) > -1) {
res = String(att[sub][0]['Total']).substring(
0,
att[sub][0]['Total'].length - 2
);
}
});
return res;
};
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3872,6 +3872,11 @@ mkdirp@^0.5.1:
dependencies:
minimist "^1.2.5"

moment@^2.26.0:
version "2.26.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.26.0.tgz#5e1f82c6bafca6e83e808b30c8705eed0dcbd39a"
integrity sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw==

morgan@^1.9.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7"
Expand Down Expand Up @@ -4446,6 +4451,14 @@ range-parser@~1.2.1:
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
integrity sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==

[email protected]:
version "15.6.2"
resolved "https://registry.yarnpkg.com/react-addons-shallow-compare/-/react-addons-shallow-compare-15.6.2.tgz#198a00b91fc37623db64a28fd17b596ba362702f"
integrity sha1-GYoAuR/DdiPbZKKP0XtZa6NicC8=
dependencies:
fbjs "^0.8.4"
object-assign "^4.1.0"

react-devtools-core@^3.6.3:
version "3.6.3"
resolved "https://registry.yarnpkg.com/react-devtools-core/-/react-devtools-core-3.6.3.tgz#977d95b684c6ad28205f0c62e1e12c5f16675814"
Expand Down Expand Up @@ -4512,6 +4525,14 @@ react-native-screens@~2.2.0:
dependencies:
debounce "^1.2.0"

react-native-snap-carousel@^3.9.1:
version "3.9.1"
resolved "https://registry.yarnpkg.com/react-native-snap-carousel/-/react-native-snap-carousel-3.9.1.tgz#6fd9bd8839546c2c6043a41d2035afbc6fe0443e"
integrity sha512-xWEGusacIgK1YaDXLi7Gao2+ISLoGPVEBR8fcMf4tOOJQufutlNwkoLu0l6B8Qgsrre0nTxoVZikRgGRDWlLaQ==
dependencies:
prop-types "^15.6.1"
react-addons-shallow-compare "15.6.2"

[email protected]:
version "3.1.2"
resolved "https://registry.yarnpkg.com/react-native-view-shot/-/react-native-view-shot-3.1.2.tgz#8c8e84c67a4bc8b603e697dbbd59dbc9b4f84825"
Expand Down

0 comments on commit e20f6c0

Please sign in to comment.