Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
teknikattan-scoring-system
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tddd96-grupp1
teknikattan-scoring-system
Merge requests
!109
Resolve "Implement background image"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Implement background image"
144-implement-background-image
into
dev
Overview
0
Commits
4
Pipelines
2
Changes
3
Merged
Albin Henriksson
requested to merge
144-implement-background-image
into
dev
3 years ago
Overview
0
Commits
4
Pipelines
2
Changes
3
Expand
Closes
#144 (closed)
0
0
Merge request reports
Viewing commit
baca4dfc
Prev
Next
Show latest version
3 files
+
5
−
4
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
baca4dfc
Fix broken components
· baca4dfc
Albin Henriksson
authored
3 years ago
client/src/pages/presentationEditor/components/BackgroundImageSelect.tsx
0 → 100644
+
126
−
0
Options
import
{
ListItem
,
ListItemText
,
Typography
}
from
'
@material-ui/core
'
import
React
,
{
useState
}
from
'
react
'
import
{
useAppDispatch
,
useAppSelector
}
from
'
../../../hooks
'
import
{
AddButton
,
AddBackgroundButton
,
Center
,
HiddenInput
,
ImportedImage
,
SettingsList
,
ImageNameText
,
ImageTextContainer
,
}
from
'
./styled
'
import
CloseIcon
from
'
@material-ui/icons/Close
'
import
axios
from
'
axios
'
import
{
Media
}
from
'
../../../interfaces/ApiModels
'
import
{
getEditorCompetition
}
from
'
../../../actions/editor
'
import
{
uploadFile
}
from
'
../../../utils/uploadImage
'
type
BackgroundImageSelectProps
=
{
variant
:
'
competition
'
|
'
slide
'
}
const
BackgroundImageSelect
=
({
variant
}:
BackgroundImageSelectProps
)
=>
{
const
activeSlideId
=
useAppSelector
((
state
)
=>
state
.
editor
.
activeSlideId
)
const
backgroundImage
=
useAppSelector
((
state
)
=>
{
if
(
variant
===
'
competition
'
)
return
state
.
editor
.
competition
.
background_image
else
return
state
.
editor
.
competition
.
slides
.
find
((
slide
)
=>
slide
.
id
===
activeSlideId
)?.
background_image
})
const
competitionId
=
useAppSelector
((
state
)
=>
state
.
editor
.
competition
.
id
)
const
dispatch
=
useAppDispatch
()
const
updateBackgroundImage
=
async
(
mediaId
:
number
)
=>
{
// Creates a new image component on the database using API call.
if
(
variant
===
'
competition
'
)
{
await
axios
.
put
(
`/api/competitions/
${
competitionId
}
`
,
{
background_image_id
:
mediaId
})
.
then
(()
=>
{
dispatch
(
getEditorCompetition
(
competitionId
.
toString
()))
})
.
catch
(
console
.
log
)
}
else
{
await
axios
.
put
(
`/api/competitions/
${
competitionId
}
/slides/
${
activeSlideId
}
`
,
{
background_image_id
:
mediaId
})
.
then
(()
=>
{
dispatch
(
getEditorCompetition
(
competitionId
.
toString
()))
})
.
catch
(
console
.
log
)
}
}
const
removeBackgroundImage
=
async
()
=>
{
// Removes background image media and from competition using API calls.
await
axios
.
delete
(
`/api/media/images/
${
backgroundImage
?.
id
}
`
).
catch
(
console
.
log
)
if
(
variant
===
'
competition
'
)
{
await
axios
.
put
(
`/api/competitions/
${
competitionId
}
`
,
{
background_image_id
:
null
})
.
then
(()
=>
{
dispatch
(
getEditorCompetition
(
competitionId
.
toString
()))
})
.
catch
(
console
.
log
)
}
else
{
await
axios
.
put
(
`/api/competitions/
${
competitionId
}
/slides/
${
activeSlideId
}
`
,
{
background_image_id
:
null
})
.
then
(()
=>
{
dispatch
(
getEditorCompetition
(
competitionId
.
toString
()))
})
.
catch
(
console
.
log
)
}
}
const
handleFileSelected
=
async
(
e
:
React
.
ChangeEvent
<
HTMLInputElement
>
)
=>
{
// Reads the selected image file and uploads it to the server.
// Creates a new image component containing the file.
if
(
e
.
target
.
files
!==
null
&&
e
.
target
.
files
[
0
])
{
const
files
=
Array
.
from
(
e
.
target
.
files
)
const
file
=
files
[
0
]
const
formData
=
new
FormData
()
formData
.
append
(
'
image
'
,
file
)
const
media
=
await
uploadFile
(
formData
,
competitionId
.
toString
())
if
(
media
)
{
updateBackgroundImage
(
media
.
id
)
}
}
}
return
(
<
SettingsList
>
{
!
backgroundImage
&&
(
<
ListItem
button
style
=
{
{
padding
:
0
}
}
>
<
HiddenInput
accept
=
"image/*"
id
=
"background-button-file"
multiple
type
=
"file"
onChange
=
{
handleFileSelected
}
/>
<
AddBackgroundButton
htmlFor
=
"background-button-file"
>
<
Center
>
<
AddButton
variant
=
"button"
>
Välj bakgrundsbild...
</
AddButton
>
</
Center
>
</
AddBackgroundButton
>
</
ListItem
>
)
}
{
backgroundImage
&&
(
<>
<
ListItem
divider
>
<
ImageTextContainer
>
<
ListItemText
>
Bakgrundsbild
</
ListItemText
>
<
Typography
variant
=
"body2"
>
(Bilden bör ha sidförhållande 16:9)
</
Typography
>
</
ImageTextContainer
>
</
ListItem
>
<
ListItem
divider
button
>
<
ImportedImage
src
=
{
`/static/images/thumbnail_
${
backgroundImage
.
filename
}
`
}
/>
<
Center
>
<
ImageNameText
primary
=
{
backgroundImage
.
filename
}
/>
</
Center
>
<
CloseIcon
onClick
=
{
removeBackgroundImage
}
/>
</
ListItem
>
</>
)
}
</
SettingsList
>
)
}
export
default
BackgroundImageSelect
Loading