refactor: publish project store (#2068)

This commit is contained in:
Aaryan Khandelwal 2023-09-04 12:34:12 +05:30 committed by GitHub
parent 0de62b3b0c
commit 4559a1bd5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 195 additions and 178 deletions

View File

@ -6,7 +6,14 @@ import { Controller, useForm } from "react-hook-form";
// headless ui // headless ui
import { Dialog, Transition } from "@headlessui/react"; import { Dialog, Transition } from "@headlessui/react";
// ui components // ui components
import { ToggleSwitch, PrimaryButton, SecondaryButton, Icon, DangerButton } from "components/ui"; import {
ToggleSwitch,
PrimaryButton,
SecondaryButton,
Icon,
DangerButton,
Loader,
} from "components/ui";
import { CustomPopover } from "./popover"; import { CustomPopover } from "./popover";
// mobx react lite // mobx react lite
import { observer } from "mobx-react-lite"; import { observer } from "mobx-react-lite";
@ -146,22 +153,14 @@ export const PublishProjectModal: React.FC<Props> = observer(() => {
const projectId = projectPublish.project_id; const projectId = projectPublish.project_id;
return projectPublish return projectPublish
.createProjectSettingsAsync( .publishProject(workspaceSlug.toString(), projectId?.toString() ?? "", payload, user)
workspaceSlug.toString(), .then((res) => {
projectId?.toString() ?? "",
payload,
user
)
.then((response) => {
mutateProjectDetails(); mutateProjectDetails();
handleClose(); handleClose();
if (projectId) window.open(`${plane_deploy_url}/${workspaceSlug}/${projectId}`, "_blank"); if (projectId) window.open(`${plane_deploy_url}/${workspaceSlug}/${projectId}`, "_blank");
return response; return res;
}) })
.catch((error) => { .catch((err) => err);
console.error("error", error);
return error;
});
}; };
const handleUpdatePublishSettings = async (payload: IProjectPublishSettings) => { const handleUpdatePublishSettings = async (payload: IProjectPublishSettings) => {
@ -199,7 +198,7 @@ export const PublishProjectModal: React.FC<Props> = observer(() => {
setIsUnpublishing(true); setIsUnpublishing(true);
projectPublish projectPublish
.deleteProjectSettingsAsync( .unPublishProject(
workspaceSlug.toString(), workspaceSlug.toString(),
projectPublish.project_id as string, projectPublish.project_id as string,
publishId, publishId,
@ -329,7 +328,7 @@ export const PublishProjectModal: React.FC<Props> = observer(() => {
{/* heading */} {/* heading */}
<div className="px-6 pt-4 flex items-center justify-between gap-2"> <div className="px-6 pt-4 flex items-center justify-between gap-2">
<h5 className="font-semibold text-xl inline-block">Publish</h5> <h5 className="font-semibold text-xl inline-block">Publish</h5>
{watch("id") && ( {projectPublish.projectPublishSettings !== "not-initialized" && (
<DangerButton <DangerButton
onClick={() => handleUnpublishProject(watch("id") ?? "")} onClick={() => handleUnpublishProject(watch("id") ?? "")}
className="!px-2 !py-1.5" className="!px-2 !py-1.5"
@ -341,137 +340,145 @@ export const PublishProjectModal: React.FC<Props> = observer(() => {
</div> </div>
{/* content */} {/* content */}
<div className="space-y-3 px-6"> {projectPublish.fetchSettingsLoader ? (
<div className="border border-custom-border-100 bg-custom-background-80 rounded-md px-3 py-2 relative flex gap-2 items-center"> <Loader className="px-6 space-y-4">
<div className="truncate flex-grow text-sm"> <Loader.Item height="30px" />
{`${plane_deploy_url}/${workspaceSlug}/${projectPublish.project_id}`} <Loader.Item height="30px" />
</div> <Loader.Item height="30px" />
<div className="flex-shrink-0 relative flex items-center gap-1"> <Loader.Item height="30px" />
<CopyLinkToClipboard </Loader>
copy_link={`${plane_deploy_url}/${workspaceSlug}/${projectPublish.project_id}`} ) : (
/> <div className="px-6">
</div> {watch("id") && (
</div> <>
<div className="border border-custom-border-100 bg-custom-background-80 rounded-md px-3 py-2 relative flex gap-2 items-center">
<div className="truncate flex-grow text-sm">
{`${plane_deploy_url}/${workspaceSlug}/${projectPublish.project_id}`}
</div>
<div className="flex-shrink-0 relative flex items-center gap-1">
<CopyLinkToClipboard
copy_link={`${plane_deploy_url}/${workspaceSlug}/${projectPublish.project_id}`}
/>
</div>
</div>
<div className="flex items-center gap-1 text-custom-primary-100 mt-3">
<div className="w-5 h-5 overflow-hidden flex items-center">
<Icon iconName="radio_button_checked" className="!text-lg" />
</div>
<div className="text-sm">This project is live on web</div>
</div>
</>
)}
{watch("id") && ( <div className="space-y-4 mt-6">
<div className="flex items-center gap-1 text-custom-primary-100"> <div className="relative flex justify-between items-center gap-2">
<div className="w-5 h-5 overflow-hidden flex items-center"> <div className="text-sm">Views</div>
<Icon iconName="radio_button_checked" className="!text-lg" /> <Controller
</div> control={control}
<div className="text-sm">This project is live on web</div> name="views"
</div> render={({ field: { onChange, value } }) => (
)} <CustomPopover
label={
<div className="space-y-4"> value.length > 0
<div className="relative flex justify-between items-center gap-2"> ? viewOptions
<div className="text-sm">Views</div> .filter((v) => value.includes(v.key))
<Controller .map((v) => v.label)
control={control} .join(", ")
name="views" : ``
render={({ field: { onChange, value } }) => ( }
<CustomPopover placeholder="Select views"
label={ >
value.length > 0 <>
? viewOptions {viewOptions.map((option) => (
.filter((v) => value.includes(v.key))
.map((v) => v.label)
.join(", ")
: ``
}
placeholder="Select views"
>
<>
{viewOptions.map((option) => (
<div
key={option.key}
className={`relative flex items-center gap-2 justify-between p-1 m-1 px-2 cursor-pointer rounded-sm text-custom-text-200 ${
value.includes(option.key)
? "bg-custom-background-80 text-custom-text-100"
: "hover:bg-custom-background-80 hover:text-custom-text-100"
}`}
onClick={() => {
const _views =
value.length > 0
? value.includes(option.key)
? value.filter((_o: string) => _o !== option.key)
: [...value, option.key]
: [option.key];
if (_views.length === 0) return;
onChange(_views);
checkIfUpdateIsRequired();
}}
>
<div className="text-sm">{option.label}</div>
<div <div
className={`w-[18px] h-[18px] relative flex justify-center items-center`} key={option.key}
className={`relative flex items-center gap-2 justify-between p-1 m-1 px-2 cursor-pointer rounded-sm text-custom-text-200 ${
value.includes(option.key)
? "bg-custom-background-80 text-custom-text-100"
: "hover:bg-custom-background-80 hover:text-custom-text-100"
}`}
onClick={() => {
const _views =
value.length > 0
? value.includes(option.key)
? value.filter((_o: string) => _o !== option.key)
: [...value, option.key]
: [option.key];
if (_views.length === 0) return;
onChange(_views);
checkIfUpdateIsRequired();
}}
> >
{value.length > 0 && value.includes(option.key) && ( <div className="text-sm">{option.label}</div>
<Icon iconName="done" className="!text-lg" /> <div
)} className={`w-[18px] h-[18px] relative flex justify-center items-center`}
>
{value.length > 0 && value.includes(option.key) && (
<Icon iconName="done" className="!text-lg" />
)}
</div>
</div> </div>
</div> ))}
))} </>
</> </CustomPopover>
</CustomPopover> )}
)} />
/> </div>
</div> <div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow comments</div>
<Controller
control={control}
name="comments"
render={({ field: { onChange, value } }) => (
<ToggleSwitch
value={value}
onChange={(val) => {
onChange(val);
checkIfUpdateIsRequired();
}}
size="sm"
/>
)}
/>
</div>
<div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow reactions</div>
<Controller
control={control}
name="reactions"
render={({ field: { onChange, value } }) => (
<ToggleSwitch
value={value}
onChange={(val) => {
onChange(val);
checkIfUpdateIsRequired();
}}
size="sm"
/>
)}
/>
</div>
<div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow voting</div>
<Controller
control={control}
name="votes"
render={({ field: { onChange, value } }) => (
<ToggleSwitch
value={value}
onChange={(val) => {
onChange(val);
checkIfUpdateIsRequired();
}}
size="sm"
/>
)}
/>
</div>
<div className="relative flex justify-between items-center gap-2"> {/* <div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow comments</div>
<Controller
control={control}
name="comments"
render={({ field: { onChange, value } }) => (
<ToggleSwitch
value={value}
onChange={(val) => {
onChange(val);
checkIfUpdateIsRequired();
}}
size="sm"
/>
)}
/>
</div>
<div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow reactions</div>
<Controller
control={control}
name="reactions"
render={({ field: { onChange, value } }) => (
<ToggleSwitch
value={value}
onChange={(val) => {
onChange(val);
checkIfUpdateIsRequired();
}}
size="sm"
/>
)}
/>
</div>
<div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow voting</div>
<Controller
control={control}
name="votes"
render={({ field: { onChange, value } }) => (
<ToggleSwitch
value={value}
onChange={(val) => {
onChange(val);
checkIfUpdateIsRequired();
}}
size="sm"
/>
)}
/>
</div>
{/* <div className="relative flex justify-between items-center gap-2">
<div className="text-sm">Allow issue proposals</div> <div className="text-sm">Allow issue proposals</div>
<Controller <Controller
control={control} control={control}
@ -481,8 +488,9 @@ export const PublishProjectModal: React.FC<Props> = observer(() => {
)} )}
/> />
</div> */} </div> */}
</div>
</div> </div>
</div> )}
{/* modal handlers */} {/* modal handlers */}
<div className="border-t border-custom-border-200 px-6 py-5 relative flex justify-between items-center"> <div className="border-t border-custom-border-200 px-6 py-5 relative flex justify-between items-center">
@ -490,22 +498,24 @@ export const PublishProjectModal: React.FC<Props> = observer(() => {
<Icon iconName="public" className="!text-base" /> <Icon iconName="public" className="!text-base" />
<div className="text-sm">Anyone with the link can access</div> <div className="text-sm">Anyone with the link can access</div>
</div> </div>
<div className="relative flex items-center gap-2"> {!projectPublish.fetchSettingsLoader && (
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton> <div className="relative flex items-center gap-2">
{watch("id") ? ( <SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
<> {watch("id") ? (
{isUpdateRequired && ( <>
<PrimaryButton type="submit" loading={isSubmitting}> {isUpdateRequired && (
{isSubmitting ? "Updating..." : "Update settings"} <PrimaryButton type="submit" loading={isSubmitting}>
</PrimaryButton> {isSubmitting ? "Updating..." : "Update settings"}
)} </PrimaryButton>
</> )}
) : ( </>
<PrimaryButton type="submit" loading={isSubmitting}> ) : (
{isSubmitting ? "Publishing..." : "Publish"} <PrimaryButton type="submit" loading={isSubmitting}>
</PrimaryButton> {isSubmitting ? "Publishing..." : "Publish"}
)} </PrimaryButton>
</div> )}
</div>
)}
</div> </div>
</form> </form>
</Dialog.Panel> </Dialog.Panel>

View File

@ -21,7 +21,8 @@ export interface IProjectPublishSettings {
} }
export interface IProjectPublishStore { export interface IProjectPublishStore {
loader: boolean; generalLoader: boolean;
fetchSettingsLoader: boolean;
error: any | null; error: any | null;
projectPublishModal: boolean; projectPublishModal: boolean;
@ -35,7 +36,7 @@ export interface IProjectPublishStore {
project_slug: string, project_slug: string,
user: any user: any
) => Promise<void>; ) => Promise<void>;
createProjectSettingsAsync: ( publishProject: (
workspace_slug: string, workspace_slug: string,
project_slug: string, project_slug: string,
data: IProjectPublishSettings, data: IProjectPublishSettings,
@ -48,7 +49,7 @@ export interface IProjectPublishStore {
data: IProjectPublishSettings, data: IProjectPublishSettings,
user: any user: any
) => Promise<void>; ) => Promise<void>;
deleteProjectSettingsAsync: ( unPublishProject: (
workspace_slug: string, workspace_slug: string,
project_slug: string, project_slug: string,
project_publish_id: string, project_publish_id: string,
@ -57,7 +58,8 @@ export interface IProjectPublishStore {
} }
class ProjectPublishStore implements IProjectPublishStore { class ProjectPublishStore implements IProjectPublishStore {
loader: boolean = false; generalLoader: boolean = false;
fetchSettingsLoader: boolean = false;
error: any | null = null; error: any | null = null;
projectPublishModal: boolean = false; projectPublishModal: boolean = false;
@ -72,7 +74,8 @@ class ProjectPublishStore implements IProjectPublishStore {
constructor(_rootStore: RootStore) { constructor(_rootStore: RootStore) {
makeObservable(this, { makeObservable(this, {
// observable // observable
loader: observable, generalLoader: observable,
fetchSettingsLoader: observable,
error: observable, error: observable,
projectPublishModal: observable, projectPublishModal: observable,
@ -80,6 +83,10 @@ class ProjectPublishStore implements IProjectPublishStore {
projectPublishSettings: observable.ref, projectPublishSettings: observable.ref,
// action // action
handleProjectModal: action, handleProjectModal: action,
getProjectSettingsAsync: action,
publishProject: action,
updateProjectSettingsAsync: action,
unPublishProject: action,
// computed // computed
}); });
@ -100,7 +107,7 @@ class ProjectPublishStore implements IProjectPublishStore {
getProjectSettingsAsync = async (workspace_slug: string, project_slug: string, user: any) => { getProjectSettingsAsync = async (workspace_slug: string, project_slug: string, user: any) => {
try { try {
this.loader = true; this.fetchSettingsLoader = true;
this.error = null; this.error = null;
const response = await this.projectPublishService.getProjectSettingsAsync( const response = await this.projectPublishService.getProjectSettingsAsync(
@ -128,30 +135,30 @@ class ProjectPublishStore implements IProjectPublishStore {
runInAction(() => { runInAction(() => {
this.projectPublishSettings = _projectPublishSettings; this.projectPublishSettings = _projectPublishSettings;
this.loader = false; this.fetchSettingsLoader = false;
this.error = null; this.error = null;
}); });
} else { } else {
this.projectPublishSettings = "not-initialized"; this.projectPublishSettings = "not-initialized";
this.loader = false; this.fetchSettingsLoader = false;
this.error = null; this.error = null;
} }
return response; return response;
} catch (error) { } catch (error) {
this.loader = false; this.fetchSettingsLoader = false;
this.error = error; this.error = error;
return error; return error;
} }
}; };
createProjectSettingsAsync = async ( publishProject = async (
workspace_slug: string, workspace_slug: string,
project_slug: string, project_slug: string,
data: IProjectPublishSettings, data: IProjectPublishSettings,
user: any user: any
) => { ) => {
try { try {
this.loader = true; this.generalLoader = true;
this.error = null; this.error = null;
const response = await this.projectPublishService.createProjectSettingsAsync( const response = await this.projectPublishService.createProjectSettingsAsync(
@ -174,14 +181,14 @@ class ProjectPublishStore implements IProjectPublishStore {
runInAction(() => { runInAction(() => {
this.projectPublishSettings = _projectPublishSettings; this.projectPublishSettings = _projectPublishSettings;
this.loader = false; this.generalLoader = false;
this.error = null; this.error = null;
}); });
return response; return response;
} }
} catch (error) { } catch (error) {
this.loader = false; this.generalLoader = false;
this.error = error; this.error = error;
return error; return error;
} }
@ -195,7 +202,7 @@ class ProjectPublishStore implements IProjectPublishStore {
user: any user: any
) => { ) => {
try { try {
this.loader = true; this.generalLoader = true;
this.error = null; this.error = null;
const response = await this.projectPublishService.updateProjectSettingsAsync( const response = await this.projectPublishService.updateProjectSettingsAsync(
@ -219,27 +226,27 @@ class ProjectPublishStore implements IProjectPublishStore {
runInAction(() => { runInAction(() => {
this.projectPublishSettings = _projectPublishSettings; this.projectPublishSettings = _projectPublishSettings;
this.loader = false; this.generalLoader = false;
this.error = null; this.error = null;
}); });
return response; return response;
} }
} catch (error) { } catch (error) {
this.loader = false; this.generalLoader = false;
this.error = error; this.error = error;
return error; return error;
} }
}; };
deleteProjectSettingsAsync = async ( unPublishProject = async (
workspace_slug: string, workspace_slug: string,
project_slug: string, project_slug: string,
project_publish_id: string, project_publish_id: string,
user: any user: any
) => { ) => {
try { try {
this.loader = true; this.generalLoader = true;
this.error = null; this.error = null;
const response = await this.projectPublishService.deleteProjectSettingsAsync( const response = await this.projectPublishService.deleteProjectSettingsAsync(
@ -251,13 +258,13 @@ class ProjectPublishStore implements IProjectPublishStore {
runInAction(() => { runInAction(() => {
this.projectPublishSettings = "not-initialized"; this.projectPublishSettings = "not-initialized";
this.loader = false; this.generalLoader = false;
this.error = null; this.error = null;
}); });
return response; return response;
} catch (error) { } catch (error) {
this.loader = false; this.generalLoader = false;
this.error = error; this.error = error;
return error; return error;
} }