Styled Components: How can I rotate an svg icon on click? - styled-components

const IconButtonWrapper = styled((props: IconWrapperProps) => {
return <IconButton {...props} />;
})`
float: right;
transform: rotate(0deg);
overflow: hidden;
transition: all 0.3s ease-out;
${(props) =>
props.rotate &&
css`
transform: rotate(180deg);
`};
`;
Right now, I'm trying it with this and it's not quite correct. I currently have props.rotate as a boolean setup with a click handler to rotate if on click. What should I do?

It's unclear from your example how you pass "rotate" prop down to styled component, so I did a little refactoring on your code and added a container component to hold rotation state:
https://codesandbox.io/s/wyx6pqj13w
Hope this helps!
Code in the link is shown here
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';
import './styles.css';
const IconButton = () => <button>Icon</button>;
const IconButtonWrapper = styled.div`
float: right;
transform: rotate(0deg);
overflow: hidden;
transition: all 0.3s ease-out;
transform: ${(props) => (props.rotate ? `rotate(180deg)` : '')};
`;
class IconButtonContainer extends Component {
state = {
rotate: false,
};
handleClick = () =>
this.setState(
(prevState) => ({ rotate: !prevState.rotate }),
() => console.log(this.state.rotate)
);
render() {
return (
<IconButtonWrapper rotate={this.state.rotate} onClick={this.handleClick}>
<IconButton />
</IconButtonWrapper>
);
}
}
const App = () => {
return (
<div className="App">
<IconButtonContainer />
</div>
);
};
const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);

Related

Can't return currentVideio value using react and Redux-toolkit

