Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add free drawing space #7280

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/api/bootcamp/drawings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class API::Bootcamp::DrawingsController < API::Bootcamp::BaseController
def update
@drawing.update(code: params[:code]) if params[:code].present?
@drawing.update(title: params[:title]) if params[:title].present?
@drawing.update(background_slug: params[:background_slug]) if params[:background_slug].present?

render json: {}, status: :ok
end
Expand Down
17 changes: 16 additions & 1 deletion app/helpers/react_components/bootcamp/drawing_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ def data
{
drawing: {
uuid: drawing.uuid,
title: drawing.title
title: drawing.title,
background_slug: drawing.background_slug
},
code: {
code: drawing.code,
stored_at: drawing.updated_at
},
backgrounds: BACKGROUNDS,
links: {
update_code: Exercism::Routes.api_bootcamp_drawing_url(drawing),
drawings_index: Exercism::Routes.bootcamp_project_path(:drawing)
}
}
end
end

BACKGROUNDS = [
{
slug: "none",
title: "No background",
image_url: nil
},
{
slug: "room",
title: "A room to decorate",
image_url: "https://shared.cdn.galeriekodl.cz/plain/w:1200/rs:fit:1200:909:1/czM6Ly9zaGFyZWQucHJhZ3VlL2l0ZW1zLzAzMjY0NzQxLWFiNDQtNGE0Mi1iNDg2LTk2NjEwOWFkYTJlNS5qcGVn"
}
].freeze
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { CodeMirror } from '../SolveExercisePage/CodeMirror/CodeMirror'
import ErrorBoundary from '../common/ErrorBoundary/ErrorBoundary'
import { useDrawingEditorHandler } from './useDrawingEditorHandler'
import { useLocalStorage } from '@uidotdev/usehooks'
import Scrubber from './Scrubber/Scrubber'
import Scrubber from '../SolveExercisePage/Scrubber/Scrubber'
import { debounce } from 'lodash'
import { useSetupDrawingPage } from './useSetupDrawingPage'

export default function DrawingPage({
drawing,
code,
links,
backgrounds,
}: DrawingPageProps) {
const [savingStateLabel, setSavingStateLabel] = useState<string>('')

Expand All @@ -37,7 +38,8 @@ export default function DrawingPage({
viewContainerRef,
animationTimeline,
frames,
} = useDrawingEditorHandler({ code, links, drawing })
setBackgroundImage,
} = useDrawingEditorHandler()

const [editorLocalStorageValue, setEditorLocalStorageValue] = useLocalStorage(
'bootcamp-editor-value-' + drawing.uuid,
Expand All @@ -63,8 +65,10 @@ export default function DrawingPage({
<div id="bootcamp-solve-exercise-page">
<Header
links={links}
backgrounds={backgrounds}
savingStateLabel={savingStateLabel}
drawing={drawing}
setBackgroundImage={setBackgroundImage}
/>
<div className="page-body">
<div style={{ width: LHSWidth }} className="page-body-lhs">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react'
import React, { useCallback, useEffect, useState } from 'react'
import { wrapWithErrorBoundary } from '@/components/bootcamp/common/ErrorBoundary/wrapWithErrorBoundary'
import { assembleClassNames } from '@/utils/assemble-classnames'

Expand All @@ -11,7 +11,12 @@ function _Header({
links,
savingStateLabel,
drawing,
}: { savingStateLabel: string } & Pick<DrawingPageProps, 'links' | 'drawing'>) {
backgrounds,
setBackgroundImage,
}: {
savingStateLabel: string
setBackgroundImage: ((imageUrl: string | null) => void) | null
} & Pick<DrawingPageProps, 'links' | 'drawing' | 'backgrounds'>) {
const [titleInputValue, setTitleInputValue] = useState(drawing.title)
const [editMode, setEditMode] = useState(false)
const [titleSavingStateLabel, setTitleSavingStateLabel] = useState<string>(
Expand All @@ -28,6 +33,28 @@ function _Header({
.catch(() => setTitleSavingStateLabel('Try again'))
}, [links, titleInputValue])

const handleBackgroundChange = useCallback(
(background: Background) => {
if (setBackgroundImage) {
setBackgroundImage(background.imageUrl)
patchCanvasBackground(links, background.slug)
}
},
[setBackgroundImage]
)

// setup the background on mount
useEffect(() => {
if (setBackgroundImage && drawing.backgroundSlug) {
const background = backgrounds.find(
(bg) => bg.slug === drawing.backgroundSlug
)
if (background) {
setBackgroundImage(background.imageUrl)
}
}
}, [drawing?.backgroundSlug, setBackgroundImage])

return (
<div className="page-header">
<div className="ident">
Expand All @@ -42,6 +69,26 @@ function _Header({
{savingStateLabel}
</span>
)}

<select
onChange={(e) => {
const selectedBackground: Background = JSON.parse(
e.target.selectedOptions[0].dataset.background!
)
handleBackgroundChange(selectedBackground)
}}
value={drawing.backgroundSlug}
>
{backgrounds.map((background) => (
<option
key={background.slug}
value={background.slug}
data-background={JSON.stringify(background)}
>
{background.title}
</option>
))}
</select>
<div className="flex items-center gap-12">
{editMode ? (
<>
Expand Down Expand Up @@ -107,3 +154,24 @@ async function patchDrawingTitle(

return response.json()
}

async function patchCanvasBackground(
links: DrawingPageProps['links'],
backgroundSlug: string
) {
const response = await fetch(links.updateCode, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
background_slug: backgroundSlug,
}),
})

if (!response.ok) {
throw new Error('Failed to save code')
}

return response.json()
}

This file was deleted.

172 changes: 0 additions & 172 deletions app/javascript/components/bootcamp/DrawingPage/Scrubber/Scrubber.tsx

This file was deleted.

Loading
Loading