Skip to content

Commit

Permalink
Added state management for pricing api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dlymonkai committed Oct 15, 2024
1 parent 56cd03c commit ee2ecce
Show file tree
Hide file tree
Showing 17 changed files with 572 additions and 7 deletions.
63 changes: 63 additions & 0 deletions packages/common/src/state/actions/createdOnePricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { PricingV2Details } from '@monkvision/types';
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkCreatedOnePricingPayload.
*/
export interface MonkCreatedOnePricingPayload {
/**
* The pricing created.
*/
pricing: PricingV2Details;
}

/**
* Action dispatched when a vehicle have been updated.
*/
export interface MonkCreatedOnePricingAction extends MonkAction {
/**
* The type of the action : `MonkActionType.CREATED_ONE_PRICING`.
*/
type: MonkActionType.CREATED_ONE_PRICING;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkCreatedOnePricingPayload;
}

/**
* Matcher function that matches a CreatedOnePricing while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isCreatedOnePricingAction(
action: MonkAction,
): action is MonkCreatedOnePricingAction {
return action.type === MonkActionType.CREATED_ONE_PRICING;
}

/**
* Reducer function for a createdOnePricing action.
*/
export function createdOnePricing(
state: MonkState,
action: MonkCreatedOnePricingAction,
): MonkState {
const { pricings, inspections } = state;
const { payload } = action;

const inspection = inspections.find((value) => value.id === payload.pricing.inspectionId);

if (pricings) {
pricings.push(action.payload.pricing);
}

if (inspection) {
inspection.pricings?.push(action.payload.pricing.id);
}
return {
...state,
pricings,
inspections: [...inspections],
};
}
71 changes: 71 additions & 0 deletions packages/common/src/state/actions/deletedOnePricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkDeletedOnePricingPayload.
*/
export interface MonkDeletedtedOnePricingPayload {
/**
* The ID of the inspection to which the pricing was deleted.
*/
inspectionId: string;
/**
* The pricing ID deleted.
*/
pricingId: string;
}

/**
* Action dispatched when a pricing have been deleted.
*/
export interface MonkDeletedOnePricingAction extends MonkAction {
/**
* The type of the action : `MonkActionType.DELETED_ONE_PRICING`.
*/
type: MonkActionType.DELETED_ONE_PRICING;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkDeletedtedOnePricingPayload;
}

