-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added state management for pricing api routes
- Loading branch information
Showing
17 changed files
with
572 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/common/src/state/actions/updatedOneInspectionAdditionalData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.