diff --git a/client/src/actions/editor.ts b/client/src/actions/editor.ts index db688dcb59f3732c6bda801b0aada1b3063a1238..526ad9ff6ae1698644e32ea02806c7a6e8e2cd78 100644 --- a/client/src/actions/editor.ts +++ b/client/src/actions/editor.ts @@ -18,16 +18,28 @@ export const getEditorCompetition = (id: string) => async (dispatch: AppDispatch if (getState().editor.activeSlideId === -1 && res.data.slides[0]) { setEditorSlideId(res.data.slides[0].id)(dispatch) } + const defaultViewType = getState().types.viewTypes.find((viewType) => viewType.name === 'Audience') + if (getState().editor.activeViewTypeId === -1 && defaultViewType) { + setEditorViewId(defaultViewType.id)(dispatch) + } }) .catch((err) => { console.log(err) }) } -// Set currentSlideId in editor state +// Set activeSlideId in editor state export const setEditorSlideId = (id: number) => (dispatch: AppDispatch) => { dispatch({ type: Types.SET_EDITOR_SLIDE_ID, payload: id, }) } + +// Set activeViewTypeId in editor state +export const setEditorViewId = (id: number) => (dispatch: AppDispatch) => { + dispatch({ + type: Types.SET_EDITOR_VIEW_ID, + payload: id, + }) +} diff --git a/client/src/actions/types.ts b/client/src/actions/types.ts index 445a8d86eadbcdcdd698838fcc78aeee3d307f6e..2e9fc776f4c1828427df8424ef93d9588f20afea 100644 --- a/client/src/actions/types.ts +++ b/client/src/actions/types.ts @@ -24,6 +24,7 @@ export default { SET_COMPETITIONS_COUNT: 'SET_COMPETITIONS_COUNT', SET_EDITOR_COMPETITION: 'SET_EDITOR_COMPETITION', SET_EDITOR_SLIDE_ID: 'SET_EDITOR_SLIDE_ID', + SET_EDITOR_VIEW_ID: 'SET_EDITOR_VIEW_ID', SET_PRESENTATION_COMPETITION: 'SET_PRESENTATION_COMPETITION', SET_PRESENTATION_SLIDE: 'SET_PRESENTATION_SLIDE', SET_PRESENTATION_SLIDE_PREVIOUS: 'SET_PRESENTATION_SLIDE_PREVIOUS', diff --git a/client/src/interfaces/ApiModels.ts b/client/src/interfaces/ApiModels.ts index 4de675fb43475f432cfa2f13a93f30c8ef46e782..faecc98f43bdaee26b871616c84cca7aac155913 100644 --- a/client/src/interfaces/ApiModels.ts +++ b/client/src/interfaces/ApiModels.ts @@ -81,6 +81,8 @@ export interface Component { w: number h: number type_id: number + view_type_id: number + slide_id: number } export interface ImageComponent extends Component { diff --git a/client/src/pages/presentationEditor/PresentationEditorPage.tsx b/client/src/pages/presentationEditor/PresentationEditorPage.tsx index 3c86afe9b76d2a618bd6396ff6c4e7edd782d8e7..50c3ae52999721aa27be54cae1cc7cd67084a0ce 100644 --- a/client/src/pages/presentationEditor/PresentationEditorPage.tsx +++ b/client/src/pages/presentationEditor/PresentationEditorPage.tsx @@ -14,7 +14,7 @@ import axios from 'axios' import React, { useEffect, useState } from 'react' import { Link, useParams } from 'react-router-dom' import { getCities } from '../../actions/cities' -import { getEditorCompetition, setEditorSlideId } from '../../actions/editor' +import { getEditorCompetition, setEditorSlideId, setEditorViewId } from '../../actions/editor' import { getTypes } from '../../actions/typesAction' import { useAppDispatch, useAppSelector } from '../../hooks' import { RichSlide } from '../../interfaces/ApiRichModels' @@ -165,6 +165,14 @@ const PresentationEditorPage: React.FC = () => { })((props: CheckboxProps) => <Checkbox color="default" {...props} />) const [checkbox, setCheckbox] = useState(false) + const viewTypes = useAppSelector((state) => state.types.viewTypes) + const changeView = (clickedViewTypeName: string) => { + const clickedViewTypeId = viewTypes.find((viewType) => viewType.name === clickedViewTypeName)?.id + if (clickedViewTypeId) { + dispatch(setEditorViewId(clickedViewTypeId)) + } + } + return ( <PresentationEditorContainer> <CssBaseline /> @@ -182,10 +190,10 @@ const PresentationEditorPage: React.FC = () => { <Typography className={classes.alignCheckboxText} variant="button"> Applicera ändringar på samtliga vyer </Typography> - <ViewButton variant="contained" color="secondary"> + <ViewButton variant="contained" color="secondary" onClick={() => changeView('Audience')}> Åskådarvy </ViewButton> - <ViewButton variant="contained" color="secondary"> + <ViewButton variant="contained" color="secondary" onClick={() => changeView('Team')}> Deltagarvy </ViewButton> </ViewButtonGroup> diff --git a/client/src/pages/presentationEditor/components/SlideSettings.tsx b/client/src/pages/presentationEditor/components/SlideSettings.tsx index 278887c1cb8b841224704ceb1f2e1bb9fa5c7b6a..41c1354fb8e1415bf9b81822f65b2acab2cb92ad 100644 --- a/client/src/pages/presentationEditor/components/SlideSettings.tsx +++ b/client/src/pages/presentationEditor/components/SlideSettings.tsx @@ -24,6 +24,7 @@ const SlideSettings: React.FC = () => { // Gets the slide with id=activeSlideId from the database. state.editor.competition.slides.find((slide) => slide && slide.id === state.editor.activeSlideId) ) + const activeViewTypeId = useAppSelector((state) => state.editor.activeViewTypeId) return ( <PanelContainer> @@ -47,9 +48,13 @@ const SlideSettings: React.FC = () => { <MultipleChoiceAlternatives activeSlide={activeSlide} competitionId={competitionId} /> )} - {activeSlide && <Texts activeSlide={activeSlide} competitionId={competitionId} />} + {activeSlide && ( + <Texts activeViewTypeId={activeViewTypeId} activeSlide={activeSlide} competitionId={competitionId} /> + )} - {activeSlide && <Images activeSlide={activeSlide} competitionId={competitionId} />} + {activeSlide && ( + <Images activeViewTypeId={activeViewTypeId} activeSlide={activeSlide} competitionId={competitionId} /> + )} <SettingsList> <ListItem button> diff --git a/client/src/pages/presentationEditor/components/TextComponentEdit.tsx b/client/src/pages/presentationEditor/components/TextComponentEdit.tsx index b03696f37987a83347f57fb81c6b25febcb84824..f6cda5760ec8aff5bce211a8caf7d2435a8d5c61 100644 --- a/client/src/pages/presentationEditor/components/TextComponentEdit.tsx +++ b/client/src/pages/presentationEditor/components/TextComponentEdit.tsx @@ -20,6 +20,7 @@ const TextComponentEdit = ({ component }: ImageComponentProps) => { const [content, setContent] = useState('') const [timerHandle, setTimerHandle] = React.useState<number | undefined>(undefined) const activeSlideId = useAppSelector((state) => state.editor.activeSlideId) + const activeViewTypeId = useAppSelector((state) => state.editor.activeViewTypeId) const dispatch = useAppDispatch() useEffect(() => { diff --git a/client/src/pages/presentationEditor/components/slideSettingsComponents/Images.tsx b/client/src/pages/presentationEditor/components/slideSettingsComponents/Images.tsx index fa087dccbb39c77a0a2e433d5a6a6c8b4d201fd1..bc83d2fa8fec821d1058aab1ed2af5114d0d00d2 100644 --- a/client/src/pages/presentationEditor/components/slideSettingsComponents/Images.tsx +++ b/client/src/pages/presentationEditor/components/slideSettingsComponents/Images.tsx @@ -11,11 +11,12 @@ import { ImageComponent, Media } from '../../../../interfaces/ApiModels' import { useAppDispatch, useAppSelector } from '../../../../hooks' type ImagesProps = { + activeViewTypeId: number activeSlide: RichSlide competitionId: string } -const Images = ({ activeSlide, competitionId }: ImagesProps) => { +const Images = ({ activeViewTypeId, activeSlide, competitionId }: ImagesProps) => { const dispatch = useAppDispatch() const uploadFile = async (formData: FormData) => { @@ -37,6 +38,7 @@ const Images = ({ activeSlide, competitionId }: ImagesProps) => { y: 0, media_id: media.id, type_id: 2, + view_type_id: activeViewTypeId, } await axios .post(`/api/competitions/${competitionId}/slides/${activeSlide?.id}/components`, imageData) diff --git a/client/src/pages/presentationEditor/components/slideSettingsComponents/Texts.tsx b/client/src/pages/presentationEditor/components/slideSettingsComponents/Texts.tsx index 39cf09af3ca31b73f30c258570e28365117a4282..b1e9fa9b68e517615bb2b73029b108724c752183 100644 --- a/client/src/pages/presentationEditor/components/slideSettingsComponents/Texts.tsx +++ b/client/src/pages/presentationEditor/components/slideSettingsComponents/Texts.tsx @@ -9,11 +9,12 @@ import axios from 'axios' import { getEditorCompetition } from '../../../../actions/editor' type TextsProps = { + activeViewTypeId: number activeSlide: RichSlide competitionId: string } -const Texts = ({ activeSlide, competitionId }: TextsProps) => { +const Texts = ({ activeViewTypeId, activeSlide, competitionId }: TextsProps) => { const texts = useAppSelector( (state) => state.editor.competition.slides @@ -29,6 +30,7 @@ const Texts = ({ activeSlide, competitionId }: TextsProps) => { text: 'Ny text', w: 315, h: 50, + view_type_id: activeViewTypeId, }) dispatch(getEditorCompetition(competitionId)) } diff --git a/client/src/reducers/editorReducer.ts b/client/src/reducers/editorReducer.ts index 88e6e3687ea6f4348ebd5956e677ef26319e33c2..4f245d1d05638cc059cf8d26921642d8239c169a 100644 --- a/client/src/reducers/editorReducer.ts +++ b/client/src/reducers/editorReducer.ts @@ -5,6 +5,7 @@ import { RichCompetition } from '../interfaces/ApiRichModels' interface EditorState { competition: RichCompetition activeSlideId: number + activeViewTypeId: number loading: boolean } @@ -19,6 +20,7 @@ const initialState: EditorState = { background_image: undefined, }, activeSlideId: -1, + activeViewTypeId: -1, loading: true, } @@ -35,6 +37,11 @@ export default function (state = initialState, action: AnyAction) { ...state, activeSlideId: action.payload as number, } + case Types.SET_EDITOR_VIEW_ID: + return { + ...state, + activeViewTypeId: action.payload as number, + } default: return state } diff --git a/server/app/apis/components.py b/server/app/apis/components.py index 8ab67b8cb8bd001c9641b6331325f328639b581e..8f2531513a54fd6392983c7bd0b0657c3d4c6455 100644 --- a/server/app/apis/components.py +++ b/server/app/apis/components.py @@ -22,6 +22,7 @@ component_edit_parser.add_argument("media_id", type=str, location="json") component_create_parser = component_edit_parser.copy() component_create_parser.add_argument("type_id", type=int, required=True, location="json") +component_create_parser.add_argument("view_type_id", type=int, required=True, location="json") @api.route("/<component_id>") diff --git a/server/app/core/schemas.py b/server/app/core/schemas.py index 0abe02526ef28041e7c0722fc5aa0b0bc5e7f55d..31e79e69a8c181a27bf0c9ee79f8b3ce62c19ecd 100644 --- a/server/app/core/schemas.py +++ b/server/app/core/schemas.py @@ -161,6 +161,7 @@ class ComponentSchema(BaseSchema): h = ma.auto_field() slide_id = ma.auto_field() type_id = ma.auto_field() + view_type_id = ma.auto_field() text = fields.fields.String() media = fields.Nested(MediaSchema, many=False) diff --git a/server/app/core/sockets.py b/server/app/core/sockets.py index 00b2ddf5cc9313aed0e3eded03dc05d6548a55f8..b62f82c9365818ac76faa0cd05e09bc795d0c071 100644 --- a/server/app/core/sockets.py +++ b/server/app/core/sockets.py @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) logger.propagate = False logger.setLevel(logging.INFO) -formatter = logging.Formatter('[%(levelname)s] %(funcName)s: %(message)s') +formatter = logging.Formatter("[%(levelname)s] %(funcName)s: %(message)s") stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) @@ -44,7 +44,9 @@ def start_presentation(data): competition_id = data["competition_id"] if competition_id in presentations: - logger.error(f"Client '{request.sid}' failed to start competition '{competition_id}', presentation already active") + logger.error( + f"Client '{request.sid}' failed to start competition '{competition_id}', presentation already active" + ) return presentations[competition_id] = { @@ -58,16 +60,21 @@ def start_presentation(data): logger.info(f"Client '{request.sid}' started competition '{competition_id}'") + @sio.on("end_presentation") def end_presentation(data): competition_id = data["competition_id"] if competition_id not in presentations: - logger.error(f"Client '{request.sid}' failed to end presentation '{competition_id}', no such presentation exists") + logger.error( + f"Client '{request.sid}' failed to end presentation '{competition_id}', no such presentation exists" + ) return if request.sid not in presentations[competition_id]["clients"]: - logger.error(f"Client '{request.sid}' failed to end presentation '{competition_id}', client not in presentation") + logger.error( + f"Client '{request.sid}' failed to end presentation '{competition_id}', client not in presentation" + ) return if presentations[competition_id]["clients"][request.sid]["view_type"] != "Operator": @@ -96,11 +103,15 @@ def join_presentation(data): competition_id = item_code.competition_id if competition_id not in presentations: - logger.error(f"Client '{request.sid}' failed to join presentation '{competition_id}', no such presentation exists") + logger.error( + f"Client '{request.sid}' failed to join presentation '{competition_id}', no such presentation exists" + ) return if request.sid in presentations[competition_id]["clients"]: - logger.error(f"Client '{request.sid}' failed to join presentation '{competition_id}', client already in presentation") + logger.error( + f"Client '{request.sid}' failed to join presentation '{competition_id}', client already in presentation" + ) return # TODO: Write function in database controller to do this @@ -120,21 +131,29 @@ def set_slide(data): slide_order = data["slide_order"] if competition_id not in presentations: - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', no such presentation exists") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', no such presentation exists" + ) return if request.sid not in presentations[competition_id]["clients"]: - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client not in presentation") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client not in presentation" + ) return if presentations[competition_id]["clients"][request.sid]["view_type"] != "Operator": - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client is not operator") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client is not operator" + ) return num_slides = db.session.query(Slide).filter(Slide.competition_id == competition_id).count() if not (0 <= slide_order < num_slides): - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', slide number {slide_order} does not exist") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', slide number {slide_order} does not exist" + ) return presentations[competition_id]["slide"] = slide_order @@ -151,15 +170,21 @@ def set_timer(data): timer = data["timer"] if competition_id not in presentations: - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', no such presentation exists") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', no such presentation exists" + ) return if request.sid not in presentations[competition_id]["clients"]: - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client not in presentation") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client not in presentation" + ) return if presentations[competition_id]["clients"][request.sid]["view_type"] != "Operator": - logger.error(f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client is not operator") + logger.error( + f"Client '{request.sid}' failed to set slide in presentation '{competition_id}', client is not operator" + ) return # TODO: Save timer in presentation, maybe? @@ -168,4 +193,3 @@ def set_timer(data): logger.debug(f"Emitting event 'set_timer' to room {competition_id} including self") logger.info(f"Client '{request.sid}' set timer '{timer}' in presentation '{competition_id}'") - diff --git a/server/app/database/controller/add.py b/server/app/database/controller/add.py index a83f5b95e8988a3d0024ca6d2f3b9601426147cc..f7b0edbb17ac91ea9ef3786182ff58614b43cd45 100644 --- a/server/app/database/controller/add.py +++ b/server/app/database/controller/add.py @@ -59,7 +59,7 @@ def db_add(item): return item -def component(type_id, slide_id, x=0, y=0, w=0, h=0, **data): +def component(type_id, slide_id, view_type_id, x=0, y=0, w=0, h=0, **data): """ Adds a component to the slide at the specified coordinates with the provided size and data . @@ -80,10 +80,10 @@ def component(type_id, slide_id, x=0, y=0, w=0, h=0, **data): h *= ratio if type_id == 1: - item = db_add(TextComponent(slide_id, type_id, x, y, w, h)) + item = db_add(TextComponent(slide_id, type_id, view_type_id, x, y, w, h)) item.text = data.get("text") elif type_id == 2: - item = db_add(ImageComponent(slide_id, type_id, x, y, w, h)) + item = db_add(ImageComponent(slide_id, type_id, view_type_id, x, y, w, h)) item.media_id = data.get("media_id") else: abort(codes.BAD_REQUEST, f"Invalid type_id{type_id}") diff --git a/server/app/database/controller/copy.py b/server/app/database/controller/copy.py index 7e9d28d02381dfe36e2a0c7792745618b595588e..1874b5dc548be39b1965fb2d4b041a957e26fe47 100644 --- a/server/app/database/controller/copy.py +++ b/server/app/database/controller/copy.py @@ -45,6 +45,7 @@ def _component(item_component, item_slide_new): add.component( item_component.type_id, item_slide_new.id, + item_component.view_type_id, item_component.x, item_component.y, item_component.w, diff --git a/server/app/database/models.py b/server/app/database/models.py index 3012938955f5c9e4fe1e285a0d95478ca7f7b8dd..c84677b2d04fc7ce2554548d67fb3d8a21d5ccf2 100644 --- a/server/app/database/models.py +++ b/server/app/database/models.py @@ -199,13 +199,14 @@ class Component(db.Model): __mapper_args__ = {"polymorphic_on": type_id} - def __init__(self, slide_id, type_id, x=0, y=0, w=1, h=1): + def __init__(self, slide_id, type_id, view_type_id, x=0, y=0, w=1, h=1): self.x = x self.y = y self.w = w self.h = h self.slide_id = slide_id self.type_id = type_id + self.view_type_id = view_type_id class TextComponent(Component): diff --git a/server/populate.py b/server/populate.py index 6a4be4ef42cbe14d938466282c6db2addb92dc57..8bd2badc54ba2f666e2e18b5cb8e042f623d0c91 100644 --- a/server/populate.py +++ b/server/populate.py @@ -86,7 +86,7 @@ def _add_items(): y = random.randrange(1, 500) w = random.randrange(150, 400) h = random.randrange(150, 400) - dbc.add.component(1, item_slide.id, x, y, w, h, text=f"hej{k}") + dbc.add.component(1, item_slide.id, 1, x, y, w, h, text=f"hej{k}") # item_slide = dbc.add.slide(item_comp) # item_slide.title = f"Slide {len(item_comp.slides)}" diff --git a/server/tests/test_helpers.py b/server/tests/test_helpers.py index b5f1e54e136b759d9924a534acf2f37e6f7b08cd..9603fa59d5e8b11872367dd1a73c83e334e600f1 100644 --- a/server/tests/test_helpers.py +++ b/server/tests/test_helpers.py @@ -59,7 +59,7 @@ def add_default_values(): # dbc.add.question(name=f"Q{i+1}", total_score=i + 1, type_id=1, slide_id=item_slide.id) # Add text component - dbc.add.component(1, item_slide.id, i, 2 * i, 3 * i, 4 * i, text="Text") + dbc.add.component(1, item_slide.id, 1, i, 2 * i, 3 * i, 4 * i, text="Text") def get_body(response):