Skip to content

Commit

Permalink
Added functionality to move tasks from local storage to database upon…
Browse files Browse the repository at this point in the history
… user sign-up. Ensured tasks can be created, read, updated, and deleted whether the user is signed up or not.
  • Loading branch information
trisDeveloper committed Feb 25, 2024
1 parent 9e8e752 commit a8a1a38
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 deletions.
13 changes: 13 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"chart.js": "^3.9.1",
"pinia": "^2.1.7",
"sortablejs": "^1.15.2",
"uuid": "^9.0.1",
"v-calendar": "^3.1.2",
"vue": "^3.4.15",
"vue-router": "^4.2.5",
Expand Down
34 changes: 25 additions & 9 deletions frontend/src/components/task-card.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,42 @@ import axios from 'axios'
import { useStore } from '@/stores'
const props = defineProps(['fetchData', 'task'])
const store = useStore()
let nextTaskId = localStorage.getItem('nextTaskId') || 1
const deleteTask = async (task) => {
try {
task.done = !task.done
closeTaskCard()
await axios.delete(`/api/users/${store.user.id}/tasks/${task.id}/`, { done: task.done })
props.fetchData()
if (localStorage.getItem('userId')) {
await axios.delete(`/api/users/${store.user.id}/tasks/${task.id}/`, { done: task.done })
} else {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
const updatedTasks = localTasks.filter((t) => t.id !== task.id)
localStorage.setItem('tasks', JSON.stringify(updatedTasks))
}
} catch (error) {
// Handle errors
console.error(error)
}
props.fetchData()
}
const updateTaskDoneStatus = async (task) => {
try {
task.done = !task.done
await axios.patch(`/api/users/${store.user.id}/tasks/${task.id}/`, { done: task.done })
props.fetchData()
if (localStorage.getItem('userId')) {
await axios.patch(`/api/users/${store.user.id}/tasks/${task.id}/`, { done: task.done })
} else {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
const updatedTaskIndex = localTasks.findIndex((t) => t.id == Number(task.id))
if (updatedTaskIndex !== -1) {
localTasks[updatedTaskIndex].done = task.done
localStorage.setItem('tasks', JSON.stringify(localTasks))
}
}
} catch (error) {
// Handle errors
console.error(error)
}
props.fetchData()
}
const closeTaskCard = () => {
store.setIsOpenCard(false)
Expand Down Expand Up @@ -144,7 +159,6 @@ const saveTaskAndClose = async () => {
}
} else {
if (store.selectedTask.id) {
console.log(store.selectedTask.id)
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
const existingTaskIndex = localTasks.findIndex((task) => task.id === store.selectedTask.id)
if (existingTaskIndex !== -1) {
Expand All @@ -154,17 +168,19 @@ const saveTaskAndClose = async () => {
localStorage.setItem('tasks', JSON.stringify(localTasks))
} else {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
store.selectedTask.id = localTasks.length + 1
store.selectedTask.id = String(nextTaskId++)
localTasks.push(store.selectedTask)
localStorage.setItem('tasks', JSON.stringify(localTasks))
localStorage.setItem('nextTaskId', nextTaskId)
}
}
props.fetchData()
closeTaskCard()
} catch (error) {
// Handle errors
console.error(error)
}
props.fetchData()
}
props.fetchData()
</script>
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/components/task-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ const store = useStore()
const updateTaskDoneStatus = async (task) => {
try {
task.done = !task.done
await axios.patch(`/api/users/${store.user.id}/tasks/${task.id}/`, { done: task.done })
props.fetchData()
if (localStorage.getItem('userId')) {
await axios.patch(`/api/users/${store.user.id}/tasks/${task.id}/`, { done: task.done })
} else {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
const updatedTaskIndex = localTasks.findIndex((t) => t.id == Number(task.id))
if (updatedTaskIndex !== -1) {
localTasks[updatedTaskIndex].done = task.done
localStorage.setItem('tasks', JSON.stringify(localTasks))
}
}
} catch (error) {
// Handle errors
console.error(error)
}
props.fetchData()
}
props.fetchData()
</script>
Expand Down
44 changes: 39 additions & 5 deletions frontend/src/views/CalendarView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const store = useStore()
const props = defineProps(['filterdays'])
const today = new Date()
const tasks = ref([])
let nextTaskId = localStorage.getItem('nextTaskId') || 1
// resposive week view
const isWideScreen = ref(window.innerWidth >= 1075 || window.innerWidth <= 600)
Expand Down Expand Up @@ -46,6 +49,7 @@ const displayedDays = computed(() => {
onMounted(() => {
createSortableInstances()
fetchData()
moveTasksTodb()
})
const createSortableInstances = () => {
Expand All @@ -69,13 +73,24 @@ const createSortableInstances = () => {
const dropTaskDate = async (task, date) => {
try {
await axios.patch(`/api/users/${store.user.id}/tasks/${task}/`, { date: date })
if (localStorage.getItem('userId')) {
await axios.patch(`/api/users/${store.user.id}/tasks/${task}/`, { date: date })
} else {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
const updatedTaskIndex = localTasks.findIndex((t) => t.id == Number(task))
if (updatedTaskIndex !== -1) {
localTasks[updatedTaskIndex].date = date
localStorage.setItem('tasks', JSON.stringify(localTasks))
}
}
sortTasks()
fetchData()
} catch (error) {
// Handle errors
console.error(error)
}
fetchData()
}
// date format - thu feb 8 -
const formatDatePart = (date, part) => {
Expand Down Expand Up @@ -130,7 +145,24 @@ const fetchData = () => {
tasks.value = localTasks
}
}
const moveTasksTodb = async () => {
try {
const userId = localStorage.getItem('userId')
if (userId) {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
if (localTasks.length >= 1) {
// Move tasks from local storage to database
for (let i = 0; i < localTasks.length; i++) {
localTasks[i].user = userId // Assign user ID to each task
await axios.post(`/api/users/${userId}/tasks/`, localTasks[i])
}
localStorage.removeItem('tasks')
}
}
} catch (error) {
console.error('Error moving tasks to database:', error)
}
}
const sortTasks = () => {
tasks.value.sort((a, b) => {
// If a task doesn't have a time, it comes first
Expand Down Expand Up @@ -187,17 +219,19 @@ const saveTaskAndClose = async () => {
localStorage.setItem('tasks', JSON.stringify(localTasks))
} else {
const localTasks = JSON.parse(localStorage.getItem('tasks')) || []
store.selectedTask.id = localTasks.length + 1
store.selectedTask.id = String(nextTaskId++)
localTasks.push(store.selectedTask)
localStorage.setItem('tasks', JSON.stringify(localTasks))
localStorage.setItem('nextTaskId', nextTaskId)
}
}
fetchData()
closeTaskCard()
} catch (error) {
// Handle errors
console.error(error)
}
fetchData()
}
const openTaskCard = (event, task, day) => {
Expand Down

0 comments on commit a8a1a38

Please sign in to comment.