Skip to content

Commit 3f5f039

Browse files
committed
expanding subrows working
1 parent 91d20bf commit 3f5f039

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/ui/pages/screen/ScreenComponent.tsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,54 @@ import { MantineReactTable, type MRT_ColumnDef } from 'mantine-react-table';
44
import { z } from 'zod';
55
import { OrganizationList } from '@common/orgs';
66

7+
const OrganizationListEnum = z.enum(OrganizationList);
8+
79
const userSchema = z.object({
810
netid: z.string().min(1),
9-
org: z.enum(OrganizationList),
11+
org: OrganizationListEnum,
1012
firstName: z.string().min(1),
1113
middleName: z.string().optional(),
1214
lastName: z.string().min(1),
1315
});
16+
17+
const orgSuperRowSchema = userSchema.extend({
18+
netid: z.string().max(0),
19+
firstName: z.string().max(0),
20+
lastName: z.string().max(0),
21+
subRows: userSchema.array(),
22+
});
23+
1424
export type User = z.infer<typeof userSchema>;
25+
export type OrgSuperRow = z.infer<typeof orgSuperRowSchema>;
26+
export type Org = z.infer<typeof OrganizationListEnum>;
27+
28+
function groupUsersByOrg(users: User[]): OrgSuperRow[] {
29+
const grouped: Record<Org, User[]> = {} as Record<Org, User[]>;
30+
31+
// Group users by organization
32+
users.forEach((user) => {
33+
if (!grouped[user.org]) {
34+
grouped[user.org] = [];
35+
}
36+
grouped[user.org].push(user);
37+
});
38+
39+
// Transform into the desired structure
40+
const reformatted = Object.entries(grouped).map(([org, subRows]) =>
41+
orgSuperRowSchema.parse({
42+
netid: '',
43+
org,
44+
firstName: '',
45+
lastName: '',
46+
subRows,
47+
})
48+
);
49+
50+
return reformatted;
51+
}
1552

1653
export const ScreenComponent: React.FC = () => {
17-
const [userList, setUserList] = useState<User[]>([]);
54+
const [userList, setUserList] = useState<OrgSuperRow[]>([]);
1855
const columns = useMemo<MRT_ColumnDef<User>[]>(
1956
() => [
2057
{
@@ -119,7 +156,8 @@ export const ScreenComponent: React.FC = () => {
119156
return { ...orgObj, ...nameObj } as User;
120157
});
121158

122-
setUserList(mergedResponse);
159+
// console.log(mergedResponse);
160+
setUserList(groupUsersByOrg(mergedResponse));
123161
};
124162
getUsers();
125163
}, []);

0 commit comments

Comments
 (0)