Skip to content

Commit

Permalink
Merge commit 'cdfd18cc6c5b878e0a5f30062f50cf7261fd4d27'
Browse files Browse the repository at this point in the history
  • Loading branch information
advu committed Oct 17, 2024
2 parents 9960784 + cdfd18c commit 1d28841
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
14 changes: 14 additions & 0 deletions client/components/GroupView/GroupFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getTestQuestions } from 'Utilities/redux/testReducer'
import { Button } from 'react-bootstrap'
import useWindowDimensions from 'Utilities/windowDimensions'
import GroupLearningSettingsModal from './GroupLearningSettingsModal'
import ImportStoryModal from './ImportStoryModal'

const GroupFunctions = ({
group,
Expand All @@ -29,6 +30,7 @@ const GroupFunctions = ({
} = group
const teacherView = useSelector(({ user }) => user.data.user.is_teacher)
const [learningModalGroupId, setLearningModalGroupId] = useState(null)
const [importStoryModalOpen, setImportStoryModalOpen] = useState(false)
const { width } = useWindowDimensions()
const testEnabled = currTestDeadline - Date.now() > 0
const testButtonVariant = testEnabled ? 'danger' : 'primary'
Expand Down Expand Up @@ -89,6 +91,9 @@ const GroupFunctions = ({
dispatch(getTestQuestions(language, groupId, true))
}




return (
<>
{width >= 640 ? (
Expand All @@ -114,6 +119,9 @@ const GroupFunctions = ({
groupId={learningModalGroupId}
/>
)}
{isTeaching && teacherView && (
<ImportStoryModal open={importStoryModalOpen} setOpen={setImportStoryModalOpen} groupId={groupId} />
)}
{isTeaching && teacherView && (
<Button
data-cy="enable-test-button"
Expand Down Expand Up @@ -146,6 +154,12 @@ const GroupFunctions = ({
<Icon name="key" /> <FormattedMessage id="show-group-token" />
</Button>
)}
{isTeaching && teacherView && (
<Button onClick={()=> setImportStoryModalOpen(true)}>
<Icon name="share" /> <FormattedMessage id="import-story" />
</Button>

)}
</div>
) : (
<SemanticButton.Group>
Expand Down
74 changes: 74 additions & 0 deletions client/components/GroupView/ImportStoryModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { Modal, Dropdown, Input } from 'semantic-ui-react'
import { FormattedMessage, FormattedHTMLMessage, useIntl } from 'react-intl'
import { Button } from 'react-bootstrap'
import { importStoriesFromGroup } from 'Utilities/redux/groupsReducer'
import { getAllStories } from 'Utilities/redux/storiesReducer'

const ImportStoryModal = ({ open, setOpen, groupId }) => {
const dispatch = useDispatch()
const intl = useIntl()
const { groups, pending, storyImported } = useSelector(({ groups }) => groups)
const [selectedGroups, setSelectedGroups] = useState([])
const [message, setMessage] = useState('')
const group = groups.find(group => group.group_id === groupId)

const options = groups.filter(group => group.group_id !== groupId).map(
group => ({key: group.group_id, text: group.groupName, value: group.group_id}))
const submitGroupImport = async () => {
// console.log(selectedGroups)
// console.log(message)
await dispatch(importStoriesFromGroup(groupId, selectedGroups, message))
dispatch(
getAllStories(group.language, {
sort_by: 'date',
order: -1,
})
)
setOpen(false)
setSelectedGroups([])
setMessage('')
}

return (
<Modal onClose={() => setOpen(false)} onOpen={() => setOpen(true)} open={open}>
<Modal.Header>
<FormattedMessage id="import-story" /> : {group.groupName}
</Modal.Header>
<Modal.Content style={{ display: 'flex', flexDirection: 'column', height: '260px' }}>
<h2 style={{ fontSize: '17px', fontWeight: '550' }}>
<FormattedMessage id="import-story-label" />
</h2>
<FormattedHTMLMessage id="import-story-description" />
<Dropdown
placeholder={intl.formatMessage({id: 'import-from'})}
fluid multiple selection
options={options}
onChange={(e, { value }) => setSelectedGroups(value)}
style={{ marginTop: '1em' }}
/>
<span style={{marginTop: '1em'}}>
<label style={{marginRight: '2em', fontWeight: 'bold'}}><FormattedMessage id="import-story-message" /></label>
<Input type="text" onChange={(e)=> setMessage(e.target.value)} />
</span>
</Modal.Content>
<Modal.Actions>
<Button onClick={submitGroupImport} disabled={selectedGroups.length === 0}>
<FormattedMessage id="import" />
</Button>
<Button
style={{ marginLeft: '1em' }}
onClick={() => {
setOpen(false)
}}
variant="secondary"
>
<FormattedMessage id="cancel" />
</Button>
</Modal.Actions>
</Modal>
)
}

export default ImportStoryModal
33 changes: 32 additions & 1 deletion client/util/redux/groupsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,18 @@ export const confirmGroupInvitation = token => {
return callBuilder(route, prefix, 'post', payload)
}

export const importStoriesFromGroup = (groupId, selectedGroups, message) => {
const route = `/groups/${groupId}/import`
const prefix = 'IMPORT_STORIES'
const payload = { src_group_ids: selectedGroups, message }
return callBuilder(route, prefix, 'post', payload)
}

export const emptyLastAddInfo = () => ({
type: 'EMPTY_LAST_ADD_INFO',
})

export default (state = { groups: [], joinPending: false, deleteSuccessful: false }, action) => {
export default (state = { groups: [], joinPending: false, deleteSuccessful: false, storyImported: -1 }, action) => {
switch (action.type) {
case 'GET_GROUPS_ATTEMPT':
return {
Expand Down Expand Up @@ -408,6 +415,30 @@ export default (state = { groups: [], joinPending: false, deleteSuccessful: fals
...state,
pending: false,
}

case 'IMPORT_STORIES_ATTEMPT':
return {
...state,
pending: true,
}
case 'IMPORT_STORIES_FAILURE':
return {
...state,
pending: false,
}
case 'IMPORT_STORIES_SUCCESS':
return {
...state,
storyImported: action.response.num_story_imported,
pending: false,
}

case 'CLEAN_STORY_IMPORT':
return {
...state,
storyImported: -1,
}

default:
return state
}
Expand Down

0 comments on commit 1d28841

Please sign in to comment.