Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cris Fandino committed Feb 4, 2021
1 parent c4d3b8a commit 5e2de70
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/

client/node_modules/
client/node_modules/`
38 changes: 10 additions & 28 deletions client/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
<script>
import { onMount } from 'svelte'
import io from 'socket.io-client'
import Tailwindcss from './Tailwindcss.svelte'
import Login from './components/Login.svelte'
import ChatRoom from './components/ChatRoom.svelte'
import { ID_KEY } from './const'
import { joinRoom, loggedUser, users } from './store'
let username, room, data
const socket = io('http://localhost:3000', {
transports: ['websocket', 'polling'],
})
$: console.log($users)
let usernameValue = '',
roomValue = ''
function joinRoom(name, roomID) {
socket.emit('join-room', { username: name, room: roomID }, (err) => {
if (err) {
alert("Room doesn't exist")
data = ''
return
}
data = { username: name, room: roomID }
localStorage.setItem(ID_KEY, JSON.stringify(data))
})
}
function handleLogin() {
if (!username || !room) return
joinRoom(username, room)
if (!usernameValue || !roomValue) return
joinRoom(usernameValue, roomValue)
}
onMount(() => {
const local = JSON.parse(localStorage.getItem(ID_KEY))
if (!local) return
joinRoom(local.username, local.room)
})
</script>

<Tailwindcss />
<main class="bg-gray-300 h-screen flex items-center justify-center w-full">
{#if data}
<ChatRoom {...data} {socket} />
{#if $loggedUser}
<ChatRoom />
{:else}
<Login bind:username bind:room {handleLogin} />
<Login bind:usernameValue bind:roomValue {handleLogin} />
{/if}

</main>
14 changes: 14 additions & 0 deletions client/src/Tailwindcss.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@
@tailwind components;
@tailwind utilities;
/* @layer components {
.btn {
@apply p-2 my-2 disabled\:opacity-50 disabled\:cursor-default text-white rounded-md focus\:outline-none focus\:ring-2 ring-offset-2 flex items-center justify-center shadow-xl;
}
.btn-primary {
@apply bg-blue-500 ring-blue-300;
}
.btn-danger {
@apply bg-red-500 ring-red-300;
}
.btn-warning {
@apply bg-yellow-500 ring-yellow-300;
}
} */
</style>
13 changes: 3 additions & 10 deletions client/src/components/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
const dispathcer = createEventDispatcher()
export let variant = 'primary'
const variants = {
primary: 'blue',
danger: 'red',
success: 'green',
secondary: 'gray',
info: 'light-blue',
}
const color = variants[variant]
function forward(e) {
dispathcer('click', e)
Expand All @@ -20,7 +12,8 @@
<button
on:click={forward}
{...$$restProps}
class={`p-2 my-2 bg-${color}-500 disabled:opacity-50 disabled:cursor-default text-white rounded-md focus:outline-none
focus:ring-2 ring-${color}-300 ring-offset-2 ${$$restProps.class}`}>
class={`p-2 my-2 bg-blue-500 disabled:opacity-50 disabled:cursor-default text-white rounded-md focus:outline-none
focus:ring-2 ring-blue-300 ring-offset-2 ${$$restProps.class} flex items-center justify-center shadow-xl`}>
<slot />
<slot name="icon" />
</button>
70 changes: 40 additions & 30 deletions client/src/components/ChatRoom.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<script>
import { afterUpdate } from 'svelte'
import { uuid } from '../utils'
import Button from './Button.svelte'
import Container from './Container.svelte'
import Input from './Input.svelte'
import Logo from './Logo.svelte'
import Message from './Message.svelte'
import People from './People.svelte'
import { ID_KEY } from '../const'
import Toggler from './Toggler.svelte'
import { ID_KEY } from '../const'
import { uuid } from '../utils'
import { users, socket, loggedUser } from '../store'
const { username, room } = $loggedUser
const uid = uuid('user')
export let username, room, socket, message
let message
let messages = []
let users = []
let chatMessages
let isOpen
function handleMessage() {
Expand All @@ -28,46 +28,55 @@
}
function handleLeave() {
localStorage.removeItem(ID_KEY)
window.location.reload()
users.set([])
loggedUser.set(null)
socket.emit('user-leave')
}
afterUpdate(() => {
if (!chatMessages) return
chatMessages.scrollTop = chatMessages.scrollHeight
chatMessages.focus()
})
socket.on('server-message', (data) => {
messages = [...messages, data]
})
socket.on('users', (data) => {
users = data
})
</script>

<div
class="flex md:flex-row flex-col h-full w-full overflow-x-hidden
bg-transparent ">

<!-- Side Bar -->
<Toggler on:click={() => (isOpen = !isOpen)} />
<div
class={`flex-col py-8 px-3 sm:w-full md:w-64 w-2/4 bg-white flex-shrink-0
overflow-auto fixed md:static top-0 h-screen z-50 transform
${isOpen ? '' : '-translate-x-full'} duration-150 md:transform-none`}>
class:-translate-x-full={!isOpen}
class={`flex-col py-8 px-3 md:w-64 w-2/4 bg-white flex-shrink-0
overflow-auto fixed md:static top-0 h-screen z-50 transform duration-150 md:transform-none`}>
<Logo />
<div class="text-md mt-5">
<span class="font-bold">Room:</span>
{room}
</div>
<div class="text-md mt-5">
<span class="font-bold">Name:</span>
{username}
</div>
<div class="flex flex-col mt-8">
<div class="flex flex-row items-center justify-between text-xs">
<span class="font-bold">Active Conversations</span>
</div>
<div class="flex flex-col space-y-1 -mx-2 h-50 mb-2 overflow-y-auto">
{#each users as { username, id } (id)}
<People name={username} />
{#each $users as { username, id, isSelf } (id)}
<People name={username} {isSelf} />
{/each}

</div>
</div>
<Button class="w-full" on:click={handleLeave}>Leave Room</Button>
</div>
<!-- SideBarEnd -->

<!-- ChatMessages -->
<Container class="flex flex-col flex-auto h-full p-6 bg-gray-200">
<div class="flex flex-col flex-auto flex-shrink-0 rounded-2xl h-full p-4">
<div
Expand All @@ -90,28 +99,29 @@
<Input
bind:value={message}
autocomplete="off"
autofocus={true}
class="w-full shadow-md"
name="message" />

<Button class="flex items-center justify-center ml-2">
<span>Send</span>
<span class="ml-2">
<svg
class="w-4 h-4 transform rotate-90 -mt-px"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8" />
</svg>
</span>
<Button class=" ml-2">
Send
<svg
slot="icon"
class="w-4 h-4 transform rotate-90 ml-1"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8" />
</svg>
</Button>

</form>
</div>
</Container>
<!-- CHat Messages End -->
</div>
6 changes: 3 additions & 3 deletions client/src/components/Login.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Input from './Input.svelte'
import Logo from './Logo.svelte'
export let username, room, handleLogin
export let usernameValue, roomValue, handleLogin
</script>

<div class="grid place-items-center w-full">
Expand All @@ -14,11 +14,11 @@
<Logo />
<form on:submit|preventDefault={handleLogin}>
<Input
bind:value={username}
bind:value={usernameValue}
label="Name"
name="name"
placeholder="Name" />
<Input bind:value={room} name="room" label="Room Name" />
<Input bind:value={roomValue} name="room" label="Room Name" />
<Button class="block w-full font-semibold">Enter or Create room</Button>
</form>
</div>
Expand Down
42 changes: 16 additions & 26 deletions client/src/components/Message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,24 @@
message = ''
export let isSelf = false
const class1 = isSelf
? 'col-start-6 col-end-13 p-3'
: 'col-start-1 col-end-8 p-3'
const class2 = isSelf ? 'justify-start flex-row-reverse' : 'flex-row'
</script>

{#if isSelf}
<div in:fade class="col-start-6 col-end-13 p-3 rounded-lg">
<div class="flex items-center justify-start flex-row-reverse">
<div
class="flex items-center justify-center h-10 w-10 rounded-full
bg-indigo-500 flex-shrink-0">
{name.substr(0, 1)}
</div>
<div
class="relative mr-3 text-sm bg-indigo-100 py-2 px-4 shadow rounded-xl">
<div>{message}</div>
</div>
<div in:fade class={`rounded-lg ${class1}`}>
<div class={`flex items-center ${class2}`}>
<div
class="flex items-center justify-center h-10 w-10 rounded-full
bg-indigo-500 flex-shrink-0">
{name.substr(0, 1).toUpperCase()}
</div>
</div>
{:else}

<div in:fade class="col-start-1 col-end-8 p-3 rounded-lg">
<div class="flex flex-row items-center">
<div
class="flex items-center justify-center h-10 w-10 rounded-full
bg-indigo-500 flex-shrink-0">
{name.substr(0, 1)}
</div>
<div class="relative ml-3 text-sm bg-white py-2 px-4 shadow rounded-xl">
<div>{message}</div>
</div>
<div
class:bg-indigo-100={isSelf}
class:bg-white={!isSelf}
class="relative mr-3 text-sm py-2 px-4 shadow rounded-xl">
<div>{message}</div>
</div>
</div>
{/if}
</div>
7 changes: 5 additions & 2 deletions client/src/components/People.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<script>
export let name
export let name, isSelf
</script>

<button class="flex flex-row items-center hover:bg-gray-100 rounded-xl p-2">
<button
class:bg-green-100={isSelf}
class:hover:bg-gray-100={!isSelf}
class="flex flex-row items-center rounded-xl p-2">
<div
class="flex items-center justify-center h-8 w-8 bg-indigo-200 rounded-full">
{name.substr(0, 1)}
Expand Down
34 changes: 34 additions & 0 deletions client/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import io from 'socket.io-client'
import { writable } from 'svelte/store'
import { ID_KEY } from './const'

export const users = writable([])
export const loggedUser = writable()

export const socket = io('http://localhost:3000', {
transports: ['websocket', 'polling'],
})

socket.on('user-disconnect', (leavingUser) => {
users.update((currentUsers) => {
return currentUsers.filter((user) => user.id !== leavingUser.id)
})
})
socket.on('user-join', (joinedUser) => {
users.update((currentUsers) => [...currentUsers, joinedUser])
})

export function joinRoom(name, roomID) {
const user = { username: name, room: roomID }
socket.emit('join-room', user, (roomUsers, currentUser) => {
const person = roomUsers.find(({ id }) => id === currentUser.id)
person.isSelf = true
users.set(roomUsers)
loggedUser.set(user)
localStorage.setItem(ID_KEY, JSON.stringify(user))
})
}
const saveUser = JSON.parse(localStorage.getItem(ID_KEY))
if (saveUser) {
joinRoom(saveUser.username, saveUser.room)
}
Loading

0 comments on commit 5e2de70

Please sign in to comment.