Skip to content
Open
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
11 changes: 10 additions & 1 deletion src/store/goalsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ export const goalsSlice = createSlice({
updateGoal: (state, action: PayloadAction<Goal>) => {
state.map[action.payload.id] = action.payload
},

createGoals: (state, action: PayloadAction<Goal[]>) => {
action.payload.forEach((goal) => {
state.map[goal.id] = goal
if (!state.list.includes(goal.id)) {
state.list.push(goal.id)
}
})
},
},
})

export const { createGoal, updateGoal } = goalsSlice.actions
export const { createGoal, updateGoal, createGoals } = goalsSlice.actions

export const selectGoalsMap = (state: RootState) => state.goals.map
export const selectGoalsList = (state: RootState) => state.goals.list
Expand Down
12 changes: 9 additions & 3 deletions src/ui/pages/Main/goals/GoalsSection.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { faPlusCircle } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import React, { useEffect } from 'react'
import { useEffect } from 'react'
import styled from 'styled-components'
import { createGoal as createGoalApi, getGoals } from '../../../../api/lib'
import { createGoal as createGoalRedux, selectGoalsList } from '../../../../store/goalsSlice'
import {
createGoal as createGoalRedux,
createGoals as createGoalsRedux,
selectGoalsList,
} from '../../../../store/goalsSlice'
import { useAppDispatch, useAppSelector } from '../../../../store/hooks'
import {
setContent as setContentRedux,
Expand All @@ -21,7 +25,9 @@ export default function GoalsSection() {
useEffect(() => {
async function fetch() {
const goals = await getGoals()
goals?.forEach((goal) => dispatch(createGoalRedux(goal)))
if (goals != null) {
dispatch(createGoalsRedux(goals))
}
}
fetch()
}, [dispatch])
Expand Down