+ {this.props.msgs.map(chat => {
if (
- chat.senderID === this.props.currentUser.userID &&
- chat.receiverID === this.props.receiver.userID
+ chat.senderId === this.props.currentUser.userID &&
+ chat.receiverId === this.props.receiver.userID
) {
return (
@@ -278,8 +302,8 @@ class AccountScreen extends Component {
);
} else if (
- chat.senderID === this.props.receiver.userID &&
- chat.receiverID === this.props.currentUser.userID
+ chat.senderId === this.props.receiver.userID &&
+ chat.receiverId === this.props.currentUser.userID
) {
return (
@@ -316,7 +340,7 @@ const mapStateToProps = state => {
const mapDispatchToProps = dispatch =>
bindActionCreators(
{
- getUser,
+ getUsers,
getCurrentUser,
changeUser,
addMsg,
@@ -327,7 +351,8 @@ const mapDispatchToProps = dispatch =>
getUserChats,
filterUserChats,
changeSelectedChat,
- changeReciever
+ changeReciever,
+ updateMsgs
},
dispatch
);
diff --git a/src/components/headerNav/headerNav.js b/src/components/headerNav/headerNav.js
index 6523c24..7691799 100644
--- a/src/components/headerNav/headerNav.js
+++ b/src/components/headerNav/headerNav.js
@@ -1,147 +1,255 @@
-import './headerNav.css';
-import React, { Component } from 'react';
-import { bindActionCreators } from 'redux';
-import { connect } from 'react-redux';
-import { push } from 'react-router-redux';
-import { Route } from 'react-router-dom';
-import IconButton from 'material-ui/IconButton';
-import IconMenu from 'material-ui/IconMenu';
-import MenuItem from 'material-ui/MenuItem';
-
-import {
- getUser,
- changeUser,
- getCurrentUser,
- registerUser,
- loginUser,
- logOut
-} from '../../reducers/Account';
-
-import { addMsg, getMsgs, getChats } from '../../reducers/Chat';
-
-class HeaderNav extends Component {
- constructor(props) {
- super(props);
- this.props = props;
- try {
- props.getCurrentUser();
- } catch (exc) {
- window.location.href = '/';
- }
- }
-
- handleChange = (event, logged) => {
- this.setState({ logged: logged });
- };
-
- changePage(value) {
- push('/' + value);
- }
-
- styles = {
- 'padding-bottom': '2px'
- };
-
- render() {
- return (
-
-
(
- {
- history.push('/accountScreen');
- }}
- >
-
-
- )}
- />
-
- {}}>
-
-
-
-
-
- {this.props.getCurrentUser ? (
-
- ) : (
-
- )}
-
-
-
-
-
- }
- targetOrigin={{ horizontal: 'right', vertical: 'top' }}
- anchorOrigin={{ horizontal: 'right', vertical: 'top' }}
- >
- (
-
-
-
-
-
-
- );
- }
-}
-
-const mapStateToProps = state => {
- return {
- chats: state.Chat.chats,
- msgs: state.Chat.msgs,
- currentUser: state.Account.currentUser,
- users: state.Account.users,
- receiver: state.Account.receiver
- };
-};
-
-const mapDispatchToProps = dispatch =>
- bindActionCreators(
- {
- getUser,
- getCurrentUser,
- changeUser,
- addMsg,
- getMsgs,
- registerUser,
- loginUser,
- getChats,
- logOut
- },
- dispatch
- );
-
-export default connect(mapStateToProps, mapDispatchToProps)(HeaderNav);
+import './headerNav.css';
+import React, { Component } from 'react';
+import { bindActionCreators } from 'redux';
+import { connect } from 'react-redux';
+import { push } from 'react-router-redux';
+import { Route } from 'react-router-dom';
+import IconButton from 'material-ui/IconButton';
+import IconMenu from 'material-ui/IconMenu';
+import MenuItem from 'material-ui/MenuItem';
+import Popover from 'material-ui/Popover';
+import Menu from 'material-ui/Menu';
+
+import Avatar from 'material-ui/Avatar';
+import { List, ListItem } from 'material-ui/List';
+import Subheader from 'material-ui/Subheader';
+
+import FlatButton from 'material-ui/FlatButton/FlatButton';
+
+import TextField from 'material-ui/TextField';
+
+import { Card, CardActions, CardHeader, CardMedia, CardTitle, CardText } from 'material-ui/Card';
+
+import {
+ getUsers,
+ getUser,
+ changeUser,
+ getCurrentUser,
+ registerUser,
+ loginUser,
+ logOut
+} from '../../reducers/Account';
+
+import { addMsg, getMsgs, getChats } from '../../reducers/Chat';
+
+class HeaderNav extends Component {
+ constructor(props) {
+ super(props);
+ this.props = props;
+ props.getUsers();
+ try {
+ props.getCurrentUser();
+ } catch (exc) {
+ window.location.href = '/';
+ }
+ }
+
+ state = {
+ open: false,
+ txtSearch: ''
+ };
+
+ handleChange = (event, logged) => {
+ this.setState({ logged: logged });
+ };
+
+ changePage(value) {
+ push('/' + value);
+ }
+
+ styles = {
+ 'padding-bottom': '2px'
+ };
+
+ render() {
+ return (
+
+
(
+ {
+ this.setState({ open: true });
+ }}
+ onClick={() => {
+ history.push('/accountScreen');
+ }}
+ >
+
+
+ )}
+ />
+
+
+
+
+
+
+ {
+ this.setState({ open: false });
+ }}
+ />
+
+ {
+ if (e.key === 'Enter' && e.target.value.length > 0) {
+ e.target.value = '';
+ }
+ this.setState({ txtSearch: e.target.value });
+ }}
+ style={{
+ padding: '1px 0px 1px 25px',
+ width: '100%'
+ }}
+ />
+
+
+
+ Users that contains : {this.state.txtSearch}
+
+ this.props.users.map(
+ user =>
+ user.userEmail.indexOf(this.state.txtSearch) !== -1 ||
+ user.userSurname.indexOf(this.state.txtSearch) !== -1 ||
+ user.userName.indexOf(this.state.txtSearch) !== -1 ? (
+ }
+ primaryText={user.userSurname + ' ' + user.userName}
+ secondaryText={user.userEmail}
+ onClick={e => {
+ let found = false;
+ for (
+ let i = 0;
+ i < this.props.msgs.length;
+ i++
+ ) {
+ if (
+ (this.props.msgs[i].senderID ===
+ this.props.currentUser.userID &&
+ this.props.msgs[i].receiverID ===
+ user.userID) ||
+ (this.props.msgs[i].receiverID ===
+ this.props.currentUser.userID &&
+ this.props.msgs[i].senderID ===
+ user.userID)
+ ) {
+ found = true;
+ }
+ }
+ if (!found) {
+ this.props.addMsg(
+ 'Hi',
+ this.props.currentUser.userID,
+ user.userID
+ );
+ }
+ history.push('/accountScreen');
+ this.setState({ open: false });
+ }}
+ />
+ ) : (
+
+ )
+ )
+ }
+ />
+
+
+
+ {}}>
+
+
+
+
+
+ {this.props.getCurrentUser ? (
+
+ ) : (
+
+ )}
+
+
+
+
+
+ }
+ targetOrigin={{ horizontal: 'right', vertical: 'top' }}
+ anchorOrigin={{ horizontal: 'right', vertical: 'top' }}
+ >
+ (
+ {
+ history.push('/settings');
+ }}
+ />
+ )}
+ />
+ (
+ {
+ this.props.logOut();
+ history.push('/');
+ }}
+ />
+ )}
+ />
+
+
+
+
+
+
+ );
+ }
+}
+
+const mapStateToProps = state => {
+ return {
+ chats: state.Chat.chats,
+ msgs: state.Chat.msgs,
+ currentUser: state.Account.currentUser,
+ users: state.Account.users,
+ receiver: state.Account.receiver
+ };
+};
+
+const mapDispatchToProps = dispatch =>
+ bindActionCreators(
+ {
+ getUsers,
+ getUser,
+ getCurrentUser,
+ changeUser,
+ addMsg,
+ getMsgs,
+ registerUser,
+ loginUser,
+ getChats,
+ logOut
+ },
+ dispatch
+ );
+
+export default connect(mapStateToProps, mapDispatchToProps)(HeaderNav);
diff --git a/src/components/settings/settings.js b/src/components/settings/settings.js
index 958b96e..83ee2c7 100644
--- a/src/components/settings/settings.js
+++ b/src/components/settings/settings.js
@@ -1,264 +1,265 @@
-import React, { Component } from 'react';
-import { GridList, GridTile } from 'material-ui/GridList';
-import IconButton from 'material-ui/IconButton';
-import ActionDone from 'material-ui/svg-icons/action/done';
-import EditorModeEdit from 'material-ui/svg-icons/editor/mode-edit';
-import TextField from 'material-ui/TextField';
-
-import { Route } from 'react-router-dom';
-
-import { bindActionCreators } from 'redux';
-import { connect } from 'react-redux';
-import HeaderNav from '../headerNav/headerNav';
-
-import '../headerNav/headerNav.css';
-import './settings.css';
-import {
- getUser,
- changeUser,
- getCurrentUser,
- registerUser,
- loginUser,
- logOut
-} from '../../reducers/Account';
-
-import { addMsg, getMsgs, getChats } from '../../reducers/Chat';
-
-const styles = {
- gridList: {
- width: '100%',
- overflowX: 'auto'
- }
-};
-
-class settings extends Component {
- changeImg = e => {
- console.log(e.target);
- console.log(e.target.files[0]);
-
- if (e.target.files && e.target.files[0]) {
- let reader = new FileReader();
- reader.onload = event => {
- this.setState({ profilephoto: event.target.result });
- };
- reader.readAsDataURL(e.target.files[0]);
- }
- this.setState({ hasimg: true });
- this.props.changeUser({ userImg: this.state.profilephoto });
-
- this.props.getCurrentUser();
- this.forceUpdate();
- };
-
- constructor(props) {
- super(props);
- try {
- props.getCurrentUser();
-
- var JIU = this.props.currentUser.userID;
- } catch (exc) {
- window.location.href = '/';
- }
- }
-
- saveChanges() {
- this.props.changeUser({
- userName: this.state.user.name,
- userSurname: this.state.user.surname
- });
-
- this.forceUpdate();
- this.props.getCurrentUser();
- }
-
- state = {
- editingEmail: false,
- editingNames: false,
- profilephoto: '',
- user: '',
- hasimg: true
- };
-
- render() {
- return (
-
-
-
-
-
-
-
-
-
-
-
{
- this.changeImg(e);
- }}
- style={inputimg}
- />
-
-
-
-
-
-
- {this.state.editingNames ? (
-
-
{
- var newSelected = this.props.currentUser;
- newSelected.surname = e.target.value.split(
- ' '
- )[0];
- newSelected.name = e.target.value.split(' ')[1];
- this.setState({ user: newSelected });
- }}
- hintText="Surname Name"
- />
- {
- this.saveChanges();
- this.setState({ editingNames: false });
- }}
- iconStyle={styles.mediumIcon}
- style={styles.medium}
- >
-
-
-
- ) : (
-
-
-
- {
- this.setState({ editingNames: true });
- }}
- iconStyle={styles.mediumIcon}
- style={styles.medium}
- >
-
-
-
- )}
-
-
-
-
-
- {this.state.editingEmail ? (
-
-
{
- var newSelected = this.props.currentUser;
- newSelected.email = e.target.value;
- this.setState({ user: newSelected });
- }}
- hintText="Email Address"
- />
- {
- this.saveChanges();
- this.setState({ editingEmail: false });
- }}
- iconStyle={styles.mediumIcon}
- style={styles.medium}
- >
-
-
-
- ) : (
-
-
- {
- this.setState({ editingEmail: true });
- }}
- iconStyle={styles.mediumIcon}
- style={styles.medium}
- >
-
-
-
- )}
-
-
-
-
- (
- {
- history.push('/accountScreen');
- }}
- label="Done"
- secondary={true}
- >
-
-
- )}
- />
-
-
-
-
-
-
- );
- }
-}
-
-const inputimg = {
- opacity: 0,
- position: 'absolute',
- top: 0,
- bottom: 0,
- left: 0,
- right: 0,
- width: '100%'
-};
-
-const mapStateToProps = state => {
- return {
- chats: state.Chat.chats,
- msgs: state.Chat.msgs,
- currentUser: state.Account.currentUser,
- users: state.Account.users,
- receiver: state.Account.receiver
- };
-};
-
-const mapDispatchToProps = dispatch =>
- bindActionCreators(
- {
- getUser,
- getCurrentUser,
- changeUser,
- addMsg,
- getMsgs,
- registerUser,
- loginUser,
- getChats,
- logOut
- },
- dispatch
- );
-
-export default connect(mapStateToProps, mapDispatchToProps)(settings);
+import React, { Component } from 'react';
+import { GridList, GridTile } from 'material-ui/GridList';
+import IconButton from 'material-ui/IconButton';
+import ActionDone from 'material-ui/svg-icons/action/done';
+import EditorModeEdit from 'material-ui/svg-icons/editor/mode-edit';
+import TextField from 'material-ui/TextField';
+
+import { Route } from 'react-router-dom';
+
+import { bindActionCreators } from 'redux';
+import { connect } from 'react-redux';
+import HeaderNav from '../headerNav/headerNav';
+
+import '../headerNav/headerNav.css';
+
+import {
+ getUser,
+ changeUser,
+ getCurrentUser,
+ registerUser,
+ loginUser,
+ logOut
+} from '../../reducers/Account';
+
+import { addMsg, getMsgs, getChats } from '../../reducers/Chat';
+
+const styles = {
+ gridList: {
+ width: '100%',
+ overflowX: 'auto'
+ }
+};
+
+class settings extends Component {
+ changeImg = e => {
+ console.log(e.target);
+ console.log(e.target.files[0]);
+
+ if (e.target.files && e.target.files[0]) {
+ let reader = new FileReader();
+ reader.onload = event => {
+ this.setState({ profilephoto: event.target.result });
+ };
+ reader.readAsDataURL(e.target.files[0]);
+ }
+ this.setState({ hasimg: true });
+ this.props.changeUser({ userImg: this.state.profilephoto });
+
+ this.props.getCurrentUser();
+ this.forceUpdate();
+ };
+
+ constructor(props) {
+ super(props);
+ props.getCurrentUser();
+ try {
+ props.getCurrentUser();
+ var id = props.currentUser.userID;
+ } catch (exc) {
+ window.location.href = '/';
+ }
+ }
+
+ saveChanges() {
+ this.props.changeUser({
+ userName: this.state.user.name,
+ userSurname: this.state.user.surname
+ });
+
+ this.forceUpdate();
+ this.props.getCurrentUser();
+ }
+
+ state = {
+ editingEmail: false,
+ editingNames: false,
+ profilephoto: '',
+ user: '',
+ hasimg: true
+ };
+
+ render() {
+ return (
+
+
+
+
+
+
+
+
{
+ this.changeImg(e);
+ }}
+ style={inputimg}
+ />
+
+
+
+ {/*
+
+
*/}
+
+
+
+
+ {this.state.editingNames ? (
+
+
{
+ var newSelected = this.props.currentUser;
+ newSelected.surname = e.target.value.split(' ')[0];
+ newSelected.name = e.target.value.split(' ')[1];
+ this.setState({ user: newSelected });
+ }}
+ hintText="Surname Name"
+ />
+ {
+ this.saveChanges();
+ this.setState({ editingNames: false });
+ }}
+ iconStyle={styles.mediumIcon}
+ style={styles.medium}
+ >
+
+
+
+ ) : (
+
+
+
+
+ {
+ this.setState({ editingNames: true });
+ }}
+ iconStyle={styles.mediumIcon}
+ style={styles.medium}
+ >
+
+
+
+ )}
+
+
+
+
+
+ {this.state.editingEmail ? (
+
+
{
+ var newSelected = this.props.currentUser;
+ newSelected.email = e.target.value;
+ this.setState({ user: newSelected });
+ }}
+ hintText="Email Address"
+ />
+ {
+ this.saveChanges();
+ this.setState({ editingEmail: false });
+ }}
+ iconStyle={styles.mediumIcon}
+ style={styles.medium}
+ >
+
+
+
+ ) : (
+
+
+ {
+ this.setState({ editingEmail: true });
+ }}
+ iconStyle={styles.mediumIcon}
+ style={styles.medium}
+ >
+
+
+
+ )}
+
+
+
+
+ (
+ {
+ history.push('/accountScreen');
+ }}
+ label="Done"
+ secondary={true}
+ >
+
+
+ )}
+ />
+
+
+
+ );
+ }
+}
+
+const inputimg = {
+ opacity: 0,
+ position: 'absolute',
+ top: 0,
+ bottom: 0,
+ left: 0,
+ right: 0,
+ width: '100%'
+};
+
+const mapStateToProps = state => {
+ return {
+ chats: state.Chat.chats,
+ msgs: state.Chat.msgs,
+ currentUser: state.Account.currentUser,
+ users: state.Account.users,
+ receiver: state.Account.receiver
+ };
+};
+
+const mapDispatchToProps = dispatch =>
+ bindActionCreators(
+ {
+ getUser,
+ getCurrentUser,
+ changeUser,
+ addMsg,
+ getMsgs,
+ registerUser,
+ loginUser,
+ getChats,
+ logOut
+ },
+ dispatch
+ );
+
+export default connect(mapStateToProps, mapDispatchToProps)(settings);
diff --git a/src/reducers/Account.js b/src/reducers/Account.js
index a2436c2..a91ecad 100644
--- a/src/reducers/Account.js
+++ b/src/reducers/Account.js
@@ -1,3 +1,5 @@
+import axios from 'axios';
+
export const USER_ACCOUNT = 'Account/USER_ACCOUNT';
export const REGISTER_USER_ACCOUNT = 'Account/REGISTER_USER_ACCOUNT';
export const LOGIN_USER_ACCOUNT = 'Account/LOGIN_USER_ACCOUNT';
@@ -8,6 +10,9 @@ export const CURRENT_USER_CHATS = 'Account/CURRENT_USER_CHATS';
export const FILTER_USER_CHATS = 'Account/FILTER_USER_CHATS';
export const CHANGE_SELECTED_CHAT = 'Account/CHANGE_SELECTED_CHAT';
export const CHANGE_RECIEVER = 'Account/CHANGE_RECIEVER';
+export const ALL_USERS = 'Account/ALL_USERS';
+
+const link = 'http://192.168.0.20:52499';
const initialState = {
currentUserChats: [],
@@ -22,63 +27,31 @@ const initialState = {
userImg:
'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1'
},
- users: [
- {
- userID: 20,
- userName: 'Joe',
- userSurname: 'Sirwali',
- userPassword: 'joe',
- userEmail: 'joe@gmail.com',
- userImg:
- 'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1'
- },
- {
- userID: 10,
- userName: 'Takie',
- userSurname: 'Ndou',
- userEmail: 'mulavhe@gmail.com',
- userPassword: 'joseph',
- userImg:
- 'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1',
- latestMessage: ''
- },
- {
- userID: 2,
- userName: 'Rendani',
- userSurname: 'Sirwali',
- userEmail: 'mulavhe@gmail.com',
- userImg:
- 'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1'
- },
- {
- userID: 4,
- userName: 'Joseph',
- userSurname: 'Sirwali',
- userEmail: 'mulavhe@gmail.com',
- userImg:
- 'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1'
- },
- {
- userID: 0,
- userName: 'Joseph',
- userSurname: 'Sirwali',
- userEmail: 'mulavhe@gmail.com',
- userImg:
- 'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1'
- },
- {
- userID: 10,
- userName: 'Victim',
- userSurname: 'That',
- userEmail: 'mulavhe@gmail.com',
- userImg:
- 'https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fpbs.twimg.com%2Fprofile_images%2F1396485177%2Ftrevor_cartoon_profile.jpg&f=1'
- }
- ]
+ users: []
};
export default (state = initialState, action) => {
switch (action.type) {
+ case ALL_USERS:
+ var users = [];
+
+ for (let i = 0; i < action.users.length; i++) {
+ users.push({
+ userID: action.users[i].id,
+ userName: action.users[i].name,
+ userSurname: action.users[i].surname,
+ userImg: action.users[i].image,
+ userEmail: action.users[i].email
+ });
+ }
+
+ console.log(users.length + ' users in db');
+
+ return {
+ ...state,
+ users: users
+ };
+
case REGISTER_USER_ACCOUNT:
var user = null;
user = action.user;
@@ -90,15 +63,15 @@ export default (state = initialState, action) => {
};
case LOGIN_USER_ACCOUNT:
- user = null;
- for (let i = 0; i < state.users.length; i++) {
- if (
- state.users[i].userEmail === action.user.userEmail &&
- state.users[i].userPassword === action.user.userPassword
- ) {
- user = state.users[i];
- }
- }
+ var user = null;
+ user = action.user;
+ user.userID = action.user.id;
+ user.userName = action.user.name;
+ user.userSurname = action.user.surname;
+ user.userEmail = action.user.email;
+ user.userImg = action.user.image;
+
+ state.currentUser = user;
return {
...state,
@@ -111,16 +84,12 @@ export default (state = initialState, action) => {
};
case USER_ACCOUNT:
- user = null;
- for (let i = 0; i < state.users.length; i++) {
- if (state.users[i].userID === action.ID) {
- user = state.users[i];
- }
- }
-
+ console.log('here ....');
+ console.log('here ....');
+ console.log(action.user);
return {
...state,
- currentUser: user
+ currentUser: action.user
};
case CHANGE_USER_ACCOUNT:
@@ -146,43 +115,37 @@ export default (state = initialState, action) => {
};
case CURRENT_USER_CHATS:
- console.log('dispatching CURRENT_USER_CHATS');
- let msgs = action.allMsgs;
- let userChats = [];
-
- for (let msg of msgs) {
- if (msg.recieverID === state.currentUser.userID) {
- for (let user of state.users) {
- if (msg.senderID === user.userID) {
- if (userChats.length > 0) {
- let duplicatefound = false;
- for (let userDet of userChats) {
- if (userDet.userID === user.userID) {
- userDet = user;
- userDet.latestMessage = msg.text;
- userDet.selected = false;
- duplicatefound = true;
- }
- }
- if (!duplicatefound) {
- user.latestMessage = msg.text;
- user.selected = false;
- userChats.push(user);
- }
- } else {
- user.latestMessage = msg.text;
- user.selected = false;
- userChats.push(user);
- }
- }
- }
- }
- }
- console.log(userChats);
+ console.log("Are you sure")
+ var users = state.users;
+ for(let i=0;i
{
export const getUser = userID => {
return dispatch => {
- dispatch({ type: USER_ACCOUNT, ID: userID });
+ axios
+ .get(link + 'api/Users/' + userID)
+ .then(function(response) {
+ dispatch({ type: USER_ACCOUNT, user: response.data });
+ });
};
};
@@ -234,12 +201,56 @@ export const changeSelectedChat = tempChats => {
};
};
-export const getUserChats = allMsgs => {
+export const getUsers = () => {
return dispatch => {
- dispatch({ type: CURRENT_USER_CHATS, allMsgs: allMsgs });
+ console.log('About to');
+ axios
+ .get(link + '/api/Users/GetAllUsers')
+ .then(function(response) {
+ dispatch({ type: ALL_USERS, users: response.data });
+ })
+ .catch(function(error) {});
};
};
+export const getUserChats = (userID) => {
+ return dispatch => {
+ axios
+ .get(link + '/api/Chats/getChatsforuser/' + userID)
+ .then(function(response) {
+ var userChats = []
+ var chat = response.data[0];
+ chat.latestMessage = response.data[0].text;
+ chat.selected = false;
+ userChats.push(chat);
+
+ for(let i=1;i {
return dispatch => {
dispatch({ type: CURRENT_USER_ACCOUNT });
@@ -259,9 +270,20 @@ export const registerUser = user => {
};
export const loginUser = user => {
- return dispatch => {
- dispatch({ type: LOGIN_USER_ACCOUNT, user: user });
- };
+ return dispatch => {
+ console.log('About to');
+ axios
+ .get(link + '/api/Users/GetAllUsers')
+ .then(function(response) {
+ for(let i =0;i {
diff --git a/src/reducers/Chat.js b/src/reducers/Chat.js
index 24181fa..47e3189 100644
--- a/src/reducers/Chat.js
+++ b/src/reducers/Chat.js
@@ -1,125 +1,16 @@
import { getUser } from './Account';
+import axios from 'axios';
export const GET_CHATS = 'Chat/GET_CHATS';
export const GET_MSGS = 'Chat/GET_MSGS';
export const ADD_MSG = 'Chat/ADD_MSG';
+export const UPDATE_MSGS = 'Chat/UPDATE_MSGS'
+
+const link = 'http://192.168.0.20:52499';
const initialState = {
msgs: [{}],
- allMsgs: [
- {
- senderID: 20,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from me to him'
- },
- {
- senderID: 20,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from me to him'
- },
- {
- senderID: 20,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from me to him'
- },
- {
- senderID: 20,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from me to him'
- },
- {
- senderID: 20,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from me to him'
- },
- {
- senderID: 2,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to someone'
- },
- {
- senderID: 2,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to someone'
- },
- {
- senderID: 2,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to someone'
- },
- {
- senderID: 2,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to someone'
- },
- {
- senderID: 2,
- receiverID: 10,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to someone'
- },
- {
- senderID: 2,
- recieverID: 20,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from her to me'
- },
- {
- senderID: 10,
- recieverID: 20,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to me'
- },
- {
- senderID: 10,
- recieverID: 20,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from him to me again'
- },
- {
- senderID: 10,
- receiverID: 1,
- sender: '',
- receiver: '',
- timeSent: new Date(),
- text: 'This is from me to someone'
- }
- ],
+ allMsgs: [{}],
currentChat: [],
chats: []
};
@@ -127,49 +18,37 @@ const initialState = {
export default (state = initialState, action) => {
switch (action.type) {
case ADD_MSG:
- var newMsg = {
- senderID: action.senderID,
- receiverID: action.receiverID,
- sender: getUser(action.senderID),
- receiver: getUser(action.receiverID),
- timeSent: new Date(),
- text: action.msg
- };
-
- state.allMsgs.push(newMsg);
- var chat = state.allMsgs.filter(msg => {
- return (
- (msg.senderID === action.senderID && msg.receiverID === action.receiverID) ||
- (msg.senderID === action.receiverID && msg.senderID === action.receiverID)
- );
- });
-
+ console.log('Check your log file');
return {
- ...state,
- msgs: chat
+ ...state
};
case GET_CHATS:
- var chats = state.allMsgs.filter(msg => {
- return msg.senderID === action.senderID;
- });
-
+ var chats = action.chats;
+ state.chats = chats;
+ console.log('got ' + chats.length);
return {
...state,
chats: chats
};
+ case UPDATE_MSGS:
+ console.log(state.reciever.receiverId + " is their id")
+ state.msgs = action.msgs;
+ return {
+ ...state,
+ msgs:action.msgs
+ }
+
case GET_MSGS:
- var msgs = state.allMsgs.filter(msg => {
- return (
- (msg.senderID === action.senderID && msg.receiverID === action.receiverID) ||
- (msg.senderID === action.receiverID && msg.senderID === action.receiverID)
- );
- });
+ //state.allMsgs = msgs;
+ state.allMsgs = action.msgs;
+ state.msgs = action.msgs;
return {
...state,
- msgs: msgs
+ msgs: action.msgs,
+ allMsgs:action.msgs
};
default:
@@ -179,30 +58,86 @@ export default (state = initialState, action) => {
export const getChats = userID => {
return dispatch => {
- dispatch({
- type: GET_CHATS,
- senderID: userID
- });
+ var msgs = [];
+ axios
+ .get(link +
+ '/api/chats/getchatsforuser/' +
+ userID
+ )
+ .then(function(response) {
+ dispatch({
+ type: GET_CHATS,
+ senderId: userID,
+ chats: response.data
+ });
+ })
+ .catch(function(error) {});
};
};
-export const getMsgs = (senderID, receiverID) => {
+export const getMsgs = (senderId, receiverId) => {
return dispatch => {
- dispatch({
- type: GET_MSGS,
- senderID: senderID,
- receiverID: receiverID
- });
+ var msgs = [];
+ axios
+ .get(link + '/api/chats')
+ .then(function(response) {
+ dispatch({
+ type: GET_MSGS,
+ senderId: senderId,
+ receiverId: receiverId,
+ msgs: response.data
+ });
+ })
+ .catch(function(error) {
+ console.log('Error : sasasaffnasjfnaskjfasf ');
+ console.log(error);
+ });
};
};
-export const addMsg = (msg, senderID, receiverID) => {
+export const updateMsgs = (senderID,receiverID) => {
+ return dispatch => {
+ var msgs = [];
+ axios.get(link + '/api/GetChatsBySR/' + senderID + '/' + receiverID )
+ .then(function(response) {
+ dispatch({
+ type: UPDATE_MSGS,
+ msgs: response.data
+ });
+ })
+ .catch(function(error) {
+ console.log('Error : sasasaffnasjfnaskjfasf ');
+ console.log(error);
+ });
+ };
+}
+
+export const addMsg = (msg, senderId, receiverId) => {
return dispatch => {
- dispatch({
- type: ADD_MSG,
- msg: msg,
- senderID: senderID,
- receiverID: receiverID
- });
+ var newMsg = {
+ SenderId: senderId,
+ ReceiverId: receiverId,
+ Text: msg
+ };
+
+ if(receiverId != senderId){
+ axios.post(link + '/api/chats', newMsg).then(function(response) {
+ console.log(response);
+ console.log('sent');
+
+ dispatch({
+ type: ADD_MSG,
+ msg: msg,
+ results: response.data
+ });
+
+ getMsgs(senderId, receiverId);
+ }).catch(function(error) {
+ console.log('Error : safnasjfnaskjfasf ');
+ console.log(error);
+ });
+ }else{
+
+ }
};
};