@trackunit/custom-field-api 0.1.47 → 0.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs.js CHANGED
@@ -933,7 +933,7 @@ const getCustomFieldValueAndDefinitionFromRelevantNode = (node) => getFragmentDa
933
933
  * @param {SystemOfMeasurement} props.systemOfMeasurement - The system of measurement to be used in the custom fields.
934
934
  * @returns {UseCustomFieldsValueAndDefinitionReturnValue} An object containing a loading state and an array of custom fields and definitions.
935
935
  */
936
- const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasurement }) => {
936
+ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasurement, }) => {
937
937
  var _a, _b, _c, _d;
938
938
  const { data: assetFields, loading: loadingAssetFields } = client.useQuery(GetCustomFieldsForAssetDocument, {
939
939
  variables: {
@@ -944,9 +944,9 @@ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasu
944
944
  skip: entityType !== "ASSET" || !entityId,
945
945
  context: {
946
946
  headers: {
947
- "TU-PREVIEW": "JUNGLE-DIGGER"
948
- }
949
- }
947
+ "TU-PREVIEW": "JUNGLE-DIGGER",
948
+ },
949
+ },
950
950
  });
951
951
  const { data: siteFields, loading: loadingSiteFields } = client.useQuery(GetCustomFieldsForSiteDocument, {
952
952
  variables: {
@@ -957,17 +957,17 @@ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasu
957
957
  skip: entityType !== "SITE" || !entityId,
958
958
  context: {
959
959
  headers: {
960
- "TU-PREVIEW": "JUNGLE-DIGGER"
961
- }
962
- }
960
+ "TU-PREVIEW": "JUNGLE-DIGGER",
961
+ },
962
+ },
963
963
  });
