Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unit modal #1201

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
91 changes: 46 additions & 45 deletions src/renderer/api/cadt/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import initialState from '@/store/slices/app/app.initialstate'; // Ensure this path is correct

const projectsTag = 'projects';
const organizationsTag = 'organizations';
const unitsTag = 'projects';
const auditTag = 'audit';

const baseQuery = fetchBaseQuery({
baseUrl: '/',
});

const baseQueryWithDynamicHost = async (args, api, extraOptions) => {
let modifiedArgs = args;
const state = api.getState();
const currentHost = state.app.apiHost;

// Check if currentHost is equal to the initialState's apiHost
const effectiveHost = (currentHost === initialState.apiHost && import.meta.env.VITE_API_HOST) ? import.meta.env.VITE_API_HOST : currentHost;

if (!args.url.startsWith('/')) {
return await baseQuery(args, api, extraOptions);
}

// Modify the URL based on the effectiveHost
if (typeof args === 'string') {
modifiedArgs = `${effectiveHost}${args}`;
} else if (args && typeof args === 'object') {
modifiedArgs = {
...args,
url: `${effectiveHost}${args.url}`,
};
}

return await baseQuery(modifiedArgs, api, extraOptions);
};

export const cadtApi = createApi({
baseQuery: baseQueryWithDynamicHost,
reducerPath: 'cadtApi',
tagTypes: [projectsTag, organizationsTag, unitsTag, auditTag],
endpoints: () => ({}),
});

export { projectsTag, organizationsTag, unitsTag, auditTag };
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import initialState from '@/store/slices/app/app.initialstate'; // Ensure this path is correct

const projectsTag = 'projects';
const organizationsTag = 'organizations';
const unitsTag = 'projects';
const auditTag = 'audit';
const issuancesTag = 'issuances';

const baseQuery = fetchBaseQuery({
baseUrl: '/',
});

const baseQueryWithDynamicHost = async (args, api, extraOptions) => {
let modifiedArgs = args;
const state = api.getState();
const currentHost = state.app.apiHost;

// Check if currentHost is equal to the initialState's apiHost
const effectiveHost = (currentHost === initialState.apiHost && import.meta.env.VITE_API_HOST) ? import.meta.env.VITE_API_HOST : currentHost;

if (!args.url.startsWith('/')) {
return await baseQuery(args, api, extraOptions);
}

// Modify the URL based on the effectiveHost
if (typeof args === 'string') {
modifiedArgs = `${effectiveHost}${args}`;
} else if (args && typeof args === 'object') {
modifiedArgs = {
...args,
url: `${effectiveHost}${args.url}`,
};
}

return await baseQuery(modifiedArgs, api, extraOptions);
};

export const cadtApi = createApi({
baseQuery: baseQueryWithDynamicHost,
reducerPath: 'cadtApi',
tagTypes: [projectsTag, organizationsTag, unitsTag, auditTag, issuancesTag],
endpoints: () => ({}),
});

export { projectsTag, organizationsTag, unitsTag, auditTag, issuancesTag };
1 change: 1 addition & 0 deletions src/renderer/api/cadt/v1/issuances/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './issuances.api';
37 changes: 37 additions & 0 deletions src/renderer/api/cadt/v1/issuances/issuances.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { cadtApi, issuancesTag } from "..";
import { Issuance } from "@/schemas/Issuance.schema";

interface GetIssuancesParams {
issuanceIds: string[];
}

const issuanceApi = cadtApi.injectEndpoints({
endpoints: (builder) => ({
getIssuances: builder.query<Issuance, GetIssuancesParams>({
query: ({ issuanceIds }: GetIssuancesParams) => ({
url: `/v1/issuances`,
params: { issuanceIds },
method: 'GET',
}),
// Adding error handling in providesTags
providesTags: (result, error) => {
// Check if there was an error or if the result is undefined or null
if (error || !result) {
// Handle errors or no result by returning a minimal tag
// This prevents caching potentially faulty data
return [{ type: issuancesTag, id: 'ERROR' }];
}

// Provide a tag for each issuance returned by the query
return [
...result.map(issuance => ({ type: issuancesTag, id: issuance.id })),
{ type: issuancesTag, id: 'PARTIAL-LIST' } // Additional tag for managing partial lists
];
}
}),
})
});