/**
* Matcher function that matches a DeletedOnePricing while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isDeletedOnePricingAction(
action: MonkAction,
): action is MonkDeletedOnePricingAction {
return action.type === MonkActionType.DELETED_ONE_PRICING;
}

/**
* Reducer function for a deletedOnePricing action.
*/
export function deletedOnePricing(
state: MonkState,
action: MonkDeletedOnePricingAction,
): MonkState {
const { pricings, inspections } = state;
const { payload } = action;

const newPricings = pricings
? pricings.filter((pricing) => pricing.id !== payload.pricingId)
: [];
const inspection = inspections.find((value) => value.id === payload.inspectionId);

const newInspections = inspections.map((ins) =>
ins.id === payload.inspectionId && inspection?.pricings
? {
...ins,
pricings: ins.pricings?.filter((pricingId) => pricingId !== payload.pricingId),
}
: ins,
);

return {
...state,
pricings: newPricings,
inspections: newInspections,
};
}
4 changes: 4 additions & 0 deletions packages/common/src/state/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export * from './gotOneInspection';
export * from './createdOneImage';
export * from './updatedManyTasks';
export * from './updatedVehicle';
export * from './createdOnePricing';
export * from './deletedOnePricing';
export * from './updatedOnePricing';
export * from './updatedOneInspectionAdditionalData';
24 changes: 20 additions & 4 deletions packages/common/src/state/actions/monkAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export enum MonkActionType {
* An inspection has been fetched from the API.
*/
GOT_ONE_INSPECTION = 'got_one_inspection',
/**
* An inspection additional data has been updated.
*/
UPDATED_ONE_INSPECTION_ADDITIONAL_DATA = 'updated_one_inspection_additional_data',
/**
* An image has been uploaded to the API.
*/
Expand All @@ -14,14 +18,26 @@ export enum MonkActionType {
* One or more tasks have been updated.
*/
UPDATED_MANY_TASKS = 'updated_many_tasks',
/**
* Clear and reset the state.
*/
RESET_STATE = 'reset_state',
/**
* A vehicle has been updated.
*/
UPDATED_VEHICLE = 'updated_vehicle',
/**
* A pricing has been uploaded to the API.
*/
CREATED_ONE_PRICING = 'created_one_pricing',
/**
* A pricing has been updated.
*/
UPDATED_ONE_PRICING = 'updated_one_pricing',
/**
* A pricing has been deleted.
*/
DELETED_ONE_PRICING = 'deleted_one_pricing',
/**
* Clear and reset the state.
*/
RESET_STATE = 'reset_state',
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { AdditionalData } from '@monkvision/types';
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkUpdatedOneInspectionAdditionalDataPayload.
*/
export interface MonkUpdatedOneInspectionAdditionalDataPayload {
/**
* The ID of the inspection to which the pricing was updated.
*/
inspectionId: string;
/**
* Additional data used for the update operation.
*/
additionalData?: AdditionalData;
}

/**
* Action dispatched when a inspection have been updated.
*/
export interface MonkUpdatedOneInspectionAdditionalDataAction extends MonkAction {
/**
* The type of the action : `MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA`.
*/
type: MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkUpdatedOneInspectionAdditionalDataPayload;
}

/**
* Matcher function that matches a UpdatedOneInspection while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isUpdatedOneInspectionAdditionalDataAction(
action: MonkAction,
): action is MonkUpdatedOneInspectionAdditionalDataAction {
return action.type === MonkActionType.UPDATED_ONE_INSPECTION_ADDITIONAL_DATA;
}

/**
* Reducer function for a UpdatedOneInspection action.
*/
export function updatedOneInspectionAdditionalData(
state: MonkState,
action: MonkUpdatedOneInspectionAdditionalDataAction,
): MonkState {
const { inspections } = state;
const { payload } = action;

const inspection = inspections.find((value) => value.id === payload.inspectionId);
if (inspection) {
inspection.additionalData = payload.additionalData;
}
return {
...state,
inspections,
};
}
59 changes: 59 additions & 0 deletions packages/common/src/state/actions/updatedOnePricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PricingV2Details } from '@monkvision/types';
import { MonkAction, MonkActionType } from './monkAction';
import { MonkState } from '../state';

/**
* The payload of a MonkUpdatedOnePricingPayload.
*/
export interface MonkUpdatedOnePricingPayload {
/**
* The pricing created.
*/
pricing: PricingV2Details;
}

/**
* Action dispatched when a pricing have been updated.
*/
export interface MonkUpdatedOnePricingAction extends MonkAction {
/**
* The type of the action : `MonkActionType.UPDATED_ONE_PRICING`.
*/
type: MonkActionType.UPDATED_ONE_PRICING;
/**
* The payload of the action containing the fetched entities.
*/
payload: MonkUpdatedOnePricingPayload;
}

/**
* Matcher function that matches a updatedOnePricing while also inferring its type using TypeScript's type predicate
* feature.
*/
export function isUpdatedOnePricingAction(
action: MonkAction,
): action is MonkUpdatedOnePricingAction {
return action.type === MonkActionType.UPDATED_ONE_PRICING;
}

/**
* Reducer function for a updatedOnePricing action.
*/
export function updatedOnePricing(
state: MonkState,
action: MonkUpdatedOnePricingAction,
): MonkState {
const { pricings } = state;
const { payload } = action;

let updatedPricings: PricingV2Details[] = [];
if (pricings) {
updatedPricings = pricings.map((pricing) =>
pricing.id === payload.pricing.id ? { ...pricing, ...payload.pricing } : pricing,
);
}
return {
...state,
pricings: updatedPricings,
};
}
25 changes: 25 additions & 0 deletions packages/common/src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import {
isGotOneInspectionAction,
isResetStateAction,
isUpdatedManyTasksAction,
isCreatedOnePricingAction,
isDeletedOnePricingAction,
isUpdatedOnePricingAction,
isUpdatedOneInspectionAdditionalDataAction,
isUpdatedVehicleAction,
MonkAction,
resetState,
updatedManyTasks,
createdOnePricing,
deletedOnePricing,
updatedOnePricing,
updatedOneInspectionAdditionalData,
updatedVehicle,
} from './actions';
import { MonkState } from './state';

Expand All @@ -21,11 +31,26 @@ export function monkReducer(state: MonkState, action: MonkAction): MonkState {
if (isGotOneInspectionAction(action)) {
return gotOneInspection(state, action);
}
if (isUpdatedOneInspectionAdditionalDataAction(action)) {
return updatedOneInspectionAdditionalData(state, action);
}
if (isCreatedOneImageAction(action)) {
return createdOneImage(state, action);
}
if (isUpdatedManyTasksAction(action)) {
return updatedManyTasks(state, action);
}
if (isCreatedOnePricingAction(action)) {
return createdOnePricing(state, action);
}
if (isDeletedOnePricingAction(action)) {
return deletedOnePricing(state, action);
}
if (isUpdatedOnePricingAction(action)) {
return updatedOnePricing(state, action);
}
if (isUpdatedVehicleAction(action)) {
return updatedVehicle(state, action);
}
return state;
}
6 changes: 6 additions & 0 deletions packages/common/src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Inspection,
Part,
PartOperation,
PricingV2Details,
RenderedOutput,
SeverityResult,
Task,
Expand Down Expand Up @@ -55,6 +56,10 @@ export interface MonkState {
* The views created during inspections.
*/
views: View[];
/**
* The pricings created during inspections.
*/
pricings: PricingV2Details[];
}

/**
Expand All @@ -72,5 +77,6 @@ export function createEmptyMonkState(): MonkState {
tasks: [],
vehicles: [],
views: [],
pricings: [],
};
}
Loading

0 comments on commit ee2ecce

Please sign in to comment.