Skip to content

Commit

Permalink
feat(content-sidebar): disable version modifications for archive file (
Browse files Browse the repository at this point in the history
…#3637)

* feat(content-sidebar): add unchangeable versions feature to VersionsItem

* feat(content-sidebar): create tests for unchangeableVersionsFeature

* feat(content-sidebar): add more exhaustive test cases for permissions

* feat(content-sidebar): block restore on unchangeableVersions enabled

* feat(content-sidebar): fix VersionsItem test names

* feat(content-sidebar): revert changes to VersionsItem

* feat(content-sidebar): update VersionsItem to use archive metadata

* feat(content-sidebar): implement archive fetch for versions

* feat(content-sidebar): fix flow error

* feat(content-sidebar): udpate VersionsItem tests to RTL

* feat(content-sidebar): updated prop name and order

* feat(content-sidebar): reverted changes to en-US.properties

* feat(content-sidebar): moved tests to new file

* feat(content-sidebar): updated snapshot
  • Loading branch information
michalkowalczyk-box authored Oct 11, 2024
1 parent 95735e0 commit e735c4c
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 320 deletions.
12 changes: 7 additions & 5 deletions src/elements/content-sidebar/versions/VersionsItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import './VersionsItem.scss';

type Props = {
fileId: string,
isArchived?: boolean,
isCurrent?: boolean,
isSelected?: boolean,
isWatermarked?: boolean,
Expand All @@ -51,6 +52,7 @@ const FIVE_MINUTES_MS = 5 * 60 * 1000;

const VersionsItem = ({
fileId,
isArchived = false,
isCurrent = false,
isSelected = false,
isWatermarked = false,
Expand Down Expand Up @@ -104,10 +106,10 @@ const VersionsItem = ({

// Version action helpers
const canPreview = can_preview && !isDeleted && !isLimited && !isRestricted;
const showDelete = can_delete && !isDeleted && !isCurrent;
const showDelete = can_delete && !isDeleted && !isArchived && !isCurrent;
const showDownload = can_download && !isDeleted && isDownloadable;
const showPromote = can_upload && !isDeleted && !isCurrent;
const showRestore = can_delete && isDeleted;
const showPromote = can_upload && !isDeleted && !isArchived && !isCurrent;
const showRestore = can_delete && isDeleted && !isArchived;
const showPreview = canPreview && !isSelected;
const hasActions = showDelete || showDownload || showPreview || showPromote || showRestore;

Expand Down Expand Up @@ -145,7 +147,7 @@ const VersionsItem = ({
/>
</div>

<div className="bcs-VersionsItem-info">
<div className="bcs-VersionsItem-info" data-testid="bcs-VersionsItem-info">
{versionTimestamp && (
<time className="bcs-VersionsItem-date" dateTime={versionTime}>
<ReadableTime
Expand All @@ -159,7 +161,7 @@ const VersionsItem = ({
</div>

{isRetained && (
<div className="bcs-VersionsItem-retention">
<div className="bcs-VersionsItem-retention" data-testid="bcs-VersionsItem-retention">
<VersionsItemRetention retention={retention} />
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/content-sidebar/versions/VersionsSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const VersionsSidebar = ({ error, isLoading, parentName, versions, ...rest }: Pr
)}

{showVersions && (
<div className="bcs-Versions-menu">
<div className="bcs-Versions-menu" data-testid="bcs-Versions-menu">
<VersionsMenu versions={versions} {...rest} />
</div>
)}
Expand Down
21 changes: 18 additions & 3 deletions src/elements/content-sidebar/versions/VersionsSidebarAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author Box
*/
import API from '../../../api';
import { FILE_VERSION_FIELDS_TO_FETCH } from '../../../utils/fields';
import { FILE_VERSION_FIELDS_TO_FETCH, FILE_VERSION_FIELDS_TO_FETCH_ARCHIVE } from '../../../utils/fields';
import type { BoxItem, FileVersions, BoxItemVersion } from '../../../common/types/core';

export type fetchPayload = [BoxItem, FileVersions];
Expand All @@ -14,9 +14,20 @@ export default class VersionsSidebarAPI {

fileId: string;

constructor({ api, fileId }: { api: API, fileId: string }) {
isArchiveFeatureEnabled: boolean;

constructor({
api,
fileId,
isArchiveFeatureEnabled,
}: {
api: API,
fileId: string,
isArchiveFeatureEnabled: boolean,
}) {
this.api = api;
this.fileId = fileId;
this.isArchiveFeatureEnabled = isArchiveFeatureEnabled;
}

fetchData = (): Promise<fetchPayload> => {
Expand All @@ -34,9 +45,13 @@ export default class VersionsSidebarAPI {
};

fetchFile = (): Promise<BoxItem> => {
const fields = this.isArchiveFeatureEnabled
? FILE_VERSION_FIELDS_TO_FETCH_ARCHIVE
: FILE_VERSION_FIELDS_TO_FETCH;

return new Promise((resolve, reject) =>
this.api.getFileAPI().getFile(this.fileId, resolve, reject, {
fields: FILE_VERSION_FIELDS_TO_FETCH,
fields,
forceFetch: true,
}),
);
Expand Down
24 changes: 17 additions & 7 deletions src/elements/content-sidebar/versions/VersionsSidebarContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ import noop from 'lodash/noop';
import { generatePath, withRouter } from 'react-router-dom';
import type { Match, RouterHistory } from 'react-router-dom';
import type { MessageDescriptor } from 'react-intl';
import { withFeatureConsumer, isFeatureEnabled } from '../../common/feature-checking';
import API from '../../../api';
import { FIELD_METADATA_ARCHIVE } from '../../../constants';
import messages from './messages';
import openUrlInsideIframe from '../../../utils/iframe';
import StaticVersionsSidebar from './StaticVersionSidebar';
import VersionsSidebar from './VersionsSidebar';
import VersionsSidebarAPI from './VersionsSidebarAPI';
import { withAPIContext } from '../../common/api-context';
import type { FeatureConfig } from '../../common/feature-checking';
import type { VersionActionCallback, VersionChangeCallback, SidebarLoadCallback } from './flowTypes';
import type { BoxItemVersion, BoxItem, FileVersions } from '../../../common/types/core';

type Props = {
api: API,
features: FeatureConfig,
fileId: string,
hasSidebarInitialized?: boolean,
history: RouterHistory,
Expand All @@ -42,6 +46,7 @@ type Props = {

type State = {
error?: MessageDescriptor,
isArchived: boolean,
isLoading: boolean,
isWatermarked: boolean,
versionCount: number,
Expand All @@ -66,6 +71,7 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
props: Props;

state: State = {
isArchived: false,
isLoading: true,
isWatermarked: false,
versionCount: Infinity,
Expand Down Expand Up @@ -168,16 +174,18 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
handleFetchError = (): void => {
this.setState({
error: messages.versionFetchError,
isArchived: false,
isLoading: false,
isWatermarked: false,
versionCount: 0,
versions: [],
});
};

handleFetchSuccess = ([fileResponse, versionsResponse]): [BoxItem, FileVersions] => {
handleFetchSuccess = ([fileResponse, versionsResponse]: [BoxItem, FileVersions]): [BoxItem, FileVersions] => {
const { api } = this.props;
const { version_limit } = fileResponse;
const isArchived = !!getProp(fileResponse, FIELD_METADATA_ARCHIVE);
const isWatermarked = getProp(fileResponse, 'watermark_info.is_watermarked', false);
const versionLimit = version_limit !== null && version_limit !== undefined ? version_limit : Infinity;
const versionsWithPermissions = api.getVersionsAPI(false).addPermissions(versionsResponse, fileResponse) || {};
Expand All @@ -186,6 +194,7 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
this.setState(
{
error: undefined,
isArchived,
isLoading: false,
isWatermarked,
versionCount,
Expand All @@ -207,14 +216,14 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
};

initialize = (): void => {
this.api = new VersionsSidebarAPI(this.props);
const { api, features, fileId }: Props = this.props;
const isArchiveFeatureEnabled = isFeatureEnabled(features, 'contentSidebar.archive.enabled');

this.api = new VersionsSidebarAPI({ api, fileId, isArchiveFeatureEnabled });
};

fetchData = (): Promise<?[BoxItem, FileVersions]> => {
return this.api
.fetchData()
.then(this.handleFetchSuccess)
.catch(this.handleFetchError);
return this.api.fetchData().then(this.handleFetchSuccess).catch(this.handleFetchError);
};

findVersion = (versionId: ?string): ?BoxItemVersion => {
Expand Down Expand Up @@ -299,4 +308,5 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
}

export type VersionsSidebarProps = Props;
export default flow([withRouter, withAPIContext])(VersionsSidebarContainer);
export { VersionsSidebarContainer as VersionsSidebarContainerComponent };
export default flow([withRouter, withAPIContext, withFeatureConsumer])(VersionsSidebarContainer);
Loading

0 comments on commit e735c4c

Please sign in to comment.