From e08c78490d733986d1d4b9e8be8434d26dbf9299 Mon Sep 17 00:00:00 2001 From: idanlevi Date: Sun, 27 Jan 2019 00:59:16 +0200 Subject: [PATCH] merge with v2 --- src/store/modules/events.js | 288 +++++++++++++++++++---------------- src/store/modules/user.js | 294 ++++++++++++++++++++---------------- src/views/splash/Splash.js | 62 ++++---- 3 files changed, 350 insertions(+), 294 deletions(-) diff --git a/src/store/modules/events.js b/src/store/modules/events.js index dd521c1..f10e5f1 100644 --- a/src/store/modules/events.js +++ b/src/store/modules/events.js @@ -6,179 +6,209 @@ const REQUEST_EVENTS = 'haprev/events/REQUEST_EVENTS'; const RESPONSE_EVENTS = 'haprev/events/RESPONSE_EVENTS'; const RESPONSE_NEW_ACTIVITY = 'haprev/events/RESPONSE_NEW_ACTIVITY'; const ADD_PARTICIPANT = 'haprev/events/ADD_PARTICIPANT'; -const DELETE_PARTICIPANT= 'haprev/events/DELETE_PARTICIPANT'; +const DELETE_PARTICIPANT = 'haprev/events/DELETE_PARTICIPANT'; const initalState = { status: null, - events:{}, - for:null + events: {}, + for: null }; export default (state = initalState, action = {}) => { switch (action.type) { case REQUEST_EVENTS: - return { ...state, status: 'reqEvents', for: action.payload}; + return {...state, status: 'reqEvents', for: action.payload}; case RESPONSE_EVENTS: - return { ...state, status: '', events: action.payload}; + return {...state, status: '', events: action.payload}; case RESPONSE_NEW_ACTIVITY: - return {...state, events: Object.assign(action.payload,state.events)} + return {...state, events: Object.assign(action.payload, state.events)} case ADD_PARTICIPANT: - const eventId = action.eventId - const participants=action.participants - return {...state, - events: { - ...state.events, - [eventId]: { - ...state.events[eventId] || null, - participants: participants - } - } + const eventId = action.eventId + const participants = action.participants + return { + ...state, + events: { + ...state.events, + [eventId]: { + ...state.events[eventId] || null, + participants: participants + } } + } case DELETE_PARTICIPANT: - const newParticipants=action.newParticipants - return {...state, - for:action.insId, - events: { - ...state.events, - [action.eventId]: { - ...state.events[action.eventId] || null, - participants: newParticipants - } - } + const newParticipants = action.newParticipants + return { + ...state, + for: action.insId, + events: { + ...state.events, + [action.eventId]: { + ...state.events[action.eventId] || null, + participants: newParticipants + } } + } default: return state; } }; const eventsReq = instituteId => { - return ({ - type: REQUEST_EVENTS, - payload: instituteId - }) + return ({ + type: REQUEST_EVENTS, + payload: instituteId + }) } -const eventsRes = data =>({ - type: RESPONSE_EVENTS, - payload:data +const eventsRes = data => ({ + type: RESPONSE_EVENTS, + payload: data }); -const newActivityRes = activity =>({ - type: RESPONSE_NEW_ACTIVITY, - payload: activity, +const newActivityRes = activity => ({ + type: RESPONSE_NEW_ACTIVITY, + payload: activity, }); -const addParticipantRes = (participants,eventId) =>({ - type: ADD_PARTICIPANT, - participants: participants, - eventId: eventId, +const addParticipantRes = (participants, eventId) => ({ + type: ADD_PARTICIPANT, + participants: participants, + eventId: eventId, }); -const deleteParticipantRes = (eventId,insId,newParticipants) =>({ - type: DELETE_PARTICIPANT, - eventId: eventId, - insId:insId, - newParticipants: newParticipants, +const deleteParticipantRes = (eventId, insId, newParticipants) => ({ + type: DELETE_PARTICIPANT, + eventId: eventId, + insId: insId, + newParticipants: newParticipants, }); -export const getEvents = instituteId => async (dispatch,state) =>{ - if(state().events.status != 'reqEvents'){ - dispatch(eventsReq(instituteId)) - res = firebase.database().ref('events/').child(instituteId).once('value', - snapshot =>{ - dispatch(eventsRes(snapshot.val())); - }) - .then(() => {return 'ok'}) - .catch(error => {return 'err'}); - return res; - } - return 'reqEvents' +export const getEvents = instituteId => async (dispatch, state) => { + if (state().events.status != 'reqEvents') { + dispatch(eventsReq(instituteId)) + res = firebase.database().ref('events/').child(instituteId).once('value', + snapshot => { + dispatch(eventsRes(snapshot.val())); + }) + .then(() => { + return 'ok' + }) + .catch(error => { + return 'err' + }); + return res; + } + return 'reqEvents' } -export const addNewActivity = (activityName,appId,coordinator,date,time,fullFormatDate) => async(dispatch) => { - let res = null - var objActivity = {} - ref = await firebase.database().ref('events/'+coordinator).push() - let newActivity = { - caption: activityName, - coordinator: appId, - institute:coordinator, - date: date, - time: time, - fullFormatDate: fullFormatDate, - id: ref.key - } - objActivity[ref.key] = newActivity; - await ref.set(newActivity) - .then(() => { - dispatch(newActivityRes(objActivity)) - res = 'ok' - }) - .catch(error => {console.log('Data could not be saved.',error); res = 'err'}); - return res +export const addNewActivity = (activityName, appId, coordinator, date, time, fullFormatDate) => async (dispatch) => { + let res = null + var objActivity = {} + ref = await firebase.database().ref('events/' + coordinator).push() + let newActivity = { + caption: activityName, + coordinator: appId, + institute: coordinator, + date: date, + time: time, + fullFormatDate: fullFormatDate, + id: ref.key + } + objActivity[ref.key] = newActivity; + await ref.set(newActivity) + .then(() => { + dispatch(newActivityRes(objActivity)) + res = 'ok' + }) + .catch(error => { + console.log('Data could not be saved.', error); + res = 'err' + }); + return res } -export const deleteActivity = (activityId) => async(dispatch,state) => { - eventsObj = state().events.events - eventsArray = Object.keys(eventsObj).map(key => {return eventsObj[key]}) - currentEvents = filter(eventsArray,(event) => {return event.id !== activityId}) - currentEventsObjects = keyBy(currentEvents, 'id'); - hospitalId = state().user.user.coordinator - newEvents = {} - newEvents[hospitalId] = currentEventsObjects +export const deleteActivity = (activityId) => async (dispatch, state) => { + eventsObj = state().events.events + eventsArray = Object.keys(eventsObj).map(key => { + return eventsObj[key] + }) + currentEvents = filter(eventsArray, (event) => { + return event.id !== activityId + }) + currentEventsObjects = keyBy(currentEvents, 'id'); + hospitalId = state().user.user.coordinator + newEvents = {} + newEvents[hospitalId] = currentEventsObjects - let res = await firebase.database().ref('events/') + let res = await firebase.database().ref('events/') .update(newEvents) .then(() => { - dispatch(eventsRes(currentEventsObjects)) - return 'ok' + dispatch(eventsRes(currentEventsObjects)) + return 'ok' }) .catch(error => { - console.log('Data could not be saved.' + error); - return 'err' + console.log('Data could not be saved.' + error); + return 'err' }); - return res; + return res; } -export const addUserToEvent = (event,appId,fullName) => async(dispatch,state) => { - let res = 'ok' - let newUser = { - appId: appId, - name: fullName, - } - ref = await firebase.database().ref('events/'+event.institute+'/'+event.id) +export const addUserToEvent = (event, appId, fullName, extraParticipants) => async (dispatch, state) => { + let res = 'ok' + let newUser = { + appId: appId, + name: fullName, + extraParticipants + } + + await firebase.database().ref('events/' + event.institute + '/' + event.id) .child('participants') .push() .set(newUser) - .then(() => { - newParticipants = state().events.events[event.id]['participants'] || [] - newParticipantsArray = Object.keys(newParticipants).map(key => {return newParticipants[key]}) - newParticipantsArray.push(newUser) - dispatch(addParticipantRes(newParticipantsArray,event.id,event.institute)) - res = 'ok' - }) - .catch(error => {console.log('Data could not be saved.',error); res = 'err'}); - return res - } + .then(() => { + const newParticipants = state().events.events[event.id]['participants'] || [] + const newParticipantsArray = Object.keys(newParticipants).map(key => newParticipants[key]) + + newParticipantsArray.push(newUser) + dispatch(addParticipantRes(newParticipantsArray, event.id, event.institute)) + + res = 'ok' + }) + .catch(error => { + console.log('Data could not be saved.', error); + + res = 'err' + }) - export const deleteParticipant = (activityId,insId,appId) => async(dispatch,state) => { - let participantsObj=null - let eventsState=false - if(state().events && state().events.for == insId){ - participantsObj = state().events.events[activityId]['participants'] - eventsState = true - } - else{ - await firebase.database().ref('events/'+insId).child(activityId).child('participants').once('value', - async snapshot =>{ participantsObj = await snapshot.val() }) - .catch(error => {console.log('error',error)}); - } - participantsArray = Object.keys(participantsObj).map(key => {return participantsObj[key]}) - currParticipants = filter(participantsArray,(participant) => {return participant.appId !== appId}) - await firebase.database().ref('events/'+insId).child(activityId) - .update({participants:currParticipants}) - .then(() => { - if(eventsState) - dispatch(deleteParticipantRes(activityId,insId,currParticipants)) - }) + return res +} + +export const deleteParticipant = (activityId, insId, appId) => async (dispatch, state) => { + let participantsObj = null + let eventsState = false + if (state().events && state().events.for == insId) { + participantsObj = state().events.events[activityId]['participants'] + eventsState = true + } + else { + await firebase.database().ref('events/' + insId).child(activityId).child('participants').once('value', + async snapshot => { + participantsObj = await snapshot.val() + }) + .catch(error => { + console.log('error', error) + }); + } + participantsArray = Object.keys(participantsObj).map(key => { + return participantsObj[key] + }) + currParticipants = filter(participantsArray, (participant) => { + return participant.appId !== appId + }) + await firebase.database().ref('events/' + insId).child(activityId) + .update({participants: currParticipants}) + .then(() => { + if (eventsState) + dispatch(deleteParticipantRes(activityId, insId, currParticipants)) + }) } \ No newline at end of file diff --git a/src/store/modules/user.js b/src/store/modules/user.js index cca041c..6407c01 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -14,48 +14,51 @@ const NOTIFICATION_SETTINGS = "haprev/user/NOTIFICATION_SETTINGS" const initalState = { user: {}, status: '', - splashStatus:false, - authStatus:'' + splashStatus: false, + authStatus: '' }; export default (state = initalState, action = {}) => { switch (action.type) { case AUTHORIZE_REQ: - return { ...state, authStatus: 'auth_request', user: {} }; + return {...state, authStatus: 'auth_request', user: {}}; case AUTHORIZE_RES: - return { ...state, authStatus: 'user', user: action.payload ,status:canProceed(state) } + return {...state, authStatus: 'user', user: action.payload, status: canProceed(state)} case REGISTER_REQ: - return { ...state, authStatus: 'reg_request', user: {} }; + return {...state, authStatus: 'reg_request', user: {}}; case REGISTER_RES: - return { ...state, authStatus: 'user', user: action.payload ,status:canProceed(state) } + return {...state, authStatus: 'user', user: action.payload, status: canProceed(state)} case NO_USER_FOUND: - return {...state,authStatus:'no_user',status:canProceed(state) } + return {...state, authStatus: 'no_user', status: canProceed(state)} case SPLASH: - return {...state, splashStatus: action.payload,status:canProceed(state)} + return {...state, splashStatus: action.payload, status: canProceed(state)} case SET_MESSAGE_READ: - return {...state, - user: { - ...state.user, - ["messages"]: action.payload - } - }; + return { + ...state, + user: { + ...state.user, + ["messages"]: action.payload + } + }; case UPDATE_NEW_EVENTS: - return {...state, - user: { - ...state.user, - activities:{ - ...state.user.activities || null, - [action.insId]: action.newEvents - } - } - } + return { + ...state, + user: { + ...state.user, + activities: { + ...state.user.activities || null, + [action.insId]: action.newEvents + } + } + } case NOTIFICATION_SETTINGS: - return {...state, - user: { - ...state.user, - settings: action.payload - } - }; + return { + ...state, + user: { + ...state.user, + settings: action.payload + } + }; default: return state; } @@ -76,12 +79,12 @@ const authRes = data => { return tmpRes; }; -const noUserFound = () =>({ +const noUserFound = () => ({ type: NO_USER_FOUND }) -const canProceed = state =>{ - return (!state.user.splashStatus && state.user.authStatus!='auth_request') +const canProceed = state => { + return (!state.user.splashStatus && state.user.authStatus != 'auth_request') } const registerReq = user => ({ @@ -99,16 +102,16 @@ const registerRes = data => { return tmpRes; } -const updateNewEvents = (newEvents,insId) => ({ +const updateNewEvents = (newEvents, insId) => ({ type: UPDATE_NEW_EVENTS, newEvents: newEvents, insId: insId }) export const splash = (display) => ({ - type:SPLASH , - payload:display -}) + type: SPLASH, + payload: display +}) const setMessagesRead = msgId => { return { @@ -118,105 +121,118 @@ const setMessagesRead = msgId => { } const updateNotificationSettings = settings => ({ - type:NOTIFICATION_SETTINGS, - payload:settings + type: NOTIFICATION_SETTINGS, + payload: settings }) -export const authorize = appId => dispatch => { +export const authorize = appId => dispatch => { dispatch(authReq(appId)) - // Is there any user associated with this appId? - firebase.database().ref('users').orderByChild('appId').equalTo(appId).once('value' , - snapshot => { - let dbResList = snapshot.val() - if(dbResList){ - // Get the 1st response - let userId = Object.keys(dbResList)[0] - let dbRes = dbResList[userId] - // Keep the key! - dbRes.userId = userId - dispatch (authRes(dbRes)) - } - else - dispatch (noUserFound()) - }) + + try { + firebase // Is there any user associated with this appId? + .database() + .ref('users') + .orderByChild('appId') + .equalTo(appId) + .once('value') + .then(snapshot => { + let dbResList = snapshot.val() + + if (dbResList) { + let userId = Object.keys(dbResList)[0] // Get the 1st response + let dbRes = dbResList[userId] + + dbRes.userId = userId // Keep the key! + return dispatch(authRes(dbRes)) + } else { + dispatch(noUserFound()) + } + }) + } catch (e) { + console.error(e) + } } -export const register = user => async(dispatch) => { +export const register = user => async (dispatch) => { user.appId = Expo.Constants.deviceId let ref = firebase.database().ref('users') let register = true // Query by phone first... await ref.orderByChild('phone').equalTo(user.phone) - .once('value' , - snapshot => { - let dbResList = snapshot.val() - if (dbResList) { - register = false - } - else { // New user - dispatch(registerReq(user)) - user.userId = ref.push().key - ref.child(user.userId).set(user) - dispatch (registerRes(user)) - } - }) + .once('value', + snapshot => { + let dbResList = snapshot.val() + if (dbResList) { + register = false + } + else { // New user + dispatch(registerReq(user)) + user.userId = ref.push().key + ref.child(user.userId).set(user) + dispatch(registerRes(user)) + } + }) return register } -export const signInWithAnotherDevice = (user) =>async(dispatch) =>{ +export const signInWithAnotherDevice = (user) => async (dispatch) => { let appId = Expo.Constants.deviceId let login = true let dbResUser = null // Query by phone first... - await firebase.database().ref('users').orderByChild('phone').equalTo(user.phone).once('value' , + await firebase.database().ref('users').orderByChild('phone').equalTo(user.phone).once('value', snapshot => { let dbResList = snapshot.val() if (dbResList) { let userId = Object.keys(dbResList)[0] dbResUser = dbResList[userId] - if(dbResUser.password != user.password) + if (dbResUser.password != user.password) login = false else - dbResUser['appId']=appId + dbResUser['appId'] = appId } else // Phone not exist login = false - }) + }) - if(login){ + if (login) { let userId = dbResUser.userId - await firebase.database().ref('users/'+userId).update({appId:appId}) - .then(() => { - dispatch (authRes(dbResUser)) - }) + await firebase.database().ref('users/' + userId).update({appId: appId}) + .then(() => { + dispatch(authRes(dbResUser)) + }) } return login } -export const update = user => dispatch => { +export const update = user => dispatch => { dispatch(registerReq(user)) - let ref = firebase.database().ref('users/'+user.userId) - ref.once('value', + let ref = firebase.database().ref('users/' + user.userId) + ref.once('value', snapshot => { let dbRes = snapshot.val() if (dbRes) { - dispatch (registerRes(dbRes)) + dispatch(registerRes(dbRes)) } else { //handle user not found - dispatch (noUserFound()) + dispatch(noUserFound()) } - }) + }) ref.update(user) return true } -export const readMessage = msgId => async (dispatch,state) => { +export const readMessage = msgId => async (dispatch, state) => { messagesObj = state().user.user.messages; - messagesArray = Object.keys(messagesObj).map(key => { return messagesObj[key] }); - currentMessages = messagesArray.filter(msg => { return msg.id !== msgId }) + messagesArray = Object.keys(messagesObj).map(key => { + return messagesObj[key] + }); + currentMessages = messagesArray.filter(msg => { + return msg.id !== msgId + }) await dispatch(setMessagesRead(currentMessages)); - let res = firebase.database().ref('users/'+state().user.user.userId) + let res = firebase.database().ref('users/' + state().user.user.userId) .update({['messages']: currentMessages}) .then(() => { return 'ok' @@ -225,60 +241,74 @@ export const readMessage = msgId => async (dispatch,state) => { console.log('Data could not be saved.' + error); return 'err' }); - return res; + return res; }; -export const addEventToUser = (userId,event) => async(dispatch,state) => { +export const addEventToUser = (userId, event) => async (dispatch, state) => { let res = 'ok' const insId = event.institute - newActivity = { - caption: event.caption, - fullFormatDate: event.fullFormatDate, - id:event.id, + const newActivity = { + caption: event.caption, + fullFormatDate: event.fullFormatDate, + id: event.id, } - ref = await firebase.database().ref('users/'+userId+'/activities/'+insId) - .child(event.id) - .set(newActivity) - .then(() => { - if(state().user.user.activities) - newEventsObj = state().user.user.activities[insId] || [] - else - newEventsObj = [] - newEventsArray = Object.keys(newEventsObj).map(key => {return newEventsObj[key]}) - newEventsArray.push(newActivity) - dispatch(updateNewEvents(newEventsArray,event.institute)) - res = 'ok' - }) - .catch(error => {console.log('Data could not be saved.',error); res = 'err'}); + let newEventsObj + + await firebase.database().ref('users/' + userId + '/activities/' + insId) + .child(event.id) + .set(newActivity) + .then(() => { + if (state().user.user.activities) + newEventsObj = state().user.user.activities[insId] || [] + else + newEventsObj = [] + const newEventsArray = Object.keys(newEventsObj).map(key => newEventsObj[key]) + newEventsArray.push(newActivity) + dispatch(updateNewEvents(newEventsArray, event.institute)) + res = 'ok' + }) + .catch(error => { + console.log('Data could not be saved.', error); + res = 'err' + }); return res } -export const deleteActivity = (activityId,insId) => async(dispatch,state) => { +export const deleteActivity = (activityId, insId) => async (dispatch, state) => { //function - delete my (current user) activity - let currentUser=state().user.user + let currentUser = state().user.user activitiesObj = currentUser.activities[insId] - activitiesArray = Object.keys(activitiesObj).map(key => {return activitiesObj[key]}) - currActivities = filter(activitiesArray,(activity) => {return activity.id !== activityId}) - await firebase.database().ref('users/'+currentUser.userId).child('activities') - .update({[insId]:currActivities}) - .then(() => { - dispatch(updateNewEvents(currActivities,insId)) - }) + activitiesArray = Object.keys(activitiesObj).map(key => { + return activitiesObj[key] + }) + currActivities = filter(activitiesArray, (activity) => { + return activity.id !== activityId + }) + await firebase.database().ref('users/' + currentUser.userId).child('activities') + .update({[insId]: currActivities}) + .then(() => { + dispatch(updateNewEvents(currActivities, insId)) + }) //Check for more activities - var numActivities=0 - if(currActivities.length==0){ - stateActivities=state().user.user.activities - var ins=[] - stateActivitiesArray = Object.keys(stateActivities).map(key => {ins.push(key); return stateActivities[key]}) + var numActivities = 0 + if (currActivities.length == 0) { + stateActivities = state().user.user.activities + var ins = [] + stateActivitiesArray = Object.keys(stateActivities).map(key => { + ins.push(key); + return stateActivities[key] + }) for (var index in ins) - allActivities = Object.keys(stateActivitiesArray[index]).map(key => {numActivities++}) + allActivities = Object.keys(stateActivitiesArray[index]).map(key => { + numActivities++ + }) } - if(currActivities.length==0 && numActivities == 0) + if (currActivities.length == 0 && numActivities == 0) return 'empty' } -export const updateNotificationSettingUser = (settings) => async(dispatch,state) => { - let res = firebase.database().ref('users/'+state().user.user.userId).update({settings}) +export const updateNotificationSettingUser = (settings) => async (dispatch, state) => { + let res = firebase.database().ref('users/' + state().user.user.userId).update({settings}) .then(() => { dispatch(updateNotificationSettings(settings)); return 'ok' @@ -290,17 +320,17 @@ export const updateNotificationSettingUser = (settings) => async(dispatch,state) return res; } -export const updateUserSatet = () => async(dispatch,state) => { - let ref = firebase.database().ref('users/'+state().user.user.userId) - ref.once('value', +export const updateUserSatet = () => async (dispatch, state) => { + let ref = firebase.database().ref('users/' + state().user.user.userId) + ref.once('value', snapshot => { let dbRes = snapshot.val() if (dbRes) { - dispatch (registerRes(dbRes)) + dispatch(registerRes(dbRes)) } else { //handle user not found - dispatch (noUserFound()) + dispatch(noUserFound()) } - }) + }) return true } \ No newline at end of file diff --git a/src/views/splash/Splash.js b/src/views/splash/Splash.js index 92e3a3e..5af9888 100644 --- a/src/views/splash/Splash.js +++ b/src/views/splash/Splash.js @@ -1,41 +1,37 @@ -import React, { Component } from 'react' -import { connect } from 'react-redux' -import SplashView from './SplashView' -import {authorize,splash} from '../../store/modules/user' -import { getInstitutes } from '../../store/modules/Institutes' -import AppNav from '../../nav/AppNav' -import RegisterNav from '../../nav/RegisterNav' +import React, { Component } from "react"; +import { connect } from "react-redux"; +import SplashView from "./SplashView"; +import { authorize, splash } from "../../store/modules/user"; +import { getInstitutes } from "../../store/modules/Institutes"; +import AppNav from "../../nav/AppNav"; +import RegisterNav from "../../nav/RegisterNav"; -class Splash extends Component{ +class Splash extends Component { + async componentDidMount() { + this.props.splash(true); + await this.props.getInstitutes(); + this.props.authorize(Expo.Constants.deviceId); + this.props.splash(false); + } - async componentDidMount(){ - this.props.splash(true) - await this.props.getInstitutes() - this.props.authorize(Expo.Constants.deviceId) - setTimeout( () => this.props.splash(false), 2200) - } + render() { + const { splashStatus, userStatus } = this.props; - render(){ - const { splashStatus, userStatus } = this.props - if (!splashStatus){ - if (userStatus=='user') - return ( - - ) - if (userStatus=='no_user') - return ( - - ) - } - return ( - - ) + if (!splashStatus) { + if (userStatus === "user") return ; + if (userStatus === "no_user") return ; } + + return ; + } } const mapStateToProps = state => ({ - splashStatus: state.user.splashStatus, - userStatus: state.user.authStatus -}) + splashStatus: state.user.splashStatus, + userStatus: state.user.authStatus +}); -export default connect(mapStateToProps,{authorize,splash,getInstitutes})(Splash) \ No newline at end of file +export default connect( + mapStateToProps, + { authorize, splash, getInstitutes } +)(Splash);