Skip to content

Commit

Permalink
Adding mapper to the API response
Browse files Browse the repository at this point in the history
  • Loading branch information
doracretu3pillar committed Oct 2, 2024
1 parent b7aa94a commit 4a970bf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ describe('PerconaUpdateVersion', () => {
});

it('should render modal with one update', async () => {
const changeLogsAPIResponse = {
last_check: '',
updates: [
{
version: 'PMM 3.0.1',
tag: 'string',
timestamp: '2024-09-24T09:12:31.488Z',
release_notes_url: 'http://localhost:3000',
release_notes_text: 'text1',
},
],
};
const state = {
updates: {
isLoading: false,
Expand All @@ -48,9 +60,7 @@ describe('PerconaUpdateVersion', () => {
},
},
};
jest
.spyOn(UpdatesService, 'getUpdatesChangelogs')
.mockReturnValue(Promise.resolve({ ...state.updates.changeLogs }));
jest.spyOn(UpdatesService, 'getUpdatesChangelogs').mockReturnValue(Promise.resolve({ changeLogsAPIResponse }));

const defaultState = configureStore().getState();
const store = configureStore({
Expand All @@ -70,6 +80,25 @@ describe('PerconaUpdateVersion', () => {
});

it('should render modal with multiple updates', async () => {
const changeLogsAPIResponse = {
last_check: '',
updates: [
{
version: 'PMM 3.0.1',
tag: 'string',
timestamp: '2024-09-24T09:12:31.488Z',
release_notes_url: 'http://localhost:3000',
release_notes_text: 'text1',
},
{
version: 'PMM 3.0.2',
tag: 'string',
timestamp: '2024-09-24T09:12:31.488Z',
release_notes_url: 'http://localhost:3000',
release_notes_text: 'text2',
},
],
};
const state = {
updates: {
isLoading: false,
Expand Down Expand Up @@ -97,9 +126,7 @@ describe('PerconaUpdateVersion', () => {
},
},
};
jest
.spyOn(UpdatesService, 'getUpdatesChangelogs')
.mockReturnValue(Promise.resolve({ ...state.updates.changeLogs }));
jest.spyOn(UpdatesService, 'getUpdatesChangelogs').mockReturnValue(Promise.resolve({ changeLogsAPIResponse }));

const defaultState = configureStore().getState();
const store = configureStore({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { FC, useEffect, useState } from 'react';

import { dateTimeFormat } from '@grafana/data';
import { Modal, useStyles2, Button } from '@grafana/ui';
import { checkUpdatesChangeLogs, UpdatesChangelogs } from 'app/percona/shared/core/reducers/updates';
import { checkUpdatesChangeLogs, UpdatesChangeLogs } from 'app/percona/shared/core/reducers/updates';
import { getPerconaUser, getUpdatesInfo } from 'app/percona/shared/core/selectors';
import { useAppDispatch } from 'app/store/store';
import { useSelector } from 'app/types';
Expand Down Expand Up @@ -89,7 +89,7 @@ const PerconaUpdateVersion: FC = () => {
<div data-testid="multiple-updates-modal">
<h5 className={styles.newVersionsTitle}>{Messages.newVersions}</h5>
<ul className={styles.listOfReleaseNotes}>
{changeLogs?.updates.map((update: UpdatesChangelogs) => (
{changeLogs?.updates.map((update: UpdatesChangeLogs) => (
<li key={update.toString()}>
<a target="_blank" rel="noopener noreferrer" href={update.releaseNotesUrl}>
{update.version}, {dateTimeFormat(update.timestamp, { format: 'MMM DD, YYYY' })}
Expand Down
4 changes: 2 additions & 2 deletions public/app/percona/shared/core/reducers/updates/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { UpdatesService } from 'app/percona/shared/services/updates';

import { CheckUpdatesPayload, UpdatesState } from './updates.types';
import { responseToPayload } from './updates.utils';
import { mapUpdatesChangeLogs, responseToPayload } from './updates.utils';

const initialState: UpdatesState = {
isLoading: false,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const checkUpdatesAction = createAsyncThunk('percona/checkUpdates', async
});

export const checkUpdatesChangeLogs = createAsyncThunk('percona/checkUpdatesChangelogs', async () => {
return await UpdatesService.getUpdatesChangelogs();
return mapUpdatesChangeLogs(await UpdatesService.getUpdatesChangelogs());
});

export default updatesSlice.reducer;
31 changes: 15 additions & 16 deletions public/app/percona/shared/core/reducers/updates/updates.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export interface UpdatesState {
latest?: LatestInformation;
latestNewsUrl?: string;
lastChecked?: string;
changeLogs?: CheckUpdatesChangeLogsResponse;
snoozeCurrentVersion?: SnoozePayloadResponse;
changeLogs?: CheckUpdatesChangeLogs;
}

export interface CheckUpdatesPayload {
Expand All @@ -29,28 +28,28 @@ export interface CheckUpdatesPayload {
updateAvailable: boolean;
}

export interface UpdatesChangelogs {
export interface UpdatesChangeLogsResponse {
version: string;
tag: string;
timestamp: string;
release_notes_url: string;
release_notes_text: string;
}

export interface UpdatesChangeLogs {
version: string;
tag: string;
timestamp: string;
releaseNotesUrl: string;
releaseNotesText: string;
}

export interface CheckUpdatesChangeLogsResponse {
updates: UpdatesChangelogs[];
export interface CheckUpdatesChangeLogs {
updates: UpdatesChangeLogs[];
lastCheck: string;
}

export interface SnoozePayloadBody {
productTourCompleted: boolean;
alertingTourCompleted: boolean;
snoozedPmmVersion: string;
}

export interface SnoozePayloadResponse {
userId: number;
productTourCompleted: boolean;
alertingTourCompleted: boolean;
snoozedPmmVersion: string;
export interface CheckUpdatesChangeLogsResponse {
updates: UpdatesChangeLogsResponse[];
last_check: string;
}
16 changes: 15 additions & 1 deletion public/app/percona/shared/core/reducers/updates/updates.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CheckUpdatesResponse } from 'app/percona/shared/services/updates/Updates.types';

import { CheckUpdatesPayload } from './updates.types';
import { CheckUpdatesPayload, CheckUpdatesChangeLogs, CheckUpdatesChangeLogsResponse } from './updates.types';

export const responseToPayload = (response: CheckUpdatesResponse): CheckUpdatesPayload => ({
installed: response.installed
Expand All @@ -21,3 +21,17 @@ export const responseToPayload = (response: CheckUpdatesResponse): CheckUpdatesP
latestNewsUrl: response.latest_news_url,
updateAvailable: !!response.update_available,
});

export const mapUpdatesChangeLogs = (response: CheckUpdatesChangeLogsResponse): CheckUpdatesChangeLogs => {
let responseMapping = response.updates.map((item) => ({
version: item.version,
tag: item.tag,
timestamp: item.timestamp,
releaseNotesUrl: item.release_notes_url,
releaseNotesText: item.release_notes_text,
}));
return {
lastCheck: response.last_check,
updates: responseMapping,
};
};

0 comments on commit 4a970bf

Please sign in to comment.