Solve confilict between firebase library and app component & create index for firebase services folder to have a short path

This commit is contained in:
Qolzam
2017-10-21 11:33:47 +07:00
parent df84c80899
commit 407facb109
26 changed files with 18 additions and 813 deletions

View File

@@ -1,5 +1,5 @@
// - Import react component
import { storageRef } from 'app/firebase/'
import { storageRef } from 'app/firebaseClient/'
//- Import actions

View File

@@ -1,5 +1,5 @@
// - Import react component
import { storageRef } from 'app/firebase/'
import { storageRef } from 'app/firebaseClient/'
//- Import actions

View File

@@ -3,7 +3,7 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Route, Switch, NavLink, withRouter, Redirect } from 'react-router-dom'
import { firebaseAuth, firebaseRef } from 'app/firebase'
import { firebaseAuth, firebaseRef } from 'app/firebaseClient'
import { push } from 'react-router-redux'
import Snackbar from 'material-ui/Snackbar'
import LinearProgress from 'material-ui/LinearProgress'

View File

@@ -16,15 +16,17 @@ import { IVoteService } from 'services/votes'
//#region Service implemented classes
// - Firebase services
import { AuthorizeService } from 'firebase/services/authorize'
import { CircleService } from 'firebase/services/circles'
import { CommentService } from 'firebase/services/comments'
import { CommonService } from 'firebase/services/common'
import { ImageGalleryService } from 'firebase/services/imageGallery'
import { NotificationService } from 'firebase/services/notifications'
import { PostService } from 'firebase/services/posts'
import { UserService } from 'firebase/services/users'
import { VoteService } from 'firebase/services/votes'
import {
AuthorizeService,
CircleService,
CommentService,
CommonService,
ImageGalleryService,
NotificationService,
PostService,
UserService,
VoteService
} from 'firebaseClient/services'
//#endregion

View File

@@ -1,26 +0,0 @@
import firebase from 'firebase'
try {
let config = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID
}
firebase.initializeApp(config)
} catch (e) {
}
// - Storage reference
export let storageRef = firebase.storage().ref()
// - Database authorize
export let firebaseAuth = firebase.auth
export let firebaseRef = firebase.database().ref()
// - Firebase default
export default firebase

View File