964
964
  const fields = react.useMemo(() => {
965
965
  var _a, _b, _c, _d, _e, _f;
966
966
  switch (entityType) {
967
967
  case "ASSET":
968
- return ((_c = (_b = (_a = assetFields === null || assetFields === void 0 ? void 0 : assetFields.asset) === null || _a === void 0 ? void 0 : _a.customFields) === null || _b === void 0 ? void 0 : _b.edges) === null || _c === void 0 ? void 0 : _c.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || [];
968
+ return (((_c = (_b = (_a = assetFields === null || assetFields === void 0 ? void 0 : assetFields.asset) === null || _a === void 0 ? void 0 : _a.customFields) === null || _b === void 0 ? void 0 : _b.edges) === null || _c === void 0 ? void 0 : _c.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || []);
969
969
  case "SITE":
970
- return ((_f = (_e = (_d = siteFields === null || siteFields === void 0 ? void 0 : siteFields.site) === null || _d === void 0 ? void 0 : _d.customFields) === null || _e === void 0 ? void 0 : _e.edges) === null || _f === void 0 ? void 0 : _f.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || [];
970
+ return (((_f = (_e = (_d = siteFields === null || siteFields === void 0 ? void 0 : siteFields.site) === null || _d === void 0 ? void 0 : _d.customFields) === null || _e === void 0 ? void 0 : _e.edges) === null || _f === void 0 ? void 0 : _f.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || []);
971
971
  default:
972
972
  return [];
973
973
  }
@@ -976,9 +976,52 @@ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasu
976
976
  const noneNullableFields = react.useMemo(() => fields.filter(sharedUtils.nonNullable), [fields]);
977
977
  return react.useMemo(() => ({ fields: noneNullableFields, loading }), [noneNullableFields, loading]);
978
978
  };
979
+ /**
980
+ * Utility function that given a {CustomFieldValueAndDefinition} can tell if it contains a value or not
981
+ *
982
+ * @param {CustomFieldValueAndDefinition} field - the custom field that might contain a value.
983
+ * @returns {boolean} true if the field contains a value.
984
+ */
985
+ const customFieldHasValue = (field) => {
986
+ switch (field.__typename) {
987
+ case "WebAddressFieldValueAndDefinition": {
988
+ return !!field.stringValue;
989
+ }
990
+ case "EmailFieldValueAndDefinition": {
991
+ return !!field.stringValue;
992
+ }
993
+ case "PhoneNumberFieldValueAndDefinition": {
994
+ return !!field.stringValue;
995
+ }
996
+ case "StringFieldValueAndDefinition": {
997
+ return !!field.stringValue;
998
+ }
999
+ case "NumberFieldValueAndDefinition": {
1000
+ return !!field.numberValue || field.numberValue === 0;
1001
+ }
1002
+ case "BooleanFieldValueAndDefinition": {
1003
+ return !(field.booleanValue === null);
1004
+ }
1005
+ case "DropDownFieldValueAndDefinition": {
1006
+ return !!field.stringArrayValue;
1007
+ }
1008
+ case "DateFieldValueAndDefinition": {
1009
+ return !!field.dateValue;
1010
+ }
1011
+ case "JsonFieldValueAndDefinition": {
1012
+ return false;
1013
+ }
1014
+ case "MonetaryFieldValueAndDefinition": {
1015
+ return !!field.numberValue || field.numberValue === 0;
1016
+ }
1017
+ default:
1018
+ return sharedUtils.exhaustiveCheck(field);
1019
+ }
1020
+ };
979
1021
 
980
1022
  exports.GetCustomFieldsForAssetDocument = GetCustomFieldsForAssetDocument;
981
1023
  exports.GetCustomFieldsForSiteDocument = GetCustomFieldsForSiteDocument;
1024
+ exports.customFieldHasValue = customFieldHasValue;
982
1025
  exports.getCustomFieldValueAndDefinitionFromRelevantNode = getCustomFieldValueAndDefinitionFromRelevantNode;
983
1026
  exports.useCustomFieldDefinitions = useCustomFieldDefinitions;
984
1027
  exports.useCustomFieldsValueAndDefinition = useCustomFieldsValueAndDefinition;
package/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useQuery } from '@apollo/client';
2
2
  import { useCurrentUserLanguage } from '@trackunit/react-core-hooks';
3
- import { nonNullable } from '@trackunit/shared-utils';
3
+ import { nonNullable, exhaustiveCheck } from '@trackunit/shared-utils';
4
4
  import { useMemo } from 'react';
5
5
 
6
6
  function getFragmentData(_documentNode, fragmentType) {
@@ -929,7 +929,7 @@ const getCustomFieldValueAndDefinitionFromRelevantNode = (node) => getFragmentDa
929
929
  * @param {SystemOfMeasurement} props.systemOfMeasurement - The system of measurement to be used in the custom fields.
930
930
  * @returns {UseCustomFieldsValueAndDefinitionReturnValue} An object containing a loading state and an array of custom fields and definitions.
931
931
  */
932
- const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasurement }) => {
932
+ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasurement, }) => {
933
933
  var _a, _b, _c, _d;
934
934
  const { data: assetFields, loading: loadingAssetFields } = useQuery(GetCustomFieldsForAssetDocument, {
935
935
  variables: {
@@ -940,9 +940,9 @@ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasu
940
940
  skip: entityType !== "ASSET" || !entityId,
941
941
  context: {
942
942
  headers: {
943
- "TU-PREVIEW": "JUNGLE-DIGGER"
944
- }
945
- }
943
+ "TU-PREVIEW": "JUNGLE-DIGGER",
944
+ },
945
+ },
946
946
  });
947
947
  const { data: siteFields, loading: loadingSiteFields } = useQuery(GetCustomFieldsForSiteDocument, {
948
948
  variables: {
@@ -953,17 +953,17 @@ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasu
953
953
  skip: entityType !== "SITE" || !entityId,
954
954
  context: {
955
955
  headers: {
956
- "TU-PREVIEW": "JUNGLE-DIGGER"
957
- }
958
- }
956
+ "TU-PREVIEW": "JUNGLE-DIGGER",
957
+ },
958
+ },
959
959
  });
960
960
  const fields = useMemo(() => {
961
961
  var _a, _b, _c, _d, _e, _f;
962
962
  switch (entityType) {
963
963
  case "ASSET":
964
- return ((_c = (_b = (_a = assetFields === null || assetFields === void 0 ? void 0 : assetFields.asset) === null || _a === void 0 ? void 0 : _a.customFields) === null || _b === void 0 ? void 0 : _b.edges) === null || _c === void 0 ? void 0 : _c.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || [];
964
+ return (((_c = (_b = (_a = assetFields === null || assetFields === void 0 ? void 0 : assetFields.asset) === null || _a === void 0 ? void 0 : _a.customFields) === null || _b === void 0 ? void 0 : _b.edges) === null || _c === void 0 ? void 0 : _c.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || []);
965
965
  case "SITE":
966
- return ((_f = (_e = (_d = siteFields === null || siteFields === void 0 ? void 0 : siteFields.site) === null || _d === void 0 ? void 0 : _d.customFields) === null || _e === void 0 ? void 0 : _e.edges) === null || _f === void 0 ? void 0 : _f.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || [];
966
+ return (((_f = (_e = (_d = siteFields === null || siteFields === void 0 ? void 0 : siteFields.site) === null || _d === void 0 ? void 0 : _d.customFields) === null || _e === void 0 ? void 0 : _e.edges) === null || _f === void 0 ? void 0 : _f.map(edge => getCustomFieldValueAndDefinitionFromRelevantNode(edge === null || edge === void 0 ? void 0 : edge.node))) || []);
967
967
  default:
968
968
  return [];
969
969
  }
@@ -972,5 +972,47 @@ const useCustomFieldsValueAndDefinition = ({ entityId, entityType, systemOfMeasu
972
972
  const noneNullableFields = useMemo(() => fields.filter(nonNullable), [fields]);
973
973
  return useMemo(() => ({ fields: noneNullableFields, loading }), [noneNullableFields, loading]);
974
974
  };
975
+ /**
976
+ * Utility function that given a {CustomFieldValueAndDefinition} can tell if it contains a value or not
977
+ *
978
+ * @param {CustomFieldValueAndDefinition} field - the custom field that might contain a value.
979
+ * @returns {boolean} true if the field contains a value.
980
+ */
981
+ const customFieldHasValue = (field) => {
982
+ switch (field.__typename) {
983
+ case "WebAddressFieldValueAndDefinition": {
984
+ return !!field.stringValue;
985
+ }
986
+ case "EmailFieldValueAndDefinition": {
987
+ return !!field.stringValue;
988
+ }
989
+ case "PhoneNumberFieldValueAndDefinition": {
990
+ return !!field.stringValue;
991
+ }
992
+ case "StringFieldValueAndDefinition": {
993
+ return !!field.stringValue;
994
+ }
995
+ case "NumberFieldValueAndDefinition": {
996
+ return !!field.numberValue || field.numberValue === 0;
997
+ }
998
+ case "BooleanFieldValueAndDefinition": {
999
+ return !(field.booleanValue === null);
1000
+ }
1001
+ case "DropDownFieldValueAndDefinition": {
1002
+ return !!field.stringArrayValue;
1003
+ }
1004
+ case "DateFieldValueAndDefinition": {
1005
+ return !!field.dateValue;
1006
+ }
1007
+ case "JsonFieldValueAndDefinition": {
1008
+ return false;
1009
+ }
1010
+ case "MonetaryFieldValueAndDefinition": {
1011
+ return !!field.numberValue || field.numberValue === 0;
1012
+ }
1013
+ default:
1014
+ return exhaustiveCheck(field);
1015
+ }
1016
+ };
975
1017
 
976
- export { GetCustomFieldsForAssetDocument, GetCustomFieldsForSiteDocument, getCustomFieldValueAndDefinitionFromRelevantNode, useCustomFieldDefinitions, useCustomFieldsValueAndDefinition };
1018
+ export { GetCustomFieldsForAssetDocument, GetCustomFieldsForSiteDocument, customFieldHasValue, getCustomFieldValueAndDefinitionFromRelevantNode, useCustomFieldDefinitions, useCustomFieldsValueAndDefinition };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/custom-field-api",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "engines": {
5
5
  "node": ">=18.x"
6
6
  },
@@ -1,6 +1,6 @@
1
- import { CustomFieldValueAndDefinitionFragment, CustomFieldValueAndDefinitionFragmentDoc, SystemOfMeasurement } from "./generated/graphql-api/graphql";
2
- import { FragmentType } from "./generated/graphql-api/fragment-masking";
3
1
  import { EntityType } from "@trackunit/iris-app-api";
2
+ import { FragmentType } from "./generated/graphql-api/fragment-masking";
3
+ import { CustomFieldValueAndDefinitionFragment, CustomFieldValueAndDefinitionFragmentDoc, SystemOfMeasurement } from "./generated/graphql-api/graphql";
4
4
  export type CustomFieldValueAndDefinition = CustomFieldValueAndDefinitionFragment;
5
5
  export type SupportedEntityTypes = Extract<EntityType, "ASSET" | "SITE">;
6
6
  type CustomFieldValueAndDefinitionNode = FragmentType<typeof CustomFieldValueAndDefinitionFragmentDoc>;
@@ -29,5 +29,12 @@ export interface UseCustomFieldsValueAndDefinitionReturnValue {
29
29
  * @param {SystemOfMeasurement} props.systemOfMeasurement - The system of measurement to be used in the custom fields.
30
30
  * @returns {UseCustomFieldsValueAndDefinitionReturnValue} An object containing a loading state and an array of custom fields and definitions.
31
31
  */
32
- export declare const useCustomFieldsValueAndDefinition: ({ entityId, entityType, systemOfMeasurement }: UseCustomFieldsValueAndDefinitionProps) => UseCustomFieldsValueAndDefinitionReturnValue;
32
+ export declare const useCustomFieldsValueAndDefinition: ({ entityId, entityType, systemOfMeasurement, }: UseCustomFieldsValueAndDefinitionProps) => UseCustomFieldsValueAndDefinitionReturnValue;
33
+ /**
34
+ * Utility function that given a {CustomFieldValueAndDefinition} can tell if it contains a value or not
35
+ *
36
+ * @param {CustomFieldValueAndDefinition} field - the custom field that might contain a value.
37
+ * @returns {boolean} true if the field contains a value.
38
+ */
39
+ export declare const customFieldHasValue: (field: CustomFieldValueAndDefinition) => boolean;
33
40
  export {};