export const {
useGetIssuancesQuery
} = issuanceApi;
1 change: 1 addition & 0 deletions src/renderer/api/cadt/v1/projects/projects.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const projectsApi = cadtApi.injectEndpoints({
params: { warehouseProjectId },
method: 'GET',
}),
providesTags: (_response, _error, {warehouseProjectId}) => [{type: projectsTag, id: warehouseProjectId}],
}),
})
});
Expand Down
109 changes: 61 additions & 48 deletions src/renderer/api/cadt/v1/units/units.api.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,62 @@
import {cadtApi, unitsTag} from "../";
import {Unit} from "@/schemas/Unit.schema";

interface GetUnitsParams {
page: number;
orgUid?: string;
search?: string;
order?: string;
}

interface GetUnitsResponse {
page: number,
pageCount: number,
data: Unit[]
}

const unitsApi = cadtApi.injectEndpoints({
endpoints: (builder) => ({
getUnits: builder.query<GetUnitsResponse, GetUnitsParams>({
query: ({ page, orgUid, search, order }: GetUnitsParams) => {
// Initialize the params object with page and limit
const params: GetUnitsParams & {limit: number} = { page, limit: 10 };

if (orgUid) {
params.orgUid = orgUid;
}

if (search) {
params.search = search.replace(/[^a-zA-Z0-9 _.-]+/, '');
}

if (order) {
params.order = order;
}

return {
url: `/v1/units`,
params,
method: 'GET',
};
},
providesTags: (_response, _error, {orgUid}) => [{type: unitsTag, id: orgUid}],
})
})
});