My project is a youtube clone site and I'm trying to return video data from Back-End using Node.js, Axios and Redux-Toolkit
But the response value is always null and when I don't use dispatch, I can return the data from the backend and I don't know why. I tried a lot, but I can't find a solution
I need your help
thanks
Store.js
import { configureStore, combineReducers } from '#reduxjs/toolkit'
import userReducer from '../redux/userSlice.js'
import videoReducer from '../redux/videoSlice.js'
import storage from 'redux-persist/lib/storage'
import { PersistGate } from 'redux-persist/integration/react'
import { persistStore, persistReducer, FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER, } from 'redux-persist'
const persistConfig = {
key: 'root',
version: 1,
storage,
}
const rootReducer = combineReducers({ user: userReducer, video: videoReducer })
const persistedReducer = persistReducer(persistConfig, rootReducer)
export const store = configureStore({
reducer: persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
})
export const persistor = persistStore(store)
videoSlice.js
import { createSlice } from "#reduxjs/toolkit";
const initialState = {
currentVideo: null,
loading: false,
error: false,
};
export const videoSlice = createSlice({
name: "video",
initialState,
reducers: {
fetchStart: (state) => {
state.loading = true;
},
fetchSuccess: (state, action) => {
state.loading = false;
state.currentVideo = action.payload;
},
fetchFailure: (state) => {
state.loading = false;
state.error = true;
},
},
})
export const { fetchStart, fetchSuccess, fetchFailure } = videoSlice.actions
export default videoSlice.reducer
Video.jsx
import React, { useEffect, useState } from 'react'
import styled from 'styled-components'
import ThumbUpAltOutlinedIcon from '#mui/icons-material/ThumbUpAltOutlined';
import ThumbDownAltOutlinedIcon from '#mui/icons-material/ThumbDownAltOutlined';
import ReplayOutlinedIcon from '#mui/icons-material/ReplayOutlined';
import AddTaskOutlinedIcon from '#mui/icons-material/AddTaskOutlined';
import Comments from '../components/Comments'
import Card from '../components/Card'
import { useDispatch, useSelector } from "react-redux";
import { useLocation } from "react-router-dom";
import axios from 'axios';
import { fetchStart, fetchSuccess, fetchFailure } from '../redux/videoSlice'
import { format } from 'timeago.js';
const Container = styled.div`
display: flex;
gap: 1.5rem;
`
const Content = styled.div`
flex: 5;
`
const Recommendation = styled.div`
flex: 2;
`
const VideoWrapper = styled.div`
`
const Details = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
`
const Info = styled.span`
color: ${({ theme }) => theme.textSoft};
`
const Hr = styled.hr`
border: 0.5px solid ${({ theme }) => theme.soft};
`
const Title = styled.h1`
font-size: 18px;
font-weight: 400;
margin-top: 20px;
margin-bottom: 10px;
color: ${({ theme }) => theme.text};
`
const Buttons = styled.div`
display:flex;
gap: 20px;
color: ${({ theme }) => theme.text};
`
const Button = styled.div`
display: flex;
align-items: center;
gap:5px;
cursor: pointer;
`
const Chanel = styled.div`
display:flex;
justify-content:space-between;
`
const ChanelInfo = styled.div`
display:flex;
gap: 20px;
`
const Image = styled.img`
width: 50px;
height:50px;
border-radius: 50%;
`
const ChanelDetails = styled.div`
display:flex;
flex-direction:column;
color: ${({ theme }) => theme.text};
`
const ChanelName = styled.span`
font-weight: 500;`
const ChanelCounter = styled.span`
margin-top: 5px;
margin-bottom:20px;
color: ${({ theme }) => theme.textSoft};
font-size:12px ;
`
const Description = styled.p`
font-size:12px ;
`
const Subscripe = styled.button`
background-color: red;
font-weight: 500;
color: white;
border: none;
border-radius: 3px;
height: max-content;
cursor: pointer;
padding: 10px 20px;
`
const Video = () => {
const { currentVideo } = useSelector((state) => state.video);
const dispatch = useDispatch();
console.log('currentVideo',currentVideo);
const path = useLocation().pathname.split("/")[2];
const [channel, setChannel] = useState({});
// const [video, setVideo] = useState({});
useEffect(()=> {
const fetchData = async () => {
try {
dispatch(fetchStart())
const videoRes = await axios.get(`/videos/find/${path}`);
const channelRes = await axios.get(`/users/find/${videoRes.data.userId}`);
setChannel(channelRes.data);
dispatch(fetchSuccess(videoRes.data))
} catch (err) {
dispatch(fetchFailure())
}
};
fetchData();
}, [path, dispatch]);
return (
<Container>
<Content>
<VideoWrapper>
<iframe width="100%" height="720" src="https://www.youtube.com/embed/vkc99WHcDTk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</VideoWrapper>
<Title>{currentVideo.title}</Title>
<Details>
<Info>{currentVideo.views} views {format(currentVideo.createdAt)} </Info>
<Buttons>
<Button>
<ThumbUpAltOutlinedIcon /> {currentVideo.likes?.length}
</Button>
<Button>
<ThumbDownAltOutlinedIcon /> Dislike
</Button>
<Button>
<ReplayOutlinedIcon /> Share
</Button>
<Button>
<AddTaskOutlinedIcon /> Save
</Button>
</Buttons>
</Details>
<Hr />
<Chanel>
<ChanelInfo>
<Image src={channel.img} />
<ChanelDetails>
<ChanelName>{channel.name}</ChanelName>
<ChanelCounter>{channel.subscribers} subscriper</ChanelCounter>
<Description>{currentVideo.description}</Description>
</ChanelDetails>
</ChanelInfo>
<Subscripe>Subscripe</Subscripe>
</Chanel>
<Hr />
<Comments />
</Content>
{/* <Recommendation>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
<Card type="sm"/>
</Recommendation> */}
</Container>
)
}
export default Video

Express jwt is undefiend

So, i am making this request from my frontend to get all conversations of a user.
When the request made to the node server, it checks for "express-jwt" and says access denied.
When i console.log the jwt, it shows that the jwt is undefiend
(i think the bearer is ok, since it shows in the req.headers.authorization)
My react code:
import React, { useState, useEffect, useRef } from "react";
import styled from "styled-components";
import apiCalls from "../../backend/apiCalls";
import Conversion from "../../Components/Shared/Conversion";
import Message from "../../Components/Shared/Message";
import ToastSuccess from "../../Components/Shared/ToastSuccess";
import Toast from "../../Components/Shared/ToastSuccess";
import { io } from "socket.io-client";
function ChiefNotification({ id }) {
const [list, setList] = useState([]);
const socket = useRef();
useEffect(() => {
socket.current = io("ws://localhost:8900");
socket.current.on("getMessage", (data) => {
setRecievedMessages({
sender: data.senderId,
text: data.text,
createdAt: Date.now(),
});
});
}, []);
const [showConversations, setShowConversations] = useState([]);
const [conversations, setConversations] = useState([]);
const [currentChat, setCurrentChat] = useState(null);
const [messages, setMessages] = useState([]);
const [newMessage, setNewMessage] = useState("");
const [recievedMessages, setRecievedMessages] = useState("");
const scrollRef = useRef();
useEffect(() => {
recievedMessages &&
currentChat?.members.includes(recievedMessages.sender) &&
setMessages((prev) => [...prev, recievedMessages]);
}, [recievedMessages, currentChat]);
const getConversation = async () => {
await apiCalls
.getConversation(id)
.then((data) => {
setConversations(data?.data);
setShowConversations(data?.data);
})
.catch((err) => {
Toast.ToastFailure(err.response.data);
});
};
const handleSubmit = async (e) => {
e.preventDefault();
const message = {
sender: id,
text: newMessage,
conversationId: currentChat._id,
};
const recieverId = currentChat.members[1];
socket.current.emit("sendMessaage", {
senderId: id,
recieverId: recieverId,
text: newMessage,
});
await apiCalls
.sendMessage(message)
.then((data) => {
setMessages([...messages, data?.data]);
setNewMessage("");
})
.catch((err) => {
Toast.ToastFailure(err.response.data);
});
};
//handling sockets
const getSocketInfo = () => {
socket.current.emit("addUser", id);
socket.current.on("getUsers", (users) => {
console.log(users);
});
};
const getAllUsers = async () => {
await apiCalls
.getAllConvo()
.then((res) => {
setList(res?.data);
})
.catch((err) => {
ToastSuccess.ToastFailure(err?.response.data);
});
};
const NewConversation = (keyword) => {
if (keyword.length > 2) {
setShowConversations(
list.filter(
(listItem) =>
listItem.firstName.toLowerCase().includes(keyword.toLowerCase()) ||
listItem.lastName.toLowerCase().includes(keyword.toLowerCase())
)
);
} else {
setShowConversations(conversations);
}
};
useEffect(() => {
Promise.all([getConversation(), getSocketInfo(), getAllUsers()]);
}, []);
useEffect(() => {
const getMessages = async () => {
await apiCalls
.getMessages(currentChat?._id)
.then((data) => {
setMessages(data?.data);
})
.catch((err) => {
ToastSuccess.ToastFailure(err.response.data);
});
};
if (currentChat) getMessages();
}, [currentChat]);
useEffect(() => {
scrollRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages]);
return (
<>
<Breadcrum>Dashboard / Notifications</Breadcrum>
<Messenger>
<CheckMenu>
<CheckMenuWrapper>
<ChatMenuInp
placeholder="Search for Friends"
onChange={(e) => NewConversation(e.target.value)}
/>
{showConversations.map((c, index) => (
<div onClick={() => setCurrentChat(c)} key={index}>
<Conversion conversation={c} />
</div>
))}
</CheckMenuWrapper>
</CheckMenu>
<ChatBox>
<CheckBoxWrapper>
{currentChat ? (
<>
<CheckBoxTop>
{messages.map((m, index) => {
return (
<div ref={scrollRef} key={index}>
<Message message={m} own={m.sender === id} />
</div>
);
})}
</CheckBoxTop>
<CheckBoxBottom>
<ChatMessageArea
placeholder="write something...."
onChange={(e) => setNewMessage(e.target.value)}
value={newMessage}
/>
<ChatSendButton onClick={handleSubmit}>Send</ChatSendButton>
</CheckBoxBottom>
</>
) : (
<NoConversation>Open a Conversation to Start Chat</NoConversation>
)}
</CheckBoxWrapper>
</ChatBox>
</Messenger>
</>
);
}
export default ChiefNotification;
const Messenger = styled.div`
display: flex;
margin: 0% 5% 1% 5%;
border: 1px solid #e0e0e0;
border-radius: 5px;
max-height: 80vh;
min-height: 70vh;
`;
const CheckMenu = styled.div`
flex: 3;
`;
const CheckMenuWrapper = styled.div`
padding: 10px;
border-right: 1px solid #e0e0e0;
height: 100%;
`;
const ChatMenuInp = styled.input`
width: 90%;
padding: 10px;
border: none;
border-bottom: 1px solid #e0e0e0;
`;
const ChatMessageArea = styled.textarea`
width: 80%;
height: 90px;
padding: 10px;
`;
const CheckBoxBottom = styled.div`
margin-top: 5px;
display: flex;
align-items: center;
justify-content: space-between;
background-color: #f5f5f5;
padding: 2%;
`;
const ChatBox = styled.div`
flex: 7;
padding: 5px 15px;
`;
const CheckBoxWrapper = styled.div`
display: flex;
flex-direction: column;
justify-content: space-between;
position: relative;
height: 100%;
position: relative;
`;
const CheckBoxTop = styled.div`
height: 100%;
overflow-y: scroll;
padding-right: 10px;
`;
const ChatSendButton = styled.button`
width: 100px;
height: 40px;
border: none;
color: white;
border-radius: 1px;
background-color: #5f9ab4;
cursor: pointer;
`;
const Breadcrum = styled.h6`
color: #003951;
padding: 1% 5%;
display: flex;
`;
const NoConversation = styled.div`
position: absolute;
top: 10%;
font-size: 50px;
color: lightgray;
cursor: default;
width: 100%;
text-align: center;
`;
The Axios Setup Code:
import axios from "axios";
import { getCookie } from "../Auth/auth";
const authToken = getCookie("token");
axios.defaults.baseURL = "http://localhost:5000";
axios.defaults.headers.common["Authorization"] = `Bearer ${authToken}`;
axios.interceptors.response.use(null, (error) => {
const expectedError =
error.response &&
error.response.status >= 400 &&
error.response.status < 500;
if (!expectedError) {
console.log("Logging error", error);
alert("An unexpected Error occurred");
}
return Promise.reject(error);
});
// eslint-disable-next-line import/no-anonymous-default-export
export default {
get: axios.get,
post: axios.post,
put: axios.put,
delete: axios.delete,
};
The code for the API call and the jwt file
//get list of people that you can have conversation with
router.get("/allConversations", [chief], async (req, res) => {
const getUsers = await User.find({ accountStatus: true }).select(
"id firstName lastName"
);
if (getUsers) {
return res.status(200).json(getUsers);
} else return res.status(400).json("Error Occured while getting users");
});
The code for JWT
const expressJwt = require("express-jwt");
function authJwt() {
const secret = process.env.SECRET;
return expressJwt({
secret,
algorithms: ["HS256"],
isRevoked: isRevoked,
}).unless({
path: [
{ url: /\/issues(.*)/, methods: ["GET", "OPTIONS"] },
{ url: /\journal\/download(.*)/, methods: ["GET", "OPTIONS"] },
{ url: /\user\/activate(.*)/, methods: ["GET", "POST", "OPTIONS"] },
{
url: /\user\/auth\/password\/reset(.*)/,
methods: ["GET", "POST", "OPTIONS"],
},
{ url: /\/public\/uploads(.*)/, methods: ["GET", "OPTIONS"] },
{ url: /\/journal\/search(.*)/, methods: ["GET", "OPTIONS"] },
{ url: /\/journal\/currentIssues(.*)/, methods: ["GET", "OPTIONS"] },
{ url: /\journal/, methods: ["POST", "OPTIONS"] },
{ url: /\user\/chief(.*)/, methods: ["GET", "DELETE", "OPTIONS"] },
{ url: /\messages(.*)/, methods: ["GET", "OPTIONS"] },
"/user/login",
"/user/signup",
"/journal/latest",
"/user/forget",
"/user/reset",
"/user/chief/all",
],
});
}
async function isRevoked(req, payload, done) {
if (!payload.userType) {
done(null, true);
}
done();
}
module.exports = authJwt;

How i can display sender and receiver message on different side in angular using socket.io-client

All things working well but problem is that i want to show sender and receiver message on different side but i don't know how i can implement this i also getting socket ids from server side but i'm not able to create logic how i can differentiate sender and receiver message on my html page please anyone can help me Thanks in advance
this is my server side code
io.on('connection', (socket) => {
console.log('a user connected');
socket.emit('myId', socket.id);
socket.on('sendMessage', (message) => {
const sockitID = socket.id;
console.log(sockitID);
io.emit('recieveMessage', message); //`${socket.id.substr(0, 2)} said--${message}`
});
socket.on('disconnect', () => {
console.log('a user disconnected!');
});
});
this is my chatserivce file from agular side
import { Injectable, OnInit } from '#angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { io, Socket } from "socket.io-client";
import { DefaultEventsMap } from 'socket.io-client/build/typed-events';
import { MessageModel } from './app.component';
#Injectable({
providedIn: 'root'
})
export class ChatService implements OnInit {
public message$: BehaviorSubject<MessageModel> = new BehaviorSubject({});
public socket: Socket<DefaultEventsMap, DefaultEventsMap>;
public socketID: string = ''
constructor() {
this.socket =io('http://localhost:3000');
this.socket.on('myId', (id)=>{
console.log('from service',id);
});
}
ngOnInit(): void {
}
public sendMessage(message:any) {
this.socket.emit('sendMessage', {message: message, senderId : this.socketID});
const senderID = this.socket.id;
console.log('Sender ID = ' ,senderID);
}
public getNewMessage = () => {
this.socket.on('recieveMessage', (message) =>{
this.message$.next(message);
const reciverID = this.socket.id;
console.log('Receiver ID = ', reciverID)
});
return this.message$.asObservable();
};
}
This is my angular typescript file
import { Component } from '#angular/core';
import { ChatService } from './chat.service';
export interface MessageModel{
message?: string;
senderId?:string;
}
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public socketId: string = '';
constructor(private chatService: ChatService){}
newMessage!: string;
messageList: MessageModel[] = [];
ngOnInit(){
this.socketId = this.chatService.socketID;
console.log('this.socketID', this.socketId)
this.chatService.getNewMessage().subscribe((message: MessageModel) => {
const socketId = message.senderId;
console.log('Sockit ID = ',socketId);
this.messageList.push(message);
});
}
sendMessage() {
this.chatService.sendMessage(this.newMessage);
this.newMessage = '';
}
}
this is my html file for displaying messages
<div class="chats" *ngFor="let message of messageList">
<div *ngIf="message">
<div *ngIf="socketId === message.senderId" class="client-chat">
{{message.message}}
</div>
<div *ngIf="socketId !== message.senderId" class="my-chat">
{{message.message}}
</div>
</div>
</div>
This is my css style to seprate sender message to receiver through styling
.client-chat{
width: 60%;
word-wrap: break-word;
background-color: #4f5d73c7;
padding: 7px 10px;
border-radius: 10px 10px 10px 0px;
margin: 10px 0px;
}
.my-chat{
width: 60%;
word-wrap: break-word;
background-color: #77b3d4c7;
padding: 7px 10px;
border-radius: 10px 10px 0px 10px;
margin: 5px 0px 5px auto;
}
this is sender side result
this is receiver side result
i am not able to create logic for sepeerating message sender and receiver please anyone can solve my problem Thanks in advance
You have never updated socketID in ChatService
this.socket.on('myId', (id)=>{
console.log('from service',id);
this.socketID = id; //missing this line
});
Edit:
When you update socketID in ChatService, the socketId in AppComponent is not updated, because you only set socketId once in ngOnInit.
The simplest thing you can do is to completely get rid of the socketId field, make chatService public and access chatService.socketID directly in your HTML template.
export class AppComponent {
//public socketId: string = '';
constructor(public chatService: ChatService){}
//...
}
<div class="chats" *ngFor="let message of messageList">
<div *ngIf="message">
<div *ngIf="chatService.socketID === message.senderId" class="client-chat">
{{message.message}}
</div>
<div *ngIf="chatService.socketID !== message.senderId" class="my-chat">
{{message.message}}
</div>
</div>
</div>

TypeError: Cannot read property 'data' of null

They have passed me a project of a web shop made with node.js and reactj and when I execute it loads the web but then it leaves an error message, it is because in some section there is no data or there is no code, but I do not want to insert it, my Professor told me to put a conditional so that when I do not find code I also upload the web but I do not know where to put the code or where to act, i am new on stackoverflow, thanks a lot
60 | let campaigns: any = [];
61 |
62 | if (this.props.categories && this.props.categories.isFinished) {
> 63 | if (this.props.categories.queryResult.data) {
| ^
64 | categories = this.props.categories.queryResult.data;
65 | } else if (this.props.categories.queryResult.length) {
66 | categories = this.props.categories.queryResult;
import * as React from "react";
import { Component } from "react";
import "./MenuBar.css";
import Menu from "./Menu";
import { List } from "semantic-ui-react";
import icon1 from "../../assets/icons/bars1.png";
import icon2 from "../../assets/icons/bars2.png";
import icon3 from "../../assets/icons/bars3.png";
import icon1r from "../../assets/icons/bars1_w.png";
import icon2r from "../../assets/icons/bars2_w.png";
import icon3r from "../../assets/icons/bars3_w.png";
import { services } from "../../store";
import { connect } from "react-redux";
import Radium from "radium";
import MenuFilters from "./MenuFilters";
export interface Props {
// Actions
fetchCategories: any;
fetchShops: any;
fetchCampaigns: any;
// Props
name: string;
avatar: string;
userId: string;
classes: any;
categories: any;
shops: any;
campaigns: any;
// Events
onChangeGrid: any;
}
interface State {
isOpen: boolean;
grid: string;
}
class _MenuBar extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
isOpen: true,
grid: "grid1"
};
}
public componentDidMount() {
this.props.fetchCategories();
this.props.fetchShops();
this.props.fetchCampaigns();
}
public render() {
let categories: any = [];
let shops: any = [];
let campaigns: any = [];
if (this.props.categories && this.props.categories.isFinished) {
if (this.props.categories.queryResult.data) {
categories = this.props.categories.queryResult.data;
} else if (this.props.categories.queryResult.length) {
categories = this.props.categories.queryResult;
}
}
if (this.props.shops && this.props.shops.isFinished) {
if (this.props.shops.queryResult.data) {
shops = this.props.shops.queryResult.data;
} else if (this.props.shops.queryResult.length) {
shops = this.props.shops.queryResult;
}
}
if (this.props.campaigns && this.props.campaigns.isFinished) {
if (this.props.campaigns.queryResult.data) {
campaigns = this.props.campaigns.queryResult.data;
} else if (this.props.campaigns.queryResult.length) {
campaigns = this.props.campaigns.queryResult;
}
}
return (
<div className="MCMenuBar">
<div className="MCMenuBarContainer">
<div
style={{
display: "inline-flex",
width: "50%"
}}
>
<Menu categories={categories} shops={shops} campaigns={campaigns} />
<div className="MCMenuBarDivider" />
<div
style={{
height: "50px",
marginTop: "10px"
}}
>
<List horizontal>
<List.Item as="a">
<span style={{ color: "#000", fontWeight: "bold" }}>
NUEVOS
</span>
</List.Item>
<List.Item as="a">
<span style={{ color: "#000", fontWeight: "bold" }}>
GRATIS
</span>
</List.Item>
<List.Item as="a">
<span style={{ color: "#000", fontWeight: "bold" }}>
PROMOS
</span>
</List.Item>
<List.Item as="a">
<span style={{ color: "#000", fontWeight: "bold" }}>
JUEGOS
</span>
</List.Item>
</List>
</div>
</div>
<div
style={{
height: "38px",
width: "50%",
textAlign: "right"
}}
>
<List horizontal>
<List.Item
as="a"
onClick={() => {
if (this.props.onChangeGrid) {
this.setState({ grid: "grid1" });
this.props.onChangeGrid("grid1");
}
}}
>
<span style={{ color: "#000", fontWeight: "bold" }}>
<img
src={this.state.grid === "grid1" ? icon1 : icon1r}
alt="Mi chollo"
style={style.baricon}
/>
</span>
</List.Item>
<List.Item
as="a"
onClick={() => {
if (this.props.onChangeGrid) {
this.setState({ grid: "grid2" });
this.props.onChangeGrid("grid2");
}
}}
>
<span style={{ color: "#000", fontWeight: "bold" }}>
<img
src={this.state.grid === "grid2" ? icon2 : icon2r}
alt="Mi chollo"
style={style.baricon}
/>
</span>
</List.Item>
<List.Item
as="a"
onClick={() => {
if (this.props.onChangeGrid) {
this.setState({ grid: "grid3" });
this.props.onChangeGrid("grid3");
}
}}
>
<span style={{ color: "#000", fontWeight: "bold" }}>
<img
src={this.state.grid === "grid3" ? icon3 : icon3r}
alt="Mi chollo"
style={style.baricon}
/>
</span>
</List.Item>
<List.Item>
<div />
</List.Item>
</List>
<div
style={{
display: "inline-flex",
borderLeft: "1px solid #ededed",
paddingLeft: "10px",
height: "58px",
marginTop: "-12px",
position: "relative",
top: "2px"
}}
>
<div
style={{
display: "inline-flex",
paddingTop: "10px"
}}
>
<MenuFilters />
</div>
</div>
</div>
</div>
</div>
);
}
}
const style = {
baricon: {
width: "24px",
height: "24px",
opacity: 0.4
}
};
const mapDispatchToProps = (dispatch: any) => {
return {
// same effect
fetchCategories: () => {
dispatch(services["api/v1/categories"].find());
},
fetchShops: () => {
dispatch(services["api/v1/shops"].find());
},
fetchCampaigns: () => {
dispatch(services["api/v1/campaigns"].find());
}
};
};
const mapStateToProps = (store: any) => {
return {
categories: store.categories,
shops: store.shops,
campaigns: store.campaigns
};
};
const MenuBar = connect(
mapStateToProps,
mapDispatchToProps
)(_MenuBar);
export default Radium(MenuBar);
As the error explains your queryResult property is null. Add another conditional check to see if the queryResult field is not empty in the line above (62)
if (
this.props.categories &&
this.props.categories.isFinished &&
!!this.props.categores.queryResult
) {
// user queryResult
}
Another option would be to set a default value for queryResult and update all it's references.
const qResult = this.props.categories.queryResult || [];
if (response && response.data && response.data.length > 0) {
}

Socket.emit not sending or socket.on not receiving?

I am building a mobile Chat app using Node.js, MySql, Sequelize, Socket, React Native and Redux.
I am trying to socket.emit the credential of the user to:
1) find or create the user and;
2) move to the next screen via the reducer.
I believe that either:
socket.emit('newUser', credentials) in ./store/index.js
doesn't work, and / or both
socket.on('newUser', credentials => {...} in ./index.js
socket.on('newUser', user => {...} in ./store/index.js
do not work, because nothing is added to database an I do not move to next screen. Simply, nothing happen when I enter some credentials and hit the button.
I am fairly new to development and tried to understand where is the problem by using multiple console.log (removed from the code below), as I don't know how else to test it.
I have also checked all other threads on socket.emit and on how to test if socket is working for the past few days, but I am still stuck.
Below is my code.
1) why socket.emit doesn't send (or socket.on doesn't listen)?
2) How can I test if socket is working (both on client and server side).
Thank you!!
./index.js
const server = require('http');
server.createServer(function (req, res) {}).listen(3000);
const io = require('socket.io')(server);
const { User, Conversation, Message } = require('./db').models;
const mobileSockets = {};
io.on('connection', socket => {
socket.on('newUser', credentials => {
const { name, password } = credentials;
Promise.all([
User.findOrCreate({
where: {
name,
password
}
}),
User.findAll()
])
.then(([user, users]) => {
mobileSockets[user[0].id] = socket.id;
socket.emit('userCreated', { user: user[0], users });
socket.broadcast.emit('newUser', user[0]);
});
});
});
./App.js
import React from 'react';
import { Provider } from 'react-redux';
import store from './store';
import { createStackNavigator } from 'react-navigation';
export default class App extends React.Component {
render() {
return (
<Provider store={ store }>
<RootStack />
</Provider>
);
}
}
import LoginScreen from './components/Login';
import Users from './components/Users';
import Chat from './components/Chat';
const RootStack = createStackNavigator({
Login: LoginScreen,
Users: Users,
Chat: Chat,
}, {
initialRouteName: 'Login',
navigationOptions: {
headerTitle: 'login to Chat!'
}
});
./components/Login.js
import React from 'react';
import { View, Text, TextInput, StyleSheet, TouchableOpacity } from 'react-native';
import { login } from '../store';
export default class LoginScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
password: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(type, value) {
this.setState({ [type]: value });
}
handleSubmit() {
login(this.state, this.props.navigation);
}
render() {
return (
<View style={ styles.container }>
<Text style={ styles.text }>Enter your name and password:</Text>
<TextInput
onChangeText={ value => this.handleChange('name', value) }
returnKeyType='next'
autoCapitalize='none'
autoCorrect={ false }
onSubmitEditing={ () => this.passwordInput.focus() }
style={ styles.input }
/>
<TextInput
onChangeText={ value => this.handleChange('password', value)}
secureTextEntry
returnKeyType='go'
autoCapitalize='none'
style={ styles.input }
ref={ input => this.passwordInput = input }
/>
<TouchableOpacity
onPress={ this.handleSubmit }
style={ styles.button }
>
<Text style={ styles.buttonText }>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'powderblue',
height: '100%',
width: '100%'
},
text: {
fontSize: 20,
fontWeight: 'bold'
},
input: {
height: 40,
width: '90%',
borderWidth: 0.5,
borderColor: 'black',
backgroundColor: '#fff',
color: '#000',
textAlign: 'center',
marginTop: 10
},
button: {
width: '75%',
backgroundColor: 'blue',
borderRadius: 50,
alignItems: 'center',
justifyContent: 'center',
marginTop: 20,
paddingVertical: 15
},
buttonText: {
color: '#fff',
textAlign: 'center',
fontSize: 15,
fontWeight: 'bold',
}
});
./store/index.js
import { createStore, combineReducers } from 'redux';
import users, { gotUsers, gotNewUser } from './users';
import messages, { gotMessages, gotMessage } from './messages';
import user, { gotUser } from './user';
let navigate;
const reducers = combineReducers({ users, messages, user });
const store = createStore(reducers);
export default store;
export * from './users';
export * from './messages';
import socket from './socket';
});
socket.on('userCreated', response => {
const { user, users } = response;
store.dispatch(gotUser(user));
store.dispatch(gotUsers(users));
navigate('Users');
});
socket.on('newUser', user => {
console.log('store/index.js has received a "newUser"');
store.dispatch(gotNewUser(user));
});
export const login = (credentials, navigation) => {
socket.emit('newUser', credentials);
navigation = navigation.navigate;
};
./store/socket.js
import io from 'socket.io-client';
const socket = io('http://localhost:3000');
socket.connect();
export default socket;
It's not a real solution to the problem, but I've managed to make it work by using express.
./index.js
// replaced the connection part of the code with this
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
app.get('/', function (req, res) {
res.send('Hello World');
});
server.listen(3000);
// the rest of the code goes here

Resources