138 lines
3.8 KiB
JavaScript
138 lines
3.8 KiB
JavaScript
import queryString from "query-string";
|
|
import {useLocation} from "react-router-dom";
|
|
import {useMutation, useQuery} from "@apollo/client";
|
|
import {
|
|
MUTATION_TOGGLE_TASK_COMPLETED,
|
|
MUTATION_TOGGLE_TASK_DELETED,
|
|
QUERY_MY_TASKS_PAGINATED
|
|
} from "../../graphql/tasks.queries.js";
|
|
import {pageLimit} from "../../utils/config.js";
|
|
import AlertComponent from "../alert/alert.component.jsx";
|
|
import React, {useEffect} from "react";
|
|
import TaskListComponent from "./task-list.component.jsx";
|
|
import {notification} from "antd";
|
|
import {useTranslation} from "react-i18next";
|
|
|
|
export default function TaskListContainer({bodyshop, currentUser}) {
|
|
const {t} = useTranslation();
|
|
const searchParams = queryString.parse(useLocation().search);
|
|
const {page, sortcolumn, sortorder, deleted, completed} = searchParams;
|
|
const {loading, error, data, refetch} = useQuery(
|
|
QUERY_MY_TASKS_PAGINATED,
|
|
{
|
|
fetchPolicy: "network-only",
|
|
nextFetchPolicy: "network-only",
|
|
variables: {
|
|
bodyshop: bodyshop.id,
|
|
user: currentUser.email,
|
|
deleted: deleted === 'true',
|
|
completed: completed === "true",
|
|
offset: page ? (page - 1) * pageLimit : 0,
|
|
limit: pageLimit,
|
|
order: [
|
|
{
|
|
[sortcolumn || "created_at"]: sortorder
|
|
? sortorder === "descend"
|
|
? "desc"
|
|
: "asc"
|
|
: "desc",
|
|
},
|
|
],
|
|
},
|
|
}
|
|
);
|
|
|
|
/**
|
|
* Refetch tasks when a task is updated
|
|
*/
|
|
useEffect(() => {
|
|
const handleTaskUpdated = () => {
|
|
refetch().catch((e) => {
|
|
console.error(`Something went wrong fetching tasks: ${e.message || ''}`);
|
|
});
|
|
};
|
|
}, [refetch]);
|
|
|
|
/**
|
|
* Toggle task completed mutation
|
|
*/
|
|
const [toggleTaskCompleted] = useMutation(MUTATION_TOGGLE_TASK_COMPLETED);
|
|
|
|
/**
|
|
* Toggle task completed status
|
|
* @param id
|
|
* @param currentStatus
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const toggleCompletedStatus = async (id, currentStatus) => {
|
|
const completed_at = !currentStatus ? new Date().toISOString() : null;
|
|
try {
|
|
await toggleTaskCompleted({
|
|
variables: {
|
|
id: id,
|
|
completed: !currentStatus,
|
|
completed_at: completed_at
|
|
}
|
|
});
|
|
refetch().catch((e) => {
|
|
console.error(`Something went wrong fetching tasks: ${e.message || ''}`);
|
|
});
|
|
notification["success"]({
|
|
message: t("tasks.successes.completed"),
|
|
});
|
|
} catch (err) {
|
|
notification["error"]({
|
|
message: t("tasks.failures.completed"),
|
|
});
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Toggle task deleted mutation
|
|
*/
|
|
const [toggleTaskDeleted] = useMutation(MUTATION_TOGGLE_TASK_DELETED);
|
|
|
|
/**
|
|
* Toggle task deleted status
|
|
* @param id
|
|
* @param currentStatus
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const toggleDeletedStatus = async (id, currentStatus) => {
|
|
const deleted_at = !currentStatus ? new Date().toISOString() : null;
|
|
try {
|
|
await toggleTaskDeleted({
|
|
variables: {
|
|
id: id,
|
|
deleted: !currentStatus,
|
|
deleted_at: deleted_at
|
|
}
|
|
});
|
|
refetch().catch((e) => {
|
|
console.error(`Something went wrong fetching tasks: ${e.message || ''}`);
|
|
});
|
|
notification["success"]({
|
|
message: t("tasks.successes.deleted"),
|
|
});
|
|
} catch (err) {
|
|
notification["error"]({
|
|
message: t("tasks.failures.deleted"),
|
|
});
|
|
}
|
|
|
|
};
|
|
|
|
if (error) return <AlertComponent message={error.message} type="error"/>;
|
|
|
|
return (
|
|
<TaskListComponent
|
|
loading={loading}
|
|
tasks={data ? data.tasks : null}
|
|
total={data ? data.tasks_aggregate.aggregate.count : 0}
|
|
refetch={refetch}
|
|
toggleCompletedStatus={toggleCompletedStatus}
|
|
toggleDeletedStatus={toggleDeletedStatus}
|
|
/>
|
|
);
|
|
}
|