Skip to content

Commit

Permalink
fix: refactor task list to not call db for all labels separately
Browse files Browse the repository at this point in the history
  • Loading branch information
MattCMcCoy authored and wyattchris committed Apr 17, 2024
1 parent d000cdd commit 27e1d85
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 56 deletions.
10 changes: 9 additions & 1 deletion backend/db/migrations/4.label.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,13 @@ VALUES
(3, 3, 'Financial'),
(4, 4, 'Household'),
(6, 5, 'Financial'),
(7, 5, 'Appointments')
(7, 5, 'Appointments'),
(8, 5, 'Financial'),
(9, 5, 'Appointments'),
(10, 5, 'Financial'),
(11, 5, 'Appointments'),
(12, 5, 'Financial'),
(13, 5, 'Appointments'),
(14, 5, 'Financial'),
(15, 5, 'Appointments')
;
39 changes: 39 additions & 0 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,45 @@ const docTemplate = `{
}
}
},
"/tasks/labels/tasks": {
"get": {
"description": "gets the information about multiple labals given their task id",
"tags": [
"task labels"
],
"summary": "gets the information about multiple labels",
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "csv",
"description": "Task IDs",
"name": "taskIDs",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Task_Label"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
}
}
}
},
"/tasks/{tid}": {
"get": {
"description": "get a task given its id",
Expand Down
39 changes: 39 additions & 0 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,45 @@
}
}
},
"/tasks/labels/tasks": {
"get": {
"description": "gets the information about multiple labals given their task id",
"tags": [
"task labels"
],
"summary": "gets the information about multiple labels",
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "csv",
"description": "Task IDs",
"name": "taskIDs",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Task_Label"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
}
}
}
},
"/tasks/{tid}": {
"get": {
"description": "get a task given its id",
Expand Down
26 changes: 26 additions & 0 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,32 @@ paths:
summary: Get Filtered Tasks
tags:
- tasks
/tasks/labels/tasks:
get:
description: gets the information about multiple labals given their task id
parameters:
- collectionFormat: csv
description: Task IDs
in: query
items:
type: string
name: taskIDs
required: true
type: array
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/models.Task_Label'
type: array
"400":
description: Bad Request
schema:
type: string
summary: gets the information about multiple labels
tags:
- task labels
/user:
get:
description: gets the information about multiple users given their user id
Expand Down
54 changes: 50 additions & 4 deletions backend/schema/task_labels/routes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package task_labels

import (
"carewallet/models"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
Expand All @@ -13,11 +15,19 @@ type PgModel struct {

func TaskGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {

tasks := v1.Group(":tid/labels")
tasks := v1.Group("")
{
tasks.POST("", c.addLabelToTask)
tasks.DELETE("", c.removeLabelFromTask)
tasks.GET("", c.getLabelsByTask)
labelByTaskID := tasks.Group(":tid/labels")
{
labelByTaskID.POST("", c.addLabelToTask)
labelByTaskID.DELETE("", c.removeLabelFromTask)
labelByTaskID.GET("", c.getLabelsByTask)
}

labelByTaskIDs := tasks.Group("labels/tasks")
{
labelByTaskIDs.GET("", c.getLabelsByTasks)
}
}

return tasks
Expand Down Expand Up @@ -109,3 +119,39 @@ func (pg *PgModel) removeLabelFromTask(c *gin.Context) {

c.JSON(http.StatusOK, "")
}

type LabelsQuery struct {
TaskIDs []string `form:"taskIDs"`
}

// getLabelsByTasks godoc
//
// @summary gets the information about multiple labels
// @description gets the information about multiple labals given their task id
// @tags task labels
//
// @param taskIDs query []string true "Task IDs"
//
// @success 200 {array} models.Task_Label
// @failure 400 {object} string
// @router /tasks/labels/tasks [GET]
func (pg *PgModel) getLabelsByTasks(c *gin.Context) {

taskIDs := c.Query("taskIDs")
taskQuery := LabelsQuery{
TaskIDs: strings.Split(taskIDs, ","),
}

var labels []models.Task_Label

for _, element := range taskQuery.TaskIDs {
label, err := getLabelsByTaskInDB(pg.Conn, element)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
labels = append(labels, label...)
}

c.JSON(http.StatusOK, labels)
}
2 changes: 1 addition & 1 deletion backend/schema/task_labels/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func getLabelsByTaskInDB(conn *pgxpool.Pool, taskId string) ([]models.Task_Label

for rows.Next() {
task := models.Task_Label{}
err := rows.Scan(&task.GroupId, &task.TaskId, &task.LabelName)
err := rows.Scan(&task.TaskId, &task.GroupId, &task.LabelName)

if err != nil {
print(err, "error scanning tasks by query")
Expand Down
64 changes: 26 additions & 38 deletions client/components/calendar/TaskInfoCard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import { Text, View } from 'react-native';

import clsx from 'clsx';
import { clsx } from 'clsx';
import moment from 'moment';

import Calendar from '../../assets/Date_today.svg';
import Time from '../../assets/Time.svg';
import { useTaskById } from '../../services/task';
import { Task } from '../../types/task';
import { TaskLabel } from '../../types/taskLabel';
import {
CategoryIconsMap,
TaskTypeDescriptions,
Expand Down Expand Up @@ -59,82 +60,69 @@ function categoryToBGColor(category: string) {
}

export function TaskInfoComponent({
name,
id,
category,
status,
date
task,
taskLabels
}: {
id: number;
name: string;
category: string;
status: string;
date: Date;
task: Task;
taskLabels: TaskLabel[] | undefined;
}) {
const { task } = useTaskById(id.toString());
const { taskLabels } = useTaskById(id.toString());

console.log(task?.task_type);

return (
<View className="mb-6 rounded-2xl border border-carewallet-gray bg-carewallet-white p-4">
<View className="mb-2 flex flex-col justify-between">
<View className="flex-row items-center">
<View className="flex flex-row items-center space-x-2">
<Text className="font-carewallet-manrope-semibold text-xl">
{name}
{task.task_title}
</Text>
</View>
<View className="ml-auto flex flex-row items-center space-x-2 rounded-full border border-carewallet-lightgray px-2 py-1">
<View
className={`h-4 w-4 rounded-full ${statusToString(status)}`}
className={`h-4 w-4 rounded-full ${statusToString(task.task_status)}`}
/>
<Text className="font-carewallet-manrope">{`${status?.charAt(0)}${status?.slice(1).toLowerCase()}`}</Text>
<Text className="font-carewallet-manrope">{`${task.task_status?.charAt(0)}${task.task_status?.slice(1).toLowerCase()}`}</Text>
</View>
</View>

<View className="mt-3 flex flex-row space-x-2">
<View className="flex flex-row items-center space-x-2">
<Calendar />
<Text className="font-carewallet-manrope">
{moment(date).format('MMMM DD')}
{moment(task.start_date).format('MMMM DD')}
</Text>
</View>
<View className="flex flex-row items-center space-x-2">
<Time />
<Text className="font-carewallet-manrope">
{moment(date).format('hh:mm A')}
{moment(task.end_date).format('hh:mm A')}
</Text>
</View>
</View>
</View>
<View className="space-y-2">
<View
className={clsx(
'mr-auto flex flex-row items-center space-x-2 rounded-full border border-carewallet-lightgray px-2 py-1',
categoryToBGColor(category)
)}
className={`mr-auto flex flex-row items-center space-x-2 rounded-full border bg-${categoryToBGColor(category)} border-carewallet-lightgray px-2 py-1`}
>
<View>
{CategoryIconsMap[TypeToCategoryMap[task?.task_type ?? 'Other']]}
</View>
<Text
className={`font-carewallet-manrope text-${categoryToColor(category)}`}
className={`font-carewallet-manrope text-${categoryToColor(task.task_type)}`}
>
{TaskTypeDescriptions[category]}
{TaskTypeDescriptions[task.task_type]}
</Text>
</View>
<View>
{taskLabels?.map((label) => (
<View
key={label.label_name + label.task_id}
className="mr-auto flex flex-row items-center rounded-full border border-carewallet-lightgray px-2 py-1"
>
<Text className="ml-1 self-start py-1 font-carewallet-manrope">
{label.label_name}
</Text>
</View>
))}
{taskLabels &&
taskLabels.map((label) => (
<View
key={label.label_name + label.task_id}
className="mr-auto flex flex-row items-center rounded-full border border-carewallet-lightgray px-2 py-1"
>
<Text className="ml-1 self-start py-1 font-carewallet-manrope">
{label.label_name}
</Text>
</View>
))}
</View>
</View>
</View>
Expand Down
2 changes: 1 addition & 1 deletion client/components/filter/FilterCircleCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { Text, View } from 'react-native';

import clsx from 'clsx';
import { clsx } from 'clsx';

export function FilterCircleCard({
selected,
Expand Down
Loading

0 comments on commit 27e1d85

Please sign in to comment.