diff options
Diffstat (limited to 'app/assets/javascripts/import_entities/import_groups/graphql/services/source_groups_manager.js')
-rw-r--r-- | app/assets/javascripts/import_entities/import_groups/graphql/services/source_groups_manager.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/assets/javascripts/import_entities/import_groups/graphql/services/source_groups_manager.js b/app/assets/javascripts/import_entities/import_groups/graphql/services/source_groups_manager.js new file mode 100644 index 00000000000..f752ecc8cd6 --- /dev/null +++ b/app/assets/javascripts/import_entities/import_groups/graphql/services/source_groups_manager.js @@ -0,0 +1,45 @@ +import { defaultDataIdFromObject } from 'apollo-cache-inmemory'; +import produce from 'immer'; +import ImportSourceGroupFragment from '../fragments/bulk_import_source_group_item.fragment.graphql'; + +function extractTypeConditionFromFragment(fragment) { + return fragment.definitions[0]?.typeCondition.name.value; +} + +function generateGroupId(id) { + return defaultDataIdFromObject({ + __typename: extractTypeConditionFromFragment(ImportSourceGroupFragment), + id, + }); +} + +export class SourceGroupsManager { + constructor({ client }) { + this.client = client; + } + + findById(id) { + const cacheId = generateGroupId(id); + return this.client.readFragment({ fragment: ImportSourceGroupFragment, id: cacheId }); + } + + update(group, fn) { + this.client.writeFragment({ + fragment: ImportSourceGroupFragment, + id: generateGroupId(group.id), + data: produce(group, fn), + }); + } + + updateById(id, fn) { + const group = this.findById(id); + this.update(group, fn); + } + + setImportStatus(group, status) { + this.update(group, sourceGroup => { + // eslint-disable-next-line no-param-reassign + sourceGroup.status = status; + }); + } +} |