-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
118 lines (100 loc) · 3.7 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
// import Config from 'react-native-config';
// import isString from 'lodash/isString';
// import castArray from 'lodash/castArray';
// import { Buffer } from 'buffer';
/* eslint-disable no-param-reassign */
// export const addModelState = (reactComponentInstance, modalName, isOpen = false) => {
// reactComponentInstance.modals = reactComponentInstance.modals || {};
// reactComponentInstance.state.modals = reactComponentInstance.state.modals || {};
// const setState = (state) => {
// reactComponentInstance.setState({
// modals: { ...reactComponentInstance.state.modals, [modalName]: state },
// });
// };
// reactComponentInstance.modals[modalName] = {
// show: () => setState(true),
// hide: () => setState(false),
// get isVisible() {
// return reactComponentInstance.state.modals[modalName];
// },
// };
// reactComponentInstance.state.modals[modalName] = isOpen;
// };
// eslint-disable-next-line
export function makeNumGenerator() {
let i = -1;
return () => {
i += 1;
return i;
};
}
const convertS3ToImgix = ({ image, /* height, */ width }) => (
image.replace('https://s3.amazonaws.com/sportyspots-prd', 'http://sportyspots.imgix.net')
.concat('?auto=compress')
// .concat(height ? `&h=${height}` : '')
.concat(width ? `&w=${width}` : '')
);
const getImageUrl = ({ image, height, width }) => (
image.startsWith('http') // TODO: this should be https://s3.amazonaws.com/sportyspots-
? convertS3ToImgix({ image, height, width })
: `${publicRuntimeConfig.seedorfHost}${image}`
);
const DEFAULT_SPOT_IMG = 'https://raw.githubusercontent.com/SportySpots/cruijff/master/App/Images/spot-placeholder.png';
export const getSpotImages = ({ images, height, width }) => {
if (!height || !width) {
throw new Error('Height | width is not defined');
}
return images && images.length > 0
? images.map(({ image }) => getImageUrl({ image, height, width }))
: [DEFAULT_SPOT_IMG];
};
// const routeToString = (route, depth = 0) => {
// let str = route.routeName || 'ROOT';
// if (route.routes) {
// str += '\n' + route.routes.map((subRoute, idx) => {
// const isActive = (idx === route.index);
// return ' '.repeat(3 * depth) + (isActive ? ' * ' : ' ')
// + routeToString(subRoute, depth + 1);
// }).join('\n');
// }
// return str;
// };
// export const logNavigationState = (route = globalRefs.rootNavigator.state.nav) => {
// console.log(routeToString(route));
// };
/**
* errors = {
* email: ['This field must be unique.'],
* password1: ['This password is too short. It must contain at least 8 characters.']
* }
* OR
* errors = ['Invalid Spot. Spot being assigned doesnt have the already associated sport']
* OR
* errors = 'Email already in use'
*/
// export const curateErrors = (curateFieldName, curateErrorMsg) => (errors) => {
// if (isString(errors)) {
// return { [curateFieldName(null)]: [errors] }; // curatedErrors
// }
// if (Array.isArray(errors)) {
// return { [curateFieldName(null)]: errors }; // curatedErrors
// }
// // In case errors is an object
// const keys = Object.keys(errors);
// const curatedErrors = {};
// keys.forEach((key) => {
// const arrayError = castArray(errors[key]);
// const curatedArray = arrayError.map(errorMsg => (curateErrorMsg(errorMsg)));
// curatedErrors[curateFieldName(key)] = curatedArray;
// });
// return curatedErrors;
// };
// export const decodeJWTToken = (token) => {
// if (!token) {
// return null;
// }
// console.log('decode token', token);
// return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString('ascii'));
// };