Skip to content

Commit

Permalink
Merge pull request #27 from knovator/updates_fixes
Browse files Browse the repository at this point in the history
Updates & Fixes
  • Loading branch information
chavda-bhavik authored Aug 29, 2022
2 parents 727de31 + d292198 commit f2795dd
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 45 deletions.
34 changes: 9 additions & 25 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,16 @@ const App = () => {
baseUrl="http://localhost:8080"
token="ABCD" // token={getToken()}
>
{/* <h2>Master</h2>
<Master
explicitForm
permissions={{ add: false, list: true, update: true, destroy: true }}
limits={[1, 2, 3]}
>
<Master.Search />
<Master.AddButton />
<Master.Table />
<Master.Pagination />
</Master> */}
{/* <Master.FormWrapper>
{(data) => (
data.open ? <p onClick={data.onClose}>Drawer</p> : null
)}
</Master.FormWrapper> */}

<h2>Sub-Master</h2>
<Master>
<Master.Lister />
<SubMaster>
<SubMaster.Table />
<h2>Sub-Master</h2>
<Master>
<Master.Lister />
<SubMaster limits={[1, 2, 3, 4, 5]}>
<SubMaster.Search />
<SubMaster.AddButton />
<SubMaster.Table />
<SubMaster.Pagination />
</SubMaster>
</Master>
</SubMaster>
</Master>
</Provider>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion src/api/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const apiList = {
method: "PUT",
}),
DELETE: ({ module }: API_INPUT_TYPE) => ({
url: `admin/${module}/soft-delete`,
url: `admin/${module}/delete`,
method: "PUT",
}),
SEQUENCE: ({ module }: API_INPUT_TYPE) => ({
Expand Down
3 changes: 2 additions & 1 deletion src/components/Common/DNDTable/DNDTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DNDTable = ({
loading,
onMove,
dragEnable = false,
noDataText = "No data found",
}: TableProps) => {
const getSortConfigClassName = useCallback(
(accessor: string, up = true) => {
Expand Down Expand Up @@ -128,7 +129,7 @@ const DNDTable = ({
})
) : (
<tr>
<td colSpan={columns?.length || 0}>No data found</td>
<td colSpan={columns?.length || 0}>{noDataText}</td>
</tr>
)}
</tbody>
Expand Down
13 changes: 11 additions & 2 deletions src/components/Common/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import React, { useCallback } from "react"
import { useTable } from "react-table"
import { EXCLUDE_SORT_COLUMNS, SORT_ASCENDING, SORT_DESCENDING } from "../../../constants/common"

const Table = ({ data, columns, sortConfig, sortable = true, setSortConfig, loader, loading }: TableProps) => {
const Table = ({
data,
columns,
sortConfig,
sortable = true,
setSortConfig,
loader,
loading,
noDataText = "No data found",
}: TableProps) => {
const getSortConfigClassName = useCallback(
(accessor: string, up = true) => {
if (!sortConfig || accessor !== sortConfig[0]) return "kms_sort-inactive"
Expand Down Expand Up @@ -84,7 +93,7 @@ const Table = ({ data, columns, sortConfig, sortable = true, setSortConfig, load
})
) : (
<tr>
<td colSpan={columns?.length || 0}>No data found</td>
<td colSpan={columns?.length || 0}>{noDataText}</td>
</tr>
)}
</tbody>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Master/MasterTable/MasterTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const MasterTable = ({ columns, actions }: TableWrapperProps) => {
(item: any, key: string) => {
return function (value: string) {
if (onUpdate && canPartialUpdate) {
onUpdate(item.id, { [key]: value })
onUpdate(item._id, { [key]: value })
}
}
},
Expand Down Expand Up @@ -109,6 +109,7 @@ const MasterTable = ({ columns, actions }: TableWrapperProps) => {
setSortConfig={setSortConfig}
loader={loader}
loading={loading}
noDataText={t("master:noDataText")}
/>
)
}
Expand Down
8 changes: 7 additions & 1 deletion src/components/SubMaster/SubMasterSearch/SubMasterSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import React, { useRef, useState } from "react"
import React, { useEffect, useRef, useState } from "react"
import { Input } from "../../../components/Common"
import { useProviderState } from "../../../context/ProviderContext"
import { useSubMasterState } from "../../../context/SubMasterContext"

const SubMasterSearch = () => {
const { selectedMaster } = useProviderState()
const { getSubMastersList, t } = useSubMasterState()
const callerRef = useRef<NodeJS.Timeout | null>(null)
const [search, setSearch] = useState<string>("")

useEffect(() => {
setSearch("")
}, [selectedMaster])

const onChangeSearch = (str: string) => {
setSearch(str)
if (callerRef.current) clearTimeout(callerRef.current)
Expand Down
18 changes: 11 additions & 7 deletions src/components/SubMaster/SubMasterTable/SubMasterTable.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React, { useCallback, useEffect, useState } from "react"
import { useSubMasterState } from "../../../context/SubMasterContext"
import { DNDTable } from "../../../components/Common"
import DeleteIcon from "../../../icons/deleteIcon"
import UpdateIcon, { UpdateSVG } from "../../../icons/updateIcon"
import { CheckSVG } from "../../../icons/checkIcon"
import classNames from "classnames"
import { XSVG } from "../../../icons/xIcon"
import MoveIcon from "../../../icons/moveIcon"
import classNames from "classnames"
import DeleteIcon from "../../../icons/deleteIcon"
import { CheckSVG } from "../../../icons/checkIcon"
import { DNDTable } from "../../../components/Common"
import UpdateIcon, { UpdateSVG } from "../../../icons/updateIcon"

import { useProviderState } from "../../../context/ProviderContext"
import { useSubMasterState } from "../../../context/SubMasterContext"

const SubMasterTable = ({ columns, actions }: TableWrapperProps) => {
const { selectedMaster } = useProviderState()
const {
onUpdate,
sortable,
Expand All @@ -35,7 +38,7 @@ const SubMasterTable = ({ columns, actions }: TableWrapperProps) => {
(item: any, key: string) => {
return function (value: string) {
if (onUpdate && canPartialUpdate) {
onUpdate(item.id, { [key]: value })
onUpdate(item._id, { [key]: value })
}
}
},
Expand Down Expand Up @@ -189,6 +192,7 @@ const SubMasterTable = ({ columns, actions }: TableWrapperProps) => {
loading={loading}
onMove={updateSequence}
dragEnable={sequencing}
noDataText={selectedMaster ? t("submaster:noDataText") : t("submaster:selectMaster")}
/>
)
}
Expand Down
3 changes: 3 additions & 0 deletions src/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const TRANSLATION_PAIRS_SUBMASTERS = {
searchSubMasters: "Search Sub Masters", // Search Placeholder
addSubMaster: "Add Sub Master", // Add Button, Sidebar Title
updateSubMaster: "Edit Sub Master", // Edit Button, Sidebar Title
"submaster:noDataText": "No data found! Click on 'Add Sub Master' to add one.", // No Data Text
"submaster:selectMaster": "select 'Master' to see respective 'SubMasters'", // Select Master Message
}

const TRANSLATION_PAIRS_MASTERS = {
Expand All @@ -66,6 +68,7 @@ const TRANSLATION_PAIRS_MASTERS = {
description: "Discription", // Form,
enterDiscription: "Enter Description", // Description Placeholder
active: "Active",
"master:noDataText": "No data found! Click on 'Add Master' to add one.", // No Data Text
}

const TRANSLATION_PAIRS_COMMON = {
Expand Down
8 changes: 5 additions & 3 deletions src/hook/useMaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
})
if (response?.code === "SUCCESS") {
onSuccess(CALLBACK_CODES.UPDATE, response.code, response.message)
getMastersList()
setList((oldList) => oldList.map((item) => (item._id === id ? { ...item, ...data } : item)))
} else {
onError(CALLBACK_CODES.UPDATE, response.code, response.message)
}
Expand Down Expand Up @@ -132,7 +132,9 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
getMastersList()
onCloseForm()
}
} catch (error) {}
} catch (error) {
setLoading(false)
}
}
const onCloseForm = () => {
setFormState(undefined)
Expand Down Expand Up @@ -163,7 +165,7 @@ const useMaster = ({ defaultLimit, routes, defaultSort = ["createdAt", 1], preCo
url: api.url,
onError: handleError(CALLBACK_CODES.DELETE),
data: {
id: [itemData?.id],
id: itemData?.id,
},
})
if (response?.code === "SUCCESS") {
Expand Down
21 changes: 17 additions & 4 deletions src/hook/useSubMaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
})
if (response?.code === "SUCCESS") {
onSuccess(CALLBACK_CODES.UPDATE, response.code, response.message)
getSubMastersList()
setList((oldList) => oldList.map((item) => (item._id === id ? { ...item, ...data } : item)))
} else {
onError(CALLBACK_CODES.UPDATE, response.code, response.message)
}
Expand Down Expand Up @@ -146,7 +146,9 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
getSubMastersList()
onCloseForm()
}
} catch (error) {}
} catch (error) {
setLoading(false)
}
}
const onCloseForm = () => {
setFormState(undefined)
Expand Down Expand Up @@ -177,7 +179,7 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
url: api.url,
onError: handleError(CALLBACK_CODES.DELETE),
data: {
id: [itemData?.id],
id: itemData?.id,
},
})
if (response?.code === "SUCCESS") {
Expand Down Expand Up @@ -308,7 +310,18 @@ const useSubMaster = ({ defaultLimit, routes, defaultSort = ["seq", 1], preConfi
getSubMastersList()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pageSize, currentPage, selectedMaster])
}, [pageSize, currentPage])

useEffect(() => {
if (selectedMaster) {
if (currentPage === 1) {
setSequencing(false)
getSubMastersList()
} else {
setCurrentPage(1)
}
}
}, [selectedMaster])

return {
list,
Expand Down
1 change: 1 addition & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ interface TableProps {
setSortConfig?: (data: SortConfigType) => void
onMove?: (sourceIndex: number, destinationIndex: number) => void
dragEnable?: boolean
noDataText?: string
}

interface TableActionTypes {
Expand Down

0 comments on commit f2795dd

Please sign in to comment.