Skip to content

Commit

Permalink
get spaces from unit (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
siren authored Aug 31, 2022
1 parent 51bb9d2 commit 4abbcf2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
21 changes: 11 additions & 10 deletions admin-ui/src/component/Spaces/space-editor/ParentSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import React, { useState } from "react";
import { useQuery } from "@apollo/client";
import { Select } from "hds-react";
import i18next from "i18next";
import { Maybe, Query, SpaceType } from "../../../common/gql-types";
import {
Maybe,
Query,
QueryUnitByPkArgs,
SpaceType,
} from "../../../common/gql-types";
import { SPACE_HIERARCHY_QUERY } from "./queries";
import { spacesAsHierarchy } from "./util";

Expand Down Expand Up @@ -52,16 +57,12 @@ const ParentSelector = ({
}: Props): JSX.Element | null => {
const [parentOptions, setParentOptions] = useState([] as ParentType[]);

useQuery<Query>(SPACE_HIERARCHY_QUERY, {
onCompleted: ({ spaces }) => {
const parentSpaces = spaces?.edges.map((s) => s?.node as SpaceType);
useQuery<Query, QueryUnitByPkArgs>(SPACE_HIERARCHY_QUERY, {
variables: { pk: unitPk },
onCompleted: ({ unitByPk }) => {
const parentSpaces = unitByPk?.spaces?.map((s) => s as SpaceType);
if (parentSpaces) {
const unitSpaces = spacesAsHierarchy(
parentSpaces.filter((space) => {
return space.unit?.pk === unitPk;
}),
"\u2007"
);
const unitSpaces = spacesAsHierarchy(parentSpaces, "\u2007");

const children = spacePk
? getChildrenRecursive(spacePk, unitSpaces).map((s) => s.pk)
Expand Down
17 changes: 6 additions & 11 deletions admin-ui/src/component/Spaces/space-editor/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@ export const UPDATE_SPACE = gql`
`;

export const SPACE_HIERARCHY_QUERY = gql`
query getSpaces {
spaces {
edges {
node {
query unitSpaces($pk: Int) {
unitByPk(pk: $pk) {
spaces {
pk
nameFi
parent {
pk
nameFi
parent {
pk
}
unit {
pk
}
}
}
}
Expand Down

0 comments on commit 4abbcf2

Please sign in to comment.