Création/mise à jour basique d'un DAD

This commit is contained in:
2020-08-26 14:51:53 +02:00
parent f03a0c96dc
commit 39d266f701
19 changed files with 536 additions and 97 deletions

View File

@ -3,6 +3,7 @@ import { Config } from '../config';
import { WebSocketLink } from "@apollo/client/link/ws";
import { RetryLink } from "@apollo/client/link/retry";
import { SubscriptionClient } from "subscriptions-transport-ws";
import { User } from '../types/user';
const subscriptionClient = new SubscriptionClient(Config.subscriptionEndpoint, {
reconnect: true,
@ -14,7 +15,42 @@ const link = new RetryLink({attempts: {max: 2}}).split(
new HttpLink({ uri: Config.graphQLEndpoint, credentials: 'include' })
);
const cache = new InMemoryCache({
typePolicies: {
Workgroup: {
fields: {
members: {
merge: mergeArrayByField<User>("id"),
}
}
}
}
});
export const client = new ApolloClient<any>({
cache: new InMemoryCache(),
cache: cache,
link: link,
});
});
function mergeArrayByField<T>(fieldName: string) {
return (existing: T[] = [], incoming: T[], { readField, mergeObjects }) => {
const merged: any[] = existing ? existing.slice(0) : [];
const objectFieldToIndex: Record<string, number> = Object.create(null);
if (existing) {
existing.forEach((obj, index) => {
objectFieldToIndex[readField(fieldName, obj)] = index;
});
}
incoming.forEach(obj => {
const field = readField(fieldName, obj);
const index = objectFieldToIndex[field];
if (typeof index === "number") {
merged[index] = mergeObjects(merged[index], obj);
} else {
objectFieldToIndex[name] = merged.length;
merged.push(obj);
}
});
return merged;
}
}

View File

@ -0,0 +1,40 @@
import { gql, useQuery, useMutation } from '@apollo/client';
import { QUERY_DECISION_SUPPORT_FILES } from '../queries/dsf';
export const MUTATION_CREATE_DECISION_SUPPORT_FILE = gql`
mutation createDecisionSupportFile($changes: DecisionSupportFileChanges!) {
createDecisionSupportFile(changes: $changes) {
id,
title,
status,
sections,
createdAt,
updatedAt
}
}`;
export function useCreateDecisionSupportFileMutation() {
return useMutation(MUTATION_CREATE_DECISION_SUPPORT_FILE, {
refetchQueries: [{query: QUERY_DECISION_SUPPORT_FILES}],
});
}
export const MUTATION_UPDATE_DECISION_SUPPORT_FILE = gql`
mutation updateDecisionSupportFile($id: ID!, $changes: DecisionSupportFileChanges!) {
updateDecisionSupportFile(id: $id, changes: $changes) {
id,
title,
status,
sections,
createdAt,
updatedAt
}
}`;
export function useUpdateDecisionSupportFileMutation() {
return useMutation(MUTATION_UPDATE_DECISION_SUPPORT_FILE, {
refetchQueries: [{
query: QUERY_DECISION_SUPPORT_FILES,
}],
});
}

View File

@ -1,32 +0,0 @@
import { gql, useQuery } from '@apollo/client';
import { DecisionSupportFile } from '../../types/decision';
import { useState, useEffect } from 'react';
import { useGraphQLData } from './helper';
export const QUERY_DECISIONS = gql`
query decisions($filter: DecisionFilter) {
decisions(filter: $filter) {
id,
title,
sections
createdAt,
closedAt,
votedAt,
workgroup {
id,
name
}
}
}
`;
export function useDecisionsQuery(options = {}) {
return useQuery(QUERY_DECISIONS, options);
}
export function useDecisions(options = {}) {
const { data, loading, error } = useGraphQLData<DecisionSupportFile[]>(
QUERY_DECISIONS, 'decisions', [], options
);
return { decisions: data, loading, error };
}

View File

@ -0,0 +1,35 @@
import { gql, useQuery } from '@apollo/client';
import { DecisionSupportFile } from '../../types/decision';
import { useGraphQLData } from './helper';
export const QUERY_DECISION_SUPPORT_FILES = gql`
query decisionSupportFiles($filter: DecisionSupportFileFilter) {
decisionSupportFiles(filter: $filter) {
id,
title,
sections
createdAt,
closedAt,
votedAt,
status,
workgroup {
id,
name,
members {
id
}
}
}
}
`;
export function useDecisionSupportFilesQuery(options = {}) {
return useQuery(QUERY_DECISION_SUPPORT_FILES, options);
}
export function useDecisionSupportFiles(options = {}) {
const { data, loading, error } = useGraphQLData<DecisionSupportFile[]>(
QUERY_DECISION_SUPPORT_FILES, 'decisionSupportFiles', [], options
);
return { decisionSupportFiles: data, loading, error };
}