Skip to content

Commit

Permalink
Change rest to graphql for adminlevels
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeyash07 committed Jun 19, 2024
1 parent 7c6950c commit 67ef62f
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback } from 'react';
import { useParams } from 'react-router-dom';
import { isDefined } from '@togglecorp/fujs';
import {
Expand Down Expand Up @@ -27,17 +27,15 @@ import {
UpdateAdminLevelMutationVariables,
CreateAdminLevelMutationVariables,
GalleryFileType,
AdminLevelType,
} from '#generated/types';
import NonFieldError from '#components/NonFieldError';
import GalleryFileUpload from '#components/GalleryFileUpload';
import {
AdminLevelGeoArea,
} from '#types';

import styles from './styles.css';

type AdminLevel = AdminLevelInputType & { clientId: string, id: string};
type PartialAdminLevel = PartialForm<AdminLevel, 'clientId' | 'geoShapeFileDetails'>;
type PartialAdminLevel = PartialForm<AdminLevel, 'clientId' | 'geoShapeFile'>;

const CREATE_ADMIN_LEVEL = gql`
mutation CreateAdminLevel(
Expand All @@ -49,6 +47,22 @@ const CREATE_ADMIN_LEVEL = gql`
ok
errors
result {
tolerance
title
staleGeoAreas
parentNameProp
parentCodeProp
codeProp
nameProp
geoShapeFile {
file {
url
}
id
title
mimeType
}
level
id
}
}
Expand All @@ -67,6 +81,22 @@ const UPDATE_ADMIN_LEVEL = gql`
ok
errors
result {
tolerance
title
staleGeoAreas
parentNameProp
parentCodeProp
codeProp
nameProp
geoShapeFile {
file {
url
}
id
title
mimeType
}
level
id
}
}
Expand All @@ -86,7 +116,7 @@ type FormSchemaFields = ReturnType<FormSchema['fields']>;
const schema: FormSchema = {
fields: (): FormSchemaFields => ({
clientId: [],
region: [requiredCondition],
region: [requiredStringCondition],
title: [requiredStringCondition],
level: [requiredCondition],
geoShapeFile: [],
Expand All @@ -98,19 +128,21 @@ const schema: FormSchema = {
}),
};

const adminLevelKeySelector = (d: AdminLevelGeoArea) => d.id;
const adminLevelLabelSelector = (d: AdminLevelGeoArea) => d.title;
const adminLevelKeySelector = (d: AdminLevelType) => d.id;
const adminLevelLabelSelector = (d: AdminLevelType) => d.title;

interface Props {
onSave: (adminLevel: AdminLevelGeoArea) => void;
onDelete: (id: number | undefined) => void;
regionId: string;
onSave: (adminLevel: AdminLevelType) => void;
onDelete: (id: string | undefined) => void;
value: PartialAdminLevel;
isPublished?: boolean;
adminLevelOptions?: AdminLevelGeoArea[];
adminLevelOptions?: AdminLevelType[];
}

function AddAdminLevelForm(props: Props) {
const {
regionId,
adminLevelOptions,
onSave,
value: valueFromProps,
Expand All @@ -132,7 +164,7 @@ function AddAdminLevelForm(props: Props) {
setPristine,
validate,
setError,
} = useForm(schema, valueFromProps);
} = useForm(schema, { ...valueFromProps, region: regionId });

const error = getErrorObject(riskyError);
const alert = useAlert();
Expand Down Expand Up @@ -258,7 +290,7 @@ function AddAdminLevelForm(props: Props) {
if (isDefined(valueFromProps.id)) {
updateAdminLevel({
variables: {
id: String(valueFromProps.id),
id: valueFromProps.id,
data: val,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import {
TabPanel,
} from '@the-deep/deep-ui';
import { PartialForm } from '@togglecorp/toggle-form';

import { AdminLevelGeoArea } from '#types';
import { AdminLevelInputType, AdminLevelType } from '#generated/types';

import AddAdminLevelForm from './AddAdminLevelForm';

type AdminLevel = AdminLevelGeoArea & { clientId: string };
type PartialAdminLevel = PartialForm<AdminLevel, 'clientId' | 'geoShapeFileDetails'>;
type AdminLevel = AdminLevelInputType & { clientId: string };
type PartialAdminLevel = PartialForm<AdminLevel, 'clientId' | 'geoShapeFile'>;

interface Props {
onSave: (adminLevel: AdminLevelGeoArea) => void;
onDelete: (id: number | undefined) => void;
onSave: (adminLevel: AdminLevelType) => void;
onDelete: (id: string | undefined) => void;
value: PartialAdminLevel;
isPublished?: boolean;
adminLevelOptions?: AdminLevelGeoArea[];
adminLevelOptions?: AdminLevelType[];
name: string;
regionId: string;
}

function AddAdminLevelPane(props: Props) {
Expand All @@ -28,13 +28,15 @@ function AddAdminLevelPane(props: Props) {
isPublished,
adminLevelOptions,
name,
regionId,
} = props;

return (
<TabPanel
name={name}
>
<AddAdminLevelForm
regionId={regionId}
value={value}
onSave={onSave}
onDelete={onDelete}
Expand Down
Loading

0 comments on commit 67ef62f

Please sign in to comment.