Skip to content

Commit

Permalink
collection update
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Dec 29, 2024
1 parent f1af315 commit e62a433
Show file tree
Hide file tree
Showing 23 changed files with 469 additions and 79 deletions.
2 changes: 2 additions & 0 deletions react-admin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {ModelCreatePage} from "./pages/models/ModelCreatePage";
import { ModelEditPage } from "./pages/models/ModelEditPage";
import {ComponentTablePage} from "./pages/component/ComponentTablePage";
import {CollectionTable} from "./pages/collection/CollectionTable";
import {CollectionEdit} from "./pages/collection/CollectionEdit";

function App() {
return (
Expand Down Expand Up @@ -56,6 +57,7 @@ function App() {
<Route path="/admin/model-create" element={<ModelCreatePage />} />
<Route path="/admin/model-edit/:model_id" element={<ModelEditPage />} />
<Route path="/admin/collection" element={<CollectionTable />} />
<Route path="/admin/collection-edit/:collection_id" element={<CollectionEdit />} />
<Route path="/admin/page" element={<PageTable />} />
<Route path="/admin/page-create" element={<PageCreate />} />
<Route path="/admin/page-edit/:page_id" element={<PageEdit />} />
Expand Down
1 change: 1 addition & 0 deletions react-admin/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"page_information": "Page Information",
"model": "Model",
"model_information": "Model Information",
"collection_information": "Collection Information",
"components": "Components",
"field_type": "Field type",
"upload_asset": "Upload asset",
Expand Down
135 changes: 135 additions & 0 deletions react-admin/src/pages/collection/CollectionEdit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import InputField from "../../components/InputField";
import {Link, useParams} from "react-router-dom";
import {useTranslation} from "react-i18next";
import {useState} from "react";
import {useForm} from "react-hook-form";
import {PutCollectionIdentifierType} from "../../types/collection/PutCollectionIdentifierType";
import {joiResolver} from "@hookform/resolvers/joi";
import EditableCollectionType from "../../types/collection/EditableCollectionType";
import {useGetCollection} from "./hooks/useGetCollection";
import {useCollectionPutSchema} from "./schemas/CollectionPutSchema";
import {useCollectionEditSchema} from "./schemas/CollectionEditSchema";
import {usePutCollectionIdentifier} from "./hooks/usePutCollectionIdentifier";
import {useUpdateCollection} from "./hooks/useUpdateCollection";

export const CollectionEdit = (() => {
const params = useParams();
const collection_id = params.collection_id ?? ''
const {mutate} = useUpdateCollection(collection_id);
const [t] = useTranslation("global")
const {data} = useGetCollection(collection_id)
const [isEditableIdentifier, setIsEditableIdentifier] = useState<boolean>(true)
const values = data?.data.data


const {
register: putCollectionRegister,
getValues: getCollectionIdentifierValue
} = useForm<PutCollectionIdentifierType>({
resolver: joiResolver(useCollectionPutSchema(), {allowUnknown: true}),
values: {
identifier: data?.data.data.identifier
}
});

const {
register,
handleSubmit,
formState: {errors},
} = useForm<EditableCollectionType>({
resolver: joiResolver(useCollectionEditSchema(), {allowUnknown: true}),
values
})

const {mutate: putCollectionIdentifierMutate} = usePutCollectionIdentifier(collection_id)


const editableIdentifierOnClick = (() => {
setIsEditableIdentifier(false)
})
const saveIdentifierOnClick = (() => {
putCollectionIdentifierMutate({identifier: getCollectionIdentifierValue('identifier')})
setIsEditableIdentifier(true)
console.log("teststes")
})

const cancelIdentifierOnClick = (() => {
setIsEditableIdentifier(true)
})

const submitHandler = ((data: EditableCollectionType) => {
mutate(data)
})
return (
<>
<div className="px-5">
<div className="w-full">
<div className="block rounded-lg p-6">
<h1 className="text-xl font-semibold mb-4 text-gray-900">
{t("collection_information")}
</h1>

<form onSubmit={handleSubmit(submitHandler)}>
<div className="mb-4">
<InputField
label={t("name")}
placeholder={t("name")}
name="name"
register={register("name")}
autoFocus={true}
/>
</div>
<div className="mb-4">
<InputField
label={t("identifier")}
placeholder={t("identifier")}
name="identifier"
register={putCollectionRegister("identifier")}
disabled={isEditableIdentifier}
/>
<div
className="mt-2"
>
{isEditableIdentifier ? (
<>
<span onClick={editableIdentifierOnClick}
className="text-xs text-blue-600 cursor-pointer">
{t("edit_identifier")}
</span>
</>
) : (
<>
<button type="button" onClick={saveIdentifierOnClick}
className="text-xs text-blue-600 cursor-pointer">
{t('save')}
</button>
<button type="button" onClick={cancelIdentifierOnClick}
className="ml-3 text-xs text-blue-600 cursor-pointer">
{t('cancel')}
</button>
</>
)}
</div>
</div>

<div className="flex items-center">
<button
type="submit"
className="bg-primary-600 py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500"
>
{t("save")}
</button>
<Link
to={`/admin/collection`}
className="ml-auto font-medium text-gray-600 hover:text-gray-500"
>
{t("cancel")}
</Link>
</div>
</form>
</div>
</div>
</div>
</>
)
})
23 changes: 23 additions & 0 deletions react-admin/src/pages/collection/hooks/useGetCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {useQuery} from '@tanstack/react-query'
import { useAxios } from '../../../hooks/useAxios'
import _ from 'lodash'
import {useNavigate} from 'react-router-dom'

export const useGetCollection = (collection_id: string) => {
const client = useAxios()
const redirect = useNavigate()

return useQuery({
queryKey: ['collection', collection_id],
queryFn: (async () => {
try {
return await client.get("/collection/" + collection_id)
} catch (error) {
if (_.get(error, 'response.status') === 401) {
localStorage.removeItem('AUTH_TOKEN')
redirect("/admin/login")
}
}
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useMutation} from '@tanstack/react-query'
import { useAxios } from '../../../hooks/useAxios'
import _ from 'lodash'
import {useNavigate} from 'react-router-dom'
import {PutCollectionIdentifierType} from "../../../types/collection/PutCollectionIdentifierType";

export const usePutCollectionIdentifier = (collection_id: string) => {
const client = useAxios();
const redirect = useNavigate();
return useMutation({
mutationFn: async (data: PutCollectionIdentifierType) => {
const url = '/put-collection-identifier/' + collection_id;
return await client.put(url , JSON.stringify(data));
},
onSuccess: (res) => {
if (_.get(res, 'data.status') === true) {
redirect("/admin/collection-edit/" + collection_id)
}
}
})
}
20 changes: 20 additions & 0 deletions react-admin/src/pages/collection/hooks/useStoreCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {useMutation} from '@tanstack/react-query'
import { useAxios } from '../../../hooks/useAxios'
import _ from 'lodash'
import {useNavigate} from 'react-router-dom'
import {CreatableModelType} from "../../../types/model/CreatableModelType";

export const useStoreCollection = () => {
const client = useAxios();
const redirect = useNavigate();
return useMutation({
mutationFn: async (data: CreatableModelType) => {
return await client.post('/model', JSON.stringify(data));
},
onSuccess: (res) => {
if (_.get(res, 'data.status') === true) {
redirect("/admin/model")
}
}
})
}
21 changes: 21 additions & 0 deletions react-admin/src/pages/collection/hooks/useUpdateCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useMutation} from '@tanstack/react-query'
import { useAxios } from '../../../hooks/useAxios'
import _ from 'lodash'
import {useNavigate} from 'react-router-dom'
import IEditableModel from "../../../types/model/IEditableModel";

export const useUpdateCollection = (role_id: string) => {
const client = useAxios();
const redirect = useNavigate();
return useMutation({
mutationFn: async (data: IEditableModel) => {
const url = '/collection/' + role_id;
return await client.put(url , JSON.stringify(data));
},
onSuccess: (res) => {
if (_.get(res, 'data.status') === true) {
redirect("/admin/collection")
}
}
})
}
15 changes: 15 additions & 0 deletions react-admin/src/pages/collection/schemas/CollectionCreateSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Joi from 'joi';
import {useTranslation} from "react-i18next";

export const useCollectionCreateSchema = (() => {

const [t] = useTranslation("global")
return Joi.object({
name : Joi.string().required().messages({
'string.empty': t("empty_message", {attribute: t("name")}),
}),
identifier : Joi.string().required().messages({
'string.empty': t("empty_message", {attribute: t("identifier")}),
})
});
})
12 changes: 12 additions & 0 deletions react-admin/src/pages/collection/schemas/CollectionEditSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Joi from 'joi';
import {useTranslation} from "react-i18next";

export const useCollectionEditSchema = (() => {

const [t] = useTranslation("global")
return Joi.object({
name : Joi.string().required().messages({
'string.empty': t("empty_message", {attribute: t("name")}),
})
});
})
12 changes: 12 additions & 0 deletions react-admin/src/pages/collection/schemas/CollectionPutSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Joi from 'joi';
import {useTranslation} from "react-i18next";

export const useCollectionPutSchema = (() => {

const [t] = useTranslation("global")
return Joi.object({
identifier : Joi.string().required().messages({
'string.empty': t("empty_message", {attribute: t("identifier")}),
})
});
})
4 changes: 4 additions & 0 deletions react-admin/src/types/collection/CreatableCollectionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type CreatableCollectionType = {
name: string;
identifier: string;
}
4 changes: 4 additions & 0 deletions react-admin/src/types/collection/EditableCollectionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface EditableCollectionType {
id: string;
name: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type PutCollectionIdentifierType = {
identifier: String;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::api::handlers::model::fetch_model_api_handler::FetchModelResponse;
use crate::avored_state::AvoRedState;
use crate::error::{Error, Result};
use crate::models::collection_model::CollectionModel;
Expand Down
1 change: 1 addition & 0 deletions src/api/handlers/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod fetch_collection_api_handler;
pub mod request;
pub mod store_collection_api_handler;
pub mod update_collection_api_handler;
pub mod put_collection_identifier_api_handler;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::sync::Arc;
use axum::{Extension, Json};
use axum::extract::{Path, State};
use crate::api::handlers::collection::request::put_collection_identifier_request::PutCollectionRequest;
use crate::avored_state::AvoRedState;
use crate::error::{Error, Result};
use crate::models::collection_model::{CollectionModel, PutCollectionIdentifierModel};
use crate::models::token_claim_model::LoggedInUser;
use crate::models::validation_error::ErrorResponse;
use crate::responses::ApiResponse;

pub async fn put_collection_identifier_api_handler(
Path(collection_id): Path<String>,
Extension(logged_in_user): Extension<LoggedInUser>,
state: State<Arc<AvoRedState>>,
Json(payload): Json<PutCollectionRequest>,
) -> Result<Json<ApiResponse<CollectionModel>>> {
println!("->> {:<12} - put_collection_identifier_api_handler", "HANDLER");

let has_permission_bool = state
.admin_user_service
.has_permission(logged_in_user.clone(), String::from("collection_edit"))
.await?;
if !has_permission_bool {
return Err(Error::Forbidden);
}

let error_messages = payload.validate(state.clone()).await?;

if !error_messages.is_empty() {
let error_response = ErrorResponse {
status: false,
errors: error_messages,
};

return Err(Error::BadRequest(error_response));
}

let put_collection_identifier = PutCollectionIdentifierModel {
id: collection_id,
identifier: payload.identifier,
logged_in_username: logged_in_user.email,
};
let updated_collection = state
.collection_service
.update_collection_identifier(&state.db, put_collection_identifier)
.await?;


let api_response = ApiResponse {
status: true,
data: updated_collection,
};

Ok(Json(api_response))
}
1 change: 1 addition & 0 deletions src/api/handlers/collection/request/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod collection_table_request;
pub mod store_collection_request;
pub mod update_collection_request;
pub mod put_collection_identifier_request;
Loading

0 comments on commit e62a433

Please sign in to comment.