Skip to content

Commit

Permalink
feat(canvas): save user painting, only one for now, and no erasing on…
Browse files Browse the repository at this point in the history
…ly add, but kind of a cool star
  • Loading branch information
mysterycommand committed Apr 26, 2019
1 parent 7fcf326 commit 750d074
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/components/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ class App extends Component<{}, AppState> {
savePainting = (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
const { userSession } = this.state;
const { current } = this.canvasRef;

if (!current) {
return;
}

userSession.putFile(
'painting.json',
JSON.stringify({
data: '',
data: current.toDataURL('image/png'),
createdAt: Date.now(),
}),
{
Expand All @@ -64,6 +69,17 @@ class App extends Component<{}, AppState> {
);
};

fetchPainting = () => {
const { userSession } = this.state;
const { current } = this.canvasRef;

return !current
? Promise.resolve(JSON.stringify({}))
: userSession.getFile('painting.json', {
decrypt: false,
});
};

UNSAFE_componentWillMount() {
const { userSession } = this.state;

Expand All @@ -83,6 +99,7 @@ class App extends Component<{}, AppState> {
{userSession.isUserSignedIn() ? (
<Workspace
canvasRef={this.canvasRef}
fetchPainting={this.fetchPainting}
person={new Person(userSession.loadUserData().profile)}
savePainting={this.savePainting}
signOut={this.signOut}
Expand Down
27 changes: 26 additions & 1 deletion src/components/canvas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './style.css';

type CanvasProps = {
canvasRef: RefObject<HTMLCanvasElement>;
fetchPainting: () => Promise<string | ArrayBuffer>;
};

type CanvasState = {
Expand All @@ -26,12 +27,36 @@ class Canvas extends Component<CanvasProps, CanvasState> {
public componentDidMount() {
const {
canvasRef: { current },
fetchPainting,
} = this.props;

if (!current) {
if (!(current && fetchPainting)) {
return;
}

fetchPainting().then(file => {
const { data } = JSON.parse((file as string) || '{}');

if (!data) {
return;
}

const { canvasContext } = this.state;

if (!canvasContext) {
return;
}

const ctx = canvasContext as CanvasRenderingContext2D;

const img = new Image(current.width, current.height);
img.src = data;

setImmediate(() => {
ctx.drawImage(img, 0, 0);
});
});

this.setState({
canvasContext: current.getContext('2d'),
canvasRect: current.getBoundingClientRect(),
Expand Down
6 changes: 5 additions & 1 deletion src/components/canvas/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ it('renders without crashing', () => {
const div = document.createElement('div');

const mockCanvasRef = React.createRef<HTMLCanvasElement>();
const mockFetch = () => Promise.resolve(JSON.stringify({}));

ReactDOM.render(<Canvas canvasRef={mockCanvasRef} />, div);
ReactDOM.render(
<Canvas canvasRef={mockCanvasRef} fetchPainting={mockFetch} />,
div,
);
ReactDOM.unmountComponentAtNode(div);
});
4 changes: 3 additions & 1 deletion src/components/workspace/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import Button from '../button';

type WorkspaceProps = {
canvasRef: RefObject<HTMLCanvasElement>;
fetchPainting: () => Promise<string | ArrayBuffer>;
person: Person;
savePainting: (event: MouseEvent<HTMLButtonElement>) => void;
signOut: (event: MouseEvent<HTMLButtonElement>) => void;
};

const Workspace: FC<WorkspaceProps> = ({
canvasRef,
fetchPainting,
person,
savePainting,
signOut,
Expand All @@ -26,7 +28,7 @@ const Workspace: FC<WorkspaceProps> = ({
</h2>
<Button onClick={signOut}>Sign out</Button>
</header>
<Canvas canvasRef={canvasRef} />
<Canvas canvasRef={canvasRef} fetchPainting={fetchPainting} />
<footer className="footer">
<i />
<Button onClick={savePainting}>Save</Button>
Expand Down
2 changes: 2 additions & 0 deletions src/components/workspace/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ it('renders without crashing', () => {
const div = document.createElement('div');

const mockCanvasRef = React.createRef<HTMLCanvasElement>();
const mockFetch = () => Promise.resolve(JSON.stringify({}));
const mockPerson = new Person({ name: 'John Doe' });
const mockHandler = (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
Expand All @@ -16,6 +17,7 @@ it('renders without crashing', () => {
ReactDOM.render(
<Workspace
canvasRef={mockCanvasRef}
fetchPainting={mockFetch}
person={mockPerson}
savePainting={mockHandler}
signOut={mockHandler}
Expand Down

0 comments on commit 750d074

Please sign in to comment.