Skip to content

Commit 6982b49

Browse files
fix: label should be taken from asset api
1 parent c766555 commit 6982b49

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

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

Lines changed: 26 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,15 @@ 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({
223+
ids: [],
224+
ready: true,
225+
description: undefined,
226+
name: undefined,
227+
});
212228
}
213229
};
214230

@@ -278,6 +294,11 @@ export function ItemRequestForm({
278294
}
279295
const customField = {
280296
...field,
297+
label: isAssetField(field)
298+
? assetState.name || field.label
299+
: isAssetTypeField(field)
300+
? assetTypeState.name || field.label
301+
: field.label,
281302
description: isAssetField(field)
282303
? assetState.description || field.description
283304
: isAssetTypeField(field)

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

Lines changed: 42 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,16 @@ 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(
124+
"t1,t2",
125+
true,
126+
"Type description",
127+
"Asset Type"
128+
)
107129
),
108130
});
109131

@@ -114,6 +136,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
114136
assetTypeIds?: string;
115137
isHiddenAssetsType?: boolean;
116138
assetTypeDescription?: string;
139+
assetTypeName?: string;
117140
}
118141
| undefined;
119142

@@ -129,6 +152,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
129152
assetTypeIds: "t1,t2",
130153
isHiddenAssetsType: true,
131154
assetTypeDescription: "Type description",
155+
assetTypeName: "Asset Type",
132156
});
133157
});
134158

@@ -143,6 +167,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
143167
assetTypeIds?: string;
144168
isHiddenAssetsType?: boolean;
145169
assetTypeDescription?: string;
170+
assetTypeName?: string;
146171
}
147172
| undefined;
148173

@@ -154,7 +179,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
154179
expect(out).toBeUndefined();
155180
});
156181

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

160185
const { result } = renderHook(() => useAssetDataFetchers("AO-1", "AT-err"));
@@ -164,6 +189,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
164189
assetTypeIds?: string;
165190
isHiddenAssetsType?: boolean;
166191
assetTypeDescription?: string;
192+
assetTypeName?: string;
167193
}
168194
| undefined;
169195

@@ -175,6 +201,7 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
175201
assetTypeIds: undefined,
176202
isHiddenAssetsType: undefined,
177203
assetTypeDescription: undefined,
204+
assetTypeName: undefined,
178205
});
179206
});
180207

@@ -185,13 +212,18 @@ describe("useAssetDataFetchers (direct option IDs)", () => {
185212
);
186213

187214
let assets:
188-
| { assetIds: string | undefined; assetDescription: string | undefined }
215+
| {
216+
assetIds: string | undefined;
217+
assetDescription: string | undefined;
218+
assetName: string | undefined;
219+
}
189220
| undefined;
190221
let assetTypes:
191222
| {
192223
assetTypeIds?: string;
193224
isHiddenAssetsType?: boolean;
194225
assetTypeDescription?: string;
226+
assetTypeName?: string;
195227
}
196228
| undefined;
197229

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)