Skip to content

Commit

Permalink
Merge pull request stakwork#918 from aliraza556/feature/workspace-def…
Browse files Browse the repository at this point in the history
…ault-selection

Add Default `Workspace` Selection in Bounty Creation Form
  • Loading branch information
humansinstitute authored Jan 13, 2025
2 parents a4f6ee9 + a4819d6 commit a2f93ff
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/components/form/bounty/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { observer } from 'mobx-react-lite';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useIsMobile } from 'hooks';
import { RefineDescriptionModal } from 'components/common/RefineDescriptionModal';
import { toCapitalize } from '../../../helpers/helpers';
import api from '../../../api';
import { colors } from '../../../config/colors';
import { BountyDetailsCreationData } from '../../../people/utils/BountyCreationConstant';
Expand Down Expand Up @@ -59,6 +60,7 @@ function Form(props: FormProps) {

const [schemaData, setSchemaData] = useState(BountyDetailsCreationData.step_1);
const [stepTracker, setStepTracker] = useState<number>(1);
const [userWorkspaces, setUserWorkspaces] = useState<Array<{ label: string; value: string }>>([]);

let lastPage = 1;
const scrollDiv = scrollRef ?? refBody;
Expand Down Expand Up @@ -95,6 +97,20 @@ function Form(props: FormProps) {
}
}, [stepTracker]);

const getUUIDFromURL = useCallback((url: string) => {
const urlParts = url.split('/');
return urlParts[urlParts.length - 1];
}, []);

useEffect(() => {
async function fetchWorkspaces() {
if (ui.meInfo?.id) {
await main.getUserDropdownWorkspaces(ui.meInfo?.id);
}
}
fetchWorkspaces();
}, [main, ui.meInfo?.id]);

useEffect(() => {
(async () => {
try {
Expand Down Expand Up @@ -234,6 +250,38 @@ function Form(props: FormProps) {
})();
}, [featureid, main]);

useEffect(() => {
if (main.dropDownWorkspaces.length) {
const workspaceOptions = main.dropDownWorkspaces.map((org: any) => ({
label: toCapitalize(org.name),
value: org.uuid
}));
setUserWorkspaces(workspaceOptions);

if (schema && Array.isArray(schema)) {
const orgUuidFieldIndex = schema.findIndex((field: FormField) => field.name === 'org_uuid');
if (orgUuidFieldIndex !== -1) {
schema[orgUuidFieldIndex].options = workspaceOptions;
}
}
}
}, [main.dropDownWorkspaces, schema]);

useEffect(() => {
if (userWorkspaces.length && !dynamicInitialValues?.org_uuid) {
const uuid = getUUIDFromURL(window.location.href);
const defaultWorkspace = userWorkspaces.find((obj: any) => obj.value === uuid);

if (defaultWorkspace) {
setDynamicInitialValues((prev: any) => ({
...prev,
org_uuid: defaultWorkspace.value
}));
reloadForm();
}
}
}, [userWorkspaces, dynamicInitialValues?.org_uuid, getUUIDFromURL]);

// replace schema with dynamic schema if there is one
schema = dynamicSchema || schema;
if (schema && Array.isArray(schema)) {
Expand Down

0 comments on commit a2f93ff

Please sign in to comment.