-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add merch promotions page * Add button to create promotion * Import Promotion class from types * Fix linting issue --------- Co-authored-by: limivann <71662324+limivann@users.noreply.github.com>
- Loading branch information
1 parent
1e1f45c
commit b9b6393
Showing
16 changed files
with
234 additions
and
26 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
header { | ||
} | ||
} |
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,3 @@ | ||
table { | ||
width: 100%; | ||
} |
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,133 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { Button } from "payload/components/elements"; | ||
import { AdminView } from "payload/config"; | ||
import ViewTemplate from "./ViewTemplate"; | ||
import { Column } from "payload/dist/admin/components/elements/Table/types"; | ||
import { RenderCellFactory } from "../utils/RenderCellFactory"; | ||
import SortedColumn from "../utils/SortedColumn"; | ||
import { Table } from "payload/dist/admin/components/elements/Table"; | ||
import { Promotion } from "types"; | ||
import PromotionsApi from "../../apis/promotions.api"; | ||
import './MerchPromotion.scss'; | ||
|
||
const MerchPromotion: AdminView = ({ user, canAccessAdmin }) => { | ||
// Get data from API | ||
const [data, setData] = useState<Promotion[]>(null); | ||
useEffect(() => { | ||
PromotionsApi.getPromotions() | ||
.then((res: Promotion[]) => setData(res)) | ||
.catch((error) => console.log(error)); | ||
}, []); | ||
|
||
// Output human-readable table headers based on the attribute names from the API | ||
function prettifyKey(str: string): string { | ||
let res = ""; | ||
for (const i of str.split("_")) { | ||
res += i.charAt(0).toUpperCase() + i.slice(1) + " "; | ||
} | ||
return res; | ||
} | ||
|
||
// Do not load table until we receive the data | ||
if (data == null) { | ||
return <div> Loading... </div>; | ||
} | ||
|
||
const tableCols = new Array<Column>(); | ||
if (data && data.length > 0) { | ||
const sampleProduct = data[0]; | ||
const keys = Object.keys(sampleProduct); | ||
for (const key of keys) { | ||
const renderCell: React.FC<{ children?: React.ReactNode }> = | ||
RenderCellFactory.get(sampleProduct, key); | ||
const col: Column = { | ||
accessor: key, | ||
components: { | ||
Heading: ( | ||
<SortedColumn | ||
label={prettifyKey(key)} | ||
name={key} | ||
data={data as never[]} | ||
/> | ||
), | ||
renderCell: renderCell, | ||
}, | ||
label: "", | ||
name: "", | ||
active: true, | ||
}; | ||
tableCols.push(col); | ||
} | ||
} | ||
|
||
const editColumn: Column = { | ||
accessor: "edit", | ||
components: { | ||
Heading: <div>Edit</div>, | ||
renderCell: ({ children }) => ( | ||
<Button onClick={() => handleEdit(children as string)}>Edit</Button> | ||
), | ||
}, | ||
label: "Edit", | ||
name: "edit", | ||
active: true, | ||
}; | ||
|
||
tableCols.push(editColumn); | ||
|
||
const deleteColumn: Column = { | ||
accessor: "delete", | ||
components: { | ||
Heading: <div>Delete</div>, | ||
renderCell: ({ children }) => ( | ||
<Button onClick={() => handleDelete(children as string)}>Delete</Button> | ||
), | ||
}, | ||
label: "Delete", | ||
name: "delete", | ||
active: true, | ||
}; | ||
|
||
tableCols.push(deleteColumn); | ||
|
||
const handleEdit = (promotionID: string) => { | ||
console.log(`Dummy. Promotion ID: ${promotionID}`); | ||
}; | ||
|
||
const handleDelete = (promotionID: string) => { | ||
console.log(`Dummy. Promotion ID: ${promotionID}`); | ||
}; | ||
|
||
const handleCreatePromotion = () => { | ||
console.log("Creating a new promotion..."); | ||
}; | ||
|
||
console.log(tableCols); | ||
|
||
return ( | ||
<ViewTemplate | ||
user={user} | ||
canAccessAdmin={canAccessAdmin} | ||
description="" | ||
keywords="" | ||
title="Merchandise Promotion" | ||
> | ||
|
||
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}> | ||
<Button el="link" to={"/admin"} buttonStyle="primary"> | ||
Go to Main Admin View | ||
</Button> | ||
<Button onClick={handleCreatePromotion} buttonStyle="primary"> | ||
Create Promotion | ||
</Button> | ||
</div> | ||
|
||
<div className="table"> | ||
<Table data={data} columns={tableCols}/> | ||
</div> | ||
|
||
</ViewTemplate> | ||
); | ||
}; | ||
|
||
export default MerchPromotion; |
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,35 @@ | ||
import { Promotion } from "types"; | ||
import { PromoType } from "types"; | ||
// todo turn into real api | ||
class PromotionsApi { | ||
async getPromotions(): Promise<Promotion[]> { | ||
const res: Promotion[] = [ | ||
{ | ||
promoCode: "MARCHSALES", | ||
maxRedemptions: 10, | ||
redemptionsRemaining: 10, | ||
discounts: { | ||
promoType: PromoType.FIXED_VALUE, | ||
promoValue: 5, | ||
appliesTo: ["1"], | ||
minimumQty: 1, | ||
}, | ||
}, | ||
{ | ||
promoCode: "TSHIRTPROMO", | ||
maxRedemptions: 10, | ||
redemptionsRemaining: 10, | ||
discounts: { | ||
promoType: PromoType.FIXED_VALUE, | ||
promoValue: 5, | ||
appliesTo: ["1"], | ||
minimumQty: 1, | ||
}, | ||
}, | ||
]; | ||
|
||
return Promise.resolve(res); | ||
} | ||
} | ||
|
||
export default new PromotionsApi(); |
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
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