@@ -1,106 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { IAuthorizeService } from 'services/authorize'
import { User } from 'Domain/users'
import { LoginUser, RegisterUserResult } from 'domain/authorize'
import { SocialError } from 'domain/common'
/**
* Firbase authorize service
*
* @export
* @class AuthorizeService
* @implements {IAuthorizeService}
*/
export class AuthorizeService implements IAuthorizeService {
/**
* Login the user
*
* @returns {Promise<LoginUser>}
* @memberof IAuthorizeService
*/
public login: (email: string, password: string) => Promise<LoginUser> = (email, password) => {
return new Promise<LoginUser>((resolve, reject) => {
firebaseAuth()
.signInWithEmailAndPassword(email, password)
.then((result) => {
resolve(new LoginUser(result.uid))
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
/**
* Logs out the user
*
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public logout: () => Promise<void> = () => {
return new Promise<void>((resolve, reject) => {
firebaseAuth()
.signOut()
.then((result) => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
/**
* Register a user
*
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public registerUser: (user: User) => Promise<RegisterUserResult> = (user) => {
return new Promise<RegisterUserResult>((resolve, reject) => {
firebaseAuth()
.createUserWithEmailAndPassword(user.email as string, user.password as string)
.then((signupResult) => {
firebaseRef.child(`users/${signupResult.uid}/info`)
.set({
...user,
avatar: 'noImage'
})
.then((result) => {
resolve(new RegisterUserResult(signupResult.uid))
})
.catch((error: any) => reject(new SocialError(error.name, error.message)))
})
.catch((error: any) => reject(new SocialError(error.code, error.message)))
})
}
/**
* Update user password
*
* @returns {Promise<void>}
* @memberof IAuthorizeService
*/
public updatePassword: (newPassword: string) => Promise<void> = (newPassword) => {
return new Promise<void>((resolve, reject) => {
let user = firebaseAuth().currentUser
if (user) {
user.updatePassword(newPassword).then(() => {
// Update successful.
resolve()
}).catch((error: any) => {
// An error happened.
reject(new SocialError(error.code, error.message))
})
}
})
}
}

View File

@@ -1,5 +0,0 @@
import { AuthorizeService } from './AuthorizeService'
export {
AuthorizeService
}

View File

@@ -1,120 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { ICircleService } from 'services/circles'
import { Circle, UserFollower } from 'domain/circles'
import { User } from 'domain/users'
/**
* Firbase circle service
*
* @export
* @class CircleService
* @implements {ICircleService}
*/
export class CircleService implements ICircleService {
public addCircle: (userId: string, circle: Circle)
=> Promise<string> = (userId, circle) => {
return new Promise<string>((resolve,reject) => {
let circleRef = firebaseRef.child(`userCircles/${userId}/circles`).push(circle)
circleRef.then(() => {
resolve(circleRef.key as string)
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public addFollowingUser: (userId: string, circleId: string, userCircle: User, userFollower: UserFollower, userFollowingId: string)
=> Promise<void> = (userId, circleId, userCircle, userFollower, userFollowingId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userCircles/${userId}/circles/${circleId}/users/${userFollowingId}`] = userCircle
updates[`userCircles/${userFollowingId}/circles/-Followers/users/${userId}`] = userFollower
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public deleteFollowingUser: (userId: string, circleId: string, userFollowingId: string)
=> Promise<void> = (userId, circleId, userFollowingId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userCircles/${userId}/circles/${circleId}/users/${userFollowingId}`] = null
updates[`userCircles/${userFollowingId}/circles/-Followers/users/${userId}`] = null
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public updateCircle: (userId: string, circleId: string, circle: Circle)
=> Promise<void> = (userId, circleId, circle) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userCircles/${userId}/circles/${circleId}`] = circle
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public deleteCircle: (userId: string, circleId: string)
=> Promise<void> = (userId, circleId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userCircles/${userId}/circles/${circleId}`] = null
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public getCircles: (userId: string) => Promise<{ [circleId: string]: Circle }> = (userId) => {
return new Promise<{ [circleId: string]: Circle }>((resolve,reject) => {
let circlesRef: any = firebaseRef.child(`userCircles/${userId}/circles`)
circlesRef.once('value').then((snapshot: any) => {
let circles: any = snapshot.val() || {}
let parsedCircles: { [circleId: string]: Circle } = {}
Object.keys(circles).forEach((circleId) => {
parsedCircles[circleId] = {
id: circleId,
...circles[circleId]
}
})
resolve(parsedCircles)
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
}

View File

@@ -1,5 +0,0 @@
import { CircleService } from './CircleService'
export {
CircleService
}

View File

@@ -1,71 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { ICommentService } from 'services/comments'
import { Comment } from 'domain/comments'
/**
* Firbase comment service
*
* @export
* @class CommentService
* @implements {ICommentService}
*/
export class CommentService implements ICommentService {
public addComment: (postId: string, comment: Comment)
=> Promise<string> = (postId, comment) => {
return new Promise<string>((resolve,reject) => {
let commentRef: any = firebaseRef.child(`postComments/${postId}`).push(comment)
commentRef.then(() => {
resolve(commentRef.key)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public getComments: ()
=> Promise<{ [postId: string]: { [commentId: string]: Comment } }> = () => {
return new Promise<{ [postId: string]: { [commentId: string]: Comment }}>((resolve,reject) => {
let commentsRef: any = firebaseRef.child(`postComments`)
commentsRef.on('value', (snapshot: any) => {
let comments: {[postId: string]: {[commentId: string]: Comment}} = snapshot!.val() || {}
resolve(comments)
})
})
}
public updateComment: (userId: string, postId: string, comment: Comment)
=> Promise<void> = (userId, postId, comment) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`postComments/${postId}/${userId}`] = comment
firebaseRef.update(updates)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public deleteComment: (commentId: string, postId: string)
=> Promise<void> = (commentId, postId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`postComments/${postId}/${commentId}`] = null
firebaseRef.update(updates)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
}

View File

@@ -1,5 +0,0 @@
import { CommentService } from './CommentService'
export {
CommentService
}

View File

@@ -1,17 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { ICommonService } from 'services/common'
/**
* Firbase common service
*
* @export
* @class CommonService
* @implements {ICommonService}
*/
export class CommonService implements ICommonService {
}

View File

@@ -1,5 +0,0 @@
import { CommonService } from './CommonService'
export {
CommonService
}

View File

@@ -1,110 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth, storageRef } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { IImageGalleryService } from 'services/imageGallery'
import { Image } from 'domain/imageGallery'
/**
* Firbase image gallery service
*
* @export
* @class ImageGalleryService
* @implements {IImageGalleryService}
*/
export class ImageGalleryService implements IImageGalleryService {
public getImageGallery: (userId: string)
=> Promise<Image[]> = (userId) => {
return new Promise<Image[]>((resolve,reject) => {
let imagesRef: any = firebaseRef.child(`userFiles/${userId}/files/images`)
imagesRef.once('value').then((snapshot: any) => {
let images = snapshot.val() || {}
let parsedImages: Image[] = []
Object.keys(images).forEach((imageId) => {
parsedImages.push({
id: imageId,
...images[imageId]
})
})
resolve(parsedImages)
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public saveImage: (userId: string, image: Image)
=> Promise<string> = (userId, image) => {
return new Promise<string>((resolve,reject) => {
let imageRef = firebaseRef.child(`userFiles/${userId}/files/images`).push(image)
imageRef.then(() => {
resolve(imageRef.key!)
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public deleteImage: (userId: string, imageId: string)
=> Promise<void> = (userId, imageId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userFiles/${userId}/files/images/${imageId}`] = null
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public uploadImage: (file: any, fileName: string, progressCallback: Function)
=> Promise<void> = (file, fileName, progressCallback) => {
return new Promise<void>((resolve,reject) => {
let storegeFile: any = storageRef.child(`images/${fileName}`)
// Upload file
let task: any = storegeFile.put(file)
// Upload storage bar
task.on('state_changed', (snapshot: any) => {
let percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
progressCallback(percentage)
}, (error: any) => {
reject(new SocialError(error.code, error.message))
}, () => {
resolve()
})
})
}
public downloadImage: (fileName: string)
=> Promise<string> = (fileName) => {
return new Promise<string>((resolve,reject) => {
// Create a reference to the file we want to download
let starsRef: any = storageRef.child(`images/${fileName}`)
// Get the download URL
starsRef.getDownloadURL().then((url: string) => {
resolve(url)
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
}

View File

@@ -1,5 +0,0 @@
import { ImageGalleryService } from './ImageGalleryService'
export {
ImageGalleryService
}

View File

@@ -1,5 +0,0 @@
import { NotificationService } from './NotificationService'
export {
NotificationService
}

View File

@@ -1,72 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { Notification } from 'domain/notifications'
import { INotificationService } from 'services/notifications'
/**
* Firbase notification service
*
* @export
* @class NotificationService
* @implements {INotificationService}
*/
export class NotificationService implements INotificationService {
public addNotification: (notification: Notification)
=> Promise<void> = (notification: Notification) => {
return new Promise<void>((resolve,reject) => {
firebaseRef.child(`userNotifies/${notification.notifyRecieverUserId}`)
.push(notification)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public getNotifications: (userId: string)
=> Promise<{ [notifyId: string]: Notification }> = (userId) => {
return new Promise<{ [notifyId: string]: Notification }>((resolve,reject) => {
let notifiesRef: any = firebaseRef.child(`userNotifies/${userId}`)
notifiesRef.on('value', (snapshot: any) => {
let notifies: {[notifyId: string]: Notification} = snapshot.val() || {}
resolve(notifies)
})
})
}
public deleteNotification: (notificationId: string, userId: string)
=> Promise <void> = (notificationId, userId) => {
return new Promise<void>((resolve, reject) => {
let updates: any = {}
updates[`userNotifies/${userId}/${notificationId}`] = null
firebaseRef.update(updates)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
public setSeenNotification: (notificationId: string, userId: string, notification: Notification)
=> Promise <void> = (notificationId, userId, notification) => {
return new Promise<void>((resolve, reject) => {
let updates: any = {}
updates[`userNotifies/${userId}/${notificationId}`] = notification
firebaseRef.update(updates)
.then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code, error.message))
})
})
}
}

View File

@@ -1,101 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { Post } from 'domain/posts'
import { IPostService } from 'services/posts'
/**
* Firbase post service
*
* @export
* @class PostService
* @implements {IPostService}
*/
export class PostService implements IPostService {
public addPost: (userId: string, post: Post)
=> Promise<string> = (userId, post) => {
return new Promise<string>((resolve,reject) => {
let postRef: any = firebaseRef.child(`userPosts/${userId}/posts`).push(post)
postRef.then(() => {
resolve(postRef.key)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public updatePost: (userId: string, postId: string, post: Post)
=> Promise<void> = (userId, postId, post) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userPosts/${userId}/posts/${postId}`] = post
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public deletePost: (userId: string, postId: string)
=> Promise<void> = (userId, postId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`userPosts/${userId}/posts/${postId}`] = null
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public getPosts: (userId: string)
=> Promise<{ [postId: string]: Post }> = (userId) => {
return new Promise<{ [postId: string]: Post }>((resolve,reject) => {
let postsRef: any = firebaseRef.child(`userPosts/${userId}/posts`)
postsRef.once('value').then((snapshot: any) => {
let posts: any = snapshot.val() || {}
let parsedPosts: { [postId: string]: Post } = {}
Object.keys(posts).forEach((postId) => {
parsedPosts[postId] = {
id: postId,
...posts[postId]
}
})
resolve(parsedPosts)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public getPostById: (userId: string, postId: string)
=> Promise<Post> = (userId, postId) => {
return new Promise<Post>((resolve,reject) => {
let postsRef: any = firebaseRef.child(`userPosts/${userId}/posts/${postId}`)
postsRef.once('value').then((snapshot: any) => {
let newPost = snapshot.val() || {}
let post: Post = {
id: postId,
...newPost
}
resolve(post)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
}

View File

@@ -1,5 +0,0 @@
import { PostService } from './PostService'
export {
PostService
}

View File

@@ -1,73 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { Profile } from 'domain/users'
import { IUserService } from 'services/users'
/**
* Firbase user service
*
* @export
* @class UserService
* @implements {IUserService}
*/
export class UserService implements IUserService {
public getUserProfile: (userId: string)
=> Promise<Profile> = (userId) => {
return new Promise<Profile>((resolve,reject) => {
let userProfileRef: any = firebaseRef.child(`users/${userId}/info`)
userProfileRef.once('value').then((snapshot: any) => {
let userProfile: Profile = snapshot.val() || {}
resolve(userProfile)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public updateUserProfile: (userId: string, profile: Profile)
=> Promise<void> = (userId, profile) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`users/${userId}/info`] = profile
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public getUsersProfile: (userId: string)
=> Promise<{ [userId: string]: Profile }> = (userId) => {
return new Promise<{ [userId: string]: Profile }>((resolve,reject) => {
let usersProfileRef: any = firebaseRef.child(`users`)
usersProfileRef.once('value').then((snapshot: any) => {
let usersProfile: any = snapshot.val() || {}
let parsedusersProfile: {[userId: string]: Profile} = {}
Object.keys(usersProfile).forEach((userKey) => {
if (userId !== userKey) {
let userInfo = usersProfile[userKey].info
parsedusersProfile[userKey] = {
avatar: userInfo.avatar,
email: userInfo.email,
fullName: userInfo.fullName,
banner: userInfo.banner,
tagLine: userInfo.tagLine
}
}
})
resolve(parsedusersProfile)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
}

View File

@@ -1,5 +0,0 @@
import { UserService } from './UserService'
export {
UserService
}

View File

@@ -1,57 +0,0 @@
// - Import react components
import { firebaseRef, firebaseAuth } from 'app/firebase/'
import { SocialError } from 'domain/common'
import { Vote } from 'domain/votes'
import { IVoteService } from 'services/votes'
/**
* Firbase vote service
*
* @export
* @class VoteService
* @implements {IVoteService}
*/
export class VoteService implements IVoteService {
public addVote: (vote: Vote)
=> Promise<string> = (vote) => {
return new Promise<string>((resolve,reject) => {
let voteRef = firebaseRef.child(`postVotes/${vote.postId}`)
.push(vote)
voteRef.then(() => {
resolve(voteRef.key!)
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
public getVotes: ()
=> Promise<{ [postId: string]: { [voteId: string]: Vote } }> = () => {
return new Promise<{ [postId: string]: { [voteId: string]: Vote } }>((resolve,reject) => {
let votesRef: any = firebaseRef.child(`postVotes`)
votesRef.on('value',(snapshot: any) => {
let postVotes: {[postId: string]: {[voteId: string]: Vote}} = snapshot.val() || {}
resolve(postVotes)
})
})
}
public deleteVote: (voteId: string, postId: string)
=> Promise<void> = (voteId, postId) => {
return new Promise<void>((resolve,reject) => {
let updates: any = {}
updates[`postVotes/${postId}/${voteId}`] = null
firebaseRef.update(updates).then(() => {
resolve()
})
.catch((error: any) => {
reject(new SocialError(error.code,error.message))
})
})
}
}

View File

@@ -1,5 +0,0 @@
import { VoteService } from './VoteService'
export {
VoteService
}

View File

@@ -3,7 +3,7 @@ import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
let expect = require('expect')
import firebase, {firebaseRef} from 'app/firebase'
import firebase, {firebaseRef} from 'app/firebaseClient'
let authorizeActions = require('authorizeActions')
import * as types from 'actionTypes'

View File

@@ -71,11 +71,12 @@
"babel-core": "^6.24.1",
"babel-loader": "^7.1.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.7",
"eslint": "^4.9.0",
"karma": "^1.6.0",
"karma-chrome-launcher": "^2.0.0",
"karma-mocha": "^1.3.0",

View File

@@ -87,7 +87,7 @@ module.exports = {
constants: 'app/constants',
services: 'app/services',
factories: 'app/factories',
'firebase/services': 'app/firebase/services',
'firebaseClient/services': 'app/firebaseClient/services',
domain: 'app/domain',
api: 'app/api',
db: 'app/db',