Skip to content

Commit

Permalink
alias to data-base
Browse files Browse the repository at this point in the history
  • Loading branch information
jandsonrj committed Aug 3, 2023
1 parent ceac0ef commit 9b81960
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 66 deletions.
4 changes: 2 additions & 2 deletions backend/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class ProductContentAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
return False

# def has_change_permission(self, request, obj=None):
# return False
def has_change_permission(self, request, obj=None):
return False

def has_delete_permission(self, request, obj=None):
return False
Expand Down
17 changes: 16 additions & 1 deletion backend/core/views/product_content.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from core.models import ProductContent
from core.serializers import ProductContentSerializer
from rest_framework import viewsets

from rest_framework.response import Response
from rest_framework.decorators import action

class ProductContentViewSet(viewsets.ModelViewSet):
queryset = ProductContent.objects.all()
Expand All @@ -12,3 +13,17 @@ class ProductContentViewSet(viewsets.ModelViewSet):
"order",
]
ordering = ["id"]

@action(detail=True, methods=['patch'])
def update_alias(self, request, pk=None):
product_content = self.get_object()
alias = request.data.get('alias')

if alias is not None:
product_content.alias = alias
product_content.save()

serializer = self.get_serializer(product_content)
return Response(serializer.data)
else:
return Response({'error': 'Alias value is required.'}, status=400)
4 changes: 1 addition & 3 deletions backend/pzserver/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
)
from django.contrib import admin
from django.urls import include, path
from pzserver.views import update_aliases
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularRedocView,
Expand All @@ -44,6 +43,7 @@
route.register(r"products", ProductViewSet, basename="products")
route.register(r"product-contents", ProductContentViewSet, basename="product_contents")
route.register(r"product-files", ProductFileViewSet, basename="product_files")
route.register(r"product-contents", ProductContentViewSet, basename="product_contents")


from rest_framework.authtoken import views
Expand All @@ -59,8 +59,6 @@
path("api/logged_user/", LoggedUserView.as_view(), name="logged_user"),
path("api/logout/", Logout.as_view(), name="logout"),
path("api/shib/", include("core.shibboleth_urls", namespace="shibboleth")),
# API ALIAS
path('api/update-aliases/', update_aliases, name='update_aliases'),
# API DOCs
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
path(
Expand Down
29 changes: 0 additions & 29 deletions backend/pzserver/views.py

This file was deleted.

69 changes: 38 additions & 31 deletions frontend/components/newProduct/Step3.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ export default function NewProductStep3({ productId, onNext, onPrev }) {

const loadContents = React.useCallback(async () => {
setLoading(true)
getProductContents(productId)
.then(res => {
setProductColumns(res.results)
setLoading(false)
})
.catch(res => {
setLoading(false)
if (res.response.status === 500) {
catchFormError(res.response.data)
try {
const response = await getProductContents(productId)
setProductColumns(response.results)

const aliases = {}
response.results.forEach(row => {
if (row.alias) {
aliases[row.column_name] = row.alias
}
})
setEditableFields(aliases)
setLoading(false)
} catch (error) {
setLoading(false)
if (error.response && error.response.status === 500) {
catchFormError(error.response.data)
}
}
}, [productId])

useEffect(() => {
Expand Down Expand Up @@ -114,29 +121,18 @@ export default function NewProductStep3({ productId, onNext, onPrev }) {
event.preventDefault()
}

const handleEditField = (pc, value) => {
const handleAlias = (pc, value) => {
setEditableFields(prevState => ({
...prevState,
[pc.column_name]: value
}))
updateAliasOnServer(pc.id, value)
}

const updateAliasOnServer = async (contentId, aliasValue) => {
try {
await axios.post('/api/update-aliases/', {
productId: productId,
updates: [
{
id: contentId,
alias: aliasValue
}
]
});
} catch (error) {
console.error('Error updating alias:', error);
}
};
axios.patch(`/api/product-contents/${pc.id}/`, { alias: value })
.then(response => {
})
.catch(error => {
})
}

const handleCancelEdit = pc => {
const updatedEditableFields = { ...editableFields }
Expand All @@ -157,7 +153,7 @@ export default function NewProductStep3({ productId, onNext, onPrev }) {
}
})

const isOptionSelected = pc.ucd !== null;
const isOptionSelected = pc.ucd !== null

return (
<Box sx={{ display: "flex", alignItems: "center" }}>
Expand All @@ -181,7 +177,18 @@ export default function NewProductStep3({ productId, onNext, onPrev }) {
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<TextField
value={editableFields[pc.column_name]}
onChange={e => handleEditField(pc, e.target.value)}
onChange={e => handleAlias(pc, e.target.value)}
onBlur={() => {
// console.log(`${pc.column_name}: ${editableFields[pc.column_name]}`)
handleAlias(pc, editableFields[pc.column_name])
}}
onKeyPress={event => {
if (event.key === 'Enter' || event.key === 'Tab') {
handleAlias(pc, editableFields[pc.column_name])
// console.log(`${pc.column_name}: ${editableFields[pc.column_name]}`)
editFieldRefs.current[pc.column_name].blur()
}
}}
InputProps={{
endAdornment: (
<InputAdornment position="end">
Expand All @@ -192,7 +199,7 @@ export default function NewProductStep3({ productId, onNext, onPrev }) {
)
}}
inputRef={ref => {
editFieldRefs.current[pc.column_name] = ref;
editFieldRefs.current[pc.column_name] = ref
}}
autoFocus
/>
Expand All @@ -214,7 +221,7 @@ export default function NewProductStep3({ productId, onNext, onPrev }) {
))}
</TextField>
<IconButton
onClick={() => handleEditField(pc, '')}
onClick={() => handleAlias(pc, '')}
onMouseDown={handleMouseDownPassword}
>
<EditIcon />
Expand Down

0 comments on commit 9b81960

Please sign in to comment.