Initial commit
This commit is contained in:
		
							
								
								
									
										8
									
								
								src/common/util/confirm.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/common/util/confirm.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| import { createConfirmation } from 'react-confirm'; | ||||
| import ConfirmationDialog from '../components/ConfirmationDialog'; | ||||
|  | ||||
| const confirmDialog = createConfirmation(ConfirmationDialog); | ||||
|  | ||||
| export default function confirm(confirmation, options = {}) { | ||||
|   return confirmDialog({ confirmation, ...options }); | ||||
| } | ||||
							
								
								
									
										164
									
								
								src/common/util/http.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										164
									
								
								src/common/util/http.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,164 @@ | ||||
| import fetch from "isomorphic-fetch"; | ||||
| import {appConfig} from "../config/app"; | ||||
|  | ||||
| export class Http { | ||||
|   token = ''; | ||||
|   baseUrl = ''; | ||||
|  | ||||
|   constructor(token, baseUrl = appConfig.apiUrl) { | ||||
|     if (token) { | ||||
|       this.token = token; | ||||
|     } | ||||
|  | ||||
|     this.baseUrl = baseUrl; | ||||
|   } | ||||
|  | ||||
|   checkStatus(result) { | ||||
|     if (!result.ok) { | ||||
|       return result.json().then(data => { | ||||
|         return Promise.reject(data); | ||||
|       }); | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
|  | ||||
|   raw(url, options={}) { | ||||
|     let headers = new Headers(); | ||||
|     console.log(url, options, "raw http request"); | ||||
|     return fetch(url, {headers: {}}) | ||||
|       .then(response => { | ||||
|         if (options.raw){ | ||||
|           return response; | ||||
|         } | ||||
|  | ||||
|         return response.json(); | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         console.log(err); | ||||
|         throw err; | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|   get(url, options={}) { | ||||
|     let headers = new Headers(); | ||||
|     headers.append("Authorization", `Bearer ${this.token}`); | ||||
|  | ||||
|     return fetch(this.baseUrl + url, Object.assign(options, {headers})) | ||||
|       .then(this.checkStatus) | ||||
|       .then(response => { | ||||
|         if (options.raw){ | ||||
|           return response; | ||||
|         } | ||||
|  | ||||
|         return response.json(); | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         console.log(err); | ||||
|         throw err; | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|   post(url, data, options={}) { | ||||
|     let headers = new Headers(); | ||||
|     headers.append("Authorization", `Bearer ${this.token}`); | ||||
|     headers.append("Content-Type", "application/json"); | ||||
|  | ||||
|     return fetch(this.baseUrl + url, Object.assign({ | ||||
|       method: 'POST', | ||||
|       headers, | ||||
|       body: JSON.stringify(data) | ||||
|     }, options)) | ||||
|       .then(this.checkStatus) | ||||
|       .then(response => { | ||||
|         if (options.raw){ | ||||
|           return response; | ||||
|         } | ||||
|  | ||||
|         return response.json() | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         console.log(err); | ||||
|         throw err; | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|   put(url, data, options={}) { | ||||
|     let headers = new Headers(); | ||||
|     headers.append("Authorization", `Bearer ${this.token}`); | ||||
|     headers.append("Content-Type", "application/json"); | ||||
|  | ||||
|     return fetch(this.baseUrl + url, Object.assign({ | ||||
|       method: 'PUT', | ||||
|       headers, | ||||
|       body: JSON.stringify(data) | ||||
|     }, options)) | ||||
|       .then(this.checkStatus) | ||||
|       .then(response => { | ||||
|         if (options.raw){ | ||||
|           return response; | ||||
|         } | ||||
|  | ||||
|         return response.json() | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         console.log(err); | ||||
|         throw err; | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|   delete(url, options={}) { | ||||
|     let headers = new Headers(); | ||||
|     headers.append("Authorization", `Bearer ${this.token}`); | ||||
|  | ||||
|     return fetch(this.baseUrl + url, { | ||||
|       headers, | ||||
|       method: 'DELETE' | ||||
|     }) | ||||
|       .then(this.checkStatus) | ||||
|       .then(response => { | ||||
|         if (options.raw){ | ||||
|           return response; | ||||
|         } | ||||
|  | ||||
|         return response.json() | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         console.log(err); | ||||
|         throw err; | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|   upload(file, options={}) { | ||||
|     let headers = new Headers(); | ||||
|     headers.append("Authorization", `Bearer ${this.token}`); | ||||
|     let data = new FormData(); | ||||
|     data.append('file', file); | ||||
|  | ||||
|     return fetch(appConfig.apiUrl + 'files', Object.assign({ | ||||
|       method: 'POST', | ||||
|       headers, | ||||
|       body: data | ||||
|     }, options)) | ||||
|       .then(this.checkStatus) | ||||
|       .then(response => { | ||||
|         if (options.raw){ | ||||
|           return response; | ||||
|         } | ||||
|  | ||||
|         return response.json() | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         console.log(err); | ||||
|         throw err; | ||||
|       }) | ||||
|   } | ||||
|  | ||||
|  | ||||
|   appendImagePath(path) { | ||||
|     return appConfig.imageUrl + path; | ||||
|   } | ||||
|  | ||||
|   // static appendImagePath(path) { | ||||
|   //   return appConfig.imageUrl + path; | ||||
|   // } | ||||
| } | ||||
							
								
								
									
										7
									
								
								src/common/util/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/common/util/index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| export function queryStringBuilder(q={}) { | ||||
|   return Object.keys(q) | ||||
|     .map(k => { | ||||
|       return `${k}=${q[k]}`; | ||||
|     }) | ||||
|     .join("&"); | ||||
| } | ||||
							
								
								
									
										11
									
								
								src/common/util/location.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/common/util/location.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| export function getParameterByName(name, url) { | ||||
|   if (!url) { | ||||
|     url = window.location.href; | ||||
|   } | ||||
|   name = name.replace(/[\[\]]/g, "\\$&"); | ||||
|   var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | ||||
|     results = regex.exec(url); | ||||
|   if (!results) return null; | ||||
|   if (!results[2]) return ''; | ||||
|   return decodeURIComponent(results[2].replace(/\+/g, " ")); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user