feat(ui+backend): base of data persistence
This commit is contained in:
34
client/src/gql/fragments/project.ts
Normal file
34
client/src/gql/fragments/project.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const FRAGMENT_FULL_PROJECT = gql`
|
||||
fragment FullProject on Project {
|
||||
id
|
||||
title
|
||||
taskCategories {
|
||||
id
|
||||
label
|
||||
costPerTimeUnit
|
||||
}
|
||||
tasks {
|
||||
id
|
||||
label
|
||||
category {
|
||||
id
|
||||
label
|
||||
}
|
||||
estimations {
|
||||
optimistic
|
||||
likely
|
||||
pessimistic
|
||||
}
|
||||
}
|
||||
params {
|
||||
timeUnit {
|
||||
label
|
||||
acronym
|
||||
}
|
||||
currency
|
||||
hideFinancialPreviewOnPrint
|
||||
}
|
||||
}
|
||||
`
|
69
client/src/gql/mutations/project.tsx
Normal file
69
client/src/gql/mutations/project.tsx
Normal file
@ -0,0 +1,69 @@
|
||||
import { gql, useMutation, PureQueryOptions } from '@apollo/client';
|
||||
import { FRAGMENT_FULL_PROJECT } from '../fragments/project';
|
||||
import { QUERY_PROJECTS } from '../queries/project';
|
||||
|
||||
export const MUTATION_CREATE_PROJECT = gql`
|
||||
mutation createProject($changes: CreateProjectChanges!) {
|
||||
createProject(changes: $changes) {
|
||||
...FullProject
|
||||
}
|
||||
}
|
||||
${FRAGMENT_FULL_PROJECT}
|
||||
`;
|
||||
|
||||
export function useProjectCreateMutation() {
|
||||
return useMutation(MUTATION_CREATE_PROJECT);
|
||||
}
|
||||
|
||||
export const MUTATION_UPDATE_PROJECT_TITLE = gql`
|
||||
mutation updateProjectTitle($projectId: ID!, $title: String!) {
|
||||
updateProjectTitle(projectId: $projectId, title: $title) {
|
||||
...FullProject
|
||||
}
|
||||
}
|
||||
${FRAGMENT_FULL_PROJECT}
|
||||
`;
|
||||
|
||||
export function useUpdateProjectTitleMutation() {
|
||||
return useMutation(MUTATION_UPDATE_PROJECT_TITLE, {
|
||||
refetchQueries: ({ variables }: PureQueryOptions) => {
|
||||
return [
|
||||
{ query: QUERY_PROJECTS, variables: { filters: { ids: [ variables.projectId ] } }},
|
||||
];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const MUTATION_ADD_PROJECT_TASK = gql`
|
||||
mutation addProjectTask($projectId: ID!, $changes: ProjectTaskChanges!) {
|
||||
addProjectTask(projectId: $projectId, changes: $changes) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export function useAddProjectTaskMutation() {
|
||||
return useMutation(MUTATION_ADD_PROJECT_TASK);
|
||||
}
|
||||
|
||||
export const MUTATION_UPDATE_PROJECT_TASK = gql`
|
||||
mutation updateProjectTask($projectId: ID!, $taskId: ID!, $changes: ProjectTaskChanges!) {
|
||||
updateProjectTask(projectId: $projectId, taskId: $taskId, changes: $changes) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export function useUpdateProjectTaskMutation() {
|
||||
return useMutation(MUTATION_UPDATE_PROJECT_TASK);
|
||||
}
|
||||
|
||||
export const MUTATION_REMOVE_PROJECT_TASK = gql`
|
||||
mutation removeProjectTask($projectId: ID!, $taskId: ID!) {
|
||||
removeProjectTask(projectId: $projectId, taskId: $taskId)
|
||||
}
|
||||
`;
|
||||
|
||||
export function useRemoveProjectTaskMutation() {
|
||||
return useMutation(MUTATION_REMOVE_PROJECT_TASK);
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import { useQuery, DocumentNode } from "@apollo/client";
|
||||
import { useQuery, DocumentNode, QueryHookOptions } from "@apollo/client";
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
export function useGraphQLData<T>(q: DocumentNode, key: string, defaultValue: T, options = {}) {
|
||||
export function useGraphQLData<T, A = any, R = Record<string, any>>(q: DocumentNode, key: string, defaultValue: T, options: QueryHookOptions<A, R> = {}) {
|
||||
const query = useQuery(q, options);
|
||||
const [ data, setData ] = useState<T>(defaultValue);
|
||||
useEffect(() => {
|
||||
|
49
client/src/gql/queries/project.ts
Normal file
49
client/src/gql/queries/project.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { gql, useQuery, QueryHookOptions } from '@apollo/client';
|
||||
import { User } from '../../types/user';
|
||||
import { useGraphQLData } from './helper';
|
||||
import { Project } from '../../types/project';
|
||||
|
||||
export const QUERY_PROJECTS = gql`
|
||||
query projects($filter: ProjectsFilter) {
|
||||
projects(filter: $filter) {
|
||||
id
|
||||
title
|
||||
taskCategories {
|
||||
id
|
||||
label
|
||||
costPerTimeUnit
|
||||
}
|
||||
tasks {
|
||||
id
|
||||
label
|
||||
category {
|
||||
id
|
||||
label
|
||||
}
|
||||
estimations {
|
||||
optimistic
|
||||
likely
|
||||
pessimistic
|
||||
}
|
||||
}
|
||||
params {
|
||||
timeUnit {
|
||||
label
|
||||
acronym
|
||||
}
|
||||
currency
|
||||
hideFinancialPreviewOnPrint
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
export function useProjectsQuery() {
|
||||
return useQuery(QUERY_PROJECTS);
|
||||
}
|
||||
|
||||
export function useProjects<A = any, R = Record<string, any>>(options: QueryHookOptions<A, R> = {}) {
|
||||
const { data, loading, error } = useGraphQLData<Project[]>(
|
||||
QUERY_PROJECTS, 'projects', [], options
|
||||
);
|
||||
return { projects: data, loading, error };
|
||||
}
|
Reference in New Issue
Block a user