This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const MaterialForm = () => { | |
| const { values, useInput, isValid } = useForm({ | |
| username: '', | |
| email: '' | |
| }); | |
| const handleSubmit = e => { | |
| e.preventDefault(); | |
| console.log(values) | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function useForm (defaultValues, invalidAttr = { error: true }) { | |
| const [values, setValues] = useState(defaultValues); | |
| const [mounted, setMounted] = useState(false); | |
| const [formErrors, setFormErrors] = useState([]); | |
| const handleError = useCallback((name, isValid) => { | |
| let errors = formErrors; | |
| const index = errors.findIndex(error => error === name); | |
| if (!isValid) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function useFormInput ({ | |
| name, | |
| validation = '', | |
| values: formData, | |
| setValues: setFormData, | |
| defaultInvalidAttr, | |
| handleError | |
| }) { | |
| const formValue = dot.pick(name, formData) || ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const CustomForm = () => { | |
| const defaultValues = { | |
| username: '', | |
| email: '', | |
| age: '' | |
| }; | |
| const customErrorAttribute = { | |
| className: 'has-error', | |
| 'another-attr': 'look-at-me' | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict' | |
| class RoomUpdateController { | |
| constructor (params) { | |
| const { socket, request } = params | |
| this.socket = socket | |
| this.request = request | |
| console.log('A new subscription for room topic', socket.topic) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Room = use('App/Models/Room'); | |
| const { broadcast } = require('../../utils/socket.utils'); | |
| async createMessage ({ params, request, response }) { | |
| const room = await Room.find(params.id); | |
| if (!room) { | |
| return response.notFound(`The room doesn't exist`) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Ws = use('Ws') | |
| function broadcast (id, type, data) { | |
| const channel = Ws.getChannel('room:*') | |
| if (!channel) return | |
| const topic = channel.topic(`room:${id}`) | |
| if (!topic) { | |
| console.error('Has no topic') | |
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "type": "room:newMessage", | |
| "data": { | |
| "created_at": "2019-09-01 07:47:50", | |
| "id": 1, | |
| "message": "Hello world!", | |
| "name": "Dana", | |
| "room_id": "a2451846-fa6a-2cb2-9a9e-96ff1bc54af9", | |
| "updated_at": "2019-09-01 07:47:50" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import { ROOM_FETCH } from '../actions'; | |
| import connection from '../lib/socket'; | |
| import Messages from '../components/Messages'; | |
| import AddMessage from '../components/AddMessage'; | |
| // a global variable so we can disconnect once we unmount | |
| let subscription; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ws from '@adonisjs/websocket-client'; | |
| import { getSocketProtocol } from '../utils/data'; | |
| export class SocketConnection { | |
| connect () { | |
| this.ws = Ws(`${getSocketProtocol()}${process.env.REACT_APP_API_URL}`) | |
| // .withApiToken(token) | |
| .connect(); |
OlderNewer