2020-01-11 23:53:42 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { Table } from 'semantic-ui-react';
|
|
|
|
|
2021-09-04 22:28:12 +00:00
|
|
|
export const randomString = () => Math.random().toString(36).substr(2, 10);
|
|
|
|
|
2022-04-06 22:54:55 +00:00
|
|
|
export const siteUrl = window.location.toString();
|
|
|
|
export const apiUrl = window.location.port ?
|
|
|
|
'http://' + window.location.hostname + ':8000'
|
|
|
|
:
|
|
|
|
window.location.protocol + '//api.' + window.location.hostname;
|
|
|
|
export const staticUrl = window.location.port ?
|
|
|
|
'http://' + window.location.hostname + ':8000/static'
|
|
|
|
:
|
|
|
|
window.location.protocol + '//static.' + window.location.hostname;
|
2020-01-11 06:27:30 +00:00
|
|
|
|
2020-01-20 02:54:44 +00:00
|
|
|
export const isAdmin = (user) => user.is_staff || user.member.is_director || user.member.is_staff;
|
|
|
|
export const isInstructor = (user) => isAdmin(user) || user.member.is_instructor;
|
|
|
|
|
2020-07-18 22:15:53 +00:00
|
|
|
export const getInstructor = (x) => {
|
|
|
|
if (x.course === 413 || x.course === 317 || x.course === 273) {
|
|
|
|
return 'Protospace';
|
|
|
|
} else {
|
|
|
|
return x.instructor_name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-20 02:54:44 +00:00
|
|
|
export const statusColor = {
|
2020-01-20 09:47:23 +00:00
|
|
|
'Prepaid': 'green',
|
2020-01-20 02:54:44 +00:00
|
|
|
'Current': 'green',
|
|
|
|
'Due': 'yellow',
|
|
|
|
'Overdue': 'red',
|
|
|
|
'Former Member': 'black',
|
2022-12-16 19:18:45 +00:00
|
|
|
'Paused Member': 'black',
|
|
|
|
'Expired Member': 'black',
|
2020-01-20 02:54:44 +00:00
|
|
|
};
|
|
|
|
|
2020-01-11 23:53:42 +00:00
|
|
|
export const BasicTable = (props) => (
|
|
|
|
<Table collapsing padded unstackable basic='very'>
|
|
|
|
{props.children}
|
|
|
|
</Table>
|
|
|
|
);
|
|
|
|
|
2021-11-12 01:03:10 +00:00
|
|
|
export const requester = (route, method, token, data, signal=null) => {
|
|
|
|
let options = {headers: {}, signal: signal};
|
2021-07-02 03:50:14 +00:00
|
|
|
let url = '';
|
2020-01-08 23:43:59 +00:00
|
|
|
|
|
|
|
if (token) {
|
|
|
|
options.headers.Authorization = 'Token ' + token;
|
|
|
|
}
|
2020-01-07 07:59:12 +00:00
|
|
|
|
2022-07-13 03:14:23 +00:00
|
|
|
if (method === 'GET') {
|
2020-01-08 23:43:59 +00:00
|
|
|
// pass
|
|
|
|
} else if (['POST', 'PUT', 'PATCH'].includes(method)) {
|
2020-01-07 07:59:12 +00:00
|
|
|
const formData = new FormData();
|
|
|
|
Object.keys(data).forEach(key =>
|
2020-01-13 08:01:42 +00:00
|
|
|
formData.append(key, data[key] === null ? '' : data[key])
|
2020-01-07 07:59:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
options = {
|
2020-01-08 23:43:59 +00:00
|
|
|
...options,
|
|
|
|
method: method,
|
2020-01-07 07:59:12 +00:00
|
|
|
body: formData,
|
|
|
|
};
|
2022-07-13 03:14:23 +00:00
|
|
|
} else if (method === 'DELETE') {
|
2020-01-15 00:12:50 +00:00
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
method: method,
|
|
|
|
};
|
2020-01-07 07:59:12 +00:00
|
|
|
} else {
|
2020-01-08 23:43:59 +00:00
|
|
|
throw new Error('Method not supported');
|
2020-01-07 07:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const customError = (data) => {
|
|
|
|
const error = new Error(JSON.stringify(data));
|
|
|
|
error.data = data;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-07-02 03:50:14 +00:00
|
|
|
if (route.startsWith('http')) {
|
|
|
|
url = route;
|
|
|
|
} else {
|
|
|
|
url = apiUrl + route;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetch(url, options)
|
2020-01-07 07:59:12 +00:00
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw customError(response);
|
|
|
|
}
|
2020-11-15 04:36:41 +00:00
|
|
|
|
|
|
|
if (method === 'DELETE') {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const contentType = response.headers.get('content-type');
|
|
|
|
if (contentType && contentType.indexOf('application/json') !== -1) {
|
|
|
|
return response.json();
|
|
|
|
} else {
|
|
|
|
return response;
|
|
|
|
}
|
2020-01-07 07:59:12 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2020-01-14 00:52:15 +00:00
|
|
|
const code = error.data ? error.data.status : null;
|
2022-07-13 03:14:23 +00:00
|
|
|
if (code === 413) {
|
2020-01-07 07:59:12 +00:00
|
|
|
throw customError({non_field_errors: ['File too big']});
|
2020-01-08 23:43:59 +00:00
|
|
|
} else if (code >= 400 && code < 500) {
|
2020-01-07 07:59:12 +00:00
|
|
|
return error.data.json()
|
|
|
|
.then(result => {
|
2022-07-13 03:14:23 +00:00
|
|
|
if (result.detail === 'Invalid token.') {
|
2020-01-13 01:03:02 +00:00
|
|
|
localStorage.clear();
|
|
|
|
window.location = '/';
|
|
|
|
}
|
2020-01-07 07:59:12 +00:00
|
|
|
throw customError(result);
|
|
|
|
});
|
2020-01-08 23:43:59 +00:00
|
|
|
} else if (code >= 500 && code < 600) {
|
|
|
|
throw customError({non_field_errors: ['Server Error']});
|
2021-11-12 01:03:10 +00:00
|
|
|
} else if (error.message === 'The operation was aborted. ') {
|
|
|
|
throw error;
|
2020-01-07 07:59:12 +00:00
|
|
|
} else {
|
|
|
|
throw customError({non_field_errors: ['Network Error']});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-26 21:26:13 +00:00
|
|
|
|
|
|
|
// from: https://usehooks.com/useWindowSize/
|
|
|
|
function useWindowSize() {
|
|
|
|
const [windowSize, setWindowSize] = useState({
|
|
|
|
width: undefined,
|
|
|
|
height: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function handleResize() {
|
|
|
|
setWindowSize({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
window.addEventListener('resize', handleResize);
|
|
|
|
handleResize();
|
|
|
|
return () => window.removeEventListener('resize', handleResize);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return windowSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useIsMobile = () => {
|
2022-07-13 03:14:23 +00:00
|
|
|
const {width} = useWindowSize();
|
2022-04-26 21:26:13 +00:00
|
|
|
return width <= 767;
|
|
|
|
}
|