Added created_at and updated_at fields to new tables as a feature

This commit is contained in:
Prithivee K 2024-04-09 17:34:01 +05:30
parent 9c34e6c40d
commit ae64b6b2bb
2 changed files with 49 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import {
IconUndo,
IconRedo,
IconEdit,
IconClock,
} from "@douyinfe/semi-icons";
import { Link, useNavigate } from "react-router-dom";
import icon from "../../assets/icon_dark_64.png";
@ -85,6 +86,8 @@ export default function ControlPanel({
tables,
setTables,
addTable,
addTimestamp,
toggleTimeStamp,
updateTable,
deleteTable,
updateField,
@ -1310,6 +1313,19 @@ export default function ControlPanel({
<i className="fa-regular fa-calendar-check" />
</button>
</Tooltip>
<Divider layout="vertical" margin="8px" />
<Tooltip content="Add Timestamp to tables" position="bottom">
<button
className="py-1 px-2 hover-2 rounded flex items-center"
onClick={toggleTimeStamp}
>
<IconClock
size="large"
style={{ color: addTimestamp ? "" : "#9598a6" }}
/>
{/* <i className="fa-regular fa-clock" /> */}
</button>
</Tooltip>
</div>
<button
onClick={() => invertLayout("header")}

View File

@ -9,10 +9,14 @@ export const TablesContext = createContext(null);
export default function TablesContextProvider({ children }) {
const [tables, setTables] = useState([]);
const [relationships, setRelationships] = useState([]);
// By default Include created_at and updated_at to all new tables
const [addTimestamp, setAddTimeStamp] = useState(true);
const { transform } = useTransform();
const { setUndoStack, setRedoStack } = useUndoRedo();
const { selectedElement, setSelectedElement } = useSelect();
const toggleTimeStamp = () => setAddTimeStamp(ts => !ts);
const addTable = (addToHistory = true, data) => {
if (data) {
setTables((prev) => {
@ -21,6 +25,32 @@ export default function TablesContextProvider({ children }) {
return temp.map((t, i) => ({ ...t, id: i }));
});
} else {
const timestamp = !addTimestamp ? [] : [
{
name: "created_at",
type: "TIMESTAMP",
default: "",
check: "",
primary: false,
unique: false,
notNull: true,
increment: false,
comment: "",
id: 1,
},
{
name: "updated_at",
type: "TIMESTAMP",
default: "",
check: "",
primary: false,
unique: false,
notNull: true,
increment: false,
comment: "",
id: 2,
},
];
setTables((prev) => [
...prev,
{
@ -41,6 +71,7 @@ export default function TablesContextProvider({ children }) {
comment: "",
id: 0,
},
...timestamp
],
comment: "",
indices: [],
@ -222,6 +253,8 @@ export default function TablesContextProvider({ children }) {
value={{
tables,
setTables,
toggleTimeStamp,
addTimestamp,
addTable,
updateTable,
updateField,