Skip to content

feat: implement basic archive ui to make archiving failed versions easy #10182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Re
// @Tags Templates
// @Param template path string true "Template ID" format(uuid)
// @Param after_id query string false "After ID" format(uuid)
// @Param include_archived query bool false "Include archived versions in the list"
// @Param limit query int false "Page limit"
// @Param offset query int false "Page offset"
// @Success 200 {array} codersdk.TemplateVersion
Expand Down
13 changes: 7 additions & 6 deletions docs/api/templates.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ export const patchTemplateVersion = async (
return response.data;
};

export const archiveTemplateVersion = async (templateVersionId: string) => {
const response = await axios.post<TypesGen.TemplateVersion>(
`/api/v2/templateversions/${templateVersionId}/archive`,
);
return response.data;
};

export const unarchiveTemplateVersion = async (templateVersionId: string) => {
const response = await axios.post<TypesGen.TemplateVersion>(
`/api/v2/templateversions/${templateVersionId}/unarchive`,
);
return response.data;
};

export const updateTemplateMeta = async (
templateId: string,
data: TypesGen.UpdateTemplateMeta,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useMutation, useQuery } from "react-query";
import { getTemplateVersions, updateActiveTemplateVersion } from "api/api";
import {
archiveTemplateVersion,
getTemplateVersions,
updateActiveTemplateVersion,
} from "api/api";
import { getErrorMessage } from "api/errors";
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog";
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
Expand Down Expand Up @@ -34,9 +38,31 @@ const TemplateVersionsPage = () => {
displayError(getErrorMessage(error, "Failed to promote version"));
},
});

const { mutate: archiveVersion, isLoading: isArchiving } = useMutation({
mutationFn: (templateVersionId: string) => {
return archiveTemplateVersion(templateVersionId);
},
onSuccess: async () => {
// The reload is unfortunate. When a version is archived, we should hide
// the row. I do not know an easy way to do that, so a reload makes the API call
// resend and now the version is omitted.
// TODO: Improve this to not reload the page.
location.reload();
setSelectedVersionIdToArchive(undefined);
displaySuccess("Version archived successfully");
},
onError: (error) => {
displayError(getErrorMessage(error, "Failed to archive version"));
},
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the queries and mutations we are putting into the api/queries modules

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copy pasted what was above, and changed it to archive vs promote.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor this in another PR?

I think I need to learn this new style of pushing the mutation to an external package. I thought it would be a copy paste, but it is not.

The current code is async and works on useState and callbacks, but it seems the new way is to await?


const [selectedVersionIdToPromote, setSelectedVersionIdToPromote] = useState<
string | undefined
>();
const [selectedVersionIdToArchive, setSelectedVersionIdToArchive] = useState<
string | undefined
>();

return (
<>
Expand All @@ -50,8 +76,14 @@ const TemplateVersionsPage = () => {
? setSelectedVersionIdToPromote
: undefined
}
onArchiveClick={
permissions.canUpdateTemplate
? setSelectedVersionIdToArchive
: undefined
}
activeVersionId={latestActiveVersion}
/>
{/* Promote confirm */}
<ConfirmDialog
type="info"
hideCancel={false}
Expand All @@ -65,6 +97,20 @@ const TemplateVersionsPage = () => {
confirmText="Promote"
description="Are you sure you want to promote this version? Workspaces will be prompted to “Update” to this version once promoted."
/>
{/* Archive Confirm */}
<ConfirmDialog
type="info"
hideCancel={false}
open={selectedVersionIdToArchive !== undefined}
onConfirm={() => {
archiveVersion(selectedVersionIdToArchive as string);
}}
onClose={() => setSelectedVersionIdToArchive(undefined)}
title="Archive version"
confirmLoading={isArchiving}
confirmText="Archive"
description="Are you sure you want to archive this version (this is reversible)? Archived versions cannot be used by workspaces."
/>
</>
);
};
Expand Down
22 changes: 20 additions & 2 deletions site/src/pages/TemplatePage/TemplateVersionsPage/VersionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export interface VersionRowProps {
isActive: boolean;
isLatest: boolean;
onPromoteClick?: (templateVersionId: string) => void;
onArchiveClick?: (templateVersionId: string) => void;
}

export const VersionRow: React.FC<VersionRowProps> = ({
version,
isActive,
isLatest,
onPromoteClick,
onArchiveClick,
}) => {
const styles = useStyles();
const navigate = useNavigate();
Expand Down Expand Up @@ -90,14 +92,30 @@ export const VersionRow: React.FC<VersionRowProps> = ({
<Pill text="Canceled" type="neutral" lightBorder />
)}
{jobStatus === "failed" && <Pill text="Failed" type="error" />}
{onPromoteClick && (
{jobStatus === "failed" ? (
<Button
className={styles.promoteButton}
disabled={isActive || version.archived}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
if (onArchiveClick) {
onArchiveClick(version.id);
}
}}
>
Archive&hellip;
</Button>
) : (
<Button
className={styles.promoteButton}
disabled={isActive || jobStatus !== "succeeded"}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onPromoteClick(version.id);
if (onPromoteClick) {
onPromoteClick(version.id);
}
}}
>
Promote&hellip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const Language = {
export interface VersionsTableProps {
activeVersionId: string;
onPromoteClick?: (templateVersionId: string) => void;
onArchiveClick?: (templateVersionId: string) => void;

versions?: TypesGen.TemplateVersion[];
}

export const VersionsTable: FC<VersionsTableProps> = (props) => {
const { versions, onPromoteClick, activeVersionId } = props;
const { versions, onArchiveClick, onPromoteClick, activeVersionId } = props;

const latestVersionId = versions?.reduce(
(latestSoFar, against) => {
Expand Down Expand Up @@ -55,6 +57,7 @@ export const VersionsTable: FC<VersionsTableProps> = (props) => {
getDate={(version) => new Date(version.created_at)}
row={(version) => (
<VersionRow
onArchiveClick={onArchiveClick}
onPromoteClick={onPromoteClick}
version={version}
key={version.id}
Expand Down