summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/security_configuration/graphql/cache_utils.js
blob: 6d5258b01dc46c8194f7c8a30bc621c87452e585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import produce from 'immer';

export const updateSecurityTrainingOptimisticResponse = (changes) => ({
  // False positive i18n lint: https://gitlab.com/gitlab-org/frontend/eslint-plugin-i18n/issues/26
  // eslint-disable-next-line @gitlab/require-i18n-strings
  __typename: 'Mutation',
  securityTrainingUpdate: {
    __typename: 'SecurityTrainingUpdatePayload',
    training: {
      __typename: 'ProjectSecurityTraining',
      ...changes,
    },
    errors: [],
  },
});

export const updateSecurityTrainingCache = ({ query, variables }) => (cache, { data }) => {
  const {
    securityTrainingUpdate: { training: updatedProvider },
  } = data;
  const { project } = cache.readQuery({ query, variables });
  if (!updatedProvider.isPrimary) {
    return;
  }

  // when we set a new primary provider, we need to unset the previous one(s)
  const updatedProject = produce(project, (draft) => {
    draft.securityTrainingProviders.forEach((provider) => {
      // eslint-disable-next-line no-param-reassign
      provider.isPrimary = provider.id === updatedProvider.id;
    });
  });

  // write to the cache
  cache.writeQuery({
    query,
    variables,
    data: { project: updatedProject },
  });
};