Skip to content

Commit

Permalink
fix: nextjs not accessing localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Dun-sin committed Dec 14, 2023
1 parent 88764db commit fc37ea3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 97 deletions.
22 changes: 13 additions & 9 deletions src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ export const AppProvider = ({ children }: ProviderType) => {
initialState,
defaultState => {
try {
const persistedState = JSON.parse(
localStorage.getItem('app') as string
);

if (!persistedState) {
return defaultState;
}

return persistedState;
if (typeof window !== 'undefined') {
const persistedState = JSON.parse(
localStorage.getItem('app') as string
);

if (!persistedState) {
return defaultState;
}

return persistedState;
} else {
return defaultState;
}
} catch {
return defaultState;
}
Expand Down
20 changes: 12 additions & 8 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,21 @@ export const useAuth = () => {

const initializeAuthState: unknown = (defaultState: AuthType): AuthType => {
try {
const persistedState = JSON.parse(localStorage.getItem('auth') as string);
if (typeof window !== 'undefined') {
const persistedState = JSON.parse(localStorage.getItem('auth') as string);

if (!persistedState) {
return defaultState;
}
if (!persistedState) {
return defaultState;
}

if (!persistedState.loginId && persistedState.isLoggedIn === true) {
throw new Error('Gotcha! :D');
}
if (!persistedState.loginId && persistedState.isLoggedIn === true) {
throw new Error('Gotcha! :D');
}

return persistedState;
return persistedState;
} else {
return defaultState;
}
} catch {
return defaultState;
}
Expand Down
150 changes: 71 additions & 79 deletions src/context/ChatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ export const ChatProvider = ({ children }: ProviderType) => {
initialState,
defaultState => {
try {
const persistedState = JSON.parse(
localStorage.getItem('chats') as string
);
if (typeof window !== 'undefined') {
const persistedState = JSON.parse(
localStorage.getItem('chats') as string
);

if (!persistedState) {
if (!persistedState) {
return defaultState;
}

return persistedState;
} else {
return defaultState;
}

return persistedState;
} catch (error) {
console.error('Error parsing localStorage:', error);
return defaultState;
Expand All @@ -65,88 +69,76 @@ export const ChatProvider = ({ children }: ProviderType) => {
[currentReplyMessageId, getMessage]
);

function addMessage(message: MessageType) {
dispatch({
type: 'ADD_MESSAGE',
payload: message,
});
}

function updateMessage(message: MessageType) {
dispatch({
type: 'UPDATE_MESSAGE',
payload: message,
});
}

function createChat(
chatId: string,
userIds: [string, string],
messages: MessageIdType = {},
createdAt?: Date
): void {
try {
const functions = {
addMessage: (message: MessageType) => {
dispatch({
type: 'CREATE_CHAT',
payload: { chatId, userIds, messages, createdAt },
type: 'ADD_MESSAGE',
payload: message,
});
console.log('context', { chatId, userIds, messages, createdAt });
} catch (error) {
console.error('Error creating chat:', error);
}
}

function removeMessage(id: string, chatId: string) {
dispatch({
type: 'REMOVE_MESSAGE',
payload: { id, room: chatId },
});
}

function receiveMessage(id: string, chatId: string) {
dispatch({
type: 'RECEIVE_MESSAGE',
payload: { id, room: chatId },
});
}

function closeChat(chatId: string) {
dispatch({
type: 'CLOSE_CHAT',
payload: { chatId },
});
}

function closeAllChats() {
dispatch({
type: 'CLOSE_ALL_CHATS',
payload: {},
});
}

function startReply(messageId: string) {
setCurrentReplyMessageId(messageId);
}

function cancelReply() {
setCurrentReplyMessageId('');
}
},
updateMessage: (message: MessageType) => {
dispatch({
type: 'UPDATE_MESSAGE',
payload: message,
});
},
createChat: (
chatId: string,
userIds: [string, string],
messages: MessageIdType = {},
createdAt?: Date
): any => {
try {
dispatch({
type: 'CREATE_CHAT',
payload: { chatId, userIds, messages, createdAt },
});
console.log('context', { chatId, userIds, messages, createdAt });
return { chatId, userIds };
} catch (error) {
console.error('Error creating chat:', error);
return { error };
}
},
removeMessage: (id: string, chatId: string) => {
dispatch({
type: 'REMOVE_MESSAGE',
payload: { id, room: chatId },
});
},
receiveMessage: (id: string, chatId: string) => {
dispatch({
type: 'RECEIVE_MESSAGE',
payload: { id, room: chatId },
});
},
closeChat: (chatId: string) => {
dispatch({
type: 'CLOSE_CHAT',
payload: { chatId },
});
},
closeAllChats: () => {
dispatch({
type: 'CLOSE_ALL_CHATS',
payload: {},
});
},
startReply: (messageId: string) => {
setCurrentReplyMessageId(messageId);
},
cancelReply: () => {
setCurrentReplyMessageId('');
},
};

return (
<ChatContext.Provider
value={{
messages: state,
currentReplyMessage,
currentReplyMessageId,
addMessage,
updateMessage,
createChat,
removeMessage,
closeChat,
closeAllChats,
receiveMessage,
startReply,
cancelReply,
...functions,
}}
>
{children}
Expand Down
1 change: 1 addition & 0 deletions src/pages/searching.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const Searching = () => {
"Let's Chat :)",
"You've found a match, don't keep your Partner waiting ⌛"
);

createChat(roomId, userIds);
endSearch(roomId);
router.push('/anonymous');
Expand Down
2 changes: 1 addition & 1 deletion src/types/contextTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type ChatContextType = {
userIds: [string, string],
messages?: MessageIdType,
createdAt?: Date
) => void;
) => any;
messages: ChatIdType;
removeMessage: (id: string, chatid: string) => void;
addMessage: (message: MessageType) => void;
Expand Down

1 comment on commit fc37ea3

@vercel
Copy link

@vercel vercel bot commented on fc37ea3 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.