Skip to content

Commit

Permalink
Merge pull request #101 from partio-scout/fix/groupList-accept-tasks
Browse files Browse the repository at this point in the history
Fix bug when adding achievements for groupmembers
  • Loading branch information
linneamyl authored Jun 8, 2022
2 parents 40daff7 + b44cebb commit 0a2cd49
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 144 deletions.
46 changes: 25 additions & 21 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,20 @@ export const postTaskEntry = async (entry) => {
}

export const postTaskGroupEntry = async (data, taskgroup_guid) => {
const res = await fetch(
`${API_URL}/groups/mark-taskgroup-done/${taskgroup_guid}`,
{
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
}
)
return await res.json()
for (let group of Object.values(data.groups)) {
const res = await fetch(
`${API_URL}/groups/mark-taskgroup-done/${taskgroup_guid}`,
{
method: 'POST',
body: JSON.stringify(group),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
}
)
return await res.json()
}
}

export const postMemberTaskEntry = async (entry) => {
Expand Down Expand Up @@ -118,15 +120,17 @@ export const removeMemberTaskEntry = async (entry) => {
}

export const acceptGroupMemberTasks = async (data, taskId) => {
const res = await fetch(`${API_URL}/groups/mark-task-done/${taskId}`, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
})
return res.json()
for (let group of Object.values(data)) {
const res = await fetch(`${API_URL}/groups/mark-task-done/${taskId}`, {
method: 'POST',
body: JSON.stringify(group),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
})
return res.json()
}
}

export const fetchUserTasks = async () => {
Expand Down
126 changes: 5 additions & 121 deletions src/views/AcceptTasks/group.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
import React, { useEffect } from 'react'
import styled, { keyframes, css } from 'styled-components'
import { useParams } from 'react-router-dom'
Expand All @@ -9,15 +10,8 @@ import {
AccordionItemPanel,
} from 'react-accessible-accordion'
import ListItem from 'components/ListItem'
import { COMPLETION_STATUS, ITEM_TYPES, TASK_GROUP_STATUS } from 'consts'
import { ITEM_TYPES } from 'consts'
import { useSelector } from 'react-redux'
import { StyledAcceptIcon } from '../../components/TaskActionsIcons'
import { useDispatch } from 'react-redux'
import {
updateGroupMemberTask,
updateGroupMemberTaskGroup,
} from '../../redux/actionCreators'
import { acceptGroupMemberTasks, postTaskGroupEntry } from '../../api'
import { getTermInLanguage } from '../../helpers'
import GroupMember from './GroupMember'

Expand All @@ -37,43 +31,6 @@ const Content = styled.div`
margin-bottom: 2rem;
`

const AcceptTasksAction = styled.div`
position: fixed;
bottom: 0;
left: 0;
box-sizing: border-box;
width: 100%;
padding: 3rem;
color: ${({ theme }) => theme.color.text};
background-color: ${({ theme }) => theme.color.background};
z-index: 1;
animation: ${keyframes`
0% {
opacity: 0;
}
100% {
opacity: 1;
}
`} 200ms linear;
`

const ActivityItem = styled.div`
display: flex;
align-items: center;
> span {
padding: 1rem;
}
:last-child {
justify-content: center;
> span {
padding-top: 2rem;
}
}
`

const StyledListItem = styled.div`
padding: 0.25rem;
text-decoration: none;
Expand All @@ -92,7 +49,6 @@ const StyledListHeading = styled.h4`
padding: 0 0 0 0.25rem;
text-decoration: underline;
`
const initialList = []

const getInitialCheckboxData = (group) =>
group.members.map((member) => ({
Expand All @@ -103,16 +59,12 @@ const getInitialCheckboxData = (group) =>
taskGroups: member.memberTaskGroups,
}))

const Group = ({ group, isLast }) => {
const dispatch = useDispatch()
const Group = ({ group, isLast, setMemberIdList }) => {
const { taskGuid } = useParams()
const groupsData = useSelector((state) => state.user.userGroups)
const translations = useSelector((state) => state.translations)
const itemsByGuid = useSelector((state) => state.itemsByGuid)
const user = useSelector((state) => state.user)
const activityGroupById = useSelector((state) => state.activityGroups)
const [memberIdList, setMemberIdList] = React.useState(initialList)
const [selectedGroup, setSelectedGroup] = React.useState()
const [checkboxData, setCheckboxData] = React.useState(
getInitialCheckboxData(group)
)
Expand Down Expand Up @@ -147,14 +99,6 @@ const Group = ({ group, isLast }) => {
return isGroupLeader
}

function updateGroup(group) {
if (selectedGroup) {
setSelectedGroup(null)
} else {
setSelectedGroup(group)
}
}

function handleChange(event) {
if (event.target.name === 'checkAll') {
checkboxData.map((member) => {
Expand All @@ -171,7 +115,7 @@ const Group = ({ group, isLast }) => {
return idList
}, [])

setMemberIdList(editableList)
setMemberIdList(editableList, group.id)
}

function handleCheckboxSelection(memberId, isChecked) {
Expand All @@ -187,51 +131,6 @@ const Group = ({ group, isLast }) => {
})
}

async function handleSubmit() {
try {
const data = {
userIds: memberIdList,
}
await acceptGroupMemberTasks(data, taskGuid)
for (let id of memberIdList) {
dispatch(
updateGroupMemberTask({
task_guid: taskGuid,
user_guid: Number(id),
completion_status: TASK_GROUP_STATUS.COMPLETED,
groupGuid: Number(selectedGroup),
})
)
}
} catch (e) {
console.log(e)
}
setMemberIdList(initialList)
}

async function handleTaskGroupSubmit() {
try {
const data = {
userIds: memberIdList,
group_leader_name: user.name,
}
await postTaskGroupEntry(data, taskGuid)
for (let id of memberIdList) {
dispatch(
updateGroupMemberTaskGroup({
taskgroup_guid: taskGuid,
user_guid: Number(id),
completed: COMPLETION_STATUS.COMPLETED,
groupGuid: Number(selectedGroup),
})
)
}
} catch (e) {
console.log(e)
}
setMemberIdList(initialList)
}

const renderMember = (member, checkFunction) => {
return (
checkFunction(member) && (
Expand All @@ -258,7 +157,7 @@ const Group = ({ group, isLast }) => {
<StyledAcceptTasks isLast={isLast}>
<Accordion
allowZeroExpanded
onChange={() => updateGroup(ageGroupId)}
// onChange={() => updateGroup(ageGroupId)}
key={ageGroupId}
>
<AccordionItem key={ageGroupId}>
Expand Down Expand Up @@ -309,21 +208,6 @@ const Group = ({ group, isLast }) => {
</AccordionItemPanel>
</AccordionItem>
</Accordion>
{memberIdList.length > 0 && item.type === 'TASK' ? (
<AcceptTasksAction onClick={handleSubmit}>
<ActivityItem>
<StyledAcceptIcon />
{getTermInLanguage(translations, 'lisaa-valituille')}
</ActivityItem>
</AcceptTasksAction>
) : (
<AcceptTasksAction onClick={handleTaskGroupSubmit}>
<ActivityItem>
<StyledAcceptIcon />
{getTermInLanguage(translations, 'lisaa-valituille')}
</ActivityItem>
</AcceptTasksAction>
)}
</StyledAcceptTasks>
)
}
Expand Down
Loading

0 comments on commit 0a2cd49

Please sign in to comment.