Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/app/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { usersRatingHandlers } from '@/entities/user';

import { createCollectionHandlers } from '@/features/collections/createCollection';
import { collectionDeleteHandlers } from '@/features/collections/deleteCollection';
import { collectionEditHandlers } from '@/features/collections/editCollection';
import { createCompanyHandlers } from '@/features/company/createCompany';
import { createFeatureFlagHandlers } from '@/features/featureFlag/createFeatureFlag';
import { featureFlagDeleteHandlers } from '@/features/featureFlag/deleteFeatureFlag';
Expand Down Expand Up @@ -52,6 +53,7 @@ export const handlers = [
...interviewHandlers,
...questionHandlers,
...collectionHandlers,
...collectionEditHandlers,
...collectionDeleteHandlers,
...specializationHandlers,
...featureFlagHandlers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { http, HttpResponse } from 'msw';

import { collectionsMock } from '@/entities/collection';
import { companiesMock } from '@/entities/company';
import { specializationsMock } from '@/entities/specialization';

import { editCollectionApiUrls } from '../../model/constants/editCollectionConstants';
import {
EditCollectionBodyRequest,
EditCollectionResponse,
} from '../../model/types/collectionEditTypes';

export const editCollectionMock = http.patch<
{ collectionId: string },
EditCollectionBodyRequest,
EditCollectionResponse
>(process.env.API_URL + editCollectionApiUrls.editCollection, async ({ params, request }) => {
const collectionId = Number(params.collectionId);

const index = collectionsMock.data.findIndex((collection) => collection.id === collectionId);

if (index === -1) {
return HttpResponse.json(null, { status: 404 });
}

const body = await request.json();

const currentCollection = collectionsMock.data[index];
const companyIndex = companiesMock.data.findIndex((c) => String(c.id) === body.companyId);

const updatedCollection: EditCollectionResponse = {
...currentCollection,
title: body.title ?? currentCollection.title,
description: body.description ?? currentCollection.description,
imageSrc: body.collectionImage ?? currentCollection.imageSrc ?? null,
tariff: (body.isFree ?? currentCollection.isFree) ? 'free' : 'premium',
questionsCount: body.questions?.length ?? currentCollection.questions?.length ?? 0,
tasksCount: body.taskIds?.length ?? currentCollection.tasks?.length ?? 0,
specializations: body.specializations
? body.specializations.map((id) => specializationsMock.find((spec) => spec.id === id)!)
: currentCollection.specializations,
keywords: body.keywords ?? currentCollection.keywords ?? [],
company: companiesMock.data[companyIndex],
createdBy: currentCollection.createdBy,
};

collectionsMock.data[index] = updatedCollection;

return HttpResponse.json(updatedCollection);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { editCollectionMock } from './editCollectionMock';

export const collectionEditHandlers = [editCollectionMock];
1 change: 1 addition & 0 deletions src/features/collections/editCollection/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { CollectionEditForm } from './ui/CollectionEditForm/CollectionEditForm';
export { collectionEditHandlers } from './api/__mocks__/index';
Loading