debian-mirror-gitlab/app/assets/javascripts/releases/components/app_index.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

291 lines
8.5 KiB
Vue
Raw Normal View History

2019-02-15 15:39:39 +05:30
<script>
2022-06-21 17:19:12 +05:30
import { GlButton } from '@gitlab/ui';
2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2022-06-21 17:19:12 +05:30
import { historyPushState } from '~/lib/utils/common_utils';
import { scrollUp } from '~/lib/utils/scroll_utils';
import { setUrlParams, getParameterByName } from '~/lib/utils/url_utility';
2022-08-13 15:12:31 +05:30
import { __, sprintf } from '~/locale';
2022-06-21 17:19:12 +05:30
import { PAGE_SIZE, DEFAULT_SORT } from '~/releases/constants';
2022-08-13 15:12:31 +05:30
import { convertAllReleasesGraphQLResponse, deleteReleaseSessionKey } from '~/releases/util';
2022-06-21 17:19:12 +05:30
import allReleasesQuery from '../graphql/queries/all_releases.query.graphql';
2019-02-15 15:39:39 +05:30
import ReleaseBlock from './release_block.vue';
2021-01-03 14:25:43 +05:30
import ReleaseSkeletonLoader from './release_skeleton_loader.vue';
2022-06-21 17:19:12 +05:30
import ReleasesEmptyState from './releases_empty_state.vue';
2021-03-11 19:13:27 +05:30
import ReleasesPagination from './releases_pagination.vue';
2021-01-29 00:20:46 +05:30
import ReleasesSort from './releases_sort.vue';
2019-02-15 15:39:39 +05:30
export default {
2022-06-21 17:19:12 +05:30
name: 'ReleasesIndexApp',
2019-02-15 15:39:39 +05:30
components: {
2020-05-24 23:13:21 +05:30
GlButton,
2021-01-03 14:25:43 +05:30
ReleaseBlock,
ReleaseSkeletonLoader,
2022-06-21 17:19:12 +05:30
ReleasesEmptyState,
ReleasesPagination,
2021-01-29 00:20:46 +05:30
ReleasesSort,
2019-02-15 15:39:39 +05:30
},
2022-06-21 17:19:12 +05:30
inject: {
projectPath: {
default: '',
},
newReleasePath: {
default: '',
},
},
apollo: {
/**
* The same query as `fullGraphqlResponse`, except that it limits its
* results to a single item. This causes this request to complete much more
* quickly than `fullGraphqlResponse`, which allows the page to show
* meaningful content to the user much earlier.
*/
singleGraphqlResponse: {
query: allReleasesQuery,
// This trick only works when paginating _forward_.
// When paginating backwards, limiting the query to a single item loads
// the _last_ item in the page, which is not useful for our purposes.
skip() {
return !this.includeSingleQuery;
},
variables() {
return {
...this.queryVariables,
first: 1,
};
},
update(data) {
return { data };
},
error() {
this.singleRequestError = true;
},
},
fullGraphqlResponse: {
query: allReleasesQuery,
variables() {
return this.queryVariables;
},
update(data) {
return { data };
},
error(error) {
this.fullRequestError = true;
2022-11-25 23:54:43 +05:30
createAlert({
2022-06-21 17:19:12 +05:30
message: this.$options.i18n.errorMessage,
captureError: true,
error,
});
},
},
},
data() {
return {
singleRequestError: false,
fullRequestError: false,
cursors: {
before: getParameterByName('before'),
after: getParameterByName('after'),
},
sort: DEFAULT_SORT,
};
},
2019-02-15 15:39:39 +05:30
computed: {
2022-06-21 17:19:12 +05:30
queryVariables() {
let paginationParams = { first: PAGE_SIZE };
if (this.cursors.after) {
paginationParams = {
after: this.cursors.after,
first: PAGE_SIZE,
};
} else if (this.cursors.before) {
paginationParams = {
before: this.cursors.before,
last: PAGE_SIZE,
};
}
return {
fullPath: this.projectPath,
...paginationParams,
sort: this.sort,
};
},
/**
* @returns {Boolean} Whether or not to request/include
* the results of the single-item query
*/
includeSingleQuery() {
return Boolean(!this.cursors.before || this.cursors.after);
},
isSingleRequestLoading() {
return this.$apollo.queries.singleGraphqlResponse.loading;
2019-02-15 15:39:39 +05:30
},
2022-06-21 17:19:12 +05:30
isFullRequestLoading() {
return this.$apollo.queries.fullGraphqlResponse.loading;
},
/**
* @returns {Boolean} `true` if the `singleGraphqlResponse`
* query has finished loading without errors
*/
isSingleRequestLoaded() {
return Boolean(!this.isSingleRequestLoading && this.singleGraphqlResponse?.data.project);
},
/**
* @returns {Boolean} `true` if the `fullGraphqlResponse`
* query has finished loading without errors
*/
isFullRequestLoaded() {
return Boolean(!this.isFullRequestLoading && this.fullGraphqlResponse?.data.project);
},
releases() {
if (this.isFullRequestLoaded) {
return convertAllReleasesGraphQLResponse(this.fullGraphqlResponse).data;
}
if (this.isSingleRequestLoaded && this.includeSingleQuery) {
return convertAllReleasesGraphQLResponse(this.singleGraphqlResponse).data;
}
return [];
2019-02-15 15:39:39 +05:30
},
2022-06-21 17:19:12 +05:30
pageInfo() {
if (!this.isFullRequestLoaded) {
return {
hasPreviousPage: false,
hasNextPage: false,
};
}
return this.fullGraphqlResponse.data.project.releases.pageInfo;
},
shouldRenderEmptyState() {
return this.isFullRequestLoaded && this.releases.length === 0;
},
shouldRenderLoadingIndicator() {
return (
(this.isSingleRequestLoading && !this.singleRequestError && !this.isFullRequestLoaded) ||
(this.isFullRequestLoading && !this.fullRequestError)
2020-04-08 14:13:33 +05:30
);
},
2022-06-21 17:19:12 +05:30
shouldRenderPagination() {
return this.isFullRequestLoaded && !this.shouldRenderEmptyState;
},
2019-02-15 15:39:39 +05:30
},
2022-08-13 15:12:31 +05:30
mounted() {
const key = deleteReleaseSessionKey(this.projectPath);
const deletedRelease = window.sessionStorage.getItem(key);
if (deletedRelease) {
this.$toast.show(
sprintf(__('Release %{deletedRelease} has been successfully deleted.'), {
deletedRelease,
}),
);
}
window.sessionStorage.removeItem(key);
},
2019-02-15 15:39:39 +05:30
created() {
2022-06-21 17:19:12 +05:30
this.updateQueryParamsFromUrl();
2021-01-03 14:25:43 +05:30
2022-06-21 17:19:12 +05:30
window.addEventListener('popstate', this.updateQueryParamsFromUrl);
},
destroyed() {
window.removeEventListener('popstate', this.updateQueryParamsFromUrl);
2019-02-15 15:39:39 +05:30
},
methods: {
2022-06-21 17:19:12 +05:30
getReleaseKey(release, index) {
return [release.tagName, release.name, index].join('|');
},
updateQueryParamsFromUrl() {
this.cursors.before = getParameterByName('before');
this.cursors.after = getParameterByName('after');
},
onPaginationButtonPress() {
this.updateQueryParamsFromUrl();
// In some cases, Apollo Client is able to pull its results from the cache instead of making
// a new network request. In these cases, the page's content gets swapped out immediately without
// changing the page's scroll, leaving the user looking at the bottom of the new page.
// To make the experience consistent, regardless of how the data is sourced, we manually
// scroll to the top of the page every time a pagination button is pressed.
scrollUp();
},
onSortChanged(newSort) {
if (this.sort === newSort) {
return;
}
// Remove the "before" and "after" query parameters from the URL,
// effectively placing the user back on page 1 of the results.
// This prevents the frontend from requesting the results sorted
// by one field (e.g. `released_at`) while using a pagination cursor
// intended for a different field (e.g.) `created_at`).
// For more details, see the MR that introduced this change:
// https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63434
historyPushState(
setUrlParams({
before: null,
after: null,
}),
);
this.updateQueryParamsFromUrl();
this.sort = newSort;
2020-01-01 13:55:28 +05:30
},
2019-02-15 15:39:39 +05:30
},
2022-06-21 17:19:12 +05:30
i18n: {
newRelease: __('New release'),
errorMessage: __('An error occurred while fetching the releases. Please try again.'),
},
2019-02-15 15:39:39 +05:30
};
</script>
<template>
2022-07-23 23:45:48 +05:30
<div class="gl-display-flex gl-flex-direction-column gl-mt-3">
2021-01-29 00:20:46 +05:30
<div class="gl-align-self-end gl-mb-3">
2022-06-21 17:19:12 +05:30
<releases-sort :value="sort" class="gl-mr-2" @input="onSortChanged" />
2021-01-29 00:20:46 +05:30
<gl-button
v-if="newReleasePath"
:href="newReleasePath"
:aria-describedby="shouldRenderEmptyState && 'releases-description'"
category="primary"
2022-05-07 20:08:51 +05:30
variant="confirm"
2022-06-21 17:19:12 +05:30
>{{ $options.i18n.newRelease }}</gl-button
2021-01-29 00:20:46 +05:30
>
</div>
2020-04-08 14:13:33 +05:30
2022-06-21 17:19:12 +05:30
<releases-empty-state v-if="shouldRenderEmptyState" />
<release-block
v-for="(release, index) in releases"
:key="getReleaseKey(release, index)"
:release="release"
2023-03-04 22:38:38 +05:30
:sort="sort"
2022-06-21 17:19:12 +05:30
:class="{ 'linked-card': releases.length > 1 && index !== releases.length - 1 }"
/>
<release-skeleton-loader v-if="shouldRenderLoadingIndicator" />
2020-01-01 13:55:28 +05:30
2022-06-21 17:19:12 +05:30
<releases-pagination
v-if="shouldRenderPagination"
:page-info="pageInfo"
@prev="onPaginationButtonPress"
@next="onPaginationButtonPress"
/>
2019-02-15 15:39:39 +05:30
</div>
</template>
<style>
.linked-card::after {
width: 1px;
content: ' ';
border: 1px solid #e5e5e5;
height: 17px;
top: 100%;
position: absolute;
left: 32px;
}
</style>