-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1af315
commit e62a433
Showing
23 changed files
with
469 additions
and
79 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
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
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
23
react-admin/src/pages/collection/hooks/useGetCollection.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,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") | ||
} | ||
} | ||
}) | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
react-admin/src/pages/collection/hooks/usePutCollectionIdentifier.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,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
20
react-admin/src/pages/collection/hooks/useStoreCollection.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,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
21
react-admin/src/pages/collection/hooks/useUpdateCollection.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,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
15
react-admin/src/pages/collection/schemas/CollectionCreateSchema.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,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
12
react-admin/src/pages/collection/schemas/CollectionEditSchema.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,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
12
react-admin/src/pages/collection/schemas/CollectionPutSchema.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,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")}), | ||
}) | ||
}); | ||
}) |
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,4 @@ | ||
export type CreatableCollectionType = { | ||
name: string; | ||
identifier: string; | ||
} |
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,4 @@ | ||
export default interface EditableCollectionType { | ||
id: string; | ||
name: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
react-admin/src/types/collection/PutCollectionIdentifierType.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,3 @@ | ||
export type PutCollectionIdentifierType = { | ||
identifier: String; | ||
} |
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
56 changes: 56 additions & 0 deletions
56
src/api/handlers/collection/put_collection_identifier_api_handler.rs
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,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)) | ||
} |
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 |
---|---|---|
@@ -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; |
Oops, something went wrong.