export const {
useGetUnitsQuery
import {cadtApi, unitsTag} from "../";
import {Unit} from "@/schemas/Unit.schema";

interface GetUnitsParams {
page: number;
orgUid?: string;
search?: string;
order?: string;
}

interface GetUnitParams {
warehouseUnitId: string;
}

interface GetUnitsResponse {
page: number,
pageCount: number,
data: Unit[]
}

const unitsApi = cadtApi.injectEndpoints({
endpoints: (builder) => ({
getUnits: builder.query<GetUnitsResponse, GetUnitsParams>({
query: ({ page, orgUid, search, order }: GetUnitsParams) => {
// Initialize the params object with page and limit
const params: GetUnitsParams & {limit: number} = { page, limit: 10 };

if (orgUid) {
params.orgUid = orgUid;
}

if (search) {
params.search = search.replace(/[^a-zA-Z0-9 _.-]+/, '');
}

if (order) {
params.order = order;
}

return {
url: `/v1/units`,
params,
method: 'GET',
};
},
providesTags: (_response, _error, {orgUid}) => [{type: unitsTag, id: orgUid}],
}),
getUnit: builder.query<Unit, GetUnitParams>({
query: ({ warehouseUnitId }: GetUnitParams) => ({
url: `/v1/units`,
params: { warehouseUnitId },
method: 'GET',
}),
providesTags: (_response, _error, {warehouseUnitId}) => [{type: unitsTag, id: warehouseUnitId}],
}),
})
});

export const {
useGetUnitsQuery,
useGetUnitQuery
} = unitsApi;
9 changes: 5 additions & 4 deletions src/renderer/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './cadt/v1/organizations';
export * from './cadt/v1/units';
export * from './cadt/v1/projects';
export * from './cadt/v1/audit';
export * from './cadt/v1/organizations';
export * from './cadt/v1/units';
export * from './cadt/v1/projects';
export * from './cadt/v1/audit';
export * from './cadt/v1/issuances';
84 changes: 84 additions & 0 deletions src/renderer/components/blocks/forms/UnitForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import { Form, Formik } from 'formik';
import * as yup from 'yup';
import { Field } from '@/components';
import { Unit } from '@/schemas/Unit.schema';
import { Card } from '@/components';

const validationSchema = yup.object({
projectName: yup.string(),
});

interface UnitFormProps {
onSubmit: () => Promise<any>;
readonly?: boolean;
data: Unit;
}

const UnitForm: React.FC<UnitFormProps> = ({ readonly = false, data }) => {
return (
<Formik initialValues={{ apiHost: '', apiKey: '' }} validationSchema={validationSchema} onSubmit={() => {}}>
{() => (
<Form>
<div className="flex flex-col gap-4">
<Card>
<div className="grid grid-cols-1 md:grid-cols-2 gap-x-4">
{/* prettier-ignore */}
<Field name="projectLocationId" label="Project Location Id" type="text" readonly={readonly} initialValue={data.projectLocationId} />
{/* prettier-ignore */}
<Field name="unitOwner" label="Unit Owner" type="text" readonly={readonly} initialValue={data.unitOwner} />
{/* prettier-ignore */}
<Field name="unitBlockStart" label="Unit Block Start" type="text" readonly={readonly} initialValue={data.unitBlockStart} />
{/* prettier-ignore */}
<Field name="unitBlockEnd" label="Unit Block End" type="text" readonly={readonly} initialValue={data.unitBlockEnd} />
{/* prettier-ignore */}
<Field name="unitCount" label="Unit Count" type="text" readonly={readonly} initialValue={data.unitCount} />
{/* prettier-ignore */}
<Field name="serialNumberBlock" label="Serial Number Block" type="text" readonly={readonly} initialValue={data.serialNumberBlock} />
{/* prettier-ignore */}
<Field name="countryJurisdictionOfOwner" label="Country Jurisdiction Of Owner" type="text" readonly={readonly} initialValue={data.countryJurisdictionOfOwner} />
{/* prettier-ignore */}
<Field name="inCountryJurisdictionOfOwner" label="In-Country Jurisdiction Of Owner" type="text" readonly={readonly} initialValue={data.inCountryJurisdictionOfOwner} />
{/* prettier-ignore */}
<Field name="unitType" label="Unit Type" type="text" readonly={readonly} initialValue={data.unitType} />
{/* prettier-ignore */}
<Field name="unitStatusReason" label="Unit Status Reason" type="text" readonly={readonly} initialValue={data.unitStatusReason} />
{/* prettier-ignore */}
<Field name="vintageYear" label="Unit Status Reason" type="text" readonly={readonly} initialValue={data.vintageYear} />
</div>
<div>
<Field
name="unitRegistryLink"
label="Unit Registry Link"
type="link"
readonly={readonly}
initialValue={data.unitRegistryLink}
/>
</div>
</Card>
<Card>
<div className="grid grid-cols-1 md:grid-cols-2 gap-x-4">
{/* prettier-ignore */}
<Field name="marketplace" label="Marketplace" type="text" readonly={readonly} initialValue={data.marketplace} />
{/* prettier-ignore */}
<Field name="marketplaceIdentifier" label="Marketplace Identifier" type="text" readonly={readonly} initialValue={data.marketplaceIdentifier} />
{/* prettier-ignore */}
<Field name="marketplaceLink" label="Marketplace Link" type="text" readonly={readonly} initialValue={data.marketplaceLink} />
{/* prettier-ignore */}
<Field name="correspondingAdjustmentStatus" label="Corresponding Adjustment Status" type="text" readonly={readonly} initialValue={data.correspondingAdjustmentStatus} />
{/* prettier-ignore */}
<Field name="correspondingAdjustmentDeclaration" label="Corresponding Adjustment Declaration" type="text" readonly={readonly} initialValue={data.correspondingAdjustmentDeclaration} />
</div>
</Card>
<Card>
{/* prettier-ignore */}
<Field name="unitTags" label="Unit Tags" type="tag" readonly={readonly} initialValue={data.unitTags} />
</Card>
</div>
</Form>
)}
</Formik>
);
};

export { UnitForm };
3 changes: 2 additions & 1 deletion src/renderer/components/blocks/forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './EditOrganizationForm';
export * from './ProjectForm';
export * from './IssuanceForm';
export * from './ProjectLocationForm';
export * from './EstimationsForm';
export * from './EstimationsForm';
export * from './UnitForm';
Loading