PineaFan | e69bd22 | 2022-12-04 22:18:20 +0000 | [diff] [blame] | 1 | import Axios from 'axios'; |
| 2 | import Router from 'next/router'; |
| 3 | import React from 'react'; |
| 4 | import Header from '../../Components/Header' |
| 5 | import { useReward } from 'react-rewards'; |
| 6 | |
| 7 | import { AutoLayout, Panel, Title, Text, Divider } from '../../Components/Panels'; |
| 8 | import { useColorMode } from 'theme-ui'; |
| 9 | import Styles from '../../../styles/nucleus/rolemenu.module.css'; |
| 10 | |
| 11 | function RoleMenu(props) { |
| 12 | const [clicked, setClicked] = React.useState(false); |
| 13 | const [formState, setFormState] = React.useState({}); |
| 14 | const [theme, setTheme] = useColorMode() |
| 15 | |
| 16 | const { reward: reward, isAnimating: isAnimating } = useReward('confetti', 'confetti', { |
| 17 | elementSize: 10, |
| 18 | elementCount: 150, |
| 19 | startVelocity: 35, |
| 20 | lifetime: 300, |
| 21 | decay: 0.94, |
| 22 | spread: 170, |
| 23 | position: "absolute", |
| 24 | colors: ["#68D49E"] |
| 25 | }); |
| 26 | |
| 27 | function handleChange(event) { |
| 28 | const { name, value } = event.target; |
| 29 | setFormState({ ...formState, [name]: value }); |
| 30 | } |
| 31 | |
| 32 | async function submitForm() { |
| 33 | if (!isAnimating) reward(); |
| 34 | return |
| 35 | let code = await Axios.post('/api/nucleus/rolemenu/complete', { |
| 36 | code: props.code |
| 37 | }); |
| 38 | setTimeout(() => { |
| 39 | if (code.data.success === true ) return Router.push('/nucleus/rolemenu/success','/nucleus/rolemenu') |
| 40 | else return Router.push('/nucleus/rolemenu/failure','/nucleus/rolemenu') |
| 41 | }, 2500); |
| 42 | } |
| 43 | |
| 44 | return <> |
| 45 | <Header |
| 46 | name={props.guildName} |
| 47 | customImage={props.guildIcon} |
| 48 | roundImage={true} |
| 49 | subtext={`Welcome, ${props.username}`} |
| 50 | gradient={["F27878", "D96B6B"]} |
| 51 | wave="web/waves/header/nucleus" |
| 52 | buttons={[]} |
| 53 | /> |
| 54 | <form onSubmit={submitForm}> |
| 55 | <AutoLayout> |
| 56 | {props.data.map((item, index) => { |
| 57 | return <Panel key={index} halfSize={true}> |
| 58 | <Title>{item.name}</Title> |
| 59 | <Divider/> |
| 60 | <Text>{item.description}</Text> |
| 61 | <div className={Styles.options}> |
| 62 | { |
| 63 | item.options.map((props, optionIndex) => {return <div className={Styles.optionBox}> |
| 64 | <input |
| 65 | type={item.max === 1 ? "radio" : "checkbox"} |
| 66 | style={{borderRadius: item.max === 1 ? "50%" : "5px"}} |
| 67 | className={Styles.option} |
| 68 | key={item.name + optionIndex} |
| 69 | id={item.name + optionIndex.toString()} |
| 70 | name={item.name} |
| 71 | value={props.name} |
| 72 | onChange={handleChange} |
| 73 | /> |
| 74 | <label htmlFor={item.name + optionIndex.toString()}>{props.name}</label> |
| 75 | </div>}) |
| 76 | } |
| 77 | </div> |
| 78 | </Panel> |
| 79 | })} |
| 80 | <Panel halfSize={false}> |
| 81 | <Title>Save</Title> |
| 82 | <Divider/> |
| 83 | <Text> |
| 84 | After selecting all your roles, click the button below to save your choices. |
| 85 | </Text> |
| 86 | <div id="confetti" /> |
| 87 | <div className={Styles.submit}> |
| 88 | <button type="submit" disabled={isAnimating}> |
| 89 | {isAnimating ? "Saving..." : "Save"} |
| 90 | </button> |
| 91 | </div> |
| 92 | </Panel> |
| 93 | </AutoLayout> |
| 94 | </form> |
| 95 | </> |
| 96 | } |
| 97 | |
| 98 | export default RoleMenu; |
| 99 | export async function getServerSideProps(ctx) { |
| 100 | if(!ctx.query.code) { |
| 101 | return { |
| 102 | redirect: { |
| 103 | destination: '/nucleus', |
| 104 | permanent: true |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | let code; |
| 109 | try { |
| 110 | code = (await Axios.get(`http://localhost:10000/rolemenu/${ctx.query.code}`)).data; |
| 111 | } catch (e) { |
| 112 | return { |
| 113 | redirect: { |
| 114 | destination: '/nucleus/rolemenu/failure', |
| 115 | permanent: true |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | let headers = ctx.req.headers; |
| 120 | return { |
| 121 | props: { |
| 122 | guild: code.guild, |
| 123 | guildName: code.guildName, |
| 124 | guildIcon: code.guildIcon, |
| 125 | user: code.user, |
| 126 | username: code.username, |
| 127 | data: code.data, |
| 128 | headers: headers |
| 129 | } |
| 130 | } |
| 131 | } |