-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
160 lines (144 loc) · 7.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function ReactPuzzleConfirm(_ref) {
var _ref$onSuccess = _ref.onSuccess,
onSuccess = _ref$onSuccess === void 0 ? function () {
return console.log('success');
} : _ref$onSuccess,
_ref$onFail = _ref.onFail,
onFail = _ref$onFail === void 0 ? function () {
return console.log('fail');
} : _ref$onFail,
_ref$onClose = _ref.onClose,
onClose = _ref$onClose === void 0 ? function () {
return console.log('close clicked');
} : _ref$onClose,
_ref$title = _ref.title,
title = _ref$title === void 0 ? 'Please fit the puzzle piece carefully' : _ref$title,
_ref$sliderTitle = _ref.sliderTitle,
sliderTitle = _ref$sliderTitle === void 0 ? 'Slide to complete the puzzle' : _ref$sliderTitle,
_ref$failMessage = _ref.failMessage,
failMessage = _ref$failMessage === void 0 ? 'Error' : _ref$failMessage,
_ref$successMessage = _ref.successMessage,
successMessage = _ref$successMessage === void 0 ? 'Success' : _ref$successMessage,
_ref$closeButtonLabel = _ref.closeButtonLabel,
closeButtonLabel = _ref$closeButtonLabel === void 0 ? 'Close' : _ref$closeButtonLabel,
_ref$refrefButtonLabe = _ref.refrefButtonLabel,
refrefButtonLabel = _ref$refrefButtonLabe === void 0 ? 'Refresh' : _ref$refrefButtonLabe,
_ref$disableRefreshId = _ref.disableRefreshIdleState,
disableRefreshIdleState = _ref$disableRefreshId === void 0 ? true : _ref$disableRefreshId;
var minValue = 6;
var maxValue = 36;
var multiplier = 5;
var randomValueCreator = function randomValueCreator() {
var min = Math.ceil(minValue);
var max = Math.floor(maxValue);
var randomIntegerInclusive = Math.floor(Math.random() * (max - min + 1)) + min;
return randomIntegerInclusive;
};
var _useState = (0, _react.useState)(0),
_useState2 = _slicedToArray(_useState, 2),
value = _useState2[0],
setValue = _useState2[1];
var _useState3 = (0, _react.useState)('Idle'),
_useState4 = _slicedToArray(_useState3, 2),
status = _useState4[0],
setStatus = _useState4[1];
var _useState5 = (0, _react.useState)(randomValueCreator()),
_useState6 = _slicedToArray(_useState5, 2),
randomValue = _useState6[0],
setRandomValue = _useState6[1];
var handleAttempt = function handleAttempt() {
if (value === randomValue) {
setStatus('Success');
setTimeout(function () {
return onSuccess();
}, 1000);
} else {
setStatus('Failed');
onFail();
}
};
var isFailed = status === 'Failed';
var statusMessage = isFailed ? failMessage : successMessage;
return _react.default.createElement("div", {
className: "react-puzzle-confirm-modal"
}, _react.default.createElement("div", {
className: "react-puzzle-confirm"
}, _react.default.createElement("h1", {
className: "react-puzzle-confirm-title"
}, title), _react.default.createElement("div", {
className: "react-puzzle-confirm-body"
}, _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-wrapper"
}, _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle"
}, _react.default.createElement("div", {
className: "react-puzzle-confirm-flash ".concat(status === 'Success' ? 'flash--me' : '')
}), _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-missing-piece",
style: {
left: "".concat(value * multiplier, "px")
}
}, _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-missing-piece-main",
style: {
backgroundPositionX: -randomValue * multiplier - 1 / 2
}
}), _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-missing-piece-left"
}), _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-missing-piece-right"
}), _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-missing-piece-circle"
})), _react.default.createElement("div", {
className: "react-puzzle-confirm-puzzle-missing-piece missing-piece-placeholder",
style: {
left: "".concat(randomValue * multiplier, "px")
}
}))), _react.default.createElement("input", {
disabled: isFailed,
type: "range",
className: isFailed ? 'react-puzzle-confirm-slider-disabled' : '',
onMouseUp: handleAttempt,
onTouchEnd: handleAttempt,
name: "points",
value: value,
onChange: function onChange(event) {
event.persist();
var selectedValue = event.target.value;
!isNaN(selectedValue) && setValue(parseInt(selectedValue));
},
min: "0",
max: maxValue
}), _react.default.createElement("div", {
className: "react-puzzle-confirm-slider-note"
}, sliderTitle)), _react.default.createElement("div", {
className: "react-puzzle-confirm-state ".concat(isFailed ? 'react-puzzle-confirm-state--fail' : '')
}, "\xA0 ", status !== 'Idle' && statusMessage), _react.default.createElement("div", {
className: "react-puzzle-confirm-button-group"
}, _react.default.createElement("button", {
className: "react-puzzle-confirm-button react-puzzle-confirm-button--secondary",
onClick: onClose
}, closeButtonLabel), _react.default.createElement("button", {
className: "react-puzzle-confirm-button",
disabled: !isFailed && disableRefreshIdleState,
onClick: function onClick() {
setRandomValue(randomValueCreator());
setValue(0);
setStatus('Idle');
}
}, refrefButtonLabel))));
}
var _default = ReactPuzzleConfirm;
exports.default = _default;