Skip to content

Commit

Permalink
refactor: Use ProjectsCoordinator.getVisibilities for new projects an…
Browse files Browse the repository at this point in the history
…d fork a project form
  • Loading branch information
andre-code committed Dec 22, 2021
1 parent a0b2dcf commit 9e5bacc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 47 deletions.
9 changes: 8 additions & 1 deletion client/src/project/new/ProjectNew.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ class NewProject extends Component {
return this.coordinator.getTemplates(repositories, true);
}

async calculateVisibilities(namespace) {
// temporarily reset visibility metadata
this.coordinator.resetVisibility(namespace);
const availableVisibilities = await this.projectsCoordinator.getVisibilities(namespace);
this.coordinator.setVisibilities(availableVisibilities, namespace);
}

refreshUserProjects() {
this.projectsCoordinator.getFeatured();
}
Expand All @@ -502,7 +509,7 @@ class NewProject extends Component {

setNamespace(namespace) {
this.setProperty("namespace", namespace.full_path);
this.coordinator.getVisibilities(namespace);
this.calculateVisibilities(namespace);
}

setTemplateProperty(property, value) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/project/new/ProjectNew.present.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ class Visibility extends Component {
main = (
<Fragment>
<br />
<Label className="font-italic">Verifying... <Loader inline={true} size={16} /></Label>
<Label className="font-italic">Determining options... <Loader inline={true} size={16} /></Label>
</Fragment>
);
}
Expand Down
65 changes: 24 additions & 41 deletions client/src/project/new/ProjectNew.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,55 +463,38 @@ class NewProjectCoordinator {
return "No templates available in this repo.";
}

async getVisibilities(namespace) {
// function to compute available visibilities based on the current group visibility
const computeVisibilities = (max) => {
if (max === "private")
return ["private"];
else if (max === "internal")
return ["private", "internal"];
return ["private", "internal", "public"];
};
resetVisibility(namespace) {
this.model.setObject({
meta: {
namespace: {
fetched: null,
fetching: true,
id: namespace.full_path,
visibility: null,
visibilities: { $set: null },
}
}
});
}

let visibilities, updateObject = { meta: { namespace: {} } };
if (namespace.kind === "user") {
visibilities = computeVisibilities("public");
updateObject.meta.namespace = {
visibility: "public",
visibilities: { $set: visibilities },
};
}
else {
// temporarily reset visibility metadata
this.model.setObject({
meta: {
async setVisibilities(availableVisibilities, namespace) {
let updateObject = {
meta:
{
namespace: {
fetched: null,
fetching: true,
visibility: availableVisibilities.default,
visibilities: availableVisibilities.visibilities,
fetched: new Date(),
fetching: false,
id: namespace.full_path,
visibility: null,
visibilities: { $set: [] },
}
}
});
// verify group visibility
const group = await this.client.getGroupByPath(namespace.full_path).then(r => r.data);
visibilities = computeVisibilities(group.visibility);
updateObject.meta.namespace = {
visibility: group.visibility,
visibilities: { $set: visibilities },
};
}

// set common properties
updateObject.meta.namespace.fetched = new Date();
updateObject.meta.namespace.fetching = false;
updateObject.meta.namespace.id = namespace.full_path;
};

// save the model and invoke the normal setProperty
this.model.setObject(updateObject);
this.setProperty("visibility", visibilities[visibilities.length - 1]);
return visibilities;
this.setProperty("visibility", availableVisibilities.default);
return availableVisibilities.visibilities;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions client/src/project/shared/Projects.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class ProjectsCoordinator {
});
}

async getVisibilities(namespace, projectVisibility = "private") {
async getVisibilities(namespace, projectVisibility) {
const computeVisibilities = (options) => {
if (options.includes("private")) {
return {
Expand All @@ -180,16 +180,19 @@ class ProjectsCoordinator {
};

let availableVisibilities = null;
if (!namespace || !projectVisibility)
let options = projectVisibility ? [projectVisibility] : [];
if (!namespace)
return null;

if (namespace?.kind === "user") {
return computeVisibilities(["public", projectVisibility]);
options.push("public");
return computeVisibilities(options);
}
else if (namespace?.kind === "group") {
// get group visibility
const group = await this.client.getGroupByPath(namespace.full_path).then(r => r.data);
return computeVisibilities([group.visibility, projectVisibility]);
options.push(group.visibility);
return computeVisibilities(options);
}
return availableVisibilities;
}
Expand Down

0 comments on commit 9e5bacc

Please sign in to comment.