Skip to content

Commit

Permalink
made sync drawing with PUT fetch to API and save in db
Browse files Browse the repository at this point in the history
  • Loading branch information
denbite committed Jan 15, 2021
1 parent 41f5c50 commit d121754
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
45 changes: 33 additions & 12 deletions frontend/components/Board.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React, {useEffect, useRef} from 'react';
import { connect } from 'react-redux';
import styles from '../styles/Board.module.css';
import {createNewPic, addPointToLastPic} from '../store/board/actions'
import {createNewPic, addPointToLastPic, __transformBrushToKey} from '../store/board/actions'

export const Board = props => {
const store_points = props.points;
const currentBrushWidth = props.brushWidth;
const currentBrushColor = props.brushColor;

const ws = props.websocket

const canvas_ref = useRef(null);

function createNewPicEvent(e) {
Expand All @@ -34,23 +32,46 @@ export const Board = props => {
}

function saveLastPic(e) {
if (!props.websocket || !props.url) return;

console.log('event mouseup')

let key = currentBrushWidth + '.' + currentBrushColor;
let key = __transformBrushToKey({
width: currentBrushWidth,
color: currentBrushColor
})

let lastPic = store_points[key][ store_points[key].length - 1 ]

console.log(lastPic)

ws.send(JSON.stringify({
action: 'saveLastPic',
brush: {
width: currentBrushWidth,
color: currentBrushColor,
},
pic: lastPic
}));
fetch('http://192.168.0.100:8000/api/board', {
method:'PUT',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
},
body: new URLSearchParams({
board_url: props.url,
data_delta: JSON.stringify(lastPic),
key: key
})
})
.then(r => r.json())
.then(response => {
if (response.success){
props.websocket.send(JSON.stringify({
action: 'saveLastPic',
brush: {
width: currentBrushWidth,
color: currentBrushColor,
},
pic: lastPic
}));
} else {
console.log('error on fetch PUT /board: ', response.error.message)
}
})
}

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/b/[board_url].js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function SyncBoard() {
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<Toolbar websocket={websocket} />
<Board websocket={websocket}/>
<Board websocket={websocket} url={board_url}/>
</>
)
}
2 changes: 1 addition & 1 deletion frontend/store/board/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export const addPic = (pic, brush) => ({
}
})

const __transformBrushToKey = (brush) => (
export const __transformBrushToKey = (brush) => (
[brush.width, brush.color].join('.')
)
2 changes: 1 addition & 1 deletion frontend/store/toolbar/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export const TOOLBAR_BRUSH_WIDTH_BIG = 6

export const TOOLBAR_BRUSH_COLOR_RED = "#C0392B"
export const TOOLBAR_BRUSH_COLOR_BLUE = "#0056C5"
export const TOOLBAR_BRUSH_COLOR_GREEN = "#83BE42"
export const TOOLBAR_BRUSH_COLOR_GREEN = "#5BAC31"

0 comments on commit d121754

Please sign in to comment.