• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alkem-io / client-web / #9731

03 Jan 2025 01:52PM UTC coverage: 5.781%. First build
#9731

Pull #7386

travis-ci

Pull Request #7386: Ability to select SpaceLevel2 on create VC

190 of 10868 branches covered (1.75%)

Branch coverage included in aggregate %.

1 of 44 new or added lines in 4 files covered. (2.27%)

1524 of 18780 relevant lines covered (8.12%)

0.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/topLevelPages/myDashboard/newVirtualContributorWizard/ExistingSpace.tsx
1
import { useMemo, useState } from 'react';
2
import DialogHeader from '@/core/ui/dialog/DialogHeader';
3
import { Button, DialogActions, DialogContent } from '@mui/material';
4
import { Caption } from '@/core/ui/typography';
5
import { useTranslation } from 'react-i18next';
6
import Gutters from '@/core/ui/grid/Gutters';
7
import { Formik } from 'formik';
8
import * as yup from 'yup';
9
import FormikAutocomplete from '@/core/ui/forms/FormikAutocomplete';
10
import { SelectableSpace } from './useVirtualContributorWizard';
11
import Loading from '@/core/ui/loading/Loading';
12
import { textLengthValidator } from '@/core/ui/forms/validator/textLengthValidator';
13

14
export interface SelectableKnowledgeSpace {
15
  id: string;
16
  name: string;
17
  about: {
18
    profile: {
19
      displayName: string;
20
      url: string | undefined;
21
    };
22
    membership: {
23
      roleSetID: string;
24
    };
25
  };
26
  parentRoleSetIds?: string[];
27
}
28

29
interface ExistingSpaceProps {
×
30
  onClose: () => void;
×
31
  onBack: () => void;
32
  onSubmit: (subspace: SelectableKnowledgeSpace) => Promise<void>;
×
33
  loading: boolean;
34
  spaces: SelectableSpace[];
35
  titleId?: string;
NEW
36
}
×
NEW
37

×
NEW
38
const ExistingSpace = ({ onClose, onBack, onSubmit, spaces, loading, titleId }: ExistingSpaceProps) => {
×
NEW
39
  const { t } = useTranslation();
×
40
  const [submitLoading, setSubmitLoading] = useState(false);
41

×
42
  const initialValues = {
×
43
    subspaceId: '',
NEW
44
  };
×
45

46
  const listItems = useMemo(() => {
47
    const result: SelectableKnowledgeSpace[] = [];
NEW
48
    const addSelectableSpace = (space: SelectableSpace, parentSpaces: SelectableSpace[] = []) => {
×
NEW
49
      const name = `${space.about.profile.displayName}${parentSpaces.length > 0 ? '' : ` (${t('common.space')})`}`;
×
NEW
50
      result.push({
×
NEW
51
        id: space.id,
×
NEW
52
        name,
×
NEW
53
        about: {
×
54
          profile: {
55
            displayName: `${space.about.profile.displayName}${
56
              parentSpaces.length > 0 ? '' : ` (${t('common.space')})`
NEW
57
            }`,
×
58
            url:
59
              parentSpaces.length > 0
60
                ? parentSpaces[parentSpaces.length - 1].about.profile.url
×
61
                : space.about.profile.url, // If available, go to the parent space
62
          },
63
          membership: {
64
            roleSetID: space.about.membership?.roleSetID ?? '',
×
NEW
65
          },
×
66
        },
×
67
        parentRoleSetIds: parentSpaces.map(space => space?.about.membership?.roleSetID ?? ''),
68
      });
69
    };
×
70

71
    // Hierarchy loop
72
    spaces.forEach((space: SelectableSpace) => {
73
      addSelectableSpace(space);
74
      space.subspaces?.forEach(subspace => {
75
        addSelectableSpace(subspace, [space]);
76
        subspace.subspaces?.forEach(subsubspace => {
77
          addSelectableSpace(subsubspace, [space, subspace]);
78
        });
×
79
      });
80
    });
81

×
82
    return result;
83
  }, [spaces]);
84

×
85
  const validationSchema = yup.object().shape({
86
    subspaceId: textLengthValidator({ required: true }),
87
  });
88

89
  const onCreate = async (values: { subspaceId: string }) => {
90
    setSubmitLoading(true);
91

92
    const bok = listItems.filter(s => s.id === values.subspaceId)[0];
93
    if (bok) {
94
      await onSubmit(bok);
95
    }
96
    setSubmitLoading(false);
97
  };
98

99
  return (
100
    <Formik
101
      initialValues={initialValues}
×
102
      validationSchema={validationSchema}
103
      enableReinitialize
104
      validateOnMount
×
105
      onSubmit={onCreate}
106
    >
×
107
      {({ values, isValid }) => (
108
        <>
109
          <DialogHeader
110
            id={titleId}
111
            onClose={onClose}
112
            title={t('createVirtualContributorWizard.existingSpace.title')}
113
          />
114
          <DialogContent>
115
            {loading && spaces.length === 0 && <Loading />}
116
            {!loading && spaces.length === 0 && (
117
              <Caption>{t('createVirtualContributorWizard.existingSpace.noSpaces')}</Caption>
118
            )}
119
            {spaces.length > 0 && (
120
              <Gutters disablePadding>
121
                <Caption>{t('createVirtualContributorWizard.existingSpace.description')}</Caption>
122
                <FormikAutocomplete
123
                  name="subspaceId"
124
                  title={t('createVirtualContributorWizard.existingSpace.label')}
125
                  values={listItems}
126
                  required
127
                  disablePortal={false}
128
                />
129
              </Gutters>
130
            )}
131
          </DialogContent>
132
          <DialogActions>
133
            <Button variant="text" onClick={onBack}>
134
              {t('buttons.back')}
135
            </Button>
136
            {spaces.length > 0 && (
137
              <Button
138
                variant="contained"
139
                disabled={!isValid || loading || submitLoading}
140
                loading={submitLoading}
141
                onClick={() => onCreate(values)}
142
              >
143
                {t('buttons.create')}
144
              </Button>
145
            )}
146
          </DialogActions>
147
        </>
148
      )}
149
    </Formik>
150
  );
151
};
152

153
export default ExistingSpace;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc