Skip to content

Commit 5462bf5

Browse files
fix: label should be taken from asset api
1 parent c766555 commit 5462bf5

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

src/modules/service-catalog/components/service-catalog-item/ItemRequestForm.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,14 @@ type AssetTypeState = {
9090
hidden: boolean;
9191
ready: boolean;
9292
description?: string;
93+
name?: string;
94+
};
95+
type AssetState = {
96+
ids: string[];
97+
ready: boolean;
98+
description?: string;
99+
name?: string;
93100
};
94-
type AssetState = { ids: string[]; ready: boolean; description?: string };
95101

96102
const isAssetField = (f: TicketFieldObject) =>
97103
f.relationship_target_type === ASSET_KEY;
@@ -143,11 +149,13 @@ export function ItemRequestForm({
143149
hidden: false,
144150
ready: false,
145151
description: undefined,
152+
name: undefined,
146153
});
147154
const [assetState, setAssetState] = useState<AssetState>({
148155
ids: [],
149156
ready: false,
150157
description: undefined,
158+
name: undefined,
151159
});
152160
const [isHiddenAssetsType, setIsHiddenAssetsType] = useState(false);
153161

@@ -165,6 +173,7 @@ export function ItemRequestForm({
165173
const hidden = !!res?.isHiddenAssetsType;
166174
const raw = res?.assetTypeIds;
167175
const description = res?.assetTypeDescription;
176+
const name = res?.assetTypeName;
168177

169178
let ids: string[] = [];
170179

@@ -176,7 +185,7 @@ export function ItemRequestForm({
176185

177186
ids = ids.map((s) => s.trim()).filter(Boolean);
178187

179-
setAssetTypeState({ ids, hidden, ready: true, description });
188+
setAssetTypeState({ ids, hidden, ready: true, description, name });
180189
setIsHiddenAssetsType(hidden);
181190
} catch (e) {
182191
if (!alive) return;
@@ -185,6 +194,7 @@ export function ItemRequestForm({
185194
hidden: false,
186195
ready: true,
187196
description: undefined,
197+
name: undefined,
188198
});
189199
}
190200

@@ -193,7 +203,8 @@ export function ItemRequestForm({
193203
if (!alive) return;
194204

195205
const raw = res?.assetIds;
196-
const description = res?.assetDescription as string | undefined;
206+
const description = res?.assetDescription;
207+
const name = res?.assetName;
197208

198209
let ids: string[] = [];
199210

@@ -205,10 +216,10 @@ export function ItemRequestForm({
205216

206217
ids = ids.map((s) => s.trim()).filter(Boolean);
207218

208-
setAssetState({ ids, ready: true, description });
219+
setAssetState({ ids, ready: true, description, name });
209220
} catch (e) {
210221
if (!alive) return;
211-
setAssetState({ ids: [], ready: true, description: undefined });
222+
setAssetState({ ids: [], ready: true, description: undefined, name: undefined });
212223
}
213224
};
214225

@@ -278,6 +289,11 @@ export function ItemRequestForm({
278289
}
279290
const customField = {
280291
...field,
292+
label: isAssetField(field)
293+
? assetState.name || field.label
294+
: isAssetTypeField(field)
295+
? assetTypeState.name || field.label
296+
: field.label,
281297
description: isAssetField(field)
282298
? assetState.description || field.description
283299
: isAssetTypeField(field)

src/modules/service-catalog/hooks/useAssetDataFetchers.spec.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import { renderHook, act } from "@testing-library/react-hooks";
22
import { useAssetDataFetchers } from "./useAssetDataFetchers";
33

44
describe("useAssetDataFetchers (direct option IDs)", () => {
5-
const assetOptionRecordPayload = (ids?: string, description?: string) => ({
5+
const assetOptionRecordPayload = (
6+
ids?: string,
7+
description?: string,
8+
name?: string
9+
) => ({
610
custom_object_record: {
11+
...(name !== undefined && { name }),
712
custom_object_fields: {
813
...(ids !== undefined && { "standard::asset_filter_ids": ids }),
914
...(description !== undefined && {
@@ -16,9 +21,11 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
1621
const assetTypeOptionRecordPayload = (
1722
ids?: string,
1823
isHidden?: boolean,
19-
description?: string
24+
description?: string,
25+
name?: string
2026
) => ({
2127
custom_object_record: {
28+
...(name !== undefined && { name }),
2229
custom_object_fields: {
2330
...(ids !== undefined && { "standard::asset_type_ids": ids }),
2431
...(isHidden !== undefined && { "standard::is_hidden": isHidden }),
@@ -34,10 +41,11 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
3441
global.fetch = jest.fn() as jest.Mock;
3542
});
3643

37-
it("fetchAssets: returns ids and description when assetOptionId exists", async () => {
44+
it("fetchAssets: returns ids, description, and name when assetOptionId exists", async () => {
3845
const mockAssetResponse = assetOptionRecordPayload(
3946
"a1,a2,a3",
40-
"Test description"
47+
"Test description",
48+
"Assigned asset"
4149
);
4250

4351
(global.fetch as jest.Mock).mockResolvedValueOnce({
@@ -49,7 +57,11 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
4957
);
5058

5159
let response:
52-
| { assetIds: string | undefined; assetDescription: string | undefined }
60+
| {
61+
assetIds: string | undefined;
62+
assetDescription: string | undefined;
63+
assetName: string | undefined;
64+
}
5365
| undefined;
5466
await act(async () => {
5567
response = await result.current.fetchAssets();
@@ -62,6 +74,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
6274
expect(response).toEqual({
6375
assetIds: "a1,a2,a3",
6476
assetDescription: "Test description",
77+
assetName: "Assigned asset",
6578
});
6679
});
6780

@@ -72,7 +85,11 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
7285
);
7386

7487
let response:
75-
| { assetIds: string | undefined; assetDescription: string | undefined }
88+
| {
89+
assetIds: string | undefined;
90+
assetDescription: string | undefined;
91+
assetName: string | undefined;
92+
}
7693
| undefined;
7794
await act(async () => {
7895
response = await result.current.fetchAssets();
@@ -99,11 +116,11 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
99116
expect(response).toBeUndefined();
100117
});
101118

102-
it("fetchAssetTypes: returns object with ids, hidden flag, and description", async () => {
119+
it("fetchAssetTypes: returns object with ids, hidden flag, description, and name", async () => {
103120
(global.fetch as jest.Mock).mockResolvedValueOnce({
104121
json: () =>
105122
Promise.resolve(
106-
assetTypeOptionRecordPayload("t1,t2", true, "Type description")
123+
assetTypeOptionRecordPayload("t1,t2", true, "Type description", "Asset Type")
107124
),
108125
});
109126

@@ -114,6 +131,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
114131
assetTypeIds?: string;
115132
isHiddenAssetsType?: boolean;
116133
assetTypeDescription?: string;
134+
assetTypeName?: string;
117135
}
118136
| undefined;
119137

@@ -129,6 +147,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
129147
assetTypeIds: "t1,t2",
130148
isHiddenAssetsType: true,
131149
assetTypeDescription: "Type description",
150+
assetTypeName: "Asset Type",
132151
});
133152
});
134153

@@ -143,6 +162,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
143162
assetTypeIds?: string;
144163
isHiddenAssetsType?: boolean;
145164
assetTypeDescription?: string;
165+
assetTypeName?: string;
146166
}
147167
| undefined;
148168

@@ -154,7 +174,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
154174
expect(out).toBeUndefined();
155175
});
156176

157-
it("fetchAssetTypes: returns { undefined, undefined, undefined } on error", async () => {
177+
it("fetchAssetTypes: returns { undefined, undefined, undefined, undefined } on error", async () => {
158178
(global.fetch as jest.Mock).mockRejectedValueOnce(new Error("network"));
159179

160180
const { result } = renderHook(() => useAssetDataFetchers("AO-1", "AT-err"));
@@ -164,6 +184,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
164184
assetTypeIds?: string;
165185
isHiddenAssetsType?: boolean;
166186
assetTypeDescription?: string;
187+
assetTypeName?: string;
167188
}
168189
| undefined;
169190

@@ -175,6 +196,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
175196
assetTypeIds: undefined,
176197
isHiddenAssetsType: undefined,
177198
assetTypeDescription: undefined,
199+
assetTypeName: undefined,
178200
});
179201
});
180202

@@ -185,13 +207,18 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
185207
);
186208

187209
let assets:
188-
| { assetIds: string | undefined; assetDescription: string | undefined }
210+
| {
211+
assetIds: string | undefined;
212+
assetDescription: string | undefined;
213+
assetName: string | undefined;
214+
}
189215
| undefined;
190216
let assetTypes:
191217
| {
192218
assetTypeIds?: string;
193219
isHiddenAssetsType?: boolean;
194220
assetTypeDescription?: string;
221+
assetTypeName?: string;
195222
}
196223
| undefined;
197224

src/modules/service-catalog/hooks/useAssetDataFetchers.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const useAssetDataFetchers = (
2020
data.custom_object_record.custom_object_fields?.[
2121
"standard::description"
2222
];
23-
return { assetIds: ids, assetDescription: description };
23+
const name = data.custom_object_record.name;
24+
return { assetIds: ids, assetDescription: description, assetName: name };
2425
} catch (error) {
2526
console.error(error);
2627
return undefined;
@@ -46,17 +47,20 @@ export const useAssetDataFetchers = (
4647
data.custom_object_record.custom_object_fields?.[
4748
"standard::description"
4849
];
50+
const name = data.custom_object_record.name;
4951
return {
5052
assetTypeIds: ids,
5153
isHiddenAssetsType,
5254
assetTypeDescription: description,
55+
assetTypeName: name,
5356
};
5457
} catch (error) {
5558
console.error(error);
5659
return {
5760
assetTypeIds: undefined,
5861
isHiddenAssetsType: undefined,
5962
assetTypeDescription: undefined,
63+
assetTypeName: undefined,
6064
};
6165
}
6266
}, [assetTypeOptionId]);

0 commit comments

Comments
 (0)